Skip to content

Commit 7109301

Browse files
committed
Fail RGS data processing early if there is a chain hash mismatch
No point in doing any extra processing if we don't even have a match for the chain hash.
1 parent 4dce209 commit 7109301

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
6868
}
6969

7070
let chain_hash: BlockHash = Readable::read(read_cursor)?;
71+
let ng_genesis_hash = self.network_graph.get_genesis_hash();
72+
if chain_hash != ng_genesis_hash {
73+
return Err(
74+
LightningError{err: format!(
75+
"Rapid Gossip Sync data's chain hash ({}) does not match ours ({})", chain_hash, ng_genesis_hash),
76+
action: ErrorAction::IgnoreError}.into());
77+
}
78+
7179
let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
7280

7381
if let Some(time) = current_time_unix {
@@ -667,4 +675,22 @@ mod tests {
667675
panic!("Unexpected update result: {:?}", update_result)
668676
}
669677
}
678+
679+
#[test]
680+
fn fails_early_on_chain_hash_mismatch() {
681+
let logger = TestLogger::new();
682+
// Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
683+
let network_graph = NetworkGraph::new(Network::Testnet, &logger);
684+
685+
assert_eq!(network_graph.read_only().channels().len(), 0);
686+
687+
let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
688+
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
689+
assert!(update_result.is_err());
690+
if let Err(GraphSyncError::LightningError(err)) = update_result {
691+
assert!(err.err.contains("Rapid Gossip Sync data's chain hash "));
692+
} else {
693+
panic!("Unexpected update result: {:?}", update_result)
694+
}
695+
}
670696
}

lightning/src/routing/gossip.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
366366
},
367367
}
368368
}
369+
370+
/// Gets the genesis hash for this network graph.
371+
pub fn get_genesis_hash(&self) -> BlockHash {
372+
self.genesis_hash
373+
}
369374
}
370375

371376
macro_rules! secp_verify_sig {

0 commit comments

Comments
 (0)