Skip to content

Commit 91536ed

Browse files
authored
Merge pull request #2324 from dunxen/2023-05-rgscheckgenesishash
Fail RGS data processing early if there is a chain hash mismatch
2 parents eec5ec6 + 142fdca commit 91536ed

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lightning-background-processor/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ mod tests {
11241124
fn create_nodes(num_nodes: usize, persist_dir: &str) -> (String, Vec<Node>) {
11251125
let persist_temp_path = env::temp_dir().join(persist_dir);
11261126
let persist_dir = persist_temp_path.to_string_lossy().to_string();
1127-
let network = Network::Testnet;
1127+
let network = Network::Bitcoin;
11281128
let mut nodes = Vec::new();
11291129
for i in 0..num_nodes {
11301130
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
@@ -1135,7 +1135,7 @@ mod tests {
11351135
let scorer = Arc::new(Mutex::new(TestScorer::new()));
11361136
let seed = [i as u8; 32];
11371137
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone(), ()));
1138-
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1138+
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Bitcoin));
11391139
let persister = Arc::new(FilesystemPersister::new(format!("{}_persister_{}", &persist_dir, i)));
11401140
let now = Duration::from_secs(genesis_block.header.time as u64);
11411141
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));

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

+28
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ 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 {
75+
err: "Rapid Gossip Sync data's chain hash does not match the network graph's".to_owned(),
76+
action: ErrorAction::IgnoreError,
77+
}.into()
78+
);
79+
}
80+
7181
let latest_seen_timestamp: u32 = Readable::read(read_cursor)?;
7282

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

lightning/src/routing/gossip.rs

+5
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)