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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
// Copyright (c) 2019-2026 Provable 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::{
helpers::{PeerPair, PrepareSyncRequest, SyncRequest},
locators::BlockLocators,
};
use snarkos_node_bft_ledger_service::LedgerService;
use snarkos_node_network::PeerPoolHandling;
use snarkos_node_router::messages::DataBlocks;
use snarkos_node_sync_communication_service::CommunicationService;
use snarkos_node_sync_locators::{CHECKPOINT_INTERVAL, NUM_RECENT_BLOCKS};
use snarkvm::{
console::network::{ConsensusVersion, Network},
prelude::block::Block,
utilities::flatten_error,
};
use anyhow::{Result, bail, ensure};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
#[cfg(feature = "locktick")]
use locktick::parking_lot::RwLock;
#[cfg(feature = "locktick")]
use locktick::tokio::Mutex as TMutex;
#[cfg(not(feature = "locktick"))]
use parking_lot::RwLock;
use rand::seq::{IteratorRandom, SliceRandom};
use std::{
collections::{BTreeMap, HashMap, HashSet, hash_map},
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::Arc,
time::{Duration, Instant},
};
#[cfg(not(feature = "locktick"))]
use tokio::sync::Mutex as TMutex;
use tokio::sync::Notify;
mod helpers;
use helpers::rangify_heights;
mod sync_state;
use sync_state::SyncState;
mod metrics;
use metrics::BlockSyncMetrics;
// The redundancy factor decreases the possibility of a malicious peers sending us an invalid block locator
// by requiring multiple peers to advertise the same (prefix of) block locators.
// However, we do not use this in production yet.
#[cfg(not(test))]
pub const REDUNDANCY_FACTOR: usize = 1;
#[cfg(test)]
pub const REDUNDANCY_FACTOR: usize = 3;
/// The time nodes wait between issuing batches of block requests to avoid triggering spam detection.
///
/// The current rate limit for all messages is around 160k per second (see [`Gateway::max_cache_events`]).
/// This constant limits number of block requests to a much lower 100 per second.
///
// TODO(kaimast): base rate limits on how many requests were sent to each peer instead.
pub const BLOCK_REQUEST_BATCH_DELAY: Duration = Duration::from_millis(10);
const EXTRA_REDUNDANCY_FACTOR: usize = REDUNDANCY_FACTOR * 3;
const NUM_SYNC_CANDIDATE_PEERS: usize = REDUNDANCY_FACTOR * 5;
const BLOCK_REQUEST_TIMEOUT: Duration = Duration::from_secs(600);
/// The maximum number of outstanding block requests.
/// Once a node hits this limit, it will not issue any new requests until existing requests time out or receive responses.
const MAX_BLOCK_REQUESTS: usize = 50; // 50 requests
/// The maximum number of blocks tolerated before the primary is considered behind its peers.
pub const MAX_BLOCKS_BEHIND: u32 = 1; // blocks
/// This is a dummy IP address that is used to represent the local node.
/// Note: This here does not need to be a real IP address, but it must be unique/distinct from all other connections.
pub const DUMMY_SELF_IP: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
/// Handle to an outstanding requested, containing the request itself and its timestamp.
/// This does not contain the response so that checking for responses does not require iterating over all requests.
#[derive(Clone)]
struct OutstandingRequest<N: Network> {
request: SyncRequest<N>,
timestamp: Instant,
/// The corresponding response (if any).
/// This is guaranteed to be Some if sync_ips for the given request are empty.
response: Option<Block<N>>,
}
/// Information about a block request (used for the REST API).
#[derive(Clone, serde::Serialize)]
pub struct BlockRequestInfo {
/// Seconds since the request was created
elapsed: u64,
/// Has the request been responded to?
done: bool,
}
/// Summary of completed all in-flight requests.
#[derive(Clone, serde::Serialize)]
pub struct BlockRequestsSummary {
outstanding: String,
completed: String,
}
#[derive(thiserror::Error, Debug)]
pub enum InsertBlockResponseError<N: Network> {
#[error("Empty block response")]
EmptyBlockResponse,
#[error("The peer did not send a consensus version")]
NoConsensusVersion,
#[error(
"The peer's consensus version for height {last_height} does not match ours: expected {expected_version}, got {peer_version}"
)]
ConsensusVersionMismatch { peer_version: ConsensusVersion, expected_version: ConsensusVersion, last_height: u32 },
#[error("Block Sync already advanced to block {height}")]
BlockSyncAlreadyAdvanced { height: u32 },
#[error("No such request for height {height}")]
NoSuchRequest { height: u32 },
#[error("Invalid block hash for height {height} from '{peer_ip}'")]
InvalidBlockHash { height: u32, peer_ip: SocketAddr },
#[error(
"The previous block hash in candidate block {height} from '{peer_ip}' is incorrect: expected {expected}, but got {actual}"
)]
InvalidPreviousBlockHash { height: u32, peer_ip: SocketAddr, expected: N::BlockHash, actual: N::BlockHash },
#[error("Candidate block {height} from '{peer_ip}' is malformed")]
MalformedBlock { height: u32, peer_ip: SocketAddr },
#[error("The sync pool did not request block {height} from '{peer_ip}'")]
WrongSyncPeer { height: u32, peer_ip: SocketAddr },
#[error("{}", flatten_error(.0))]
Other(#[from] anyhow::Error),
}
impl<N: Network> InsertBlockResponseError<N> {
/// Returns `true` if the error does not indicate malicious or faulty behavior.
pub fn is_benign(&self) -> bool {
matches!(self, Self::NoSuchRequest { .. } | Self::BlockSyncAlreadyAdvanced { .. })
}
// Returns true if the error is about an invalid consensus version.
pub fn is_invalid_consensus_version(&self) -> bool {
matches!(self, Self::ConsensusVersionMismatch { .. } | Self::NoConsensusVersion)
}
}
impl<N: Network> OutstandingRequest<N> {
/// Get a reference to the IPs of peers that have not responded to the request (yet).
fn sync_ips(&self) -> &IndexSet<SocketAddr> {
let (_, _, sync_ips) = &self.request;
sync_ips
}
/// Get a mutable reference to the IPs of peers that have not responded to the request (yet).
fn sync_ips_mut(&mut self) -> &mut IndexSet<SocketAddr> {
let (_, _, sync_ips) = &mut self.request;
sync_ips
}
}
/// A struct that keeps track of synchronizing blocks with other nodes.
///
/// It generates requests to send to other peers and processes responses to those requests.
/// The struct also keeps track of block locators, which indicate which peers it can fetch blocks from.
///
/// # Notes
/// - The actual network communication happens in `snarkos_node::Client` (for clients and provers) and in `snarkos_node_bft::Sync` (for validators).
///
/// - Validators only sync from other nodes using this struct if they fall behind, e.g.,
/// because they experience a network partition.
/// In the common case, validators will generate blocks from the DAG after an anchor certificate has been approved
/// by a supermajority of the committee.
pub struct BlockSync<N: Network> {
/// The ledger.
ledger: Arc<dyn LedgerService<N>>,
/// The map of peer IP to their block locators.
/// The block locators are consistent with the ledger and every other peer's block locators.
locators: RwLock<HashMap<SocketAddr, BlockLocators<N>>>,
/// The map of peer-to-peer to their common ancestor.
/// This map is used to determine which peers to request blocks from.
///
/// Lock ordering: when locking both, `common_ancestors` and `locators`, `common_ancestors` must be locked first.
common_ancestors: RwLock<IndexMap<PeerPair, u32>>,
/// The block requests in progress and their responses.
requests: RwLock<BTreeMap<u32, OutstandingRequest<N>>>,
/// The boolean indicator of whether the node is synced up to the latest block (within the given tolerance).
///
/// Lock ordering: if you lock `sync_state` and `requests`, you must lock `sync_state` first.
sync_state: RwLock<SyncState>,
/// The lock used to ensure that [`Self::advance_with_sync_blocks()`] is called by one task at a time.
advance_with_sync_blocks_lock: TMutex<()>,
/// Gets notified when there was an update to the locators or a peer disconnected.
peer_notify: Notify,
/// Gets notified when we received a new block response.
response_notify: Notify,
/// Tracks sync speed
metrics: BlockSyncMetrics,
}
impl<N: Network> BlockSync<N> {
/// Initializes a new block sync module.
pub fn new(ledger: Arc<dyn LedgerService<N>>) -> Self {
// Make sync state aware of the blocks that already exist on disk at startup.
let sync_state = SyncState::new_with_height(ledger.latest_block_height());
Self {
ledger,
sync_state: RwLock::new(sync_state),
peer_notify: Default::default(),
response_notify: Default::default(),
locators: Default::default(),
requests: Default::default(),
common_ancestors: Default::default(),
advance_with_sync_blocks_lock: Default::default(),
metrics: Default::default(),
}
}
/// Blocks until something about a peer changes,
/// or block request has been fully processed (either successfully or unsuccessfully).
///
/// Used by the outgoing task.
pub async fn wait_for_peer_update(&self) {
self.peer_notify.notified().await
}
/// Blocks until there is a new response to a block request.
///
/// Used by the incoming task.
pub async fn wait_for_block_responses(&self) {
self.response_notify.notified().await
}
/// Returns `true` if the node is synced up to the latest block (within the given tolerance).
#[inline]
pub fn is_block_synced(&self) -> bool {
self.sync_state.read().is_block_synced()
}
/// Returns `true` if there a blocks to fetch or responses to process.
///
/// This will always return true if [`Self::is_block_synced`] returns false,
/// but it can return true when [`Self::is_block_synced`] returns true
/// (due to the latter having a tolerance of one block).
#[inline]
pub fn can_block_sync(&self) -> bool {
self.sync_state.read().can_block_sync() || self.has_pending_responses()
}
/// Returns the number of blocks the node is behind the greatest peer height,
/// or `None` if no peers are connected yet.
#[inline]
pub fn num_blocks_behind(&self) -> Option<u32> {
self.sync_state.read().num_blocks_behind()
}
/// Returns the greatest block height of any connected peer.
#[inline]
pub fn greatest_peer_block_height(&self) -> Option<u32> {
self.sync_state.read().get_greatest_peer_height()
}
/// Returns the current sync height of this node.
/// The sync height is always greater or equal to the ledger height.
#[inline]
pub fn get_sync_height(&self) -> u32 {
self.sync_state.read().get_sync_height()
}
/// Returns the number of blocks we requested from peers, but have not received yet.
#[inline]
pub fn num_outstanding_block_requests(&self) -> usize {
self.requests.read().iter().filter(|(_, e)| !e.sync_ips().is_empty()).count()
}
/// The total number of block request, including the ones that have been answered already but not processed yet.
#[inline]
pub fn num_total_block_requests(&self) -> usize {
self.requests.read().len()
}
//// Returns the latest locator height for all known peers.
pub fn get_peer_heights(&self) -> HashMap<SocketAddr, u32> {
self.locators.read().iter().map(|(addr, locators)| (*addr, locators.latest_locator_height())).collect()
}
//// Returns information about all in-flight block requests.
pub fn get_block_requests_info(&self) -> BTreeMap<u32, BlockRequestInfo> {
self.requests
.read()
.iter()
.map(|(height, request)| {
(*height, BlockRequestInfo {
done: request.sync_ips().is_empty(),
elapsed: request.timestamp.elapsed().as_secs(),
})
})
.collect()
}
/// Returns a summary of all in-flight requests.
pub fn get_block_requests_summary(&self) -> BlockRequestsSummary {
let completed = self
.requests
.read()
.iter()
.filter_map(|(h, e)| if e.sync_ips().is_empty() { Some(*h) } else { None })
.collect::<Vec<_>>();
let outstanding = self
.requests
.read()
.iter()
.filter_map(|(h, e)| if !e.sync_ips().is_empty() { Some(*h) } else { None })
.collect::<Vec<_>>();
BlockRequestsSummary { completed: rangify_heights(&completed), outstanding: rangify_heights(&outstanding) }
}
pub fn get_sync_speed(&self) -> f64 {
self.metrics.get_sync_speed()
}
}
// Helper functions needed for testing
#[cfg(test)]
impl<N: Network> BlockSync<N> {
/// Returns the latest block height of the given peer IP.
fn get_peer_height(&self, peer_ip: &SocketAddr) -> Option<u32> {
self.locators.read().get(peer_ip).map(|locators| locators.latest_locator_height())
}
/// Returns the common ancestor for the given peer pair, if it exists.
fn get_common_ancestor(&self, peer_a: SocketAddr, peer_b: SocketAddr) -> Option<u32> {
self.common_ancestors.read().get(&PeerPair(peer_a, peer_b)).copied()
}
/// Returns the block request for the given height, if it exists.
fn get_block_request(&self, height: u32) -> Option<SyncRequest<N>> {
self.requests.read().get(&height).map(|e| e.request.clone())
}
/// Returns the timestamp of the last time the block was requested, if it exists.
fn get_block_request_timestamp(&self, height: u32) -> Option<Instant> {
self.requests.read().get(&height).map(|e| e.timestamp)
}
}
impl<N: Network> BlockSync<N> {
/// Returns the block locators.
#[inline]
pub fn get_block_locators(&self) -> Result<BlockLocators<N>> {
// Retrieve the latest block height.
let latest_height = self.ledger.latest_block_height();
// Initialize the recents map.
// TODO: generalize this for RECENT_INTERVAL > 1, or remove this comment if we hardwire that to 1
let mut recents = IndexMap::with_capacity(NUM_RECENT_BLOCKS);
// Retrieve the recent block hashes.
for height in latest_height.saturating_sub((NUM_RECENT_BLOCKS - 1) as u32)..=latest_height {
recents.insert(height, self.ledger.get_block_hash(height)?);
}
// Initialize the checkpoints map.
let mut checkpoints = IndexMap::with_capacity((latest_height / CHECKPOINT_INTERVAL + 1).try_into()?);
// Retrieve the checkpoint block hashes.
for height in (0..=latest_height).step_by(CHECKPOINT_INTERVAL as usize) {
checkpoints.insert(height, self.ledger.get_block_hash(height)?);
}
// Construct the block locators.
BlockLocators::new(recents, checkpoints)
}
/// Returns true if there are pending responses to block requests that need to be processed.
pub fn has_pending_responses(&self) -> bool {
self.requests.read().iter().filter(|(_, req)| req.response.is_some() && req.sync_ips().is_empty()).count() > 0
}
/// Send a batch of block requests.
pub async fn send_block_requests<C: CommunicationService>(
&self,
communication: &C,
sync_peers: &IndexMap<SocketAddr, BlockLocators<N>>,
requests: &[(u32, PrepareSyncRequest<N>)],
) -> bool {
let (start_height, max_num_sync_ips) = match requests.first() {
Some((height, (_, _, max_num_sync_ips))) => (*height, *max_num_sync_ips),
None => {
warn!("Block sync failed - no block requests");
return false;
}
};
debug!("Sending {len} block requests to peer(s) at {peers:?}", len = requests.len(), peers = sync_peers.keys());
// Use a randomly sampled subset of the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().copied().choose_multiple(&mut rand::thread_rng(), max_num_sync_ips).into_iter().collect();
// Calculate the end height.
let end_height = start_height.saturating_add(requests.len() as u32);
// Insert the chunk of block requests.
for (height, (hash, previous_hash, _)) in requests.iter() {
// Insert the block request into the sync pool using the sync IPs from the last block request in the chunk.
if let Err(err) = self.insert_block_request(*height, (*hash, *previous_hash, sync_ips.clone())) {
let err = err.context(format!("Failed to insert block request for height {height}"));
warn!("{}", flatten_error(&err));
return false;
}
}
/* Send the block request to the peers */
// Construct the message.
let message = C::prepare_block_request(start_height, end_height);
// Send the message to the peers.
let mut tasks = Vec::with_capacity(sync_ips.len());
for sync_ip in sync_ips {
let sender = communication.send(sync_ip, message.clone()).await;
let task = tokio::spawn(async move {
// Ensure the request is sent successfully.
match sender {
Some(sender) => {
if let Err(err) = sender.await {
warn!("Failed to send block request to peer '{sync_ip}': {err}");
false
} else {
true
}
}
None => {
warn!("Failed to send block request to peer '{sync_ip}': no such peer");
false
}
}
});
tasks.push(task);
}
// Wait for all sends to finish at the same time.
for result in futures::future::join_all(tasks).await {
let success = match result {
Ok(success) => success,
Err(err) => {
error!("tokio join error: {err}");
false
}
};
// If sending fails for any peer, remove the block request from the sync pool.
if !success {
// Remove the entire block request from the sync pool.
let mut requests = self.requests.write();
for height in start_height..end_height {
requests.remove(&height);
}
// Break out of the loop.
return false;
}
}
true
}
/// Inserts a new block response from the given peer IP.
///
/// Returns an error if the block was malformed, or we already received a different block for this height.
/// This function also removes all block requests from the given peer IP on failure.
///
/// Note, that this only queues the response. After this, you most likely want to call `Self::try_advancing_block_synchronization`.
///
#[inline]
pub fn insert_block_responses(
&self,
peer_ip: SocketAddr,
blocks: Vec<Block<N>>,
latest_consensus_version: Option<ConsensusVersion>,
) -> Result<(), InsertBlockResponseError<N>> {
// Attempt to insert the block responses, and break if we encounter an error.
let result = 'outer: {
let Some(last_height) = blocks.as_slice().last().map(|b| b.height()) else {
break 'outer Err(InsertBlockResponseError::EmptyBlockResponse);
};
let expected_consensus_version = N::CONSENSUS_VERSION(last_height)?;
// Perform consensus version check, if possible.
// This check is only enabled after nodes have reached V12.
if expected_consensus_version >= ConsensusVersion::V12 {
if let Some(peer_version) = latest_consensus_version {
if peer_version != expected_consensus_version {
break 'outer Err(InsertBlockResponseError::ConsensusVersionMismatch {
peer_version,
expected_version: expected_consensus_version,
last_height,
});
}
} else {
break 'outer Err(InsertBlockResponseError::NoConsensusVersion);
}
}
// Insert the candidate blocks into the sync pool.
for block in blocks {
self.insert_block_response(peer_ip, block)?;
}
Ok(())
};
// On failure, remove all block requests to the peer.
if result.is_err() {
self.remove_block_requests_to_peer(&peer_ip);
}
// Return the result.
result
}
/// Returns the next block for the given `next_height` if the request is complete,
/// or `None` otherwise. This does not remove the block from the `responses` map.
#[inline]
pub fn peek_next_block(&self, next_height: u32) -> Option<Block<N>> {
// Determine if the request is complete:
// either there is no request for `next_height`, or the request has no peer socket addresses left.
if let Some(entry) = self.requests.read().get(&next_height) {
let is_complete = entry.sync_ips().is_empty();
if !is_complete {
return None;
}
// If the request is complete, return the block from the responses, if there is one.
if entry.response.is_none() {
warn!("Request for height {next_height} is complete but no response exists");
}
entry.response.clone()
} else {
None
}
}
/// Attempts to advance synchronization by processing completed block responses.
///
/// Returns true, if new blocks were added to the ledger.
///
/// # Usage
/// This is only called in [`Client::try_block_sync`] and should not be called concurrently by multiple tasks.
/// Validators do not call this function, and instead invoke
/// [`snarkos_node_bft::Sync::try_advancing_block_synchronization`] which also updates the BFT state.
#[inline]
pub async fn try_advancing_block_synchronization(&self) -> Result<bool> {
// Acquire the lock to ensure this function is called only once at a time.
// If the lock is already acquired, return early.
//
// Note: This lock should not be needed anymore as there is only one place we call it from,
// but we keep it for now out of caution.
// TODO(kaimast): remove this eventually.
let Ok(_lock) = self.advance_with_sync_blocks_lock.try_lock() else {
trace!("Skipping attempt to advance block synchronziation as it is already in progress");
return Ok(false);
};
// Start with the current height.
let mut current_height = self.ledger.latest_block_height();
let start_height = current_height;
trace!(
"Try advancing with block responses (at block {current_height}, current sync speed is {})",
self.get_sync_speed()
);
loop {
let next_height = current_height + 1;
let Some(block) = self.peek_next_block(next_height) else {
break;
};
// Ensure the block height matches.
if block.height() != next_height {
warn!("Block height mismatch: expected {}, found {}", current_height + 1, block.height());
break;
}
let ledger = self.ledger.clone();
let advanced = tokio::task::spawn_blocking(move || {
// Try to check the next block and advance to it.
match ledger.check_next_block(&block) {
Ok(_) => match ledger.advance_to_next_block(&block) {
Ok(_) => true,
Err(err) => {
let err = err.context(format!(
"Failed to advance to next block (height: {}, hash: '{}')",
block.height(),
block.hash()
));
warn!("{}", flatten_error(&err));
false
}
},
Err(err) => {
let err = err.context(format!(
"The next block (height: {}, hash: '{}') is invalid",
block.height(),
block.hash()
));
warn!("{}", flatten_error(&err));
false
}
}
})
.await?;
// Only count successful requests.
if advanced {
self.count_request_completed();
}
// Remove the block response.
self.remove_block_response(next_height);
// If advancing failed, exit the loop.
if !advanced {
break;
}
// Update the latest height.
current_height = next_height;
}
if current_height > start_height {
self.set_sync_height(current_height);
Ok(true)
} else {
Ok(false)
}
}
}
impl<N: Network> BlockSync<N> {
/// Returns the sync peers with their latest heights, and their minimum common ancestor, if the node can sync.
/// This function returns peers that are consistent with each other, and have a block height
/// that is greater than the ledger height of this node.
///
/// # Locking
/// This will read-lock `common_ancestors` and `sync_state`, but not at the same time.
pub fn find_sync_peers(&self) -> Option<(IndexMap<SocketAddr, u32>, u32)> {
// Retrieve the current sync height.
let current_height = self.get_sync_height();
if let Some((sync_peers, min_common_ancestor)) = self.find_sync_peers_inner(current_height) {
// Map the locators into the latest height.
let sync_peers =
sync_peers.into_iter().map(|(ip, locators)| (ip, locators.latest_locator_height())).collect();
// Return the sync peers and their minimum common ancestor.
Some((sync_peers, min_common_ancestor))
} else {
None
}
}
/// Updates the block locators and common ancestors for the given peer IP.
///
/// This function does not need to check that the block locators are well-formed,
/// because that is already done in [`BlockLocators::new()`], as noted in [`BlockLocators`].
///
/// This function does **not** check
/// that the block locators are consistent with the peer's previous block locators or other peers' block locators.
pub fn update_peer_locators(&self, peer_ip: SocketAddr, locators: &BlockLocators<N>) -> Result<()> {
// -- First, update the locators entry for the given peer IP. --
// We perform this update atomically, and drop the lock as soon as we are done with the update.
match self.locators.write().entry(peer_ip) {
hash_map::Entry::Occupied(mut e) => {
// Return early if the block locators did not change.
if e.get() == locators {
return Ok(());
}
let old_height = e.get().latest_locator_height();
let new_height = locators.latest_locator_height();
if old_height > new_height {
debug!("Block height for peer {peer_ip} decreased from {old_height} to {new_height}",);
}
e.insert(locators.clone());
}
hash_map::Entry::Vacant(e) => {
e.insert(locators.clone());
}
}
// -- Second, compute the common ancestor with this node. --
let new_local_ancestor = {
let mut ancestor = 0;
// Attention: Please do not optimize this loop, as it performs fork-detection. In addition,
// by iterating upwards, it also early-terminates malicious block locators at the *first* point
// of bifurcation in their ledger history, which is a critical safety guarantee provided here.
for (height, hash) in locators.clone().into_iter() {
if let Ok(ledger_hash) = self.ledger.get_block_hash(height) {
match ledger_hash == hash {
true => ancestor = height,
false => {
warn!("Detected fork between this node and peer \"{peer_ip}\" at height {height}");
break;
}
}
}
}
ancestor
};
// -- Third, compute the common ancestor with every other peer, and determine if this peer is forked from others. --
// Do not hold write lock to `common_ancestors` here, because this can take a while with many peers.
let ancestor_updates: Vec<_> = self
.locators
.read()
.iter()
.filter_map(|(other_ip, other_locators)| {
// Skip if the other peer is the given peer.
if other_ip == &peer_ip {
return None;
}
// Compute the common ancestor with the other peer.
let mut ancestor = 0;
for (height, hash) in other_locators.clone().into_iter() {
if let Some(expected_hash) = locators.get_hash(height) {
match expected_hash == hash {
true => ancestor = height,
false => {
debug!(
"Detected fork between peers \"{other_ip}\" and \"{peer_ip}\" at height {height}"
);
break;
}
}
}
}
Some((PeerPair(peer_ip, *other_ip), ancestor))
})
.collect();
// -- Forth, update the map of common ancestors. --
// Scope the lock, so it is dropped before locking `sync_state`.
{
let mut common_ancestors = self.common_ancestors.write();
common_ancestors.insert(PeerPair(DUMMY_SELF_IP, peer_ip), new_local_ancestor);
for (peer_pair, new_ancestor) in ancestor_updates.into_iter() {
common_ancestors.insert(peer_pair, new_ancestor);
}
}
// -- Finally, update sync state and notify the sync loop about the change. --
if let Some(greatest_peer_height) = self.locators.read().values().map(|l| l.latest_locator_height()).max() {
self.sync_state.write().set_greatest_peer_height(greatest_peer_height);
} else {
error!("Got new block locators but greatest peer height is zero.");
}
// Even if the greatest peer height did not change, we still received new block locators
// that the sync loop might need to proceed.
self.peer_notify.notify_one();
Ok(())
}
/// TODO (howardwu): Remove the `common_ancestor` entry. But check that this is safe
/// (that we don't rely upon it for safety when we re-connect with the same peer).
/// Removes the peer from the sync pool, if they exist.
pub fn remove_peer(&self, peer_ip: &SocketAddr) {
trace!("Removing peer {peer_ip} from block sync");
// Remove the locators entry for the given peer IP.
self.locators.write().remove(peer_ip);
// Remove all common ancestor entries for this peers.
self.common_ancestors.write().retain(|pair, _| !pair.contains(peer_ip));
// Remove all block requests to the peer.
self.remove_block_requests_to_peer(peer_ip);
// Update sync state, because the greatest peer height may have decreased.
if let Some(greatest_peer_height) = self.locators.read().values().map(|l| l.latest_locator_height()).max() {
self.sync_state.write().set_greatest_peer_height(greatest_peer_height);
} else {
// There are no more peers left.
self.sync_state.write().clear_greatest_peer_height();
}
// Notify the sync loop that something changed.
self.peer_notify.notify_one();
}
}
// Helper type for prepare_block_requests
pub type BlockRequestBatch<N> = (Vec<(u32, PrepareSyncRequest<N>)>, IndexMap<SocketAddr, BlockLocators<N>>);
impl<N: Network> BlockSync<N> {
/// Returns a list of block requests and the sync peers, if the node needs to sync.
///
/// You usually want to call `remove_timed_out_block_requests` before invoking this function.
///
/// # Concurrency
/// This should be called by at most one task at a time.
///
/// # Usage
/// - For validators, the primary spawns exactly one task that periodically calls
/// `bft::Sync::try_issuing_block_requests`. There is no possibility of concurrent calls to it.
/// - For clients, `Client::initialize_sync` spawn exactly one task that periodically calls
/// `Client::try_issuing_block_requests` which calls this function.
/// - Provers do not call this function.
pub fn prepare_block_requests(&self) -> BlockRequestBatch<N> {
// Used to print more information when we max out on requests.
let print_requests = || {
if tracing::enabled!(tracing::Level::TRACE) {
let summary = self.get_block_requests_summary();
trace!("The following requests are complete but not processed yet: {:?}", summary.completed);
trace!("The following requests are still outstanding: {:?}", summary.outstanding);
}
};
// Do not hold lock here as, currently, `find_sync_peers_inner` can take a while.
let current_height = self.get_sync_height();
// Ensure to not exceed the maximum number of outstanding block requests.
let max_outstanding_block_requests =
(MAX_BLOCK_REQUESTS as u32) * (DataBlocks::<N>::MAXIMUM_NUMBER_OF_BLOCKS as u32);
// Ensure there is a finite bound on the number of block respnoses we receive, that have not been processed yet.
let max_total_requests = 4 * max_outstanding_block_requests;
let max_new_blocks_to_request =
max_outstanding_block_requests.saturating_sub(self.num_outstanding_block_requests() as u32);
// Prepare the block requests and sync peers, or returns an empty result if there is nothing to request.
if self.num_total_block_requests() >= max_total_requests as usize {
trace!(
"We are already requested at least {max_total_requests} blocks that have not been fully processed yet. Will not issue more."
);
print_requests();
Default::default()
} else if max_new_blocks_to_request == 0 {
trace!(
"Already reached the maximum number of outstanding blocks ({max_outstanding_block_requests}). Will not issue more."
);
print_requests();
Default::default()
} else if let Some((sync_peers, min_common_ancestor)) = self.find_sync_peers_inner(current_height) {
// Retrieve the greatest block height of any connected peer.
// We do not need to update the sync state here, as that already happens when the block locators are received.
let greatest_peer_height = sync_peers.values().map(|l| l.latest_locator_height()).max().unwrap_or(0);
// Construct the list of block requests.
let requests = self.construct_requests(
&sync_peers,
current_height,
min_common_ancestor,
max_new_blocks_to_request,
greatest_peer_height,
);
(requests, sync_peers)
} else if self.requests.read().is_empty() {
// This can happen during a race condition where the node just finished syncing.
// It does not make sense to log or change the sync status here.
// Checking the sync status here also does not make sense, as the node might as well have switched back
// from `synced` to `syncing` between calling `find_sync_peers_inner` and this line.
Default::default()
} else {
// This happens if we already requested all advertised blocks.
trace!("No new blocks can be requested, but there are still outstanding requests.");
print_requests();
Default::default()
}
}
/// Should only be called by validators when they successfully process a block request.
/// (for other nodes this will be automatically called internally)
///
/// TODO(kaimast): remove this public function once the sync logic is fully unified `BlockSync`.
pub fn count_request_completed(&self) {
self.metrics.count_request_completed();
}
/// Set the sync height to a the given value.
/// This is a no-op if `new_height` is equal or less to the current sync height.
pub fn set_sync_height(&self, new_height: u32) {
// Scope state lock to avoid locking state and metrics at the same time.
let fully_synced = {
let mut state = self.sync_state.write();
state.set_sync_height(new_height);
!state.can_block_sync()
};
if fully_synced {
self.metrics.mark_fully_synced();
}
}
/// Inserts a block request for the given height.
fn insert_block_request(&self, height: u32, (hash, previous_hash, sync_ips): SyncRequest<N>) -> Result<()> {
// Ensure the block request does not already exist.
self.check_block_request(height)?;
// Ensure the sync IPs are not empty.
ensure!(!sync_ips.is_empty(), "Cannot insert a block request with no sync IPs");
// Insert the block request.
self.requests.write().insert(height, OutstandingRequest {
request: (hash, previous_hash, sync_ips),
timestamp: Instant::now(),
response: None,
});
Ok(())
}
/// Inserts the given block response, after checking that the request exists and the response is well-formed.
/// On success, this function removes the peer IP from the request sync peers and inserts the response.
fn insert_block_response(&self, peer_ip: SocketAddr, block: Block<N>) -> Result<(), InsertBlockResponseError<N>> {
// Retrieve the block height.
let height = block.height();
let mut requests = self.requests.write();
if self.ledger.contains_block_height(height) {
return Err(InsertBlockResponseError::BlockSyncAlreadyAdvanced { height });
}
let Some(entry) = requests.get_mut(&height) else {
return Err(InsertBlockResponseError::NoSuchRequest { height });
};
// Retrieve the request entry for the candidate block.
let (expected_hash, expected_previous_hash, sync_ips) = &entry.request;
// Ensure the candidate block hash matches the expected hash.
if let Some(expected_hash) = expected_hash
&& block.hash() != *expected_hash
{
return Err(InsertBlockResponseError::InvalidBlockHash { height, peer_ip });
}
// Ensure the previous block hash matches if it exists.
if let Some(expected_previous_hash) = expected_previous_hash
&& block.previous_hash() != *expected_previous_hash
{
return Err(InsertBlockResponseError::InvalidPreviousBlockHash {
height,
peer_ip,
expected: *expected_previous_hash,
actual: block.previous_hash(),
});
}
// Ensure the sync pool requested this block from the given peer.
if !sync_ips.contains(&peer_ip) {
return Err(InsertBlockResponseError::WrongSyncPeer { height, peer_ip });
}
// Remove the peer IP from the request entry.
entry.sync_ips_mut().swap_remove(&peer_ip);
if let Some(existing_block) = &entry.response {
// If the candidate block was already present, ensure it is the same block.
if block != *existing_block {
return Err(InsertBlockResponseError::MalformedBlock { height, peer_ip });
}
} else {
entry.response = Some(block.clone());
}
trace!("Received a new and valid block response for height {height}");
// Notify the sync loop that something changed.
self.response_notify.notify_one();
Ok(())
}
/// Checks that a block request for the given height does not already exist.
fn check_block_request(&self, height: u32) -> Result<()> {
// Ensure the block height is not already in the ledger.
if self.ledger.contains_block_height(height) {
bail!("Failed to add block request, as block {height} exists in the ledger");
}
// Ensure the block height is not already requested.
if self.requests.read().contains_key(&height) {
bail!("Failed to add block request, as block {height} exists in the requests map");
}
Ok(())
}
/// Removes the block request and response for the given height
/// This may only be called after `peek_next_block`, which checked if the request for the given height was complete.
///
/// Precondition: This may only be called after `peek_next_block` has returned `Some`,
/// which has checked if the request for the given height is complete
/// and there is a block with the given `height` in the `responses` map.
pub fn remove_block_response(&self, height: u32) {
// Remove the request entry for the given height.
if let Some(e) = self.requests.write().remove(&height) {
trace!(
"Block request for height {height} was completed in {}ms (sync speed is {})",
e.timestamp.elapsed().as_millis(),
self.get_sync_speed()
);
// Notify the sending task that less requests are in-flight.
self.peer_notify.notify_one();
}
}
/// Removes all block requests for the given peer IP.
///
/// This is used when disconnecting from a peer or when a peer sends invalid block responses.
fn remove_block_requests_to_peer(&self, peer_ip: &SocketAddr) {
trace!("Block sync is removing all block requests to peer {peer_ip}...");
// Remove the peer IP from the requests map. If any request entry is now empty,
// and its corresponding response entry is also empty, then remove that request entry altogether.
self.requests.write().retain(|height, e| {
let had_peer = e.sync_ips_mut().swap_remove(peer_ip);
// Only remove requests that were sent to this peer, that have no other peer that can respond instead,
// and that were not completed yet.
let retain = !had_peer || !e.sync_ips().is_empty() || e.response.is_some();
if !retain {
trace!("Removed block request timestamp for {peer_ip} at height {height}");
}
retain
});
// No need to remove responses here, because requests with responses will be retained.
}
/// Removes block requests that have timed out, i.e, requests we sent that did not receive a response in time.
///
/// This removes the corresponding block responses and returns the set of peers/addresses that timed out.
/// It will ask the peer pool handling service to ban any timed-out peers.
///
/// # Return Value
/// On success it will return `None` if there is nothing to re-request, or a set of new of block requests that replaced the timed-out requests.
/// This set of new requests can also replace requests that timed out earlier, and which we were not able to re-request yet.
///
/// This function will return an error if it cannot re-request blocks due to a lack of peers.
/// In this case, the current iteration of block synchronization should not continue and the node should re-try later instead.
pub fn handle_block_request_timeouts<P: PeerPoolHandling<N>>(
&self,
_peer_pool_handler: &P,
) -> Result<Option<BlockRequestBatch<N>>> {
// Acquire the write lock on the requests map.
let mut requests = self.requests.write();
// Retrieve the current time.
let now = Instant::now();
// Retrieve the current block height
let current_height = self.ledger.latest_block_height();
// Track the number of timed out block requests (only used to print a log message).
let mut timed_out_requests = vec![];
// Track which peers should be banned due to unresponsiveness.
let mut peers_to_ban: HashSet<SocketAddr> = HashSet::new();
// Remove timed out block requests.
requests.retain(|height, e| {
let is_obsolete = *height <= current_height;
// Determine if the duration since the request timestamp has exceeded the request timeout.
let timer_elapsed = now.duration_since(e.timestamp) > BLOCK_REQUEST_TIMEOUT;
// Determine if the request is incomplete.
let is_complete = e.sync_ips().is_empty();
// Determine if the request has timed out.
let is_timeout = timer_elapsed && !is_complete;
// Retain if this is not a timeout and is not obsolete.
let retain = !is_timeout && !is_obsolete;
if is_timeout {
trace!("Block request at height {height} has timed out: timer_elapsed={timer_elapsed}, is_complete={is_complete}, is_obsolete={is_obsolete}");
// Increment the number of timed out block requests.
timed_out_requests.push(*height);
} else if is_obsolete {
trace!("Block request at height {height} became obsolete (current_height={current_height})");
}
// If the request timed out, also remove and ban given peer.
if is_timeout {
for peer_ip in e.sync_ips().iter() {
peers_to_ban.insert(*peer_ip);
}
}
retain
});
if !timed_out_requests.is_empty() {
debug!("{num} block requests timed out", num = timed_out_requests.len());
}
let next_request_height = requests.iter().next().map(|(h, _)| *h);
// Avoid locking `locators` and `requests` at the same time.
drop(requests);
// Now remove and ban any unresponsive peers
for peer_ip in peers_to_ban {
self.remove_peer(&peer_ip);
// TODO: Uncomment this when we have a more rigorous analysis and testing of peer banning.
// peer_pool_handler.ip_ban_peer(peer_ip, Some("timed out on block requests"));
}
// Determine if we need to re-issue any timed-out requests.
// If there are no requests remaining or no gap at the beginning,
// we do not need to re-issue requests and will just issue them regularly.
//
// This needs to be checked even if timed_out_requests is empty, because we might not be able to re-issue
// requests immediately if there are no other peers at a given time.
// Further, this only closes the first gap. So multiple calls to this might be needed.
let sync_height = self.get_sync_height();
let start_height = sync_height + 1;
let end_height = if let Some(next_height) = next_request_height
&& next_height > start_height
{
// The end height is exclusive, so use the height of the first existing block requests as the end
next_height
} else {
// Nothing to do.
// Do not log here as this check happens frequently.
return Ok(None);
};
// Set the maximum number of blocks, so that they do not exceed the end height.
let max_new_blocks_to_request = end_height - start_height;
let Some((sync_peers, min_common_ancestor)) = self.find_sync_peers_inner(start_height) else {
// This generally shouldn't happen, because there cannot be outstanding requests when no peers are connected.
bail!("Cannot re-request blocks because no or not enough peers are connected");
};
// Retrieve the greatest block height of any connected peer.
let Some(greatest_peer_height) = sync_peers.values().map(|l| l.latest_locator_height()).max() else {
// This should never happen because `sync_peers` is guaranteed to be non-empty.
bail!("Cannot re-request blocks because no or not enough peers are connected");
};
// (Try to) construct the requests.
let requests = self.construct_requests(
&sync_peers,
sync_height,
min_common_ancestor,
max_new_blocks_to_request,
greatest_peer_height,
);
// If the ledger advanced concurrenctly, there may be no requests to issue after all.
// The given height may also be greater `start_height` due to concurerent block advancement.
if let Some((height, _)) = requests.as_slice().first() {
debug!("Re-requesting blocks starting at height {height}");
Ok(Some((requests, sync_peers)))
} else {
// Do not log here as this constitutes a benign race condition.
Ok(None)
}
}
/// Finds the peers to sync from and the shared common ancestor, starting at the give height.
///
/// Unlike [`Self::find_sync_peers`] this does not only return the latest locators height, but the full BlockLocators for each peer.
/// Returns `None` if there are no peers to sync from.
///
/// # Locking
/// This function will read-lock `common_ancestors`.
fn find_sync_peers_inner(&self, current_height: u32) -> Option<(IndexMap<SocketAddr, BlockLocators<N>>, u32)> {
// Retrieve the latest ledger height.
let latest_ledger_height = self.ledger.latest_block_height();
// Pick a set of peers above the latest ledger height, and include their locators.
// This will sort the peers by locator height in descending order.
let candidate_locators: IndexMap<_, _> = self
.locators
.read()
.iter()
.filter(|(_, locators)| locators.latest_locator_height() > current_height)
.sorted_by(|(_, a), (_, b)| b.latest_locator_height().cmp(&a.latest_locator_height()))
.take(NUM_SYNC_CANDIDATE_PEERS)
.map(|(peer_ip, locators)| (*peer_ip, locators.clone()))
.collect();
// Case 0: If there are no candidate peers, return `None`.
if candidate_locators.is_empty() {
trace!("Found no sync peers with height greater {current_height}");
return None;
}
// TODO (howardwu): Change this to the highest cumulative weight for Phase 3.
// Case 1: If all of the candidate peers share a common ancestor below the latest ledger height,
// then pick the peer with the highest height, and find peers (up to extra redundancy) with
// a common ancestor above the block request range. Set the end height to their common ancestor.
// Determine the threshold number of peers to sync from.
let threshold_to_request = candidate_locators.len().min(REDUNDANCY_FACTOR);
// Breaks the loop when the first threshold number of peers are found, biasing for the peer with the highest height
// and a cohort of peers who share a common ancestor above this node's latest ledger height.
for (idx, (peer_ip, peer_locators)) in candidate_locators.iter().enumerate() {
// The height of the common ancestor shared by all selected peers.
let mut min_common_ancestor = peer_locators.latest_locator_height();
// The peers we will synchronize from.
// As the previous iteration did not succeed, restart with the next candidate peers.
let mut sync_peers = vec![(*peer_ip, peer_locators.clone())];
// Try adding other peers consistent with this one to the sync peer set.
for (other_ip, other_locators) in candidate_locators.iter().skip(idx + 1) {
// Check if these two peers have a common ancestor above the latest ledger height.
if let Some(common_ancestor) = self.common_ancestors.read().get(&PeerPair(*peer_ip, *other_ip)) {
// If so, then check that their block locators are consistent.
if *common_ancestor > latest_ledger_height && peer_locators.is_consistent_with(other_locators) {
// If their common ancestor is less than the minimum common ancestor, then update it.
min_common_ancestor = min_common_ancestor.min(*common_ancestor);
// Add the other peer to the list of sync peers.
sync_peers.push((*other_ip, other_locators.clone()));
}
}
}
// If we have enough sync peers above the latest ledger height, finish and return them.
if min_common_ancestor > latest_ledger_height && sync_peers.len() >= threshold_to_request {
// Shuffle the sync peers prior to returning. This ensures the rest of the stack
// does not rely on the order of the sync peers, and that the sync peers are not biased.
sync_peers.shuffle(&mut rand::thread_rng());
// Collect into an IndexMap and return.
return Some((sync_peers.into_iter().collect(), min_common_ancestor));
}
}
// If there is not enough peers with a minimum common ancestor above the latest ledger height, return None.
None
}
/// Given the sync peers and their minimum common ancestor, return a list of block requests.
fn construct_requests(
&self,
sync_peers: &IndexMap<SocketAddr, BlockLocators<N>>,
sync_height: u32,
min_common_ancestor: u32,
max_blocks_to_request: u32,
greatest_peer_height: u32,
) -> Vec<(u32, PrepareSyncRequest<N>)> {
// Compute the start height for the block requests.
let start_height = {
let requests = self.requests.read();
let ledger_height = self.ledger.latest_block_height();
// Do not issue requests for blocks already contained in the ledger.
let mut start_height = ledger_height.max(sync_height + 1);
// Do not issue requests that already exist.
while requests.contains_key(&start_height) {
start_height += 1;
}
start_height
};
// If the minimum common ancestor is below the start height, then return early.
if min_common_ancestor < start_height {
if start_height < greatest_peer_height {
trace!(
"No request to construct. Height for the next block request is {start_height}, but minimum common block locator ancestor is only {min_common_ancestor} (sync_height={sync_height} greatest_peer_height={greatest_peer_height})"
);
}
return Default::default();
}
// Compute the end height for the block request.
let end_height = (min_common_ancestor + 1).min(start_height + max_blocks_to_request);
// Construct the block hashes to request.
let mut request_hashes = IndexMap::with_capacity((start_height..end_height).len());
// Track the largest number of sync IPs required for any block request in the sequence of requests.
let mut max_num_sync_ips = 1;
for height in start_height..end_height {
// Ensure the current height is not in the ledger or already requested.
if let Err(err) = self.check_block_request(height) {
trace!("{err}");
// If the sequence of block requests is interrupted, then return early.
// Otherwise, continue until the first start height that is new.
match request_hashes.is_empty() {
true => continue,
false => break,
}
}
// Construct the block request.
let (hash, previous_hash, num_sync_ips, is_honest) = construct_request(height, sync_peers);
// Handle the dishonest case.
if !is_honest {
// TODO (howardwu): Consider performing an integrity check on peers (to disconnect).
warn!("Detected dishonest peer(s) when preparing block request");
// If there are not enough peers in the dishonest case, then return early.
if sync_peers.len() < num_sync_ips {
break;
}
}
// Update the maximum number of sync IPs.
max_num_sync_ips = max_num_sync_ips.max(num_sync_ips);
// Append the request.
request_hashes.insert(height, (hash, previous_hash));
}
// Construct the requests with the same sync ips.
request_hashes
.into_iter()
.map(|(height, (hash, previous_hash))| (height, (hash, previous_hash, max_num_sync_ips)))
.collect()
}
}
/// If any peer is detected to be dishonest in this function, it will not set the hash or previous hash,
/// in order to allow the caller to determine what to do.
fn construct_request<N: Network>(
height: u32,
sync_peers: &IndexMap<SocketAddr, BlockLocators<N>>,
) -> (Option<N::BlockHash>, Option<N::BlockHash>, usize, bool) {
let mut hash = None;
let mut hash_redundancy: usize = 0;
let mut previous_hash = None;
let mut is_honest = true;
for peer_locators in sync_peers.values() {
if let Some(candidate_hash) = peer_locators.get_hash(height) {
match hash {
// Increment the redundancy count if the hash matches.
Some(hash) if hash == candidate_hash => hash_redundancy += 1,
// Some peer is dishonest.
Some(_) => {
hash = None;
hash_redundancy = 0;
previous_hash = None;
is_honest = false;
break;
}
// Set the hash if it is not set.
None => {
hash = Some(candidate_hash);
hash_redundancy = 1;
}
}
}
if let Some(candidate_previous_hash) = peer_locators.get_hash(height.saturating_sub(1)) {
match previous_hash {
// Increment the redundancy count if the previous hash matches.
Some(previous_hash) if previous_hash == candidate_previous_hash => (),
// Some peer is dishonest.
Some(_) => {
hash = None;
hash_redundancy = 0;
previous_hash = None;
is_honest = false;
break;
}
// Set the previous hash if it is not set.
None => previous_hash = Some(candidate_previous_hash),
}
}
}
// Note that we intentionally do not just pick the peers that have the hash we have chosen,
// to give stronger confidence that we are syncing during times when the network is consistent/stable.
let num_sync_ips = {
// Extra redundant peers - as the block hash was dishonest.
if !is_honest {
// Choose up to the extra redundancy factor in sync peers.
EXTRA_REDUNDANCY_FACTOR
}
// No redundant peers - as we have redundancy on the block hash.
else if hash.is_some() && hash_redundancy >= REDUNDANCY_FACTOR {
// Choose one sync peer.
1
}
// Redundant peers - as we do not have redundancy on the block hash.
else {
// Choose up to the redundancy factor in sync peers.
REDUNDANCY_FACTOR
}
};
(hash, previous_hash, num_sync_ips, is_honest)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::locators::{
CHECKPOINT_INTERVAL,
NUM_RECENT_BLOCKS,
test_helpers::{sample_block_locators, sample_block_locators_with_fork},
};
use snarkos_node_bft_ledger_service::MockLedgerService;
use snarkos_node_network::{NodeType, Peer, Resolver};
use snarkos_node_tcp::{P2P, Tcp};
use snarkvm::{
ledger::committee::Committee,
prelude::{Field, TestRng},
};
use indexmap::{IndexSet, indexset};
#[cfg(feature = "locktick")]
use locktick::parking_lot::RwLock;
#[cfg(not(feature = "locktick"))]
use parking_lot::RwLock;
use rand::Rng;
use std::net::{IpAddr, Ipv4Addr};
type CurrentNetwork = snarkvm::prelude::MainnetV0;
#[derive(Default)]
struct DummyPeerPoolHandler {
peers_to_ban: RwLock<Vec<SocketAddr>>,
}
impl P2P for DummyPeerPoolHandler {
fn tcp(&self) -> &Tcp {
unreachable!();
}
}
impl<N: Network> PeerPoolHandling<N> for DummyPeerPoolHandler {
const MAXIMUM_POOL_SIZE: usize = 10;
const OWNER: &str = "[DummyPeerPoolHandler]";
const PEER_SLASHING_COUNT: usize = 0;
fn peer_pool(&self) -> &RwLock<HashMap<SocketAddr, Peer<N>>> {
unreachable!();
}
fn resolver(&self) -> &RwLock<Resolver<N>> {
unreachable!();
}
fn is_dev(&self) -> bool {
true
}
fn trusted_peers_only(&self) -> bool {
false
}
fn node_type(&self) -> NodeType {
NodeType::Client
}
fn ip_ban_peer(&self, listener_addr: SocketAddr, _reason: Option<&str>) {
self.peers_to_ban.write().push(listener_addr);
}
}
/// Returns the peer IP for the sync pool.
fn sample_peer_ip(id: u16) -> SocketAddr {
assert_ne!(id, 0, "The peer ID must not be 0 (reserved for local IP in testing)");
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), id)
}
/// Returns a sample committee.
fn sample_committee() -> Committee<CurrentNetwork> {
let rng = &mut TestRng::default();
snarkvm::ledger::committee::test_helpers::sample_committee(rng)
}
/// Returns the ledger service, initialized to the given height.
fn sample_ledger_service(height: u32) -> MockLedgerService<CurrentNetwork> {
MockLedgerService::new_at_height(sample_committee(), height)
}
/// Returns the sync pool, with the ledger initialized to the given height.
fn sample_sync_at_height(height: u32) -> BlockSync<CurrentNetwork> {
BlockSync::<CurrentNetwork>::new(Arc::new(sample_ledger_service(height)))
}
/// Returns a vector of randomly sampled block heights in [0, max_height].
///
/// The maximum value will always be included in the result.
fn generate_block_heights(max_height: u32, num_values: usize) -> Vec<u32> {
assert!(num_values > 0, "Cannot generate an empty vector");
assert!((max_height as usize) >= num_values);
let mut rng = TestRng::default();
let mut heights: Vec<u32> = (0..(max_height - 1)).choose_multiple(&mut rng, num_values);
heights.push(max_height);
heights
}
/// Returns a duplicate (deep copy) of the sync pool with a different ledger height.
fn duplicate_sync_at_new_height(sync: &BlockSync<CurrentNetwork>, height: u32) -> BlockSync<CurrentNetwork> {
BlockSync::<CurrentNetwork> {
peer_notify: Notify::new(),
response_notify: Default::default(),
ledger: Arc::new(sample_ledger_service(height)),
locators: RwLock::new(sync.locators.read().clone()),
common_ancestors: RwLock::new(sync.common_ancestors.read().clone()),
requests: RwLock::new(sync.requests.read().clone()),
sync_state: RwLock::new(sync.sync_state.read().clone()),
advance_with_sync_blocks_lock: Default::default(),
metrics: Default::default(),
}
}
/// Checks that the sync pool (starting at genesis) returns the correct requests.
fn check_prepare_block_requests(
sync: BlockSync<CurrentNetwork>,
min_common_ancestor: u32,
peers: IndexSet<SocketAddr>,
) {
let rng = &mut TestRng::default();
// Check test assumptions are met.
assert_eq!(sync.ledger.latest_block_height(), 0, "This test assumes the sync pool is at genesis");
// Determine the number of peers within range of this sync pool.
let num_peers_within_recent_range_of_ledger = {
// If no peers are within range, then set to 0.
if min_common_ancestor >= NUM_RECENT_BLOCKS as u32 {
0
}
// Otherwise, manually check the number of peers within range.
else {
peers.iter().filter(|peer_ip| sync.get_peer_height(peer_ip).unwrap() < NUM_RECENT_BLOCKS as u32).count()
}
};
// Prepare the block requests.
let (requests, sync_peers) = sync.prepare_block_requests();
// If there are no peers, then there should be no requests.
if peers.is_empty() {
assert!(requests.is_empty());
return;
}
// Otherwise, there should be requests.
let expected_num_requests = core::cmp::min(min_common_ancestor as usize, MAX_BLOCK_REQUESTS);
assert_eq!(requests.len(), expected_num_requests);
for (idx, (height, (hash, previous_hash, num_sync_ips))) in requests.into_iter().enumerate() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
assert_eq!(height, 1 + idx as u32);
assert_eq!(hash, Some((Field::<CurrentNetwork>::from_u32(height)).into()));
assert_eq!(previous_hash, Some((Field::<CurrentNetwork>::from_u32(height - 1)).into()));
if num_peers_within_recent_range_of_ledger >= REDUNDANCY_FACTOR {
assert_eq!(sync_ips.len(), 1);
} else {
assert_eq!(sync_ips.len(), num_peers_within_recent_range_of_ledger);
assert_eq!(sync_ips, peers);
}
}
}
/// Tests that height and hash values are set correctly using many different maximum block heights.
#[test]
fn test_latest_block_height() {
for height in generate_block_heights(100_001, 5000) {
let sync = sample_sync_at_height(height);
// Check that the latest block height is the maximum height.
assert_eq!(sync.ledger.latest_block_height(), height);
// Check the hash to height mapping
assert_eq!(sync.ledger.get_block_height(&(Field::<CurrentNetwork>::from_u32(0)).into()).unwrap(), 0);
assert_eq!(
sync.ledger.get_block_height(&(Field::<CurrentNetwork>::from_u32(height)).into()).unwrap(),
height
);
}
}
#[test]
fn test_get_block_hash() {
for height in generate_block_heights(100_001, 5000) {
let sync = sample_sync_at_height(height);
// Check the height to hash mapping
assert_eq!(sync.ledger.get_block_hash(0).unwrap(), (Field::<CurrentNetwork>::from_u32(0)).into());
assert_eq!(sync.ledger.get_block_hash(height).unwrap(), (Field::<CurrentNetwork>::from_u32(height)).into());
}
}
#[test]
fn test_prepare_block_requests() {
for num_peers in 0..111 {
println!("Testing with {num_peers} peers");
let sync = sample_sync_at_height(0);
let mut peers = indexset![];
for peer_id in 1..=num_peers {
// Add a peer.
sync.update_peer_locators(sample_peer_ip(peer_id), &sample_block_locators(10)).unwrap();
// Add the peer to the set of peers.
peers.insert(sample_peer_ip(peer_id));
}
// If all peers are ahead, then requests should be prepared.
check_prepare_block_requests(sync, 10, peers);
}
}
#[test]
fn test_prepare_block_requests_with_leading_fork_at_11() {
let sync = sample_sync_at_height(0);
// Intuitively, peer 1's fork is above peer 2 and peer 3's height.
// So from peer 2 and peer 3's perspective, they don't even realize that peer 1 is on a fork.
// Thus, you can sync up to block 10 from any of the 3 peers.
// When there are NUM_REDUNDANCY peers ahead, and 1 peer is on a leading fork at 11,
// then the sync pool should request blocks 1..=10 from the NUM_REDUNDANCY peers.
// This is safe because the leading fork is at 11, and the sync pool is at 0,
// so all candidate peers are at least 10 blocks ahead of the sync pool.
// Add a peer (fork).
let peer_1 = sample_peer_ip(1);
sync.update_peer_locators(peer_1, &sample_block_locators_with_fork(20, 11)).unwrap();
// Add a peer.
let peer_2 = sample_peer_ip(2);
sync.update_peer_locators(peer_2, &sample_block_locators(10)).unwrap();
// Add a peer.
let peer_3 = sample_peer_ip(3);
sync.update_peer_locators(peer_3, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, _) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
// Check the requests.
for (idx, (height, (hash, previous_hash, num_sync_ips))) in requests.into_iter().enumerate() {
assert_eq!(height, 1 + idx as u32);
assert_eq!(hash, Some((Field::<CurrentNetwork>::from_u32(height)).into()));
assert_eq!(previous_hash, Some((Field::<CurrentNetwork>::from_u32(height - 1)).into()));
assert_eq!(num_sync_ips, 1); // Only 1 needed since we have redundancy factor on this (recent locator) hash.
}
}
#[test]
fn test_prepare_block_requests_with_leading_fork_at_10() {
let rng = &mut TestRng::default();
let sync = sample_sync_at_height(0);
// Intuitively, peer 1's fork is at peer 2 and peer 3's height.
// So from peer 2 and peer 3's perspective, they recognize that peer 1 has forked.
// Thus, you don't have NUM_REDUNDANCY peers to sync to block 10.
//
// Now, while you could in theory sync up to block 9 from any of the 3 peers,
// we choose not to do this as either side is likely to disconnect from us,
// and we would rather wait for enough redundant peers before syncing.
// When there are NUM_REDUNDANCY peers ahead, and 1 peer is on a leading fork at 10,
// then the sync pool should not request blocks as 1 peer conflicts with the other NUM_REDUNDANCY-1 peers.
// We choose to sync with a cohort of peers that are *consistent* with each other,
// and prioritize from descending heights (so the highest peer gets priority).
// Add a peer (fork).
let peer_1 = sample_peer_ip(1);
sync.update_peer_locators(peer_1, &sample_block_locators_with_fork(20, 10)).unwrap();
// Add a peer.
let peer_2 = sample_peer_ip(2);
sync.update_peer_locators(peer_2, &sample_block_locators(10)).unwrap();
// Add a peer.
let peer_3 = sample_peer_ip(3);
sync.update_peer_locators(peer_3, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, _) = sync.prepare_block_requests();
assert_eq!(requests.len(), 0);
// When there are NUM_REDUNDANCY+1 peers ahead, and 1 is on a fork, then there should be block requests.
// Add a peer.
let peer_4 = sample_peer_ip(4);
sync.update_peer_locators(peer_4, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, sync_peers) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
// Check the requests.
for (idx, (height, (hash, previous_hash, num_sync_ips))) in requests.into_iter().enumerate() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
assert_eq!(height, 1 + idx as u32);
assert_eq!(hash, Some((Field::<CurrentNetwork>::from_u32(height)).into()));
assert_eq!(previous_hash, Some((Field::<CurrentNetwork>::from_u32(height - 1)).into()));
assert_eq!(sync_ips.len(), 1); // Only 1 needed since we have redundancy factor on this (recent locator) hash.
assert_ne!(sync_ips[0], peer_1); // It should never be the forked peer.
}
}
#[test]
fn test_prepare_block_requests_with_trailing_fork_at_9() {
let rng = &mut TestRng::default();
let sync = sample_sync_at_height(0);
// Peer 1 and 2 diverge from peer 3 at block 10. We only sync when there are NUM_REDUNDANCY peers
// who are *consistent* with each other. So if you add a 4th peer that is consistent with peer 1 and 2,
// then you should be able to sync up to block 10, thereby biasing away from peer 3.
// Add a peer (fork).
let peer_1 = sample_peer_ip(1);
sync.update_peer_locators(peer_1, &sample_block_locators(10)).unwrap();
// Add a peer.
let peer_2 = sample_peer_ip(2);
sync.update_peer_locators(peer_2, &sample_block_locators(10)).unwrap();
// Add a peer.
let peer_3 = sample_peer_ip(3);
sync.update_peer_locators(peer_3, &sample_block_locators_with_fork(20, 10)).unwrap();
// Prepare the block requests.
let (requests, _) = sync.prepare_block_requests();
assert_eq!(requests.len(), 0);
// When there are NUM_REDUNDANCY+1 peers ahead, and peer 3 is on a fork, then there should be block requests.
// Add a peer.
let peer_4 = sample_peer_ip(4);
sync.update_peer_locators(peer_4, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, sync_peers) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
// Check the requests.
for (idx, (height, (hash, previous_hash, num_sync_ips))) in requests.into_iter().enumerate() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
assert_eq!(height, 1 + idx as u32);
assert_eq!(hash, Some((Field::<CurrentNetwork>::from_u32(height)).into()));
assert_eq!(previous_hash, Some((Field::<CurrentNetwork>::from_u32(height - 1)).into()));
assert_eq!(sync_ips.len(), 1); // Only 1 needed since we have redundancy factor on this (recent locator) hash.
assert_ne!(sync_ips[0], peer_3); // It should never be the forked peer.
}
}
#[test]
fn test_insert_block_requests() {
let rng = &mut TestRng::default();
let sync = sample_sync_at_height(0);
// Add a peer.
sync.update_peer_locators(sample_peer_ip(1), &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, sync_peers) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
for (height, (hash, previous_hash, num_sync_ips)) in requests.clone() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Insert the block request.
sync.insert_block_request(height, (hash, previous_hash, sync_ips.clone())).unwrap();
// Check that the block requests were inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
for (height, (hash, previous_hash, num_sync_ips)) in requests.clone() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Check that the block requests are still inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
for (height, (hash, previous_hash, num_sync_ips)) in requests {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Ensure that the block requests cannot be inserted twice.
sync.insert_block_request(height, (hash, previous_hash, sync_ips.clone())).unwrap_err();
// Check that the block requests are still inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
}
#[test]
fn test_insert_block_requests_fails() {
let sync = sample_sync_at_height(9);
// Add a peer.
sync.update_peer_locators(sample_peer_ip(1), &sample_block_locators(10)).unwrap();
// Inserting a block height that is already in the ledger should fail.
sync.insert_block_request(9, (None, None, indexset![sample_peer_ip(1)])).unwrap_err();
// Inserting a block height that is not in the ledger should succeed.
sync.insert_block_request(10, (None, None, indexset![sample_peer_ip(1)])).unwrap();
}
#[test]
fn test_update_peer_locators() {
let sync = sample_sync_at_height(0);
// Test 2 peers.
let peer1_ip = sample_peer_ip(1);
for peer1_height in 0..500u32 {
sync.update_peer_locators(peer1_ip, &sample_block_locators(peer1_height)).unwrap();
assert_eq!(sync.get_peer_height(&peer1_ip), Some(peer1_height));
let peer2_ip = sample_peer_ip(2);
for peer2_height in 0..500u32 {
println!("Testing peer 1 height at {peer1_height} and peer 2 height at {peer2_height}");
sync.update_peer_locators(peer2_ip, &sample_block_locators(peer2_height)).unwrap();
assert_eq!(sync.get_peer_height(&peer2_ip), Some(peer2_height));
// Compute the distance between the peers.
let distance = peer1_height.abs_diff(peer2_height);
// Check the common ancestor.
if distance < NUM_RECENT_BLOCKS as u32 {
let expected_ancestor = core::cmp::min(peer1_height, peer2_height);
assert_eq!(sync.get_common_ancestor(peer1_ip, peer2_ip), Some(expected_ancestor));
assert_eq!(sync.get_common_ancestor(peer2_ip, peer1_ip), Some(expected_ancestor));
} else {
let min_checkpoints =
core::cmp::min(peer1_height / CHECKPOINT_INTERVAL, peer2_height / CHECKPOINT_INTERVAL);
let expected_ancestor = min_checkpoints * CHECKPOINT_INTERVAL;
assert_eq!(sync.get_common_ancestor(peer1_ip, peer2_ip), Some(expected_ancestor));
assert_eq!(sync.get_common_ancestor(peer2_ip, peer1_ip), Some(expected_ancestor));
}
}
}
}
#[test]
fn test_remove_peer() {
let sync = sample_sync_at_height(0);
let peer_ip = sample_peer_ip(1);
sync.update_peer_locators(peer_ip, &sample_block_locators(100)).unwrap();
assert_eq!(sync.get_peer_height(&peer_ip), Some(100));
sync.remove_peer(&peer_ip);
assert_eq!(sync.get_peer_height(&peer_ip), None);
sync.update_peer_locators(peer_ip, &sample_block_locators(200)).unwrap();
assert_eq!(sync.get_peer_height(&peer_ip), Some(200));
sync.remove_peer(&peer_ip);
assert_eq!(sync.get_peer_height(&peer_ip), None);
}
#[test]
fn test_locators_insert_remove_insert() {
let sync = sample_sync_at_height(0);
let peer_ip = sample_peer_ip(1);
sync.update_peer_locators(peer_ip, &sample_block_locators(100)).unwrap();
assert_eq!(sync.get_peer_height(&peer_ip), Some(100));
sync.remove_peer(&peer_ip);
assert_eq!(sync.get_peer_height(&peer_ip), None);
sync.update_peer_locators(peer_ip, &sample_block_locators(200)).unwrap();
assert_eq!(sync.get_peer_height(&peer_ip), Some(200));
}
#[test]
fn test_requests_insert_remove_insert() {
let rng = &mut TestRng::default();
let sync = sample_sync_at_height(0);
// Add a peer.
let peer_ip = sample_peer_ip(1);
sync.update_peer_locators(peer_ip, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, sync_peers) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
for (height, (hash, previous_hash, num_sync_ips)) in requests.clone() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Insert the block request.
sync.insert_block_request(height, (hash, previous_hash, sync_ips.clone())).unwrap();
// Check that the block requests were inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
// Remove the peer.
sync.remove_peer(&peer_ip);
for (height, _) in requests {
// Check that the block requests were removed.
assert_eq!(sync.get_block_request(height), None);
assert!(sync.get_block_request_timestamp(height).is_none());
}
// As there is no peer, it should not be possible to prepare block requests.
let (requests, _) = sync.prepare_block_requests();
assert_eq!(requests.len(), 0);
// Add the peer again.
sync.update_peer_locators(peer_ip, &sample_block_locators(10)).unwrap();
// Prepare the block requests.
let (requests, _) = sync.prepare_block_requests();
assert_eq!(requests.len(), 10);
for (height, (hash, previous_hash, num_sync_ips)) in requests {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Insert the block request.
sync.insert_block_request(height, (hash, previous_hash, sync_ips.clone())).unwrap();
// Check that the block requests were inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
}
#[test]
fn test_obsolete_block_requests() {
let rng = &mut TestRng::default();
let sync = sample_sync_at_height(0);
let locator_height = rng.gen_range(0..50);
// Add a peer.
let locators = sample_block_locators(locator_height);
sync.update_peer_locators(sample_peer_ip(1), &locators).unwrap();
// Construct block requests
let (requests, sync_peers) = sync.prepare_block_requests();
assert_eq!(requests.len(), locator_height as usize);
// Add the block requests to the sync module.
for (height, (hash, previous_hash, num_sync_ips)) in requests.clone() {
// Construct the sync IPs.
let sync_ips: IndexSet<_> =
sync_peers.keys().choose_multiple(rng, num_sync_ips).into_iter().copied().collect();
// Insert the block request.
sync.insert_block_request(height, (hash, previous_hash, sync_ips.clone())).unwrap();
// Check that the block requests were inserted.
assert_eq!(sync.get_block_request(height), Some((hash, previous_hash, sync_ips)));
assert!(sync.get_block_request_timestamp(height).is_some());
}
// Duplicate a new sync module with a different height to simulate block advancement.
// This range needs to be inclusive, so that the range is never empty,
// even with a locator height of 0.
let ledger_height = rng.gen_range(0..=locator_height);
let new_sync = duplicate_sync_at_new_height(&sync, ledger_height);
// Check that the number of requests is the same.
assert_eq!(new_sync.requests.read().len(), requests.len());
// Remove timed out block requests.
let c = DummyPeerPoolHandler::default();
new_sync.handle_block_request_timeouts(&c).unwrap();
// Check that the number of requests is reduced based on the ledger height.
assert_eq!(new_sync.requests.read().len(), (locator_height - ledger_height) as usize);
}
#[test]
fn test_timed_out_block_request() {
let sync = sample_sync_at_height(0);
let peer_ip = sample_peer_ip(1);
let locators = sample_block_locators(10);
let block_hash = locators.get_hash(1);
sync.update_peer_locators(peer_ip, &locators).unwrap();
let timestamp = Instant::now() - BLOCK_REQUEST_TIMEOUT - Duration::from_secs(1);
// Add a timed-out request
sync.requests.write().insert(1, OutstandingRequest {
request: (block_hash, None, [peer_ip].into()),
timestamp,
response: None,
});
assert_eq!(sync.requests.read().len(), 1);
assert_eq!(sync.locators.read().len(), 1);
// Remove timed out block requests.
let c = DummyPeerPoolHandler::default();
sync.handle_block_request_timeouts(&c).unwrap();
// let ban_list = c.peers_to_ban.write();
// assert_eq!(ban_list.len(), 1);
// assert_eq!(ban_list.iter().next(), Some(&peer_ip));
assert!(sync.requests.read().is_empty());
assert!(sync.locators.read().is_empty());
}
#[test]
fn test_reissue_timed_out_block_request() {
let sync = sample_sync_at_height(0);
let peer_ip1 = sample_peer_ip(1);
let peer_ip2 = sample_peer_ip(2);
let peer_ip3 = sample_peer_ip(3);
let locators = sample_block_locators(10);
let block_hash1 = locators.get_hash(1);
let block_hash2 = locators.get_hash(2);
sync.update_peer_locators(peer_ip1, &locators).unwrap();
sync.update_peer_locators(peer_ip2, &locators).unwrap();
sync.update_peer_locators(peer_ip3, &locators).unwrap();
assert_eq!(sync.locators.read().len(), 3);
let timestamp = Instant::now() - BLOCK_REQUEST_TIMEOUT - Duration::from_secs(1);
// Add a timed-out request
sync.requests.write().insert(1, OutstandingRequest {
request: (block_hash1, None, [peer_ip1].into()),
timestamp,
response: None,
});
// Add a timed-out request
sync.requests.write().insert(2, OutstandingRequest {
request: (block_hash2, None, [peer_ip2].into()),
timestamp: Instant::now(),
response: None,
});
assert_eq!(sync.requests.read().len(), 2);
// Remove timed out block requests.
let c = DummyPeerPoolHandler::default();
let re_requests = sync.handle_block_request_timeouts(&c).unwrap();
// let ban_list = c.peers_to_ban.write();
// assert_eq!(ban_list.len(), 1);
// assert_eq!(ban_list.iter().next(), Some(&peer_ip1));
assert_eq!(sync.requests.read().len(), 1);
assert_eq!(sync.locators.read().len(), 2);
let (new_requests, new_sync_ips) = re_requests.unwrap();
assert_eq!(new_requests.len(), 1);
let (height, (hash, _, _)) = new_requests.first().unwrap();
assert_eq!(*height, 1);
assert_eq!(*hash, block_hash1);
assert_eq!(new_sync_ips.len(), 2);
// Make sure the removed peer is not in the sync_peer set.
let mut iter = new_sync_ips.iter();
assert_ne!(iter.next().unwrap().0, &peer_ip1);
assert_ne!(iter.next().unwrap().0, &peer_ip1);
}
}