@@ -68,6 +68,14 @@ impl<NG: Deref<Target=NetworkGraph<L>>, L: Deref> RapidGossipSync<NG, L> where L
68
68
}
69
69
70
70
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
+
71
79
let latest_seen_timestamp: u32 = Readable :: read ( read_cursor) ?;
72
80
73
81
if let Some ( time) = current_time_unix {
@@ -667,4 +675,22 @@ mod tests {
667
675
panic ! ( "Unexpected update result: {:?}" , update_result)
668
676
}
669
677
}
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
+ }
670
696
}
0 commit comments