-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathgateway.rs
1635 lines (1482 loc) · 71.7 KB
/
gateway.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkOS library.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{
events::{EventCodec, PrimaryPing},
helpers::{assign_to_worker, Cache, PrimarySender, Resolver, Storage, SyncSender, WorkerSender},
spawn_blocking,
Worker,
CONTEXT,
MAX_BATCH_DELAY_IN_MS,
MEMORY_POOL_PORT,
};
use snarkos_account::Account;
use snarkos_node_bft_events::{
BlockRequest,
BlockResponse,
CertificateRequest,
CertificateResponse,
ChallengeRequest,
ChallengeResponse,
DataBlocks,
DisconnectReason,
Event,
EventTrait,
TransmissionRequest,
TransmissionResponse,
ValidatorsRequest,
ValidatorsResponse,
};
use snarkos_node_bft_ledger_service::LedgerService;
use snarkos_node_sync::{communication_service::CommunicationService, MAX_BLOCKS_BEHIND};
use snarkos_node_tcp::{
is_bogon_ip,
is_unspecified_or_broadcast_ip,
protocols::{Disconnect, Handshake, OnConnect, Reading, Writing},
Config,
Connection,
ConnectionSide,
Tcp,
P2P,
};
use snarkvm::{
console::prelude::*,
ledger::{
committee::Committee,
narwhal::{BatchHeader, Data},
},
prelude::{Address, Field},
};
use colored::Colorize;
use futures::SinkExt;
use indexmap::{IndexMap, IndexSet};
use parking_lot::{Mutex, RwLock};
use rand::seq::{IteratorRandom, SliceRandom};
use std::{collections::HashSet, future::Future, io, net::SocketAddr, sync::Arc, time::Duration};
use tokio::{
net::TcpStream,
sync::{oneshot, OnceCell},
task::{self, JoinHandle},
};
use tokio_stream::StreamExt;
use tokio_util::codec::Framed;
/// The maximum interval of events to cache.
const CACHE_EVENTS_INTERVAL: i64 = (MAX_BATCH_DELAY_IN_MS / 1000) as i64; // seconds
/// The maximum interval of requests to cache.
const CACHE_REQUESTS_INTERVAL: i64 = (MAX_BATCH_DELAY_IN_MS / 1000) as i64; // seconds
/// The maximum number of connection attempts in an interval.
const MAX_CONNECTION_ATTEMPTS: usize = 10;
/// The maximum interval to restrict a peer.
const RESTRICTED_INTERVAL: i64 = (MAX_CONNECTION_ATTEMPTS as u64 * MAX_BATCH_DELAY_IN_MS / 1000) as i64; // seconds
/// The minimum number of validators to maintain a connection to.
const MIN_CONNECTED_VALIDATORS: usize = 175;
/// The maximum number of validators to send in a validators response event.
const MAX_VALIDATORS_TO_SEND: usize = 200;
/// Part of the Gateway API that deals with networking.
/// This is a separate trait to allow for easier testing/mocking.
#[async_trait]
pub trait Transport<N: Network>: Send + Sync {
async fn send(&self, peer_ip: SocketAddr, event: Event<N>) -> Option<oneshot::Receiver<io::Result<()>>>;
fn broadcast(&self, event: Event<N>);
}
#[derive(Clone)]
pub struct Gateway<N: Network> {
/// The account of the node.
account: Account<N>,
/// The storage.
storage: Storage<N>,
/// The ledger service.
ledger: Arc<dyn LedgerService<N>>,
/// The TCP stack.
tcp: Tcp,
/// The cache.
cache: Arc<Cache<N>>,
/// The resolver.
resolver: Arc<Resolver<N>>,
/// The set of trusted validators.
trusted_validators: IndexSet<SocketAddr>,
/// The map of connected peer IPs to their peer handlers.
connected_peers: Arc<RwLock<IndexSet<SocketAddr>>>,
/// The set of handshaking peers. While `Tcp` already recognizes the connecting IP addresses
/// and prevents duplicate outbound connection attempts to the same IP address, it is unable to
/// prevent simultaneous "two-way" connections between two peers (i.e. both nodes simultaneously
/// attempt to connect to each other). This set is used to prevent this from happening.
connecting_peers: Arc<Mutex<IndexSet<SocketAddr>>>,
/// The primary sender.
primary_sender: Arc<OnceCell<PrimarySender<N>>>,
/// The worker senders.
worker_senders: Arc<OnceCell<IndexMap<u8, WorkerSender<N>>>>,
/// The sync sender.
sync_sender: Arc<OnceCell<SyncSender<N>>>,
/// The spawned handles.
handles: Arc<Mutex<Vec<JoinHandle<()>>>>,
/// The development mode.
dev: Option<u16>,
}
impl<N: Network> Gateway<N> {
/// Initializes a new gateway.
pub fn new(
account: Account<N>,
storage: Storage<N>,
ledger: Arc<dyn LedgerService<N>>,
ip: Option<SocketAddr>,
trusted_validators: &[SocketAddr],
dev: Option<u16>,
) -> Result<Self> {
// Initialize the gateway IP.
let ip = match (ip, dev) {
(None, Some(dev)) => SocketAddr::from_str(&format!("127.0.0.1:{}", MEMORY_POOL_PORT + dev))?,
(None, None) => SocketAddr::from_str(&format!("0.0.0.0:{}", MEMORY_POOL_PORT))?,
(Some(ip), _) => ip,
};
// Initialize the TCP stack.
let tcp = Tcp::new(Config::new(ip, Committee::<N>::MAX_COMMITTEE_SIZE));
// Return the gateway.
Ok(Self {
account,
storage,
ledger,
tcp,
cache: Default::default(),
resolver: Default::default(),
trusted_validators: trusted_validators.iter().copied().collect(),
connected_peers: Default::default(),
connecting_peers: Default::default(),
primary_sender: Default::default(),
worker_senders: Default::default(),
sync_sender: Default::default(),
handles: Default::default(),
dev,
})
}
/// Run the gateway.
pub async fn run(
&self,
primary_sender: PrimarySender<N>,
worker_senders: IndexMap<u8, WorkerSender<N>>,
sync_sender: Option<SyncSender<N>>,
) {
debug!("Starting the gateway for the memory pool...");
// Set the primary sender.
self.primary_sender.set(primary_sender).expect("Primary sender already set in gateway");
// Set the worker senders.
self.worker_senders.set(worker_senders).expect("The worker senders are already set");
// If the sync sender was provided, set the sync sender.
if let Some(sync_sender) = sync_sender {
self.sync_sender.set(sync_sender).expect("Sync sender already set in gateway");
}
// Enable the TCP protocols.
self.enable_handshake().await;
self.enable_reading().await;
self.enable_writing().await;
self.enable_disconnect().await;
self.enable_on_connect().await;
// Enable the TCP listener. Note: This must be called after the above protocols.
let _listening_addr = self.tcp.enable_listener().await.expect("Failed to enable the TCP listener");
// Initialize the heartbeat.
self.initialize_heartbeat();
info!("Started the gateway for the memory pool at '{}'", self.local_ip());
}
}
// Dynamic rate limiting.
impl<N: Network> Gateway<N> {
/// The current maximum committee size.
fn max_committee_size(&self) -> usize {
self.ledger
.current_committee()
.map_or_else(|_e| Committee::<N>::MAX_COMMITTEE_SIZE as usize, |committee| committee.num_members())
}
/// The maximum number of events to cache.
fn max_cache_events(&self) -> usize {
self.max_cache_transmissions()
}
/// The maximum number of certificate requests to cache.
fn max_cache_certificates(&self) -> usize {
2 * BatchHeader::<N>::MAX_GC_ROUNDS * self.max_committee_size()
}
/// The maximum number of transmission requests to cache.
fn max_cache_transmissions(&self) -> usize {
self.max_cache_certificates() * BatchHeader::<N>::MAX_TRANSMISSIONS_PER_BATCH
}
/// The maximum number of duplicates for any particular request.
fn max_cache_duplicates(&self) -> usize {
self.max_committee_size().pow(2)
}
}
#[async_trait]
impl<N: Network> CommunicationService for Gateway<N> {
/// The message type.
type Message = Event<N>;
/// Prepares a block request to be sent.
fn prepare_block_request(start_height: u32, end_height: u32) -> Self::Message {
debug_assert!(start_height < end_height, "Invalid block request format");
Event::BlockRequest(BlockRequest { start_height, end_height })
}
/// Sends the given message to specified peer.
///
/// This function returns as soon as the message is queued to be sent,
/// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
/// which can be used to determine when and whether the message has been delivered.
async fn send(&self, peer_ip: SocketAddr, message: Self::Message) -> Option<oneshot::Receiver<io::Result<()>>> {
Transport::send(self, peer_ip, message).await
}
}
impl<N: Network> Gateway<N> {
/// Returns the account of the node.
pub const fn account(&self) -> &Account<N> {
&self.account
}
/// Returns the dev identifier of the node.
pub const fn dev(&self) -> Option<u16> {
self.dev
}
/// Returns the IP address of this node.
pub fn local_ip(&self) -> SocketAddr {
self.tcp.listening_addr().expect("The TCP listener is not enabled")
}
/// Returns `true` if the given IP is this node.
pub fn is_local_ip(&self, ip: SocketAddr) -> bool {
ip == self.local_ip()
|| (ip.ip().is_unspecified() || ip.ip().is_loopback()) && ip.port() == self.local_ip().port()
}
/// Returns `true` if the given IP is not this node, is not a bogon address, and is not unspecified.
pub fn is_valid_peer_ip(&self, ip: SocketAddr) -> bool {
!self.is_local_ip(ip) && !is_bogon_ip(ip.ip()) && !is_unspecified_or_broadcast_ip(ip.ip())
}
/// Returns the resolver.
pub fn resolver(&self) -> &Resolver<N> {
&self.resolver
}
/// Returns the primary sender.
pub fn primary_sender(&self) -> &PrimarySender<N> {
self.primary_sender.get().expect("Primary sender not set in gateway")
}
/// Returns the number of workers.
pub fn num_workers(&self) -> u8 {
u8::try_from(self.worker_senders.get().expect("Missing worker senders in gateway").len())
.expect("Too many workers")
}
/// Returns the worker sender for the given worker ID.
pub fn get_worker_sender(&self, worker_id: u8) -> Option<&WorkerSender<N>> {
self.worker_senders.get().and_then(|senders| senders.get(&worker_id))
}
/// Returns `true` if the node is connected to the given Aleo address.
pub fn is_connected_address(&self, address: Address<N>) -> bool {
// Retrieve the peer IP of the given address.
match self.resolver.get_peer_ip_for_address(address) {
// Determine if the peer IP is connected.
Some(peer_ip) => self.is_connected_ip(peer_ip),
None => false,
}
}
/// Returns `true` if the node is connected to the given peer IP.
pub fn is_connected_ip(&self, ip: SocketAddr) -> bool {
self.connected_peers.read().contains(&ip)
}
/// Returns `true` if the node is connecting to the given peer IP.
pub fn is_connecting_ip(&self, ip: SocketAddr) -> bool {
self.connecting_peers.lock().contains(&ip)
}
/// Returns `true` if the given peer IP is an authorized validator.
pub fn is_authorized_validator_ip(&self, ip: SocketAddr) -> bool {
// If the peer IP is in the trusted validators, return early.
if self.trusted_validators.contains(&ip) {
return true;
}
// Retrieve the Aleo address of the peer IP.
match self.resolver.get_address(ip) {
// Determine if the peer IP is an authorized validator.
Some(address) => self.is_authorized_validator_address(address),
None => false,
}
}
/// Returns `true` if the given address is an authorized validator.
pub fn is_authorized_validator_address(&self, validator_address: Address<N>) -> bool {
// Determine if the validator address is a member of the committee lookback,
// the current committee, or the previous committee lookbacks.
// We allow leniency in this validation check in order to accommodate these two scenarios:
// 1. New validators should be able to connect immediately once bonded as a committee member.
// 2. Existing validators must remain connected until they are no longer bonded as a committee member.
// (i.e. meaning they must stay online until the next block has been produced)
// Determine if the validator is in the current committee with lookback.
if self
.ledger
.get_committee_lookback_for_round(self.storage.current_round())
.map_or(false, |committee| committee.is_committee_member(validator_address))
{
return true;
}
// Determine if the validator is in the latest committee on the ledger.
if self.ledger.current_committee().map_or(false, |committee| committee.is_committee_member(validator_address)) {
return true;
}
// Retrieve the previous block height to consider from the sync tolerance.
let previous_block_height = self.ledger.latest_block_height().saturating_sub(MAX_BLOCKS_BEHIND);
// Determine if the validator is in any of the previous committee lookbacks.
match self.ledger.get_block_round(previous_block_height) {
Ok(block_round) => (block_round..self.storage.current_round()).step_by(2).any(|round| {
self.ledger
.get_committee_lookback_for_round(round)
.map_or(false, |committee| committee.is_committee_member(validator_address))
}),
Err(_) => false,
}
}
/// Returns the maximum number of connected peers.
pub fn max_connected_peers(&self) -> usize {
self.tcp.config().max_connections as usize
}
/// Returns the number of connected peers.
pub fn number_of_connected_peers(&self) -> usize {
self.connected_peers.read().len()
}
/// Returns the list of connected addresses.
pub fn connected_addresses(&self) -> HashSet<Address<N>> {
self.connected_peers.read().iter().filter_map(|peer_ip| self.resolver.get_address(*peer_ip)).collect()
}
/// Returns the list of connected peers.
pub fn connected_peers(&self) -> &RwLock<IndexSet<SocketAddr>> {
&self.connected_peers
}
/// Attempts to connect to the given peer IP.
pub fn connect(&self, peer_ip: SocketAddr) -> Option<JoinHandle<()>> {
// Return early if the attempt is against the protocol rules.
if let Err(forbidden_error) = self.check_connection_attempt(peer_ip) {
warn!("{forbidden_error}");
return None;
}
let self_ = self.clone();
Some(tokio::spawn(async move {
debug!("Connecting to validator {peer_ip}...");
// Attempt to connect to the peer.
if let Err(error) = self_.tcp.connect(peer_ip).await {
self_.connecting_peers.lock().shift_remove(&peer_ip);
warn!("Unable to connect to '{peer_ip}' - {error}");
}
}))
}
/// Ensure we are allowed to connect to the given peer.
fn check_connection_attempt(&self, peer_ip: SocketAddr) -> Result<()> {
// Ensure the peer IP is not this node.
if self.is_local_ip(peer_ip) {
bail!("{CONTEXT} Dropping connection attempt to '{peer_ip}' (attempted to self-connect)")
}
// Ensure the node does not surpass the maximum number of peer connections.
if self.number_of_connected_peers() >= self.max_connected_peers() {
bail!("{CONTEXT} Dropping connection attempt to '{peer_ip}' (maximum peers reached)")
}
// Ensure the node is not already connected to this peer.
if self.is_connected_ip(peer_ip) {
bail!("{CONTEXT} Dropping connection attempt to '{peer_ip}' (already connected)")
}
// Ensure the node is not already connecting to this peer.
if self.is_connecting_ip(peer_ip) {
bail!("{CONTEXT} Dropping connection attempt to '{peer_ip}' (already connecting)")
}
Ok(())
}
/// Ensure the peer is allowed to connect.
fn ensure_peer_is_allowed(&self, peer_ip: SocketAddr) -> Result<()> {
// Ensure the peer IP is not this node.
if self.is_local_ip(peer_ip) {
bail!("{CONTEXT} Dropping connection request from '{peer_ip}' (attempted to self-connect)")
}
// Ensure the node is not already connecting to this peer.
if !self.connecting_peers.lock().insert(peer_ip) {
bail!("{CONTEXT} Dropping connection request from '{peer_ip}' (already shaking hands as the initiator)")
}
// Ensure the node is not already connected to this peer.
if self.is_connected_ip(peer_ip) {
bail!("{CONTEXT} Dropping connection request from '{peer_ip}' (already connected)")
}
// Ensure the peer is not spamming connection attempts.
if !peer_ip.ip().is_loopback() {
// Add this connection attempt and retrieve the number of attempts.
let num_attempts = self.cache.insert_inbound_connection(peer_ip.ip(), RESTRICTED_INTERVAL);
// Ensure the connecting peer has not surpassed the connection attempt limit.
if num_attempts > MAX_CONNECTION_ATTEMPTS {
bail!("Dropping connection request from '{peer_ip}' (tried {num_attempts} times)")
}
}
Ok(())
}
#[cfg(feature = "metrics")]
fn update_metrics(&self) {
metrics::gauge(metrics::bft::CONNECTED, self.connected_peers.read().len() as f64);
metrics::gauge(metrics::bft::CONNECTING, self.connecting_peers.lock().len() as f64);
}
/// Inserts the given peer into the connected peers.
#[cfg(not(test))]
fn insert_connected_peer(&self, peer_ip: SocketAddr, peer_addr: SocketAddr, address: Address<N>) {
// Adds a bidirectional map between the listener address and (ambiguous) peer address.
self.resolver.insert_peer(peer_ip, peer_addr, address);
// Add a transmission for this peer in the connected peers.
self.connected_peers.write().insert(peer_ip);
#[cfg(feature = "metrics")]
self.update_metrics();
}
/// Inserts the given peer into the connected peers.
#[cfg(test)]
// For unit tests, we need to make this public so we can inject peers.
pub fn insert_connected_peer(&self, peer_ip: SocketAddr, peer_addr: SocketAddr, address: Address<N>) {
// Adds a bidirectional map between the listener address and (ambiguous) peer address.
self.resolver.insert_peer(peer_ip, peer_addr, address);
// Add a transmission for this peer in the connected peers.
self.connected_peers.write().insert(peer_ip);
}
/// Removes the connected peer and adds them to the candidate peers.
fn remove_connected_peer(&self, peer_ip: SocketAddr) {
// If a sync sender was provided, remove the peer from the sync module.
if let Some(sync_sender) = self.sync_sender.get() {
let tx_block_sync_remove_peer_ = sync_sender.tx_block_sync_remove_peer.clone();
tokio::spawn(async move {
if let Err(e) = tx_block_sync_remove_peer_.send(peer_ip).await {
warn!("Unable to remove '{peer_ip}' from the sync module - {e}");
}
});
}
// Removes the bidirectional map between the listener address and (ambiguous) peer address.
self.resolver.remove_peer(peer_ip);
// Remove this peer from the connected peers, if it exists.
self.connected_peers.write().shift_remove(&peer_ip);
#[cfg(feature = "metrics")]
self.update_metrics();
}
/// Sends the given event to specified peer.
///
/// This function returns as soon as the event is queued to be sent,
/// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
/// which can be used to determine when and whether the event has been delivered.
fn send_inner(&self, peer_ip: SocketAddr, event: Event<N>) -> Option<oneshot::Receiver<io::Result<()>>> {
// Resolve the listener IP to the (ambiguous) peer address.
let Some(peer_addr) = self.resolver.get_ambiguous(peer_ip) else {
warn!("Unable to resolve the listener IP address '{peer_ip}'");
return None;
};
// Retrieve the event name.
let name = event.name();
// Send the event to the peer.
trace!("{CONTEXT} Sending '{name}' to '{peer_ip}'");
let result = self.unicast(peer_addr, event);
// If the event was unable to be sent, disconnect.
if let Err(e) = &result {
warn!("{CONTEXT} Failed to send '{name}' to '{peer_ip}': {e}");
debug!("{CONTEXT} Disconnecting from '{peer_ip}' (unable to send)");
self.disconnect(peer_ip);
}
result.ok()
}
/// Handles the inbound event from the peer.
async fn inbound(&self, peer_addr: SocketAddr, event: Event<N>) -> Result<()> {
// Retrieve the listener IP for the peer.
let Some(peer_ip) = self.resolver.get_listener(peer_addr) else {
bail!("{CONTEXT} Unable to resolve the (ambiguous) peer address '{peer_addr}'")
};
// Ensure that the peer is an authorized committee member.
if !self.is_authorized_validator_ip(peer_ip) {
bail!("{CONTEXT} Dropping '{}' from '{peer_ip}' (not authorized)", event.name())
}
// Drop the peer, if they have exceeded the rate limit (i.e. they are requesting too much from us).
let num_events = self.cache.insert_inbound_event(peer_ip, CACHE_EVENTS_INTERVAL);
if num_events >= self.max_cache_events() {
bail!("Dropping '{peer_ip}' for spamming events (num_events = {num_events})")
}
// Rate limit for duplicate requests.
if matches!(&event, &Event::CertificateRequest(_) | &Event::CertificateResponse(_)) {
// Retrieve the certificate ID.
let certificate_id = match &event {
Event::CertificateRequest(CertificateRequest { certificate_id }) => *certificate_id,
Event::CertificateResponse(CertificateResponse { certificate }) => certificate.id(),
_ => unreachable!(),
};
// Skip processing this certificate if the rate limit was exceed (i.e. someone is spamming a specific certificate).
let num_events = self.cache.insert_inbound_certificate(certificate_id, CACHE_REQUESTS_INTERVAL);
if num_events >= self.max_cache_duplicates() {
return Ok(());
}
} else if matches!(&event, &Event::TransmissionRequest(_) | Event::TransmissionResponse(_)) {
// Retrieve the transmission ID.
let transmission_id = match &event {
Event::TransmissionRequest(TransmissionRequest { transmission_id }) => *transmission_id,
Event::TransmissionResponse(TransmissionResponse { transmission_id, .. }) => *transmission_id,
_ => unreachable!(),
};
// Skip processing this certificate if the rate limit was exceeded (i.e. someone is spamming a specific certificate).
let num_events = self.cache.insert_inbound_transmission(transmission_id, CACHE_REQUESTS_INTERVAL);
if num_events >= self.max_cache_duplicates() {
return Ok(());
}
}
trace!("{CONTEXT} Received '{}' from '{peer_ip}'", event.name());
// This match statement handles the inbound event by deserializing the event,
// checking the event is valid, and then calling the appropriate (trait) handler.
match event {
Event::BatchPropose(batch_propose) => {
// Send the batch propose to the primary.
let _ = self.primary_sender().tx_batch_propose.send((peer_ip, batch_propose)).await;
Ok(())
}
Event::BatchSignature(batch_signature) => {
// Send the batch signature to the primary.
let _ = self.primary_sender().tx_batch_signature.send((peer_ip, batch_signature)).await;
Ok(())
}
Event::BatchCertified(batch_certified) => {
// Send the batch certificate to the primary.
let _ = self.primary_sender().tx_batch_certified.send((peer_ip, batch_certified.certificate)).await;
Ok(())
}
Event::BlockRequest(block_request) => {
let BlockRequest { start_height, end_height } = block_request;
// Ensure the block request is well-formed.
if start_height >= end_height {
bail!("Block request from '{peer_ip}' has an invalid range ({start_height}..{end_height})")
}
// Ensure that the block request is within the allowed bounds.
if end_height - start_height > DataBlocks::<N>::MAXIMUM_NUMBER_OF_BLOCKS as u32 {
bail!("Block request from '{peer_ip}' has an excessive range ({start_height}..{end_height})")
}
let self_ = self.clone();
let blocks = match task::spawn_blocking(move || {
// Retrieve the blocks within the requested range.
match self_.ledger.get_blocks(start_height..end_height) {
Ok(blocks) => Ok(Data::Object(DataBlocks(blocks))),
Err(error) => bail!("Missing blocks {start_height} to {end_height} from ledger - {error}"),
}
})
.await
{
Ok(Ok(blocks)) => blocks,
Ok(Err(error)) => return Err(error),
Err(error) => return Err(anyhow!("[BlockRequest] {error}")),
};
let self_ = self.clone();
tokio::spawn(async move {
// Send the `BlockResponse` message to the peer.
let event = Event::BlockResponse(BlockResponse { request: block_request, blocks });
Transport::send(&self_, peer_ip, event).await;
});
Ok(())
}
Event::BlockResponse(block_response) => {
// If a sync sender was provided, then process the block response.
if let Some(sync_sender) = self.sync_sender.get() {
// Retrieve the block response.
let BlockResponse { request, blocks } = block_response;
// Perform the deferred non-blocking deserialization of the blocks.
let blocks = blocks.deserialize().await.map_err(|error| anyhow!("[BlockResponse] {error}"))?;
// Ensure the block response is well-formed.
blocks.ensure_response_is_well_formed(peer_ip, request.start_height, request.end_height)?;
// Send the blocks to the sync module.
if let Err(e) = sync_sender.advance_with_sync_blocks(peer_ip, blocks.0).await {
warn!("Unable to process block response from '{peer_ip}' - {e}");
}
}
Ok(())
}
Event::CertificateRequest(certificate_request) => {
// If a sync sender was provided, send the certificate request to the sync module.
if let Some(sync_sender) = self.sync_sender.get() {
// Send the certificate request to the sync module.
let _ = sync_sender.tx_certificate_request.send((peer_ip, certificate_request)).await;
}
Ok(())
}
Event::CertificateResponse(certificate_response) => {
// If a sync sender was provided, send the certificate response to the sync module.
if let Some(sync_sender) = self.sync_sender.get() {
// Send the certificate response to the sync module.
let _ = sync_sender.tx_certificate_response.send((peer_ip, certificate_response)).await;
}
Ok(())
}
Event::ChallengeRequest(..) | Event::ChallengeResponse(..) => {
// Disconnect as the peer is not following the protocol.
bail!("{CONTEXT} Peer '{peer_ip}' is not following the protocol")
}
Event::Disconnect(disconnect) => {
bail!("{CONTEXT} {:?}", disconnect.reason)
}
Event::PrimaryPing(ping) => {
let PrimaryPing { version, block_locators, primary_certificate } = ping;
// Ensure the event version is not outdated.
if version < Event::<N>::VERSION {
bail!("Dropping '{peer_ip}' on event version {version} (outdated)");
}
// If a sync sender was provided, update the peer locators.
if let Some(sync_sender) = self.sync_sender.get() {
// Check the block locators are valid, and update the validators in the sync module.
if let Err(error) = sync_sender.update_peer_locators(peer_ip, block_locators).await {
bail!("Validator '{peer_ip}' sent invalid block locators - {error}");
}
}
// Send the batch certificates to the primary.
let _ = self.primary_sender().tx_primary_ping.send((peer_ip, primary_certificate)).await;
Ok(())
}
Event::TransmissionRequest(request) => {
// TODO (howardwu): Add rate limiting checks on this event, on a per-peer basis.
// Determine the worker ID.
let Ok(worker_id) = assign_to_worker(request.transmission_id, self.num_workers()) else {
warn!("{CONTEXT} Unable to assign transmission ID '{}' to a worker", request.transmission_id);
return Ok(());
};
// Send the transmission request to the worker.
if let Some(sender) = self.get_worker_sender(worker_id) {
// Send the transmission request to the worker.
let _ = sender.tx_transmission_request.send((peer_ip, request)).await;
}
Ok(())
}
Event::TransmissionResponse(response) => {
// Determine the worker ID.
let Ok(worker_id) = assign_to_worker(response.transmission_id, self.num_workers()) else {
warn!("{CONTEXT} Unable to assign transmission ID '{}' to a worker", response.transmission_id);
return Ok(());
};
// Send the transmission response to the worker.
if let Some(sender) = self.get_worker_sender(worker_id) {
// Send the transmission response to the worker.
let _ = sender.tx_transmission_response.send((peer_ip, response)).await;
}
Ok(())
}
Event::ValidatorsRequest(_) => {
// Retrieve the connected peers.
let mut connected_peers: Vec<_> = match self.dev.is_some() {
// In development mode, relax the validity requirements to make operating devnets more flexible.
true => self.connected_peers.read().iter().copied().collect(),
// In production mode, ensure the peer IPs are valid.
false => {
self.connected_peers.read().iter().copied().filter(|ip| self.is_valid_peer_ip(*ip)).collect()
}
};
// Shuffle the connected peers.
connected_peers.shuffle(&mut rand::thread_rng());
let self_ = self.clone();
tokio::spawn(async move {
// Initialize the validators.
let mut validators = IndexMap::with_capacity(MAX_VALIDATORS_TO_SEND);
// Iterate over the validators.
for validator_ip in connected_peers.into_iter().take(MAX_VALIDATORS_TO_SEND) {
// Retrieve the validator address.
if let Some(validator_address) = self_.resolver.get_address(validator_ip) {
// Add the validator to the list of validators.
validators.insert(validator_ip, validator_address);
}
}
// Send the validators response to the peer.
let event = Event::ValidatorsResponse(ValidatorsResponse { validators });
Transport::send(&self_, peer_ip, event).await;
});
Ok(())
}
Event::ValidatorsResponse(response) => {
let ValidatorsResponse { validators } = response;
// Ensure the number of validators is not too large.
ensure!(validators.len() <= MAX_VALIDATORS_TO_SEND, "{CONTEXT} Received too many validators");
// Ensure the cache contains a validators request for this peer.
if !self.cache.contains_outbound_validators_request(peer_ip) {
bail!("{CONTEXT} Received validators response from '{peer_ip}' without a validators request")
}
// Decrement the number of validators requests for this peer.
self.cache.decrement_outbound_validators_requests(peer_ip);
// If the number of connected validators is less than the minimum, connect to more validators.
if self.number_of_connected_peers() < MIN_CONNECTED_VALIDATORS {
// Attempt to connect to any validators that are not already connected.
let self_ = self.clone();
tokio::spawn(async move {
for (validator_ip, validator_address) in validators {
if self_.dev.is_some() {
// Ensure the validator IP is not this node.
if self_.is_local_ip(validator_ip) {
continue;
}
} else {
// Ensure the validator IP is not this node and is well-formed.
if !self_.is_valid_peer_ip(validator_ip) {
continue;
}
}
// Ensure the validator address is not this node.
if self_.account.address() == validator_address {
continue;
}
// Ensure the validator IP is not already connected or connecting.
if self_.is_connected_ip(validator_ip) || self_.is_connecting_ip(validator_ip) {
continue;
}
// Ensure the validator address is not already connected.
if self_.is_connected_address(validator_address) {
continue;
}
// Ensure the validator address is an authorized validator.
if !self_.is_authorized_validator_address(validator_address) {
continue;
}
// Attempt to connect to the validator.
self_.connect(validator_ip);
}
});
}
Ok(())
}
Event::WorkerPing(ping) => {
// Ensure the number of transmissions is not too large.
ensure!(
ping.transmission_ids.len() <= Worker::<N>::MAX_TRANSMISSIONS_PER_WORKER_PING,
"{CONTEXT} Received too many transmissions"
);
// Retrieve the number of workers.
let num_workers = self.num_workers();
// Iterate over the transmission IDs.
for transmission_id in ping.transmission_ids.into_iter() {
// Determine the worker ID.
let Ok(worker_id) = assign_to_worker(transmission_id, num_workers) else {
warn!("{CONTEXT} Unable to assign transmission ID '{transmission_id}' to a worker");
continue;
};
// Send the transmission ID to the worker.
if let Some(sender) = self.get_worker_sender(worker_id) {
// Send the transmission ID to the worker.
let _ = sender.tx_worker_ping.send((peer_ip, transmission_id)).await;
}
}
Ok(())
}
}
}
/// Disconnects from the given peer IP, if the peer is connected.
pub fn disconnect(&self, peer_ip: SocketAddr) -> JoinHandle<()> {
let gateway = self.clone();
tokio::spawn(async move {
if let Some(peer_addr) = gateway.resolver.get_ambiguous(peer_ip) {
// Disconnect from this peer.
let _disconnected = gateway.tcp.disconnect(peer_addr).await;
debug_assert!(_disconnected);
}
})
}
/// Initialize a new instance of the heartbeat.
fn initialize_heartbeat(&self) {
let self_clone = self.clone();
self.spawn(async move {
// Sleep briefly to ensure the other nodes are ready to connect.
tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
info!("Starting the heartbeat of the gateway...");
loop {
// Process a heartbeat in the router.
self_clone.heartbeat();
// Sleep for the heartbeat interval.
tokio::time::sleep(Duration::from_secs(15)).await;
}
});
}
/// Spawns a task with the given future; it should only be used for long-running tasks.
#[allow(dead_code)]
fn spawn<T: Future<Output = ()> + Send + 'static>(&self, future: T) {
self.handles.lock().push(tokio::spawn(future));
}
/// Shuts down the gateway.
pub async fn shut_down(&self) {
info!("Shutting down the gateway...");
// Abort the tasks.
self.handles.lock().iter().for_each(|handle| handle.abort());
// Close the listener.
self.tcp.shut_down().await;
}
}
impl<N: Network> Gateway<N> {
/// Handles the heartbeat request.
fn heartbeat(&self) {
self.log_connected_validators();
// Keep the trusted validators connected.
self.handle_trusted_validators();
// Removes any validators that not in the current committee.
self.handle_unauthorized_validators();
// If the number of connected validators is less than the minimum, send a `ValidatorsRequest`.
self.handle_min_connected_validators();
}
/// Logs the connected validators.
fn log_connected_validators(&self) {
// Log the connected validators.
let validators = self.connected_peers().read().clone();
// Resolve the total number of connectable validators.
let validators_total = self.ledger.current_committee().map_or(0, |c| c.num_members().saturating_sub(1));
// Format the total validators message.
let total_validators = format!("(of {validators_total} bonded validators)").dimmed();
// Construct the connections message.
let connections_msg = match validators.len() {
0 => "No connected validators".to_string(),
num_connected => format!("Connected to {num_connected} validators {total_validators}"),
};
// Log the connected validators.
info!("{connections_msg}");
for peer_ip in validators {
let address = self.resolver.get_address(peer_ip).map_or("Unknown".to_string(), |a| a.to_string());
debug!("{}", format!(" {peer_ip} - {address}").dimmed());
}
}
/// This function attempts to connect to any disconnected trusted validators.
fn handle_trusted_validators(&self) {
// Ensure that the trusted nodes are connected.
for validator_ip in &self.trusted_validators {
// If the trusted_validator is not connected, attempt to connect to it.
if !self.is_local_ip(*validator_ip)
&& !self.is_connecting_ip(*validator_ip)
&& !self.is_connected_ip(*validator_ip)
{
// Attempt to connect to the trusted validator.
self.connect(*validator_ip);
}
}
}
/// This function attempts to disconnect any validators that are not in the current committee.
fn handle_unauthorized_validators(&self) {
let self_ = self.clone();
tokio::spawn(async move {
// Retrieve the connected validators.
let validators = self_.connected_peers().read().clone();
// Iterate over the validator IPs.
for peer_ip in validators {
// Disconnect any validator that is not in the current committee.
if !self_.is_authorized_validator_ip(peer_ip) {
warn!("{CONTEXT} Disconnecting from '{peer_ip}' - Validator is not in the current committee");
Transport::send(&self_, peer_ip, DisconnectReason::ProtocolViolation.into()).await;
// Disconnect from this peer.
self_.disconnect(peer_ip);
}
}
});
}
/// This function sends a `ValidatorsRequest` to a random validator,
/// if the number of connected validators is less than the minimum.
fn handle_min_connected_validators(&self) {
// If the number of connected validators is less than the minimum, send a `ValidatorsRequest`.
if self.number_of_connected_peers() < MIN_CONNECTED_VALIDATORS {
// Retrieve the connected validators.
let validators = self.connected_peers().read().clone();
// If there are no validator IPs to connect to, return early.
if validators.is_empty() {
return;
}
// Select a random validator IP.
if let Some(validator_ip) = validators.into_iter().choose(&mut rand::thread_rng()) {
let self_ = self.clone();
tokio::spawn(async move {
// Increment the number of outbound validators requests for this validator.
self_.cache.increment_outbound_validators_requests(validator_ip);
// Send a `ValidatorsRequest` to the validator.
let _ = Transport::send(&self_, validator_ip, Event::ValidatorsRequest(ValidatorsRequest)).await;
});
}
}
}
}
#[async_trait]
impl<N: Network> Transport<N> for Gateway<N> {
/// Sends the given event to specified peer.
///
/// This method is rate limited to prevent spamming the peer.
///
/// This function returns as soon as the event is queued to be sent,
/// without waiting for the actual delivery; instead, the caller is provided with a [`oneshot::Receiver`]
/// which can be used to determine when and whether the event has been delivered.
async fn send(&self, peer_ip: SocketAddr, event: Event<N>) -> Option<oneshot::Receiver<io::Result<()>>> {
macro_rules! send {
($self:ident, $cache_map:ident, $interval:expr, $freq:ident) => {{
// Rate limit the number of certificate requests sent to the peer.
while $self.cache.$cache_map(peer_ip, $interval) > $self.$freq() {
// Sleep for a short period of time to allow the cache to clear.
tokio::time::sleep(Duration::from_millis(10)).await;
}
// Send the event to the peer.
$self.send_inner(peer_ip, event)
}};
}
// If the event type is a certificate request, increment the cache.
if matches!(event, Event::CertificateRequest(_)) | matches!(event, Event::CertificateResponse(_)) {
// Update the outbound event cache. This is necessary to ensure we don't under count the outbound events.
self.cache.insert_outbound_event(peer_ip, CACHE_EVENTS_INTERVAL);
// Send the event to the peer.
send!(self, insert_outbound_certificate, CACHE_REQUESTS_INTERVAL, max_cache_certificates)
}
// If the event type is a transmission request, increment the cache.
else if matches!(event, Event::TransmissionRequest(_)) | matches!(event, Event::TransmissionResponse(_)) {
// Update the outbound event cache. This is necessary to ensure we don't under count the outbound events.
self.cache.insert_outbound_event(peer_ip, CACHE_EVENTS_INTERVAL);
// Send the event to the peer.
send!(self, insert_outbound_transmission, CACHE_REQUESTS_INTERVAL, max_cache_transmissions)
}
// Otherwise, employ a general rate limit.
else {
// Send the event to the peer.
send!(self, insert_outbound_event, CACHE_EVENTS_INTERVAL, max_cache_events)
}