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
//! Health-check state machine.
//!
//! Health checks perform periodic pings to remote peers to ensure the connection is still alive. It
//! has somewhat complicated logic that is encoded in the `ConnectionHealth` struct, which has
//! multiple implicit states.
use std::{
fmt::{self, Display, Formatter},
time::{Duration, Instant},
};
use datasize::DataSize;
use rand::Rng;
use serde::{Deserialize, Serialize};
use crate::utils::specimen::{Cache, LargestSpecimen, SizeEstimator};
/// Connection health information.
///
/// All data related to the ping/pong functionality used to verify a peer's networking liveness.
#[derive(Clone, Copy, DataSize, Debug)]
pub(crate) struct ConnectionHealth {
/// The moment the connection was established.
pub(crate) connected_since: Instant,
/// The last ping that was requested to be sent.
pub(crate) last_ping_sent: Option<TaggedTimestamp>,
/// The most recent pong received.
pub(crate) last_pong_received: Option<TaggedTimestamp>,
/// Number of invalid pongs received, reset upon receiving a valid pong.
pub(crate) invalid_pong_count: u32,
/// Number of pings that timed out.
pub(crate) ping_timeouts: u32,
}
/// Health check configuration.
#[derive(DataSize, Debug)]
pub(crate) struct HealthConfig {
/// How often to send a ping to ensure a connection is established.
///
/// Determines how soon after connecting or a successful ping another ping is sent.
pub(crate) ping_interval: Duration,
/// Duration during which a ping must succeed to be considered successful.
pub(crate) ping_timeout: Duration,
/// Number of retries before giving up and disconnecting a peer due to too many failed pings.
pub(crate) ping_retries: u16,
/// How many spurious pongs to tolerate before banning a peer.
pub(crate) pong_limit: u32,
}
/// A timestamp with an associated nonce.
#[derive(Clone, Copy, DataSize, Debug)]
pub(crate) struct TaggedTimestamp {
/// The actual timestamp.
timestamp: Instant,
/// The nonce of the timestamp.
nonce: Nonce,
}
impl TaggedTimestamp {
/// Creates a new tagged timestamp with a random nonce.
pub(crate) fn new<R: Rng>(rng: &mut R, timestamp: Instant) -> Self {
Self {
timestamp,
nonce: rng.gen(),
}
}
/// Creates a new tagged timestamp from parts.
pub(crate) fn from_parts(timestamp: Instant, nonce: Nonce) -> Self {
TaggedTimestamp { nonce, timestamp }
}
/// Returns the actual timestamp.
pub(crate) fn timestamp(&self) -> Instant {
self.timestamp
}
/// Returns the nonce inside the timestamp.
pub(crate) fn nonce(self) -> Nonce {
self.nonce
}
}
/// A number-used-once, specifically one used in pings.
// Note: This nonce used to be a `u32`, but that is too small - since we immediately disconnect when
// a duplicate ping is generated, a `u32` has a ~ 1/(2^32) chance of a consecutive collision.
//
// If we ping every 5 seconds, this is a ~ 0.01% chance over a month, which is too high over
// thousands over nodes. At 64 bits, in theory the upper bound is 0.0000000002%, which is
// better (the period of the RNG used should be >> 64 bits).
//
// While we do check for consecutive ping nonces being generated, we still like the lower
// collision chance for repeated pings being sent.
#[derive(Clone, Copy, DataSize, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub(crate) struct Nonce(u64);
impl Display for Nonce {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:016X}", self.0)
}
}
impl rand::distributions::Distribution<Nonce> for rand::distributions::Standard {
#[inline(always)]
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Nonce {
Nonce(rng.gen())
}
}
impl ConnectionHealth {
/// Creates a new connection health instance, recording when the connection was established.
pub(crate) fn new(connected_since: Instant) -> Self {
Self {
connected_since,
last_ping_sent: None,
last_pong_received: None,
invalid_pong_count: 0,
ping_timeouts: 0,
}
}
}
impl ConnectionHealth {
/// Calculate the round-trip time, if possible.
pub(crate) fn calc_rrt(&self) -> Option<Duration> {
match (self.last_ping_sent, self.last_pong_received) {
(Some(last_ping), Some(last_pong)) if last_ping.nonce == last_pong.nonce => {
Some(last_pong.timestamp.duration_since(last_ping.timestamp))
}
_ => None,
}
}
/// Check current health status.
///
/// This function must be polled periodically and returns a potential action to be performed.
pub(crate) fn update_health<R: Rng>(
&mut self,
rng: &mut R,
cfg: &HealthConfig,
now: Instant,
) -> HealthCheckOutcome {
// Having received too many pongs should always result in a disconnect.
if self.invalid_pong_count > cfg.pong_limit {
return HealthCheckOutcome::GiveUp;
}
// Our honeymoon period is from first establishment of the connection until we send a ping.
if now.saturating_duration_since(self.connected_since) < cfg.ping_interval {
return HealthCheckOutcome::DoNothing;
}
let send_ping = match self.last_ping_sent {
Some(last_ping) => {
match self.last_pong_received {
Some(prev_pong) if prev_pong.nonce() == last_ping.nonce() => {
// Normal operation. The next ping should be sent in a regular interval
// after receiving the last pong.
now >= prev_pong.timestamp() + cfg.ping_interval
}
_ => {
// No matching pong on record. Check if we need to timeout the ping.
if now >= last_ping.timestamp() + cfg.ping_timeout {
self.ping_timeouts += 1;
// Clear the `last_ping_sent`, schedule another to be sent.
self.last_ping_sent = None;
true
} else {
false
}
}
}
}
None => true,
};
if send_ping {
if self.ping_timeouts > cfg.ping_retries as u32 {
// We have exceeded the timeouts and will give up as a result.
return HealthCheckOutcome::GiveUp;
}
let ping = loop {
let candidate = TaggedTimestamp::new(rng, now);
if let Some(prev) = self.last_ping_sent {
if prev.nonce() == candidate.nonce() {
// Ensure we don't produce consecutive pings.
continue;
}
}
break candidate;
};
self.last_ping_sent = Some(ping);
HealthCheckOutcome::SendPing(ping.nonce())
} else {
HealthCheckOutcome::DoNothing
}
}
/// Records a pong that has been sent.
///
/// If `true`, the maximum number of pongs has been exceeded and the peer should be banned.
pub(crate) fn record_pong(&mut self, cfg: &HealthConfig, tt: TaggedTimestamp) -> bool {
let is_valid_pong = match self.last_ping_sent {
Some(last_ping) if last_ping.nonce() == tt.nonce => {
// Check if we already received a pong for this ping, which is a protocol violation.
if self
.last_pong_received
.map(|existing| existing.nonce() == tt.nonce)
.unwrap_or(false)
{
// Ping is a collsion, ban.
return true;
}
if last_ping.timestamp() > tt.timestamp() {
// Ping is from the past somehow, ignore it (probably a bug on our side).
return false;
}
// The ping is valid if it is within the timeout period.
last_ping.timestamp() + cfg.ping_timeout >= tt.timestamp()
}
_ => {
// Either the nonce did not match, or the nonce mismatched.
false
}
};
if is_valid_pong {
// Our pong is valid, reset invalid and ping count, then record it.
self.invalid_pong_count = 0;
self.ping_timeouts = 0;
self.last_pong_received = Some(tt);
false
} else {
self.invalid_pong_count += 1;
// If we have exceeded the invalid pong limit, ban.
self.invalid_pong_count > cfg.pong_limit
}
}
}
/// The outcome of periodic health check.
#[derive(Clone, Copy, Debug)]
pub(crate) enum HealthCheckOutcome {
/// Do nothing, as we recently took action.
DoNothing,
/// Send a ping with the given nonce.
SendPing(Nonce),
/// Give up on (i.e. terminate) the connection, as we exceeded the allowable ping limit.
GiveUp,
}
impl LargestSpecimen for Nonce {
fn largest_specimen<E: SizeEstimator>(estimator: &E, cache: &mut Cache) -> Self {
Self(LargestSpecimen::largest_specimen(estimator, cache))
}
}
#[cfg(test)]
mod tests {
use std::{collections::HashSet, time::Duration};
use assert_matches::assert_matches;
use rand::Rng;
use super::{ConnectionHealth, HealthCheckOutcome, HealthConfig};
use crate::{
components::network::health::TaggedTimestamp, testing::test_clock::TestClock,
types::NodeRng,
};
impl HealthConfig {
pub(crate) fn test_config() -> Self {
// Note: These values are assumed in tests, so do not change them.
HealthConfig {
ping_interval: Duration::from_secs(5),
ping_timeout: Duration::from_secs(2),
ping_retries: 3,
pong_limit: 6,
}
}
}
struct Fixtures {
clock: TestClock,
cfg: HealthConfig,
rng: NodeRng,
health: ConnectionHealth,
}
/// Sets up fixtures used in almost every test.
fn fixtures() -> Fixtures {
let clock = TestClock::new();
let cfg = HealthConfig::test_config();
let rng = crate::new_rng();
let health = ConnectionHealth::new(clock.now());
Fixtures {
clock,
cfg,
rng,
health,
}
}
#[test]
fn scenario_no_response() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// Repeated checks should not change the outcome.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// After 4.9 seconds, we still do not send a ping.
clock.advance(Duration::from_millis(4900));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// At 5, we expect our first ping.
clock.advance(Duration::from_millis(100));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Checking health again should not result in another ping.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_millis(100));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// After two seconds, we expect another ping to be sent, due to timeouts.
clock.advance(Duration::from_millis(2000));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// At this point, two pings have been sent. Configuration says to retry 3 times, so a total
// of five pings is expected.
clock.advance(Duration::from_millis(2000));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2000));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Finally, without receiving a ping at all, we give up.
clock.advance(Duration::from_millis(2000));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::GiveUp
);
}
#[test]
fn pings_use_different_nonces() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_secs(5));
let mut nonce_set = HashSet::new();
nonce_set.insert(assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
));
clock.advance(Duration::from_secs(2));
nonce_set.insert(assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
));
clock.advance(Duration::from_secs(2));
nonce_set.insert(assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
));
clock.advance(Duration::from_secs(2));
nonce_set.insert(assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
));
// Since it is a set, we expect less than 4 items if there were any duplicates.
assert_eq!(nonce_set.len(), 4);
}
#[test]
fn scenario_all_working() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// At 5 seconds, we expect our first ping.
clock.advance(Duration::from_secs(5));
let nonce_1 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
// Record a reply 500 ms later.
clock.advance(Duration::from_millis(500));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_1)));
// Our next pong should be 5 seconds later, not 4.5.
clock.advance(Duration::from_millis(4500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_millis(500));
let nonce_2 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
// We test an edge case here where we use the same timestamp for the received pong.
clock.advance(Duration::from_millis(500));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_2)));
// Afterwards, no ping should be sent.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// Do 1000 additional ping/pongs.
for _ in 0..1000 {
clock.advance(Duration::from_millis(5000));
let nonce = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_millis(250));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce)));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
}
}
#[test]
fn scenario_intermittent_failures() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
// We miss two pings initially, before recovering.
clock.advance(Duration::from_secs(5));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_secs(2));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_secs(2));
let nonce_1 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
clock.advance(Duration::from_secs(1));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_1)));
// We successfully "recovered", this should reset our ping counts. Miss three pings before
// successfully receiving a pong from 4th from here on out.
clock.advance(Duration::from_millis(5500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
let nonce_2 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
clock.advance(Duration::from_millis(500));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_2)));
// This again should reset. We miss four more pings and are disconnected.
clock.advance(Duration::from_millis(5500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.advance(Duration::from_millis(2500));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::GiveUp
);
}
#[test]
fn ignores_unwanted_pongs() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(5));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Make the `ConnectionHealth` receive some unasked pongs, without exceeding the unasked
// pong limit.
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
// The retry delay is 2 seconds (instead of 5 for the next pong after success), so ensure
// we retry due to not having received the correct nonce in the pong.
clock.advance(Duration::from_secs(2));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
}
#[test]
fn ensure_excessive_pongs_result_in_ban() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(5));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Make the `ConnectionHealth` receive some unasked pongs, without exceeding the unasked
// pong limit.
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
// 6 unasked pongs is still okay.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
assert!(health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
// 7 is too much.
// For good measure, we expect the health check to also output a disconnect instruction.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::GiveUp
);
}
#[test]
fn time_reversal_does_not_crash_but_is_ignored() {
// Usually a pong for a given (or any) nonce should always be received with a timestamp
// equal or later than the ping sent out. Due to a programming error or a lucky attacker +
// scheduling issue, there is a very minute chance this can actually happen.
//
// In these cases, the pongs should just be discarded, not crashing due to a underflow in
// the comparison.
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(5)); // t = 5
let nonce_1 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
// Ignore the nonce if sent in the past (and also don't crash).
clock.rewind(Duration::from_secs(1)); // t = 4
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_1)));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), rng.gen())));
// Another ping should be sent out, since `nonce_1` was ignored.
clock.advance(Duration::from_secs(3)); // t = 7
let nonce_2 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
// Nonce 2 will be received seemingly before the connection was even established.
clock.rewind(Duration::from_secs(3600));
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_2)));
}
#[test]
fn handles_missed_health_checks() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(15));
// We initially exceed our scheduled first ping by 10 seconds. This will cause the ping to
// be sent right there and then.
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Going forward 1 second should not change anything.
clock.advance(Duration::from_secs(1));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
// After another second, two seconds have passed since sending the first ping in total, so
// send another once.
clock.advance(Duration::from_secs(1));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// We have missed two pings total, now wait an hour. This will trigger the third ping.
clock.advance(Duration::from_secs(3600));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Fourth right after
clock.advance(Duration::from_secs(2));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
// Followed by a disconnect.
clock.advance(Duration::from_secs(2));
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::GiveUp
);
}
#[test]
fn ignores_time_travel() {
// Any call of the health update with timestamps that are provably from the past (i.e.
// before a recorded timestamp like a previous ping) should be ignored.
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(5)); // t = 5
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
clock.rewind(Duration::from_secs(3)); // t = 2
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_secs(4)); // t = 6
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::DoNothing
);
clock.advance(Duration::from_secs(1)); // t = 7
assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(_)
);
}
#[test]
fn duplicate_pong_immediately_terminates() {
let Fixtures {
mut clock,
cfg,
mut rng,
mut health,
} = fixtures();
clock.advance(Duration::from_secs(5));
let nonce_1 = assert_matches!(
health.update_health(&mut rng, &cfg, clock.now()),
HealthCheckOutcome::SendPing(nonce) => nonce
);
clock.advance(Duration::from_secs(1));
// Recording the pong once is fine, but the second time should result in a ban.
assert!(!health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_1)));
assert!(health.record_pong(&cfg, TaggedTimestamp::from_parts(clock.now(), nonce_1)));
}
}