casper-node 2.0.3

The Casper blockchain node
Documentation
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
//! Management of outgoing connections.
//!
//! This module implements outgoing connection management, decoupled from the underlying transport
//! or any higher-level level parts. It encapsulates the reconnection and blocklisting logic on the
//! `SocketAddr` level.
//!
//! # Basic structure
//!
//! Core of this module is the `OutgoingManager`, which supports the following functionality:
//!
//! * Handed a `SocketAddr`s via the `learn_addr` function, it will permanently maintain a
//!   connection to the given address, only giving up if retry thresholds are exceeded, after which
//!   it will be forgotten.
//! * `block_addr` and `redeem_addr` can be used to maintain a `SocketAddr`-keyed block list.
//! * `OutgoingManager` maintains an internal routing table. The `get_route` function can be used to
//!   retrieve a "route" (typically a `sync::channel` accepting network messages) to a remote peer
//!   by `NodeId`.
//!
//! # Requirements
//!
//! `OutgoingManager` is decoupled from the underlying protocol, all of its interactions are
//! performed through [`DialRequest`] and [`DialOutcome`]s. This frees the `OutgoingManager` from
//! having to worry about protocol specifics.
//!
//! Three conditions not expressed in code must be fulfilled for the `OutgoingManager` to function:
//!
//! * The `Dialer` is expected to produce `DialOutcomes` for every dial [`DialRequest::Dial`]
//!   eventually. These must be forwarded to the `OutgoingManager` via the `handle_dial_outcome`
//!   function.
//! * The `perform_housekeeping` method must be called periodically to give the `OutgoingManager` a
//!   chance to initiate reconnections and collect garbage.
//! * When a connection is dropped, the connection manager must be notified via
//!   `handle_connection_drop`.
//!
//! # Lifecycle
//!
//! The following chart illustrates the lifecycle of an outgoing connection.
//!
//! ```text
//!                   forget (after n tries)
//!          ┌────────────────────────────────────┐
//!          │                 learn              ▼
//!          │               ┌──────────────  unknown/forgotten
//!          │               │                (implicit state)
//!          │               │
//!          │               │                │
//!          │               │                │ block
//!          │               │                │
//!          │               │                │
//!          │               │                ▼
//!     ┌────┴────┐          │          ┌─────────┐
//!     │         │  fail    │    block │         │
//!     │ Waiting │◄───────┐ │   ┌─────►│ Blocked │◄──────────┐
//! ┌───┤         │        │ │   │      │         │           │
//! │   └────┬────┘        │ │   │      └────┬────┘           │
//! │ block  │             │ │   │           │                │
//! │        │ timeout     │ ▼   │           │ redeem,        │
//! │        │        ┌────┴─────┴───┐       │ block timeout  │
//! │        │        │              │       │                │
//! │        └───────►│  Connecting  │◄──────┘                │
//! │                 │              │                        │
//! │                 └─────┬────┬───┘                        │
//! │                       │ ▲  │                            │
//! │               success │ │  │ detect                     │
//! │                       │ │  │      ┌──────────┐          │
//! │ ┌───────────┐         │ │  │      │          │          │
//! │ │           │◄────────┘ │  │      │ Loopback │          │
//! │ │ Connected │           │  └─────►│          │          │
//! │ │           │ dropped/  │         └──────────┘          │
//! │ └─────┬─────┴───────────┘                               │
//! │       │       timeout                                   │
//! │       │ block                                           │
//! └───────┴─────────────────────────────────────────────────┘
//! ```
//!
//! # Timeouts/safety
//!
//! The `sweep` transition for connections usually does not happen during normal operations. Three
//! causes are typical for it:
//!
//! * A configured TCP timeout above [`OutgoingConfig::sweep_timeout`].
//! * Very slow responses from remote peers (similar to a Slowloris-attack)
//! * Faulty handling by the driver of the [`OutgoingManager`], i.e. the outside component.
//!
//! Should a dial attempt exceed a certain timeout, it is considered failed and put into the waiting
//! state again.
//!
//! If a conflict (multiple successful dial results) occurs, the more recent connection takes
//! precedence over the previous one. This prevents problems when a notification of a terminated
//! connection is overtaken by the new connection announcement.

use std::{
    collections::{hash_map::Entry, HashMap},
    error::Error,
    fmt::{self, Debug, Display, Formatter},
    mem,
    net::SocketAddr,
    time::{Duration, Instant},
};

use datasize::DataSize;
use prometheus::IntGauge;
use rand::Rng;
use tracing::{debug, error, error_span, field::Empty, info, trace, warn, Span};

use super::{
    blocklist::BlocklistJustification,
    display_error,
    health::{ConnectionHealth, HealthCheckOutcome, HealthConfig, Nonce, TaggedTimestamp},
    NodeId,
};

/// An outgoing connection/address in various states.
#[derive(DataSize, Debug)]
pub struct Outgoing<H, E>
where
    H: DataSize,
    E: DataSize,
{
    /// Whether or not the address is unforgettable, see `learn_addr` for details.
    pub(super) is_unforgettable: bool,
    /// The current state the connection/address is in.
    pub(super) state: OutgoingState<H, E>,
}

/// Active state for a connection/address.
#[derive(DataSize, Debug)]
pub(crate) enum OutgoingState<H, E>
where
    H: DataSize,
    E: DataSize,
{
    /// The outgoing address has been known for the first time and we are currently connecting.
    Connecting {
        /// Number of attempts that failed, so far.
        failures_so_far: u8,
        /// Time when the connection attempt was instantiated.
        since: Instant,
    },
    /// The connection has failed at least one connection attempt and is waiting for a retry.
    Waiting {
        /// Number of attempts that failed, so far.
        failures_so_far: u8,
        /// The most recent connection error.
        ///
        /// If not given, the connection was put into a `Waiting` state due to a sweep timeout.
        error: Option<E>,
        /// The precise moment when the last connection attempt failed.
        last_failure: Instant,
    },
    /// An established outgoing connection.
    Connected {
        /// The peers remote ID.
        peer_id: NodeId,
        /// Handle to a communication channel that can be used to send data to the peer.
        ///
        /// Can be a channel to decouple sending, or even a direct connection handle.
        handle: H,
        /// Health of the connection.
        health: ConnectionHealth,
    },
    /// The address was blocked and will not be retried.
    Blocked {
        /// Since when the block took effect.
        since: Instant,
        /// The justification given for blocking.
        justification: BlocklistJustification,
        /// Until when the block took effect.
        until: Instant,
    },
    /// The address is owned by ourselves and will not be tried again.
    Loopback,
}

