Skip to content

Commit

Permalink
Fail RGS data processing early if there is a chain hash mismatch
Browse files Browse the repository at this point in the history
No point in doing any extra processing if we don't even have a match
for the chain hash.
  • Loading branch information
dunxen committed May 29, 2023
1 parent 4dce209 commit 7109301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lightning-rapid-gossip-sync/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
}

let chain_hash: BlockHash = Readable::read(read_cursor)?;
let ng_genesis_hash = self.network_graph.get_genesis_hash();
if chain_hash != ng_genesis_hash {
return Err(
LightningError{err: format!(
"Rapid Gossip Sync data's chain hash ({}) does not match ours ({})", chain_hash, ng_genesis_hash),
action: ErrorAction::IgnoreError}.into());
}

let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;

if let Some(time) = current_time_unix {
Expand Down Expand Up @@ -667,4 +675,22 @@ mod tests {
panic!("Unexpected update result: {:?}", update_result)
}
}

#[test]
fn fails_early_on_chain_hash_mismatch() {
let logger = TestLogger::new();
// Set to testnet so that the VALID_RGS_BINARY chain hash of mainnet does not match.
let network_graph = NetworkGraph::new(Network::Testnet, &logger);

assert_eq!(network_graph.read_only().channels().len(), 0);

let rapid_sync = RapidGossipSync::new(&network_graph, &logger);
let update_result = rapid_sync.update_network_graph_no_std(&VALID_RGS_BINARY, Some(0));
assert!(update_result.is_err());
if let Err(GraphSyncError::LightningError(err)) = update_result {
assert!(err.err.contains("Rapid Gossip Sync data's chain hash "));
} else {
panic!("Unexpected update result: {:?}", update_result)
}
}
}
5 changes: 5 additions & 0 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
},
}
}

/// Gets the genesis hash for this network graph.
pub fn get_genesis_hash(&self) -> BlockHash {
self.genesis_hash
}
}

macro_rules! secp_verify_sig {
Expand Down

0 comments on commit 7109301

Please sign in to comment.