@@ -1720,6 +1720,15 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1720
1720
/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1721
1721
/// routing messages from a source using a protocol other than the lightning P2P protocol.
1722
1722
pub fn update_node_from_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < ( ) , LightningError > {
1723
+ // First check if we have the announcement already to avoid the CPU cost of validating a
1724
+ // redundant announcement.
1725
+ if let Some ( node) = self . nodes . read ( ) . unwrap ( ) . get ( & msg. contents . node_id ) {
1726
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1727
+ if node_info. last_update ( ) == msg. contents . timestamp {
1728
+ return Err ( LightningError { err : "Update had the same timestamp as last processed update" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1729
+ }
1730
+ }
1731
+ }
1723
1732
verify_node_announcement ( msg, & self . secp_ctx ) ?;
1724
1733
self . update_node_from_announcement_intern ( & msg. contents , Some ( & msg) )
1725
1734
}
@@ -1788,6 +1797,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1788
1797
where
1789
1798
U :: Target : UtxoLookup ,
1790
1799
{
1800
+ self . pre_channel_announcement_validation_check ( & msg. contents , utxo_lookup) ?;
1791
1801
verify_channel_announcement ( msg, & self . secp_ctx ) ?;
1792
1802
self . update_channel_from_unsigned_announcement_intern ( & msg. contents , Some ( msg) , utxo_lookup)
1793
1803
}
@@ -1817,6 +1827,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1817
1827
where
1818
1828
U :: Target : UtxoLookup ,
1819
1829
{
1830
+ self . pre_channel_announcement_validation_check ( & msg, utxo_lookup) ?;
1820
1831
self . update_channel_from_unsigned_announcement_intern ( msg, None , utxo_lookup)
1821
1832
}
1822
1833
@@ -1913,6 +1924,52 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1913
1924
Ok ( ( ) )
1914
1925
}
1915
1926
1927
+ /// If we already have all the information for a channel that we're gonna get, there's no
1928
+ /// reason to redundantly process it.
1929
+ ///
1930
+ /// In those cases, this will return an `Err` that we can return immediately. Otherwise it will
1931
+ /// return an `Ok(())`.
1932
+ fn pre_channel_announcement_validation_check < U : Deref > (
1933
+ & self , msg : & msgs:: UnsignedChannelAnnouncement , utxo_lookup : & Option < U > ,
1934
+ ) -> Result < ( ) , LightningError > where U :: Target : UtxoLookup {
1935
+ let channels = self . channels . read ( ) . unwrap ( ) ;
1936
+
1937
+ if let Some ( chan) = channels. get ( & msg. short_channel_id ) {
1938
+ if chan. capacity_sats . is_some ( ) {
1939
+ // If we'd previously looked up the channel on-chain and checked the script
1940
+ // against what appears on-chain, ignore the duplicate announcement.
1941
+ //
1942
+ // Because a reorg could replace one channel with another at the same SCID, if
1943
+ // the channel appears to be different, we re-validate. This doesn't expose us
1944
+ // to any more DoS risk than not, as a peer can always flood us with
1945
+ // randomly-generated SCID values anyway.
1946
+ //
1947
+ // We use the Node IDs rather than the bitcoin_keys to check for "equivalence"
1948
+ // as we didn't (necessarily) store the bitcoin keys, and we only really care
1949
+ // if the peers on the channel changed anyway.
1950
+ if msg. node_id_1 == chan. node_one && msg. node_id_2 == chan. node_two {
1951
+ return Err ( LightningError {
1952
+ err : "Already have chain-validated channel" . to_owned ( ) ,
1953
+ action : ErrorAction :: IgnoreDuplicateGossip
1954
+ } ) ;
1955
+ }
1956
+ } else if utxo_lookup. is_none ( ) {
1957
+ // Similarly, if we can't check the chain right now anyway, ignore the
1958
+ // duplicate announcement without bothering to take the channels write lock.
1959
+ return Err ( LightningError {
1960
+ err : "Already have non-chain-validated channel" . to_owned ( ) ,
1961
+ action : ErrorAction :: IgnoreDuplicateGossip
1962
+ } ) ;
1963
+ }
1964
+ }
1965
+
1966
+ Ok ( ( ) )
1967
+ }
1968
+
1969
+ /// Update channel information from a received announcement.
1970
+ ///
1971
+ /// Generally [`Self::pre_channel_announcement_validation_check`] should have been called
1972
+ /// first.
1916
1973
fn update_channel_from_unsigned_announcement_intern < U : Deref > (
1917
1974
& self , msg : & msgs:: UnsignedChannelAnnouncement , full_msg : Option < & msgs:: ChannelAnnouncement > , utxo_lookup : & Option < U >
1918
1975
) -> Result < ( ) , LightningError >
@@ -1930,39 +1987,6 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1930
1987
} ) ;
1931
1988
}
1932
1989
1933
- {
1934
- let channels = self . channels . read ( ) . unwrap ( ) ;
1935
-
1936
- if let Some ( chan) = channels. get ( & msg. short_channel_id ) {
1937
- if chan. capacity_sats . is_some ( ) {
1938
- // If we'd previously looked up the channel on-chain and checked the script
1939
- // against what appears on-chain, ignore the duplicate announcement.
1940
- //
1941
- // Because a reorg could replace one channel with another at the same SCID, if
1942
- // the channel appears to be different, we re-validate. This doesn't expose us
1943
- // to any more DoS risk than not, as a peer can always flood us with
1944
- // randomly-generated SCID values anyway.
1945
- //
1946
- // We use the Node IDs rather than the bitcoin_keys to check for "equivalence"
1947
- // as we didn't (necessarily) store the bitcoin keys, and we only really care
1948
- // if the peers on the channel changed anyway.
1949
- if msg. node_id_1 == chan. node_one && msg. node_id_2 == chan. node_two {
1950
- return Err ( LightningError {
1951
- err : "Already have chain-validated channel" . to_owned ( ) ,
1952
- action : ErrorAction :: IgnoreDuplicateGossip
1953
- } ) ;
1954
- }
1955
- } else if utxo_lookup. is_none ( ) {
1956
- // Similarly, if we can't check the chain right now anyway, ignore the
1957
- // duplicate announcement without bothering to take the channels write lock.
1958
- return Err ( LightningError {
1959
- err : "Already have non-chain-validated channel" . to_owned ( ) ,
1960
- action : ErrorAction :: IgnoreDuplicateGossip
1961
- } ) ;
1962
- }
1963
- }
1964
- }
1965
-
1966
1990
{
1967
1991
let removed_channels = self . removed_channels . lock ( ) . unwrap ( ) ;
1968
1992
let removed_nodes = self . removed_nodes . lock ( ) . unwrap ( ) ;
@@ -2564,11 +2588,6 @@ pub(crate) mod tests {
2564
2588
} ;
2565
2589
}
2566
2590
2567
- match gossip_sync. handle_node_announcement ( & valid_announcement) {
2568
- Ok ( res) => assert ! ( res) ,
2569
- Err ( _) => panic ! ( )
2570
- } ;
2571
-
2572
2591
let fake_msghash = hash_to_message ! ( zero_hash. as_byte_array( ) ) ;
2573
2592
match gossip_sync. handle_node_announcement (
2574
2593
& NodeAnnouncement {
@@ -2579,6 +2598,11 @@ pub(crate) mod tests {
2579
2598
Err ( e) => assert_eq ! ( e. err, "Invalid signature on node_announcement message" )
2580
2599
} ;
2581
2600
2601
+ match gossip_sync. handle_node_announcement ( & valid_announcement) {
2602
+ Ok ( res) => assert ! ( res) ,
2603
+ Err ( _) => panic ! ( )
2604
+ } ;
2605
+
2582
2606
let announcement_with_data = get_signed_node_announcement ( |unsigned_announcement| {
2583
2607
unsigned_announcement. timestamp += 1000 ;
2584
2608
unsigned_announcement. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
@@ -2700,23 +2724,24 @@ pub(crate) mod tests {
2700
2724
}
2701
2725
}
2702
2726
2703
- // Don't relay valid channels with excess data
2704
- let valid_announcement = get_signed_channel_announcement ( |unsigned_announcement| {
2727
+ let valid_excess_data_announcement = get_signed_channel_announcement ( |unsigned_announcement| {
2705
2728
unsigned_announcement. short_channel_id += 4 ;
2706
2729
unsigned_announcement. excess_data . resize ( MAX_EXCESS_BYTES_FOR_RELAY + 1 , 0 ) ;
2707
2730
} , node_1_privkey, node_2_privkey, & secp_ctx) ;
2708
- match gossip_sync. handle_channel_announcement ( & valid_announcement) {
2709
- Ok ( res) => assert ! ( !res) ,
2710
- _ => panic ! ( )
2711
- } ;
2712
2731
2713
- let mut invalid_sig_announcement = valid_announcement . clone ( ) ;
2732
+ let mut invalid_sig_announcement = valid_excess_data_announcement . clone ( ) ;
2714
2733
invalid_sig_announcement. contents . excess_data = Vec :: new ( ) ;
2715
2734
match gossip_sync. handle_channel_announcement ( & invalid_sig_announcement) {
2716
2735
Ok ( _) => panic ! ( ) ,
2717
2736
Err ( e) => assert_eq ! ( e. err, "Invalid signature on channel_announcement message" )
2718
2737
} ;
2719
2738
2739
+ // Don't relay valid channels with excess data
2740
+ match gossip_sync. handle_channel_announcement ( & valid_excess_data_announcement) {
2741
+ Ok ( res) => assert ! ( !res) ,
2742
+ _ => panic ! ( )
2743
+ } ;
2744
+
2720
2745
let channel_to_itself_announcement = get_signed_channel_announcement ( |_| { } , node_1_privkey, node_1_privkey, & secp_ctx) ;
2721
2746
match gossip_sync. handle_channel_announcement ( & channel_to_itself_announcement) {
2722
2747
Ok ( _) => panic ! ( ) ,
0 commit comments