impl<H, E> Display for OutgoingState<H, E>
where
    H: DataSize,
    E: DataSize,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            OutgoingState::Connecting {
                failures_so_far, ..
            } => {
                write!(f, "connecting({})", failures_so_far)
            }
            OutgoingState::Waiting {
                failures_so_far, ..
            } => write!(f, "waiting({})", failures_so_far),
            OutgoingState::Connected { .. } => write!(f, "connected"),
            OutgoingState::Blocked { .. } => write!(f, "blocked"),
            OutgoingState::Loopback => write!(f, "loopback"),
        }
    }
}

/// The result of dialing `SocketAddr`.
#[derive(Debug)]
pub enum DialOutcome<H, E> {
    /// A connection was successfully established.
    Successful {
        /// The address dialed.
        addr: SocketAddr,
        /// A handle to send data down the connection.
        handle: H,
        /// The remote peer's authenticated node ID.
        node_id: NodeId,
        /// The moment the connection was established.
        when: Instant,
    },
    /// The connection attempt failed.
    Failed {
        /// The address dialed.
        addr: SocketAddr,
        /// The error encountered while dialing.
        error: E,
        /// The moment the connection attempt failed.
        when: Instant,
    },
    /// The connection was aborted, because the remote peer turned out to be a loopback.
    Loopback {
        /// The address used to connect.
        addr: SocketAddr,
    },
}

impl<H, E> DialOutcome<H, E> {
    /// Retrieves the socket address from the `DialOutcome`.
    fn addr(&self) -> SocketAddr {
        match self {
            DialOutcome::Successful { addr, .. }
            | DialOutcome::Failed { addr, .. }
            | DialOutcome::Loopback { addr, .. } => *addr,
        }
    }
}

/// A request made for dialing.
#[derive(Clone, Debug)]
#[must_use]
pub(crate) enum DialRequest<H> {
    /// Attempt to connect to the outgoing socket address.
    ///
    /// For every time this request is emitted, there must be a corresponding call to
    /// `handle_dial_outcome` eventually.
    ///
    /// Any logging of connection issues should be done in the context of `span` for better log
    /// output.
    Dial { addr: SocketAddr, span: Span },

    /// Disconnects a potentially existing connection.
    ///
    /// Used when a peer has been blocked or should be disconnected for other reasons. Note that
    /// this request can immediately be followed by a connection request, as in the case of a ping
    /// timeout.
    Disconnect { handle: H, span: Span },

    /// Send a ping to a peer.
    SendPing {
        peer_id: NodeId,
        nonce: Nonce,
        span: Span,
    },
}

impl<H> Display for DialRequest<H>
where
    H: Display,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            DialRequest::Dial { addr, .. } => {
                write!(f, "dial: {}", addr)
            }
            DialRequest::Disconnect { handle, .. } => {
                write!(f, "disconnect: {}", handle)
            }
            DialRequest::SendPing { peer_id, nonce, .. } => {
                write!(f, "ping[{}]: {}", nonce, peer_id)
            }
        }
    }
}

#[derive(DataSize, Debug)]
/// Connection settings for the outgoing connection manager.
pub struct OutgoingConfig {
    /// The maximum number of attempts before giving up and forgetting an address, if permitted.
    pub(crate) retry_attempts: u8,
    /// The basic time slot for exponential backoff when reconnecting.
    pub(crate) base_timeout: Duration,
    /// Time until an outgoing address is unblocked.
    pub(crate) unblock_after_min: Duration,
    pub(crate) unblock_after_max: Duration,
    /// Safety timeout, after which a connection is no longer expected to finish dialing.
    pub(crate) sweep_timeout: Duration,
    /// Health check configuration.
    pub(crate) health: HealthConfig,
}

impl OutgoingConfig {
    /// Calculates the backoff time.
    ///
    /// `failed_attempts` (n) is the number of previous attempts *before* the current failure (thus
    /// starting at 0). The backoff time will be double for each attempt.
    fn calc_backoff(&self, failed_attempts: u8) -> Duration {
        (1u32 << failed_attempts as u32) * self.base_timeout
    }
}

/// Manager of outbound connections.
///
/// See the module documentation for usage suggestions.
#[derive(DataSize, Debug)]
pub struct OutgoingManager<H, E>
where
    H: DataSize,
    E: DataSize,
{
    /// Outgoing connections subsystem configuration.
    config: OutgoingConfig,
    /// Mapping of address to their current connection state.
    pub(super) outgoing: HashMap<SocketAddr, Outgoing<H, E>>,
    /// Routing table.
    ///
    /// Contains a mapping from node IDs to connected socket addresses. A missing entry means that
    /// the destination is not connected.
    routes: HashMap<NodeId, SocketAddr>,
    /// A set of outgoing metrics.
    #[data_size(skip)]
    metrics: OutgoingMetrics,
}

/// A set of metrics used by the outgoing component.
#[derive(Clone, Debug)]
pub(super) struct OutgoingMetrics {
    /// Number of outgoing connections in connecting state.
    pub(super) out_state_connecting: IntGauge,
    /// Number of outgoing connections in waiting state.
    pub(super) out_state_waiting: IntGauge,
    /// Number of outgoing connections in connected state.
    pub(super) out_state_connected: IntGauge,
    /// Number of outgoing connections in blocked state.
    pub(super) out_state_blocked: IntGauge,
    /// Number of outgoing connections in loopback state.
    pub(super) out_state_loopback: IntGauge,
}

// Note: We only implement `Default` here for use in testing with `OutgoingManager::new`.
#[cfg(test)]
impl Default for OutgoingMetrics {
    fn default() -> Self {
        Self {
            out_state_connecting: IntGauge::new(
                "out_state_connecting",
                "internal out_state_connecting",
            )
            .unwrap(),
            out_state_waiting: IntGauge::new("out_state_waiting", "internal out_state_waiting")
                .unwrap(),
            out_state_connected: IntGauge::new(
                "out_state_connected",
                "internal out_state_connected",
            )
            .unwrap(),
            out_state_blocked: IntGauge::new("out_state_blocked", "internal out_state_blocked")
                .unwrap(),
            out_state_loopback: IntGauge::new("out_state_loopback", "internal loopback").unwrap(),
        }
    }
}

