Skip to content

Commit f33e0c8

Browse files
committed
Improve comments and combine related timestamp edge case unit tests into one.
1 parent f74ead9 commit f33e0c8

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

lightning-rapid-gossip-sync/src/processing.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const GOSSIP_PREFIX: [u8; 4] = [76, 68, 75, 1];
3232
/// avoid malicious updates being able to trigger excessive memory allocation.
3333
const MAX_INITIAL_NODE_ID_VECTOR_CAPACITY: u32 = 50_000;
3434

35-
/// We remove disallow gossip data that's more than two weeks old, per BOLT 7's
35+
/// We remove gossip data that's more than two weeks old, per BOLT 7's
3636
/// suggestion.
3737
const STALE_RGS_UPDATE_AGE_LIMIT_SECS: u64 = 60 * 60 * 24 * 14;
3838

@@ -558,42 +558,7 @@ mod tests {
558558
}
559559

560560
#[test]
561-
fn full_update_succeeds_with_edge_timestamp_age() {
562-
const LATEST_SEEN_TIMESTAMP: u64 = 1642291930;
563-
let valid_input = vec![
564-
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
565-
79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
566-
0, 0, 0, 4, 2, 22, 7, 207, 206, 25, 164, 197, 231, 230, 231, 56, 102, 61, 250, 251,
567-
187, 172, 38, 46, 79, 247, 108, 44, 155, 48, 219, 238, 252, 53, 192, 6, 67, 2, 36, 125,
568-
157, 176, 223, 175, 234, 116, 94, 248, 201, 225, 97, 235, 50, 47, 115, 172, 63, 136,
569-
88, 216, 115, 11, 111, 217, 114, 84, 116, 124, 231, 107, 2, 158, 1, 242, 121, 152, 106,
570-
204, 131, 186, 35, 93, 70, 216, 10, 237, 224, 183, 89, 95, 65, 3, 83, 185, 58, 138,
571-
181, 64, 187, 103, 127, 68, 50, 2, 201, 19, 17, 138, 136, 149, 185, 226, 156, 137, 175,
572-
110, 32, 237, 0, 217, 90, 31, 100, 228, 149, 46, 219, 175, 168, 77, 4, 143, 38, 128,
573-
76, 97, 0, 0, 0, 2, 0, 0, 255, 8, 153, 192, 0, 2, 27, 0, 0, 0, 1, 0, 0, 255, 2, 68,
574-
226, 0, 6, 11, 0, 1, 2, 3, 0, 0, 0, 4, 0, 40, 0, 0, 0, 0, 0, 0, 3, 232, 0, 0, 3, 232,
575-
0, 0, 0, 1, 0, 0, 0, 0, 29, 129, 25, 192, 255, 8, 153, 192, 0, 2, 27, 0, 0, 60, 0, 0,
576-
0, 0, 0, 0, 0, 1, 0, 0, 0, 100, 0, 0, 2, 224, 0, 0, 0, 0, 58, 85, 116, 216, 0, 29, 0,
577-
0, 0, 1, 0, 0, 0, 125, 0, 0, 0, 0, 58, 85, 116, 216, 255, 2, 68, 226, 0, 6, 11, 0, 1,
578-
0, 0, 1,
579-
];
580-
581-
let block_hash = genesis_block(Network::Bitcoin).block_hash();
582-
let logger = TestLogger::new();
583-
let network_graph = NetworkGraph::new(block_hash, &logger);
584-
585-
assert_eq!(network_graph.read_only().channels().len(), 0);
586-
587-
let earliest_failing_time = LATEST_SEEN_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
588-
589-
let rapid_sync = RapidGossipSync::new(&network_graph);
590-
let update_result = rapid_sync.update_network_graph_no_std(&valid_input[..], Some(earliest_failing_time));
591-
assert!(update_result.is_ok());
592-
assert_eq!(network_graph.read_only().channels().len(), 2);
593-
}
594-
595-
#[test]
596-
fn full_update_succeeds_with_oldest_possible_timestamp() {
561+
fn full_update_succeeds_at_the_beginning_of_the_unix_era() {
597562
let valid_input = vec![
598563
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
599564
79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, 97, 227, 98, 218,
@@ -619,13 +584,15 @@ mod tests {
619584
assert_eq!(network_graph.read_only().channels().len(), 0);
620585

621586
let rapid_sync = RapidGossipSync::new(&network_graph);
587+
// this is mostly for checking uint underflow issues before the fuzzer does
622588
let update_result = rapid_sync.update_network_graph_no_std(&valid_input[..], Some(0));
623589
assert!(update_result.is_ok());
624590
assert_eq!(network_graph.read_only().channels().len(), 2);
625591
}
626592

627593
#[test]
628-
fn full_update_fails_with_old_timestamp() {
594+
fn timestamp_edge_cases_are_handled_correctly() {
595+
// this is the timestamp encoded in the binary data of valid_input below
629596
const LATEST_SEEN_TIMESTAMP: u64 = 1642291930;
630597
let valid_input = vec![
631598
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
@@ -647,22 +614,35 @@ mod tests {
647614

648615
let block_hash = genesis_block(Network::Bitcoin).block_hash();
649616
let logger = TestLogger::new();
650-
let network_graph = NetworkGraph::new(block_hash, &logger);
651617

652-
assert_eq!(network_graph.read_only().channels().len(), 0);
618+
let latest_succeeding_time = LATEST_SEEN_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS;
619+
let earliest_failing_time = latest_succeeding_time + 1;
653620

654-
let earliest_failing_time = LATEST_SEEN_TIMESTAMP + STALE_RGS_UPDATE_AGE_LIMIT_SECS + 1;
621+
{
622+
let network_graph = NetworkGraph::new(block_hash, &logger);
623+
assert_eq!(network_graph.read_only().channels().len(), 0);
655624

656-
let rapid_sync = RapidGossipSync::new(&network_graph);
657-
let update_result = rapid_sync.update_network_graph_no_std(&valid_input[..], Some(earliest_failing_time));
658-
assert!(update_result.is_err());
659-
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
660-
assert_eq!(
661-
lightning_error.err,
662-
"Rapid Gossip Sync data is more than two weeks old"
663-
);
664-
} else {
665-
panic!("Unexpected update result: {:?}", update_result)
625+
let rapid_sync = RapidGossipSync::new(&network_graph);
626+
let update_result = rapid_sync.update_network_graph_no_std(&valid_input[..], Some(latest_succeeding_time));
627+
assert!(update_result.is_ok());
628+
assert_eq!(network_graph.read_only().channels().len(), 2);
629+
}
630+
631+
{
632+
let network_graph = NetworkGraph::new(block_hash, &logger);
633+
assert_eq!(network_graph.read_only().channels().len(), 0);
634+
635+
let rapid_sync = RapidGossipSync::new(&network_graph);
636+
let update_result = rapid_sync.update_network_graph_no_std(&valid_input[..], Some(earliest_failing_time));
637+
assert!(update_result.is_err());
638+
if let Err(GraphSyncError::LightningError(lightning_error)) = update_result {
639+
assert_eq!(
640+
lightning_error.err,
641+
"Rapid Gossip Sync data is more than two weeks old"
642+
);
643+
} else {
644+
panic!("Unexpected update result: {:?}", update_result)
645+
}
666646
}
667647
}
668648

0 commit comments

Comments
 (0)