use core::ops::Deref;
use core::sync::atomic::Ordering;
-use bitcoin::BlockHash;
+use bitcoin::blockdata::constants::ChainHash;
use bitcoin::secp256k1::PublicKey;
use lightning::ln::msgs::{
use lightning::util::ser::{BigSize, Readable};
use lightning::io;
-use crate::error::GraphSyncError;
-use crate::RapidGossipSync;
+use crate::{GraphSyncError, RapidGossipSync};
#[cfg(all(feature = "std", not(test)))]
use std::time::{SystemTime, UNIX_EPOCH};
-#[cfg(not(feature = "std"))]
+#[cfg(all(not(feature = "std"), not(test)))]
use alloc::{vec::Vec, borrow::ToOwned};
/// The purpose of this prefix is to identify the serialization format, should other rapid gossip
return Err(DecodeError::UnknownVersion.into());
}
- let chain_hash: BlockHash = Readable::read(read_cursor)?;
+ let chain_hash: ChainHash = Readable::read(read_cursor)?;
+ let ng_chain_hash = self.network_graph.get_chain_hash();
+ if chain_hash != ng_chain_hash {
+ return Err(
+ LightningError {
+ err: "Rapid Gossip Sync data's chain hash does not match the network graph's".to_owned(),
+ action: ErrorAction::IgnoreError,
+ }.into()
+ );
+ }
+
let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
if let Some(time) = current_time_unix {
use lightning::routing::gossip::NetworkGraph;
use lightning::util::test_utils::TestLogger;
- use crate::error::GraphSyncError;
use crate::processing::STALE_RGS_UPDATE_AGE_LIMIT_SECS;
- use crate::RapidGossipSync;
+ use crate::{GraphSyncError, RapidGossipSync};
const VALID_RGS_BINARY: [u8; 300] = [
76, 68, 75, 1, 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247,
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_eq!(err.err, "Rapid Gossip Sync data's chain hash does not match the network graph's");
+ } else {
+ panic!("Unexpected update result: {:?}", update_result)
+ }
+ }
}