impl<H, E> OutgoingManager<H, E>
where
    H: DataSize,
    E: DataSize,
{
    /// Creates a new outgoing manager with a set of metrics that is not connected to any registry.
    #[cfg(test)]
    #[inline]
    pub(super) fn new(config: OutgoingConfig) -> Self {
        Self::with_metrics(config, Default::default())
    }

    /// Creates a new outgoing manager with an already existing set of metrics.
    pub(super) fn with_metrics(config: OutgoingConfig, metrics: OutgoingMetrics) -> Self {
        Self {
            config,
            outgoing: Default::default(),
            routes: Default::default(),
            metrics,
        }
    }

    /// Returns a reference to the internal metrics.
    #[cfg(test)]
    fn metrics(&self) -> &OutgoingMetrics {
        &self.metrics
    }
}

/// Creates a logging span for a specific connection.
#[inline]
fn make_span<H, E>(addr: SocketAddr, outgoing: Option<&Outgoing<H, E>>) -> Span
where
    H: DataSize,
    E: DataSize,
{
    // Note: The jury is still out on whether we want to create a single span per connection and
    // cache it, or create a new one (with the same connection ID) each time this is called. The
    // advantage of the former is external tools have it easier correlating all related
    // information, while the drawback is not being able to change the parent span link, which
    // might be awkward.

    if let Some(outgoing) = outgoing {
        match outgoing.state {
            OutgoingState::Connected { peer_id, .. } => {
                error_span!("outgoing", %addr, state=%outgoing.state, %peer_id, consensus_key=Empty)
            }
            _ => {
                error_span!("outgoing", %addr, state=%outgoing.state, peer_id=Empty, consensus_key=Empty)
            }
        }
    } else {
        error_span!("outgoing", %addr, state = "-")
    }
}

