This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
mod.rs
592 lines (510 loc) · 19.1 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#![allow(dead_code, clippy::upper_case_acronyms)]
use self::eth::*;
use crate::{
binutil::AkulaDataDir, models::P2PParams, sentry::services::SentryService, version_string,
};
use anyhow::{format_err, Context};
use async_stream::stream;
use async_trait::async_trait;
use cidr::IpCidr;
use clap::Parser;
use derive_more::FromStr;
use devp2p::*;
use disc::dns::Resolver;
use educe::Educe;
use ethereum_interfaces::sentry::{self, sentry_server::SentryServer, InboundMessage, PeerEvent};
use fastrlp::Decodable;
use futures::stream::BoxStream;
use maplit::btreemap;
use num_traits::{FromPrimitive, ToPrimitive};
use parking_lot::RwLock;
use secp256k1::{PublicKey, SecretKey, SECP256K1};
use std::{
self,
collections::{btree_map::Entry, hash_map::Entry as HashMapEntry, BTreeMap, HashMap, HashSet},
fmt::Debug,
net::{IpAddr, SocketAddr},
num::NonZeroUsize,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use task_group::TaskGroup;
use tokio::sync::{
broadcast::{channel as broadcast_channel, Sender as BroadcastSender},
mpsc::{channel, Sender},
Mutex as AsyncMutex,
};
use tokio_stream::StreamExt;
use tonic::transport::Server;
use tracing::*;
use trust_dns_resolver::TokioAsyncResolver;
pub mod devp2p;
pub mod eth;
pub mod grpc;
pub mod services;
type OutboundSender = Sender<OutboundEvent>;
type OutboundReceiver = Arc<AsyncMutex<BoxStream<'static, OutboundEvent>>>;
pub const BUFFERING_FACTOR: usize = 5;
/// INITIAL_WINDOW_SIZE upper bound
pub const MAX_INITIAL_WINDOW_SIZE: u32 = (1 << 31) - 1;
/// MAX_FRAME_SIZE upper bound
pub const MAX_FRAME_SIZE: u32 = (1 << 24) - 1;
const THROTTLE_INTERVAL: Duration = Duration::from_secs(5);
#[derive(Clone, Debug, FromStr)]
pub struct NR(pub NodeRecord);
#[derive(Clone, Debug, FromStr)]
pub struct Discv4NR(pub crate::sentry::devp2p::disc::v4::NodeRecord);
#[derive(Clone)]
pub struct Pipes {
sender: OutboundSender,
receiver: OutboundReceiver,
}
#[derive(Clone, Debug, Default)]
struct BlockTracker {
block_by_peer: HashMap<PeerId, u64>,
peers_by_block: BTreeMap<u64, HashSet<PeerId>>,
}
impl BlockTracker {
fn set_block_number(&mut self, peer: PeerId, block: u64, force_create: bool) {
match self.block_by_peer.entry(peer) {
HashMapEntry::Vacant(e) => {
if force_create {
e.insert(block);
} else {
return;
}
}
HashMapEntry::Occupied(mut e) => {
if *e.get() > block {
return;
}
let old_block = std::mem::replace(e.get_mut(), block);
if let Entry::Occupied(mut entry) = self.peers_by_block.entry(old_block) {
entry.get_mut().remove(&peer);
if entry.get().is_empty() {
entry.remove();
}
}
}
}
self.peers_by_block.entry(block).or_default().insert(peer);
}
fn remove_peer(&mut self, peer: PeerId) {
if let Some(block) = self.block_by_peer.remove(&peer) {
if let Entry::Occupied(mut entry) = self.peers_by_block.entry(block) {
entry.get_mut().remove(&peer);
if entry.get().is_empty() {
entry.remove();
}
}
}
}
fn peers_with_min_block(&self, block: u64) -> HashSet<PeerId> {
self.peers_by_block
.range(block..)
.flat_map(|(_, v)| v)
.copied()
.collect()
}
}
#[derive(Educe)]
#[educe(Debug)]
pub struct CapabilityServerImpl {
#[educe(Debug(ignore))]
pub peer_pipes: Arc<RwLock<HashMap<PeerId, Pipes>>>,
block_tracker: Arc<RwLock<BlockTracker>>,
status_message: Arc<RwLock<Option<FullStatusData>>>,
protocol_version: EthProtocolVersion,
valid_peers: Arc<RwLock<HashSet<PeerId>>>,
data_sender: BroadcastSender<InboundMessage>,
peers_status_sender: BroadcastSender<PeerEvent>,
no_new_peers: Arc<AtomicBool>,
}
impl CapabilityServerImpl {
pub fn new(protocol_version: EthProtocolVersion, max_peers: NonZeroUsize) -> Self {
Self {
peer_pipes: Default::default(),
block_tracker: Default::default(),
status_message: Default::default(),
protocol_version,
valid_peers: Default::default(),
data_sender: broadcast_channel(max_peers.get() * BUFFERING_FACTOR).0,
peers_status_sender: broadcast_channel(max_peers.get()).0,
no_new_peers: Arc::new(AtomicBool::new(true)),
}
}
pub fn no_new_peers_handle(&self) -> Arc<AtomicBool> {
self.no_new_peers.clone()
}
fn setup_peer(&self, peer: PeerId, p: Pipes) {
let mut pipes = self.peer_pipes.write();
let mut block_tracker = self.block_tracker.write();
assert!(pipes.insert(peer, p).is_none());
block_tracker.set_block_number(peer, 0, true);
}
fn get_pipes(&self, peer: PeerId) -> Option<Pipes> {
self.peer_pipes.read().get(&peer).cloned()
}
pub fn sender(&self, peer: PeerId) -> Option<OutboundSender> {
self.peer_pipes
.read()
.get(&peer)
.map(|pipes| pipes.sender.clone())
}
fn receiver(&self, peer: PeerId) -> Option<OutboundReceiver> {
self.peer_pipes
.read()
.get(&peer)
.map(|pipes| pipes.receiver.clone())
}
#[instrument(name = "CapabilityServerImpl.teardown_peer", skip(self))]
fn teardown_peer(&self, peer: PeerId) {
let mut pipes = self.peer_pipes.write();
let mut block_tracker = self.block_tracker.write();
let mut valid_peers = self.valid_peers.write();
pipes.remove(&peer);
block_tracker.remove_peer(peer);
valid_peers.remove(&peer);
let send_status_result =
self.peers_status_sender
.send(ethereum_interfaces::sentry::PeerEvent {
peer_id: Some(ethereum_interfaces::types::H512::from(peer)),
event_id: ethereum_interfaces::sentry::peer_event::PeerEventId::Disconnect
as i32,
});
if send_status_result.is_err() {
debug!("No subscribers to report peer status to");
}
}
pub fn all_peers(&self) -> HashSet<PeerId> {
self.peer_pipes.read().keys().copied().collect()
}
pub fn connected_peers(&self) -> usize {
self.valid_peers.read().len()
}
pub fn set_status(&self, message: FullStatusData) {
*self.status_message.write() = Some(message);
self.no_new_peers.store(false, Ordering::SeqCst);
}
#[instrument(name = "CapabilityServerImpl.handle_event", skip(self, event))]
fn handle_event(&self, peer: PeerId, event: InboundEvent) -> Result<(), DisconnectReason> {
match event {
InboundEvent::Disconnect { reason } => {
debug!("Peer disconnect (reason: {:?}), tearing down peer.", reason);
self.teardown_peer(peer);
}
InboundEvent::Message {
message: Message { id, data },
..
} => {
let valid_peer = self.valid_peers.read().contains(&peer);
let message_id = EthMessageId::from_usize(id);
match message_id {
None => {
debug!("Unknown message");
}
Some(EthMessageId::Status) => {
let v = StatusMessage::decode(&mut &*data).map_err(|e| {
debug!("Failed to decode status message: {}! Kicking peer.", e);
DisconnectReason::ProtocolBreach
})?;
debug!("Decoded status message: {:?}", v);
let status_data = &*(self.status_message.read());
if let Some(FullStatusData { fork_filter, .. }) = status_data {
fork_filter.validate(v.fork_id).map_err(|reason| {
debug!("Kicking peer with incompatible fork ID: {:?}", reason);
DisconnectReason::UselessPeer
})?;
self.valid_peers.write().insert(peer);
let _ = self
.peers_status_sender
.send(ethereum_interfaces::sentry::PeerEvent {
peer_id: Some(ethereum_interfaces::types::H512::from(peer)),
event_id:
ethereum_interfaces::sentry::peer_event::PeerEventId::Connect
as i32,
});
}
}
Some(inbound_id) if valid_peer => {
let _ = self.data_sender.send(InboundMessage {
id: sentry::MessageId::from(inbound_id) as i32,
data,
peer_id: Some(peer.into()),
});
}
_ => {}
}
}
}
Ok(())
}
}
#[async_trait]
impl CapabilityServer for CapabilityServerImpl {
#[instrument(skip(self, peer), level = "debug", fields(peer=&*peer.to_string()))]
fn on_peer_connect(&self, peer: PeerId, caps: HashMap<CapabilityName, CapabilityVersion>) {
let first_events = if let Some(FullStatusData {
status,
fork_filter,
}) = &*self.status_message.read()
{
let status_message = StatusMessage {
protocol_version: *caps
.get(&capability_name())
.expect("peer without this cap would have been disconnected"),
network_id: status.network_id,
total_difficulty: status.total_difficulty,
best_hash: status.best_hash,
genesis_hash: status.fork_data.genesis,
fork_id: fork_filter.current(),
};
vec![OutboundEvent::Message {
capability_name: capability_name(),
message: Message {
id: EthMessageId::Status.to_usize().unwrap(),
data: fastrlp::encode_fixed_size(&status_message).to_vec().into(),
},
}]
} else {
vec![OutboundEvent::Disconnect {
reason: DisconnectReason::DisconnectRequested,
}]
};
let (sender, mut receiver) = channel(1);
self.setup_peer(
peer,
Pipes {
sender,
receiver: Arc::new(AsyncMutex::new(Box::pin(stream! {
for event in first_events {
yield event;
}
while let Some(event) = receiver.recv().await {
yield event;
}
}))),
},
);
}
#[instrument(skip_all, level = "debug", fields(peer=&*peer.to_string(), event=&*event.to_string()))]
async fn on_peer_event(&self, peer: PeerId, event: InboundEvent) {
debug!("Received message");
if let Err(reason) = self.handle_event(peer, event) {
match self.sender(peer) {
Some(sender) => {
let _ = sender.send(OutboundEvent::Disconnect { reason }).await;
}
None => {
self.teardown_peer(peer);
}
}
}
}
async fn next(&self, peer: PeerId) -> OutboundEvent {
self.receiver(peer)
.unwrap()
.lock()
.await
.next()
.await
.unwrap_or(OutboundEvent::Disconnect {
reason: DisconnectReason::DisconnectRequested,
})
}
}
#[derive(Educe, Parser)]
#[educe(Debug)]
pub struct Opts {
#[clap(long)]
#[educe(Debug(ignore))]
pub node_key: Option<String>,
#[clap(long, default_value = "0.0.0.0")]
pub listen_addr: IpAddr,
#[clap(long, default_value = "30303")]
pub listen_port: u16,
#[clap(long)]
pub cidr: Option<IpCidr>,
#[clap(long, default_value = "127.0.0.1:8000")]
pub sentry_addr: SocketAddr,
#[clap(long)]
pub dnsdisc_address: Option<String>,
#[clap(long, default_value = "30303")]
pub discv4_port: u16,
#[clap(long)]
pub discv4_bootnodes: Vec<Discv4NR>,
#[clap(long, default_value = "1000")]
pub discv4_cache: usize,
#[clap(long, default_value = "25")]
pub discv4_concurrent_lookups: usize,
#[clap(long)]
pub static_peers: Vec<NR>,
#[clap(long, default_value = "5000")]
pub static_peers_interval: u64,
#[clap(long, default_value = "100")]
pub max_peers: NonZeroUsize,
/// Minimum number of peers, below which we will search for peers more aggressively.
#[clap(long, default_value = "10")]
pub min_peers: usize,
/// Disable DNS and UDP discovery, only use static peers.
#[clap(long, num_args = 0)]
pub no_discovery: bool,
/// Disable DNS discovery
#[clap(long, num_args = 0)]
pub no_dns_discovery: bool,
}
pub async fn run(
opts: Opts,
db_path: AkulaDataDir,
network_params: P2PParams,
) -> anyhow::Result<Arc<Swarm<CapabilityServerImpl>>> {
let secret_key = {
let secret_key_path = db_path.nodekey();
let secret_key;
if let Some(node_key) = opts.node_key {
secret_key = SecretKey::from_slice(&hex::decode(node_key)?)?;
info!("Loaded node key from config");
std::fs::write(&secret_key_path, &hex::encode(secret_key.secret_bytes()))?;
} else {
match std::fs::read_to_string(&secret_key_path) {
Ok(nodekey) => {
secret_key = SecretKey::from_slice(&hex::decode(&nodekey)?)?;
info!(
"Loaded node key: {}",
hex::encode(secret_key.secret_bytes())
);
}
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => {
secret_key = SecretKey::new(&mut secp256k1::rand::thread_rng());
info!(
"Generated new node key: {}",
hex::encode(secret_key.secret_bytes())
);
std::fs::write(&secret_key_path, &hex::encode(secret_key.secret_bytes()))?;
}
_ => return Err(e.into()),
},
}
}
secret_key
};
let listen_addr = SocketAddr::new(opts.listen_addr, opts.listen_port);
info!("Starting Ethereum P2P node");
info!(
"Node ID: {}",
hex::encode(
devp2p::util::pk2id(&PublicKey::from_secret_key(SECP256K1, &secret_key)).as_bytes()
)
);
if let Some(cidr_filter) = &opts.cidr {
info!("Peers restricted to range {}", cidr_filter);
}
let mut discovery_tasks: HashMap<String, Discovery> = HashMap::new();
let bootnodes = if opts.discv4_bootnodes.is_empty() {
network_params
.bootnodes
.iter()
.map(|b| Discv4NR(b.parse().unwrap()))
.collect::<Vec<_>>()
} else {
opts.discv4_bootnodes
};
let dns_addr = opts.dnsdisc_address.or(network_params.dns);
let discv4_throttle = Arc::new(AtomicBool::new(false));
if !opts.no_discovery {
if !opts.no_dns_discovery {
if let Some(dns_addr) = dns_addr {
info!("Starting DNS discovery fetch from {}", dns_addr);
let dns_resolver = Resolver::new(Arc::new(
TokioAsyncResolver::tokio_from_system_conf()
.map_err(|err| format_err!("Failed to start DNS resolver: {err}"))?,
));
let task = DnsDiscovery::new(Arc::new(dns_resolver), dns_addr, None);
discovery_tasks.insert("dnsdisc".to_string(), Box::pin(task));
}
}
info!("Starting discv4 at port {}", opts.discv4_port);
let bootstrap_nodes = bootnodes
.into_iter()
.map(|Discv4NR(nr)| nr)
.collect::<Vec<_>>();
let node = disc::v4::Node::new(
format!("0.0.0.0:{}", opts.discv4_port).parse().unwrap(),
secret_key,
bootstrap_nodes,
None,
true,
opts.listen_port,
)
.await?;
let task = Discv4Builder::default()
.with_cache(opts.discv4_cache)
.with_concurrent_lookups(opts.discv4_concurrent_lookups)
.with_throttle(discv4_throttle.clone())
.build(node);
discovery_tasks.insert("discv4".to_string(), Box::pin(task));
}
if !opts.static_peers.is_empty() {
info!("Enabling static peers: {:?}", opts.static_peers);
let task = StaticNodes::new(
opts.static_peers
.iter()
.map(|&NR(NodeRecord { addr, id })| (addr, id))
.collect::<HashMap<_, _>>(),
Duration::from_millis(opts.static_peers_interval),
);
discovery_tasks.insert("static peers".to_string(), Box::pin(task));
}
if discovery_tasks.is_empty() {
warn!("All discovery methods are disabled, sentry will not search for peers.");
}
let tasks = Arc::new(TaskGroup::new());
let protocol_version = EthProtocolVersion::Eth66;
let capability_server = Arc::new(CapabilityServerImpl::new(protocol_version, opts.max_peers));
let no_new_peers = capability_server.no_new_peers_handle();
let swarm = Swarm::builder()
.with_task_group(tasks.clone())
.with_listen_options(ListenOptions::new(
discovery_tasks,
opts.min_peers,
opts.max_peers,
listen_addr,
opts.cidr,
no_new_peers,
))
.with_client_version(version_string())
.build(
btreemap! {
CapabilityId { name: capability_name(), version: protocol_version as CapabilityVersion } => 17,
},
capability_server.clone(),
secret_key,
)
.await
.context("Failed to start RLPx node")?;
if !opts.no_discovery {
let swarm = swarm.clone();
tasks.spawn_with_name("discv4 throttler", async move {
loop {
discv4_throttle.store(swarm.num_peers() >= opts.min_peers, Ordering::SeqCst);
tokio::time::sleep(THROTTLE_INTERVAL).await;
}
});
}
info!("RLPx node listening at {}", listen_addr);
tasks.spawn(async move {
let svc = SentryServer::new(SentryService::new(capability_server));
info!("Sentry gRPC server starting on {}", opts.sentry_addr);
Server::builder()
.add_service(svc)
.serve(opts.sentry_addr)
.await
.unwrap();
});
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(swarm)
}