impl<H, E> OutgoingManager<H, E>
where
    H: DataSize + Clone,
    E: DataSize + Error,
{
    /// Changes the state of an outgoing connection.
    ///
    /// Will trigger an update of the routing table if necessary. Does not emit any other
    /// side-effects.
    ///
    /// Returns the new state, as well as any residual handle.
    fn change_outgoing_state(
        &mut self,
        addr: SocketAddr,
        mut new_state: OutgoingState<H, E>,
    ) -> (&mut Outgoing<H, E>, Option<H>) {
        let (prev_state, new_outgoing) = match self.outgoing.entry(addr) {
            Entry::Vacant(vacant) => {
                let inserted = vacant.insert(Outgoing {
                    state: new_state,
                    is_unforgettable: false,
                });

                (None, inserted)
            }

            Entry::Occupied(occupied) => {
                let prev = occupied.into_mut();

                mem::swap(&mut prev.state, &mut new_state);

                // `new_state` and `prev.state` are swapped now.
                (Some(new_state), prev)
            }
        };

        // Update the routing table.
        match (&prev_state, &new_outgoing.state) {
            (Some(OutgoingState::Connected { .. }), OutgoingState::Connected { .. }) => {
                trace!("route unchanged, already connected");
            }

            // Dropping from connected to any other state requires clearing the route.
            (Some(OutgoingState::Connected { peer_id, .. }), _) => {
                debug!(%peer_id, "route removed");
                self.routes.remove(peer_id);
            }

            // Otherwise we have established a new route.
            (_, OutgoingState::Connected { peer_id, .. }) => {
                debug!(%peer_id, "route added");
                self.routes.insert(*peer_id, addr);
            }

            _ => {
                trace!("route unchanged");
            }
        }

        // Update the metrics, decreasing the count of the state that was left, while increasing
        // the new state. Note that this will lead to a non-atomic dec/inc if the previous state
        // was the same as before.
        match prev_state {
            Some(OutgoingState::Blocked { .. }) => self.metrics.out_state_blocked.dec(),
            Some(OutgoingState::Connected { .. }) => self.metrics.out_state_connected.dec(),
            Some(OutgoingState::Connecting { .. }) => self.metrics.out_state_connecting.dec(),
            Some(OutgoingState::Loopback) => self.metrics.out_state_loopback.dec(),
            Some(OutgoingState::Waiting { .. }) => self.metrics.out_state_waiting.dec(),
            None => {
                // Nothing to do, there was no previous state.
            }
        }

        match new_outgoing.state {
            OutgoingState::Blocked { .. } => self.metrics.out_state_blocked.inc(),
            OutgoingState::Connected { .. } => self.metrics.out_state_connected.inc(),
            OutgoingState::Connecting { .. } => self.metrics.out_state_connecting.inc(),
            OutgoingState::Loopback => self.metrics.out_state_loopback.inc(),
            OutgoingState::Waiting { .. } => self.metrics.out_state_waiting.inc(),
        }

        // Finally, deconstruct the previous state in case we need to preserve the handle.
        let handle = if let Some(OutgoingState::Connected { handle, .. }) = prev_state {
            Some(handle)
        } else {
            None
        };

        (new_outgoing, handle)
    }

    /// Retrieves the address by peer.
    pub(crate) fn get_addr(&self, peer_id: NodeId) -> Option<SocketAddr> {
        self.routes.get(&peer_id).copied()
    }

    /// Retrieves a handle to a peer.
    ///
    /// Primary function to send data to peers; clients retrieve a handle to it which can then
    /// be used to send data.
    pub(crate) fn get_route(&self, peer_id: NodeId) -> Option<&H> {
        let outgoing = self.outgoing.get(self.routes.get(&peer_id)?)?;

        if let OutgoingState::Connected { ref handle, .. } = outgoing.state {
            Some(handle)
        } else {
            None
        }
    }

    /// Iterates over all connected peer IDs.
    pub(crate) fn connected_peers(&'_ self) -> impl Iterator<Item = NodeId> + '_ {
        self.routes.keys().copied()
    }

    /// Notify about a potentially new address that has been discovered.
    ///
    /// Immediately triggers the connection process to said address if it was not known before.
    ///
    /// A connection marked `unforgettable` will never be evicted but reset instead when it exceeds
    /// the retry limit.
    pub(crate) fn learn_addr(
        &mut self,
        addr: SocketAddr,
        unforgettable: bool,
        now: Instant,
    ) -> Option<DialRequest<H>> {
        let span = make_span(addr, self.outgoing.get(&addr));
        span.clone()
            .in_scope(move || match self.outgoing.entry(addr) {
                Entry::Occupied(_) => {
                    trace!("ignoring already known address");
                    None
                }
                Entry::Vacant(_vacant) => {
                    info!("connecting to newly learned address");
                    let (outgoing, _) = self.change_outgoing_state(
                        addr,
                        OutgoingState::Connecting {
                            failures_so_far: 0,
                            since: now,
                        },
                    );
                    if outgoing.is_unforgettable != unforgettable {
                        outgoing.is_unforgettable = unforgettable;
                        debug!(unforgettable, "marked");
                    }
                    Some(DialRequest::Dial { addr, span })
                }
            })
    }

    pub(crate) fn block_addr<R: Rng>(
        &mut self,
        addr: SocketAddr,
        now: Instant,
        justification: BlocklistJustification,
        rng: &mut R,
    ) -> Option<DialRequest<H>> {
        let span = make_span(addr, self.outgoing.get(&addr));
        span.clone()
            .in_scope(move || match self.outgoing.entry(addr) {
                Entry::Vacant(_vacant) => {
                    info!("unknown address blocked");
                    let until = self.calculate_block_until(now, rng);
                    self.change_outgoing_state(
                        addr,
                        OutgoingState::Blocked {
                            since: now,
                            justification,
                            until,
                        },
                    );
                    None
                }
                Entry::Occupied(occupied) => match occupied.get().state {
                    OutgoingState::Blocked { .. } => {
                        debug!("address already blocked");
                        None
                    }
                    OutgoingState::Loopback => {
                        warn!("loopback address block ignored");
                        None
                    }
                    OutgoingState::Connected { ref handle, .. } => {
                        info!("connected address blocked, disconnecting");
                        let handle = handle.clone();
                        let until = self.calculate_block_until(now, rng);
                        self.change_outgoing_state(
                            addr,
                            OutgoingState::Blocked {
                                since: now,
                                justification,
                                until,
                            },
                        );
                        Some(DialRequest::Disconnect { span, handle })
                    }
                    OutgoingState::Waiting { .. } | OutgoingState::Connecting { .. } => {
                        let until = self.calculate_block_until(now, rng);
                        info!("address blocked");
                        self.change_outgoing_state(
                            addr,
                            OutgoingState::Blocked {
                                since: now,
                                justification,
                                until,
                            },
                        );
                        None
                    }
                },
            })
    }

    /// Checks if an address is blocked.
    #[cfg(test)]
    pub(crate) fn is_blocked(&self, addr: SocketAddr) -> bool {
        match self.outgoing.get(&addr) {
            Some(outgoing) => matches!(outgoing.state, OutgoingState::Blocked { .. }),
            None => false,
        }
    }

    /// Removes an address from the block list.
    ///
    /// Does nothing if the address was not blocked.
    // This function is currently not in use by `network` itself.
    #[allow(dead_code)]
    pub(crate) fn redeem_addr(&mut self, addr: SocketAddr, now: Instant) -> Option<DialRequest<H>> {
        let span = make_span(addr, self.outgoing.get(&addr));
        span.clone()
            .in_scope(move || match self.outgoing.entry(addr) {
                Entry::Vacant(_) => {
                    debug!("unknown address redeemed");
                    None
                }
                Entry::Occupied(occupied) => match occupied.get().state {
                    OutgoingState::Blocked { .. } => {
                        self.change_outgoing_state(
                            addr,
                            OutgoingState::Connecting {
                                failures_so_far: 0,
                                since: now,
                            },
                        );
                        Some(DialRequest::Dial { addr, span })
                    }
                    _ => {
                        debug!("address redemption ignored, not blocked");
                        None
                    }
                },
            })
    }

    /// Records a pong being received.
    pub(super) fn record_pong(&mut self, peer_id: NodeId, pong: TaggedTimestamp) -> bool {
        let addr = if let Some(addr) = self.routes.get(&peer_id) {
            *addr
        } else {
            debug!(%peer_id, nonce=%pong.nonce(), "ignoring pong received from peer without route");
            return false;
        };

        if let Some(outgoing) = self.outgoing.get_mut(&addr) {
            if let OutgoingState::Connected { ref mut health, .. } = outgoing.state {
                health.record_pong(&self.config.health, pong)
            } else {
                debug!(%peer_id, nonce=%pong.nonce(), "ignoring pong received from peer that is not in connected state");
                false
            }
        } else {
            debug!(%peer_id, nonce=%pong.nonce(), "ignoring pong received from peer without route");
            false
        }
    }

    /// Performs housekeeping like reconnection or unblocking peers.
    ///
    /// This function must periodically be called. A good interval is every second.
    pub(super) fn perform_housekeeping<R: Rng>(
        &mut self,
        rng: &mut R,
        now: Instant,
    ) -> Vec<DialRequest<H>> {
        let mut to_forget = Vec::new();
        let mut to_fail = Vec::new();
        let mut to_ping_timeout = Vec::new();
        let mut to_reconnect = Vec::new();
        let mut to_ping = Vec::new();

        for (&addr, outgoing) in &mut self.outgoing {
            // Note: `Span::in_scope` is no longer serviceable here due to borrow limitations.
            let _span_guard = make_span(addr, Some(outgoing)).entered();

            match outgoing.state {
                // Decide whether to attempt reconnecting a failed-waiting address.
                OutgoingState::Waiting {
                    failures_so_far,
                    last_failure,
                    ..
                } => {
                    if failures_so_far > self.config.retry_attempts {
                        if outgoing.is_unforgettable {
                            // Unforgettable addresses simply have their timer reset.
                            info!("unforgettable address reset");

                            to_reconnect.push((addr, 0));
                        } else {
                            // Address had too many attempts at reconnection, we will forget
                            // it after exiting this closure.
                            to_forget.push(addr);

                            info!("address forgotten");
                        }
                    } else {
                        // The address has not exceeded the limit, so check if it is due.
                        let due = last_failure + self.config.calc_backoff(failures_so_far);
                        if now >= due {
                            debug!(attempts = failures_so_far, "address reconnecting");

                            to_reconnect.push((addr, failures_so_far));
                        }
                    }
                }

                OutgoingState::Blocked { until, .. } => {
                    if now >= until {
                        info!("address unblocked");
                        to_reconnect.push((addr, 0));
                    }
                }

                OutgoingState::Connecting {
                    since,
                    failures_so_far,
                } => {
                    let timeout = since + self.config.sweep_timeout;
                    if now >= timeout {
                        // The outer component has not called us with a `DialOutcome` in a
                        // reasonable amount of time. This should happen very rarely, ideally
                        // never.
                        warn!("address timed out connecting, was swept");

                        // Count the timeout as a failure against the connection.
                        to_fail.push((addr, failures_so_far + 1));
                    }
                }
                OutgoingState::Connected {
                    peer_id,
                    ref mut health,
                    ..
                } => {
                    // Check if we need to send a ping, or give up and disconnect.
                    let health_outcome = health.update_health(rng, &self.config.health, now);

                    match health_outcome {
                        HealthCheckOutcome::DoNothing => {
                            // Nothing to do.
                        }
                        HealthCheckOutcome::SendPing(nonce) => {
                            trace!(%nonce, "sending ping");
                            to_ping.push((peer_id, addr, nonce));
                        }
                        HealthCheckOutcome::GiveUp => {
                            info!("disconnecting after ping retries were exhausted");
                            to_ping_timeout.push(addr);
                        }
                    }
                }
                OutgoingState::Loopback => {
                    // Entry is ignored. Not outputting any `trace` because this is log spam even at
                    // the `trace` level.
                }
            }
        }

        // Remove all addresses marked for forgetting.
        for addr in to_forget {
            self.outgoing.remove(&addr);
        }

        // Fail connections that are taking way too long to connect.
        for (addr, failures_so_far) in to_fail {
            let span = make_span(addr, self.outgoing.get(&addr));

            span.in_scope(|| {
                self.change_outgoing_state(
                    addr,
                    OutgoingState::Waiting {
                        failures_so_far,
                        error: None,
                        last_failure: now,
                    },
                )
            });
        }

        let mut dial_requests = Vec::new();

        // Request disconnection from failed pings.
        for addr in to_ping_timeout {
            let span = make_span(addr, self.outgoing.get(&addr));

            let (_, opt_handle) = span.clone().in_scope(|| {
                self.change_outgoing_state(
                    addr,
                    OutgoingState::Connecting {
                        failures_so_far: 0,
                        since: now,
                    },
                )
            });

            if let Some(handle) = opt_handle {
                dial_requests.push(DialRequest::Disconnect {
                    handle,
                    span: span.clone(),
                });
            } else {
                error!("did not expect connection under ping timeout to not have a residual connection handle. this is a bug");
            }
            dial_requests.push(DialRequest::Dial { addr, span });
        }

        // Reconnect others.
        dial_requests.extend(to_reconnect.into_iter().map(|(addr, failures_so_far)| {
            let span = make_span(addr, self.outgoing.get(&addr));

            span.clone().in_scope(|| {
                self.change_outgoing_state(
                    addr,
                    OutgoingState::Connecting {
                        failures_so_far,
                        since: now,
                    },
                )
            });

            DialRequest::Dial { addr, span }
        }));

        // Finally, schedule pings.
        dial_requests.extend(to_ping.into_iter().map(|(peer_id, addr, nonce)| {
            let span = make_span(addr, self.outgoing.get(&addr));
            DialRequest::SendPing {
                peer_id,
                nonce,
                span,
            }
        }));

        dial_requests
    }

    /// Handles the outcome of a dialing attempt.
    ///
    /// Note that reconnects will earliest happen on the next `perform_housekeeping` call.
    pub(crate) fn handle_dial_outcome(
        &mut self,
        dial_outcome: DialOutcome<H, E>,
    ) -> Option<DialRequest<H>> {
        let addr = dial_outcome.addr();
        let span = make_span(addr, self.outgoing.get(&addr));

        span.clone().in_scope(move || match dial_outcome {
            DialOutcome::Successful {
                addr,
                handle,
                node_id,
                when
            } => {
                info!("established outgoing connection");

                if let Some(Outgoing{
                    state: OutgoingState::Blocked { .. }, ..
                }) = self.outgoing.get(&addr) {
                    // If we connected to a blocked address, do not go into connected, but stay
                    // blocked instead.
                    Some(DialRequest::Disconnect{
                        handle, span
                    })
                } else {
                    // Otherwise, just record the connected state.
                    self.change_outgoing_state(
                        addr,
                        OutgoingState::Connected {
                            peer_id: node_id,
                            handle,
                            health: ConnectionHealth::new(when),
                        },
                    );
                    None
                }
            }

            DialOutcome::Failed { addr, error, when } => {
                info!(err = display_error(&error), "outgoing connection failed");

                if let Some(outgoing) = self.outgoing.get(&addr) {
                    match outgoing.state {
                        OutgoingState::Connecting { failures_so_far,.. } => {
                            self.change_outgoing_state(
                                addr,
                                OutgoingState::Waiting {
                                    failures_so_far: failures_so_far + 1,
                                    error: Some(error),
                                    last_failure: when,
                                },
                            );
                            None
                        }
                        OutgoingState::Blocked { .. } => {
                            debug!("failed dial outcome after block ignored");

                            // We do not set the connection to "waiting" if an out-of-order failed
                            // connection arrives, but continue to honor the blocking.
                            None
                        }
                        OutgoingState::Waiting { .. } |
                        OutgoingState::Connected { .. } |
                        OutgoingState::Loopback => {
                            warn!(
                                "processing dial outcome on a connection that was not marked as connecting or blocked"
                            );

                            None
                        }
                    }
                } else {
                    warn!("processing dial outcome non-existent connection");

                    // If the connection does not exist, do not introduce it!
                    None
                }
            }
            DialOutcome::Loopback { addr } => {
                info!("found loopback address");
                self.change_outgoing_state(addr, OutgoingState::Loopback);
                None
            }
        })
    }

    /// Notifies the connection manager about a dropped connection.
    ///
    /// This will usually result in an immediate reconnection.
    pub(crate) fn handle_connection_drop(
        &mut self,
        addr: SocketAddr,
        now: Instant,
    ) -> Option<DialRequest<H>> {
        let span = make_span(addr, self.outgoing.get(&addr));

        span.clone().in_scope(move || {
            if let Some(outgoing) = self.outgoing.get(&addr) {
                match outgoing.state {
                    OutgoingState::Waiting { .. }
                    | OutgoingState::Loopback
                    | OutgoingState::Connecting { .. } => {
                        // We should, under normal circumstances, not receive drop notifications for
                        // any of these. Connection failures are handled by the dialer.
                        warn!("unexpected drop notification");
                        None
                    }
                    OutgoingState::Connected { .. } => {
                        // Drop the handle, immediately initiate a reconnection.
                        self.change_outgoing_state(
                            addr,
                            OutgoingState::Connecting {
                                failures_so_far: 0,
                                since: now,
                            },
                        );
                        Some(DialRequest::Dial { addr, span })
                    }
                    OutgoingState::Blocked { .. } => {
                        // Blocked addresses ignore connection drops.
                        debug!("received drop notification for blocked connection");
                        None
                    }
                }
            } else {
                warn!("received connection drop notification for unknown connection");
                None
            }
        })
    }

    fn calculate_block_until<R: Rng>(&self, now: Instant, rng: &mut R) -> Instant {
        let min = self.config.unblock_after_min;
        let max = self.config.unblock_after_max;
        if min == max {
            return now + min;
        }
        let block_duration = rng.gen_range(min..=max);
        now + block_duration
    }
}

#[cfg(test)]
mod tests {
    use std::{net::SocketAddr, time::Duration};

    use assert_matches::assert_matches;
    use datasize::DataSize;
    use rand::Rng;
    use thiserror::Error;

    use super::{DialOutcome, DialRequest, NodeId, OutgoingConfig, OutgoingManager};
    use crate::{
        components::network::{
            blocklist::BlocklistJustification,
            health::{HealthConfig, TaggedTimestamp},
        },
        testing::{init_logging, test_clock::TestClock},
    };

    /// Error for test dialer.
    ///
    /// Tracks a configurable id for the error.
    #[derive(DataSize, Debug, Error)]
    #[error("test dialer error({})", id)]
    struct TestDialerError {
        id: u32,
    }

    /// Setup an outgoing configuration for testing.
    fn test_config() -> OutgoingConfig {
        OutgoingConfig {
            retry_attempts: 3,
            base_timeout: Duration::from_secs(1),
            unblock_after_min: Duration::from_secs(60),
            unblock_after_max: Duration::from_secs(60),
            sweep_timeout: Duration::from_secs(45),
            health: HealthConfig::test_config(),
        }
    }

    /// Setup an outgoing configuration for testing.
    fn config_variant_unblock() -> OutgoingConfig {
        OutgoingConfig {
            retry_attempts: 3,
            base_timeout: Duration::from_secs(1),
            unblock_after_min: Duration::from_secs(60),
            unblock_after_max: Duration::from_secs(80),
            sweep_timeout: Duration::from_secs(45),
            health: HealthConfig::test_config(),
        }
    }

    /// Helper function that checks if a given dial request actually dials the expected address.
    fn dials<'a, H, T>(expected: SocketAddr, requests: T) -> bool
    where
        T: IntoIterator<Item = &'a DialRequest<H>> + 'a,
        H: 'a,
    {
        for req in requests.into_iter() {
            if let DialRequest::Dial { addr, .. } = req {
                if *addr == expected {
                    return true;
                }
            }
        }

        false
    }

    /// Helper function that checks if a given dial request actually disconnects the expected
    /// address.
    fn disconnects<'a, H, T>(expected: H, requests: T) -> bool
    where
        T: IntoIterator<Item = &'a DialRequest<H>> + 'a,
        H: 'a + PartialEq,
    {
        for req in requests.into_iter() {
            if let DialRequest::Disconnect { handle, .. } = req {
                if *handle == expected {
                    return true;
                }
            }
        }

        false
    }

    #[test]
    fn successful_lifecycle() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        let id_a = NodeId::random(&mut rng);

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // We begin by learning a single, regular address, triggering a dial request.
        assert!(dials(
            addr_a,
            &manager.learn_addr(addr_a, false, clock.now())
        ));
        assert_eq!(manager.metrics().out_state_connecting.get(), 1);

        // Our first connection attempt fails. The connection should now be in waiting state, but
        // not reconnect, since the minimum delay is 2 seconds (2*base_timeout).
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 1 },
                when: clock.now(),
            },)
            .is_none());
        assert_eq!(manager.metrics().out_state_connecting.get(), 0);
        assert_eq!(manager.metrics().out_state_waiting.get(), 1);

        // Performing housekeeping multiple times should not make a difference.
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Advancing the clock will trigger a reconnection on the next housekeeping.
        clock.advance_time(2_000);
        assert!(dials(
            addr_a,
            &manager.perform_housekeeping(&mut rng, clock.now())
        ));
        assert_eq!(manager.metrics().out_state_connecting.get(), 1);
        assert_eq!(manager.metrics().out_state_waiting.get(), 0);

        // This time the connection succeeds.
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_a,
                handle: 99,
                node_id: id_a,
                when: clock.now(),
            },)
            .is_none());
        assert_eq!(manager.metrics().out_state_connecting.get(), 0);
        assert_eq!(manager.metrics().out_state_connected.get(), 1);

        // The routing table should have been updated and should return the handle.
        assert_eq!(manager.get_route(id_a), Some(&99));
        assert_eq!(manager.get_addr(id_a), Some(addr_a));

        // Time passes, and our connection drops. Reconnecting should be immediate.
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        clock.advance_time(20_000);
        assert!(dials(
            addr_a,
            &manager.handle_connection_drop(addr_a, clock.now())
        ));
        assert_eq!(manager.metrics().out_state_connecting.get(), 1);
        assert_eq!(manager.metrics().out_state_waiting.get(), 0);

        // The route should have been cleared.
        assert!(manager.get_route(id_a).is_none());
        assert!(manager.get_addr(id_a).is_none());

        // Reconnection is already in progress, so we do not expect another request on housekeeping.
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
    }

    #[test]
    fn connections_forgotten_after_too_many_tries() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        // Address `addr_b` will be a known address.
        let addr_b: SocketAddr = "5.6.7.8:5678".parse().unwrap();

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // First, attempt to connect. Tests are set to 3 retries after 2, 4 and 8 seconds.
        assert!(dials(
            addr_a,
            &manager.learn_addr(addr_a, false, clock.now())
        ));
        assert!(dials(
            addr_b,
            &manager.learn_addr(addr_b, true, clock.now())
        ));

        // Fail the first connection attempts, not triggering a retry (timeout not reached yet).
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 10 },
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_b,
                error: TestDialerError { id: 11 },
                when: clock.now(),
            },)
            .is_none());

        // Learning the address again should not cause a reconnection.
        assert!(manager.learn_addr(addr_a, false, clock.now()).is_none());
        assert!(manager.learn_addr(addr_b, false, clock.now()).is_none());

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        assert!(manager.learn_addr(addr_a, false, clock.now()).is_none());
        assert!(manager.learn_addr(addr_b, false, clock.now()).is_none());

        // After 1.999 seconds, reconnection should still be delayed.
        clock.advance_time(1_999);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Adding 0.001 seconds finally is enough to reconnect.
        clock.advance_time(1);
        let requests = manager.perform_housekeeping(&mut rng, clock.now());
        assert!(dials(addr_a, &requests));
        assert!(dials(addr_b, &requests));

        // Waiting for more than the reconnection delay should not be harmful or change
        // anything, as  we are currently connecting.
        clock.advance_time(6_000);

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Fail the connection again, wait 3.999 seconds, expecting no reconnection.
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 40 },
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_b,
                error: TestDialerError { id: 41 },
                when: clock.now(),
            },)
            .is_none());

        clock.advance_time(3_999);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Adding 0.001 seconds finally again pushes us over the threshold.
        clock.advance_time(1);
        let requests = manager.perform_housekeeping(&mut rng, clock.now());
        assert!(dials(addr_a, &requests));
        assert!(dials(addr_b, &requests));

        // Fail the connection quickly.
        clock.advance_time(25);
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 10 },
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_b,
                error: TestDialerError { id: 10 },
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // The last attempt should happen 8 seconds after the error, not the last attempt.
        clock.advance_time(7_999);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        clock.advance_time(1);
        let requests = manager.perform_housekeeping(&mut rng, clock.now());
        assert!(dials(addr_a, &requests));
        assert!(dials(addr_b, &requests));

        // Fail the last attempt. No more reconnections should be happening.
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 10 },
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_b,
                error: TestDialerError { id: 10 },
                when: clock.now(),
            },)
            .is_none());

        // Only the unforgettable address should be reconnecting.
        let requests = manager.perform_housekeeping(&mut rng, clock.now());
        assert!(!dials(addr_a, &requests));
        assert!(dials(addr_b, &requests));

        // But not `addr_a`, even after a long wait.
        clock.advance_time(1_000_000_000);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
    }

    #[test]
    fn blocking_works() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        // We use `addr_b` as an unforgettable address, which does not mean it cannot be blocked!
        let addr_b: SocketAddr = "5.6.7.8:5678".parse().unwrap();
        let addr_c: SocketAddr = "9.0.1.2:9012".parse().unwrap();
        let id_a = NodeId::random(&mut rng);
        let id_b = NodeId::random(&mut rng);
        let id_c = NodeId::random(&mut rng);

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // Block `addr_a` from the start.
        assert!(manager
            .block_addr(
                addr_a,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
            .is_none());

        // Learning both `addr_a` and `addr_b` should only trigger a connection to `addr_b` now.
        assert!(manager.learn_addr(addr_a, false, clock.now()).is_none());
        assert!(dials(
            addr_b,
            &manager.learn_addr(addr_b, true, clock.now())
        ));

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Fifteen seconds later we succeed in connecting to `addr_b`.
        clock.advance_time(15_000);
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_b,
                handle: 101,
                node_id: id_b,
                when: clock.now(),
            },)
            .is_none());
        assert_eq!(manager.get_route(id_b), Some(&101));

        // Invariant through housekeeping.
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        assert_eq!(manager.get_route(id_b), Some(&101));

        // Another fifteen seconds later, we block `addr_b`.
        clock.advance_time(15_000);
        assert!(disconnects(
            101,
            &manager.block_addr(
                addr_b,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
        ));

        // `addr_c` will be blocked during the connection phase.
        assert!(dials(
            addr_c,
            &manager.learn_addr(addr_c, false, clock.now())
        ));
        assert!(manager
            .block_addr(
                addr_c,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
            .is_none());

        // We are still expect to provide a dial outcome, but afterwards, there should be no
        // route to C and an immediate disconnection should be queued.
        assert!(disconnects(
            42,
            &manager.handle_dial_outcome(DialOutcome::Successful {
                addr: addr_c,
                handle: 42,
                node_id: id_c,
                when: clock.now(),
            },)
        ));

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        assert!(manager.get_route(id_c).is_none());

        // At this point, we have blocked all three addresses. 30 seconds later, the first one is
        // unblocked due to the block timing out.

        clock.advance_time(30_000);
        assert!(dials(
            addr_a,
            &manager.perform_housekeeping(&mut rng, clock.now())
        ));

        // Fifteen seconds later, B and C are still blocked, but we redeem B early.
        clock.advance_time(15_000);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        assert!(dials(addr_b, &manager.redeem_addr(addr_b, clock.now())));

        // Succeed both connections, and ensure we have routes to both.
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_b,
                handle: 77,
                node_id: id_b,
                when: clock.now(),
            },)
            .is_none());
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_a,
                handle: 66,
                node_id: id_a,
                when: clock.now(),
            },)
            .is_none());

        assert_eq!(manager.get_route(id_a), Some(&66));
        assert_eq!(manager.get_route(id_b), Some(&77));
    }

    #[test]
    fn loopback_handled_correctly() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let loopback_addr: SocketAddr = "1.2.3.4:1234".parse().unwrap();

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // Loopback addresses are connected to only once, and then marked as loopback forever.
        assert!(dials(
            loopback_addr,
            &manager.learn_addr(loopback_addr, false, clock.now())
        ));

        assert!(manager
            .handle_dial_outcome(DialOutcome::Loopback {
                addr: loopback_addr,
            },)
            .is_none());

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Learning loopbacks again should not trigger another connection
        assert!(manager
            .learn_addr(loopback_addr, false, clock.now())
            .is_none());

        // Blocking loopbacks does not result in a block, since regular blocks would clear after
        // some time.
        assert!(manager
            .block_addr(
                loopback_addr,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
            .is_none());

        clock.advance_time(1_000_000_000);

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
    }

    #[test]
    fn connected_peers_works() {
        init_logging();

        let mut rng = crate::new_rng();
        let clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        let addr_b: SocketAddr = "5.6.7.8:5678".parse().unwrap();

        let id_a = NodeId::random(&mut rng);
        let id_b = NodeId::random(&mut rng);

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        manager.learn_addr(addr_a, false, clock.now());
        manager.learn_addr(addr_b, true, clock.now());

        manager.handle_dial_outcome(DialOutcome::Successful {
            addr: addr_a,
            handle: 22,
            node_id: id_a,
            when: clock.now(),
        });
        manager.handle_dial_outcome(DialOutcome::Successful {
            addr: addr_b,
            handle: 33,
            node_id: id_b,
            when: clock.now(),
        });

        let mut peer_ids: Vec<_> = manager.connected_peers().collect();
        let mut expected = vec![id_a, id_b];

        peer_ids.sort();
        expected.sort();

        assert_eq!(peer_ids, expected);
    }

    #[test]
    fn sweeping_works() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();

        let id_a = NodeId::random(&mut rng);

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // Trigger a new connection via learning an address.
        assert!(dials(
            addr_a,
            &manager.learn_addr(addr_a, false, clock.now())
        ));

        // We now let enough time pass to cause the connection to be considered failed aborted.
        // No effects are expected at this point.
        clock.advance_time(50_000);
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // The connection will now experience a regular failure. Since this is the first connection
        // failure, it should reconnect after 2 seconds.
        clock.advance_time(2_000);
        assert!(dials(
            addr_a,
            &manager.perform_housekeeping(&mut rng, clock.now())
        ));

        // We now simulate the second connection (`handle: 2`) succeeding first, after 1 second.
        clock.advance_time(1_000);
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_a,
                handle: 2,
                node_id: id_a,
                when: clock.now(),
            })
            .is_none());

        // A route should now be established.
        assert_eq!(manager.get_route(id_a), Some(&2));

        // More time passes and the first connection attempt finally finishes.
        clock.advance_time(30_000);
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr: addr_a,
                handle: 1,
                node_id: id_a,
                when: clock.now(),
            })
            .is_none());

        // We now expect to be connected through the first connection (see documentation).
        assert_eq!(manager.get_route(id_a), Some(&1));
    }

    #[test]
    fn blocking_not_overridden_by_racing_failed_connections() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();

        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        assert!(!manager.is_blocked(addr_a));

        // Block `addr_a` from the start.
        assert!(manager
            .block_addr(
                addr_a,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
            .is_none());
        assert!(manager.is_blocked(addr_a));

        clock.advance_time(60);

        // Receive an "illegal" dial outcome, even though we did not dial.
        assert!(manager
            .handle_dial_outcome(DialOutcome::Failed {
                addr: addr_a,
                error: TestDialerError { id: 12345 },

                // The moment the connection attempt failed.
                when: clock.now(),
            })
            .is_none());

        // The failed connection should _not_ have reset the block!
        assert!(manager.is_blocked(addr_a));
        clock.advance_time(60);
        assert!(manager.is_blocked(addr_a));

        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());
        assert!(manager.is_blocked(addr_a));
    }

    #[test]
    fn emits_and_accepts_pings() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        let id = NodeId::random(&mut rng);

        // Setup a connection and put it into the connected state.
        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        // Trigger a new connection via learning an address.
        assert!(dials(addr, &manager.learn_addr(addr, false, clock.now())));

        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr,
                handle: 1,
                node_id: id,
                when: clock.now(),
            })
            .is_none());

        // Initial housekeeping should do nothing.
        assert!(manager
            .perform_housekeeping(&mut rng, clock.now())
            .is_empty());

        // Go through 50 pings, which should be happening every 5 seconds.
        for _ in 0..50 {
            clock.advance(Duration::from_secs(3));
            assert!(manager
                .perform_housekeeping(&mut rng, clock.now())
                .is_empty());
            clock.advance(Duration::from_secs(2));

            let (_first_nonce, peer_id) = assert_matches!(
                manager
                    .perform_housekeeping(&mut rng, clock.now())
                    .as_slice(),
                &[DialRequest::SendPing { nonce, peer_id, ..  }] => (nonce, peer_id)
            );
            assert_eq!(peer_id, id);

            // After a second, nothing should have changed.
            assert!(manager
                .perform_housekeeping(&mut rng, clock.now())
                .is_empty());

            clock.advance(Duration::from_secs(1));
            // Waiting another second (two in total) should trigger another ping.
            clock.advance(Duration::from_secs(1));

            let (second_nonce, peer_id) = assert_matches!(
                manager
                    .perform_housekeeping(&mut rng, clock.now())
                    .as_slice(),
                &[DialRequest::SendPing { nonce, peer_id, ..  }] => (nonce, peer_id)
            );

            // Ensure the ID is correct.
            assert_eq!(peer_id, id);

            // Pong arrives 1 second later.
            clock.advance(Duration::from_secs(1));

            // We now feed back the ping with the correct nonce. This should not result in a ban.
            assert!(!manager.record_pong(
                peer_id,
                TaggedTimestamp::from_parts(clock.now(), second_nonce),
            ));

            // This resets the "cycle", the next ping is due in 5 seconds.
        }

        // Now we are going to miss 4 pings in a row and expect a disconnect.
        clock.advance(Duration::from_secs(5));
        assert_matches!(
            manager
                .perform_housekeeping(&mut rng, clock.now())
                .as_slice(),
            &[DialRequest::SendPing { .. }]
        );
        clock.advance(Duration::from_secs(2));
        assert_matches!(
            manager
                .perform_housekeeping(&mut rng, clock.now())
                .as_slice(),
            &[DialRequest::SendPing { .. }]
        );
        clock.advance(Duration::from_secs(2));
        assert_matches!(
            manager
                .perform_housekeeping(&mut rng, clock.now())
                .as_slice(),
            &[DialRequest::SendPing { .. }]
        );
        clock.advance(Duration::from_secs(2));
        assert_matches!(
            manager
                .perform_housekeeping(&mut rng, clock.now())
                .as_slice(),
            &[DialRequest::SendPing { .. }]
        );

        // This results in a disconnect, followed by a reconnect.
        clock.advance(Duration::from_secs(2));
        let dial_addr = assert_matches!(
            manager
                .perform_housekeeping(&mut rng, clock.now())
                .as_slice(),
            &[DialRequest::Disconnect { .. }, DialRequest::Dial { addr, .. }] => addr
        );

        assert_eq!(dial_addr, addr);
    }

    #[test]
    fn indicates_issue_when_excessive_pongs_are_encountered() {
        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();

        let addr: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        let id = NodeId::random(&mut rng);

        // Ensure we have one connected node.
        let mut manager = OutgoingManager::<u32, TestDialerError>::new(test_config());

        assert!(dials(addr, &manager.learn_addr(addr, false, clock.now())));
        assert!(manager
            .handle_dial_outcome(DialOutcome::Successful {
                addr,
                handle: 1,
                node_id: id,
                when: clock.now(),
            })
            .is_none());

        clock.advance(Duration::from_millis(50));

        // We can now receive excessive pongs.
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(!manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
        assert!(manager.record_pong(id, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
    }

    #[test]
    fn unblocking_in_variant_block_time() {
        init_logging();

        let mut rng = crate::new_rng();
        let mut clock = TestClock::new();
        let addr_a: SocketAddr = "1.2.3.4:1234".parse().unwrap();
        let mut manager = OutgoingManager::<u32, TestDialerError>::new(config_variant_unblock());

        assert!(!manager.is_blocked(addr_a));

        // Block `addr_a` from the start.
        assert!(manager
            .block_addr(
                addr_a,
                clock.now(),
                BlocklistJustification::MissingChainspecHash,
                &mut rng,
            )
            .is_none());
        assert!(manager.is_blocked(addr_a));

        clock.advance_time(config_variant_unblock().unblock_after_max.as_millis() as u64 + 1);
        assert!(dials(
            addr_a,
            &manager.perform_housekeeping(&mut rng, clock.now())
        ));
        assert!(!manager.is_blocked(addr_a));
    }
}