@@ -64,7 +64,7 @@ const MAX_EXCESS_BYTES_FOR_RELAY: usize = 1024;
64
64
const MAX_SCIDS_PER_REPLY : usize = 8000 ;
65
65
66
66
/// Represents the compressed public key of a node
67
- #[ derive( Clone , Copy ) ]
67
+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
68
68
pub struct NodeId ( [ u8 ; PUBLIC_KEY_SIZE ] ) ;
69
69
70
70
impl NodeId {
@@ -116,14 +116,6 @@ impl core::hash::Hash for NodeId {
116
116
}
117
117
}
118
118
119
- impl Eq for NodeId { }
120
-
121
- impl PartialEq for NodeId {
122
- fn eq ( & self , other : & Self ) -> bool {
123
- self . 0 [ ..] == other. 0 [ ..]
124
- }
125
- }
126
-
127
119
impl cmp:: PartialOrd for NodeId {
128
120
fn partial_cmp ( & self , other : & Self ) -> Option < cmp:: Ordering > {
129
121
Some ( self . cmp ( other) )
@@ -885,7 +877,7 @@ impl Readable for ChannelUpdateInfo {
885
877
}
886
878
}
887
879
888
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
880
+ #[ derive( Clone , Debug , Eq ) ]
889
881
/// Details about a channel (both directions).
890
882
/// Received within a channel announcement.
891
883
pub struct ChannelInfo {
@@ -910,6 +902,24 @@ pub struct ChannelInfo {
910
902
/// (which we can probably assume we are - no-std environments probably won't have a full
911
903
/// network graph in memory!).
912
904
announcement_received_time : u64 ,
905
+
906
+ /// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_one`].
907
+ pub ( crate ) node_one_counter : u32 ,
908
+ /// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_two`].
909
+ pub ( crate ) node_two_counter : u32 ,
910
+ }
911
+
912
+ impl PartialEq for ChannelInfo {
913
+ fn eq ( & self , o : & ChannelInfo ) -> bool {
914
+ self . features == o. features &&
915
+ self . node_one == o. node_one &&
916
+ self . one_to_two == o. one_to_two &&
917
+ self . node_two == o. node_two &&
918
+ self . two_to_one == o. two_to_one &&
919
+ self . capacity_sats == o. capacity_sats &&
920
+ self . announcement_message == o. announcement_message &&
921
+ self . announcement_received_time == o. announcement_received_time
922
+ }
913
923
}
914
924
915
925
impl ChannelInfo {
@@ -1030,6 +1040,8 @@ impl Readable for ChannelInfo {
1030
1040
capacity_sats : _init_tlv_based_struct_field ! ( capacity_sats, required) ,
1031
1041
announcement_message : _init_tlv_based_struct_field ! ( announcement_message, required) ,
1032
1042
announcement_received_time : _init_tlv_based_struct_field ! ( announcement_received_time, ( default_value, 0 ) ) ,
1043
+ node_one_counter : u32:: max_value ( ) ,
1044
+ node_two_counter : u32:: max_value ( ) ,
1033
1045
} )
1034
1046
}
1035
1047
}
@@ -1505,7 +1517,7 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1505
1517
let mut channels = IndexedMap :: with_capacity ( cmp:: min ( channels_count as usize , 22500 ) ) ;
1506
1518
for _ in 0 ..channels_count {
1507
1519
let chan_id: u64 = Readable :: read ( reader) ?;
1508
- let chan_info = Readable :: read ( reader) ?;
1520
+ let chan_info: ChannelInfo = Readable :: read ( reader) ?;
1509
1521
channels. insert ( chan_id, chan_info) ;
1510
1522
}
1511
1523
let nodes_count: u64 = Readable :: read ( reader) ?;
@@ -1521,6 +1533,13 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1521
1533
nodes. insert ( node_id, node_info) ;
1522
1534
}
1523
1535
1536
+ for ( _, chan) in channels. unordered_iter_mut ( ) {
1537
+ chan. node_one_counter =
1538
+ nodes. get ( & chan. node_one ) . ok_or ( DecodeError :: InvalidValue ) ?. node_counter ;
1539
+ chan. node_two_counter =
1540
+ nodes. get ( & chan. node_two ) . ok_or ( DecodeError :: InvalidValue ) ?. node_counter ;
1541
+ }
1542
+
1524
1543
let mut last_rapid_gossip_sync_timestamp: Option < u32 > = None ;
1525
1544
read_tlv_fields ! ( reader, {
1526
1545
( 1 , last_rapid_gossip_sync_timestamp, option) ,
@@ -1590,6 +1609,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1590
1609
1591
1610
fn test_node_counter_consistency ( & self ) {
1592
1611
#[ cfg( debug_assertions) ] {
1612
+ let channels = self . channels . read ( ) . unwrap ( ) ;
1593
1613
let nodes = self . nodes . read ( ) . unwrap ( ) ;
1594
1614
let removed_node_counters = self . removed_node_counters . lock ( ) . unwrap ( ) ;
1595
1615
let next_counter = self . next_node_counter . load ( Ordering :: Acquire ) ;
@@ -1609,6 +1629,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1609
1629
assert_eq ! ( used_node_counters[ pos] & bit, 0 ) ;
1610
1630
used_node_counters[ pos] |= bit;
1611
1631
}
1632
+
1633
+ for ( _, chan) in channels. unordered_iter ( ) {
1634
+ assert_eq ! ( chan. node_one_counter, nodes. get( & chan. node_one) . unwrap( ) . node_counter) ;
1635
+ assert_eq ! ( chan. node_two_counter, nodes. get( & chan. node_two) . unwrap( ) . node_counter) ;
1636
+ }
1612
1637
}
1613
1638
}
1614
1639
@@ -1773,6 +1798,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1773
1798
capacity_sats : None ,
1774
1799
announcement_message : None ,
1775
1800
announcement_received_time : timestamp,
1801
+ node_one_counter : u32:: max_value ( ) ,
1802
+ node_two_counter : u32:: max_value ( ) ,
1776
1803
} ;
1777
1804
1778
1805
self . add_channel_between_nodes ( short_channel_id, channel_info, None )
@@ -1787,7 +1814,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1787
1814
1788
1815
log_gossip ! ( self . logger, "Adding channel {} between nodes {} and {}" , short_channel_id, node_id_a, node_id_b) ;
1789
1816
1790
- match channels. entry ( short_channel_id) {
1817
+ let channel_entry = channels. entry ( short_channel_id) ;
1818
+ let channel_info = match channel_entry {
1791
1819
IndexedMapEntry :: Occupied ( mut entry) => {
1792
1820
//TODO: because asking the blockchain if short_channel_id is valid is only optional
1793
1821
//in the blockchain API, we need to handle it smartly here, though it's unclear
@@ -1803,28 +1831,35 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1803
1831
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
1804
1832
self . remove_channel_in_nodes ( & mut nodes, & entry. get ( ) , short_channel_id) ;
1805
1833
* entry. get_mut ( ) = channel_info;
1834
+ entry. into_mut ( )
1806
1835
} else {
1807
1836
return Err ( LightningError { err : "Already have knowledge of channel" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1808
1837
}
1809
1838
} ,
1810
1839
IndexedMapEntry :: Vacant ( entry) => {
1811
- entry. insert ( channel_info) ;
1840
+ entry. insert ( channel_info)
1812
1841
}
1813
1842
} ;
1814
1843
1815
- for current_node_id in [ node_id_a, node_id_b] . iter ( ) {
1844
+ let mut node_counter_id = [
1845
+ ( & mut channel_info. node_one_counter , node_id_a) ,
1846
+ ( & mut channel_info. node_two_counter , node_id_b)
1847
+ ] ;
1848
+ for ( node_counter, current_node_id) in node_counter_id. iter_mut ( ) {
1816
1849
match nodes. entry ( current_node_id. clone ( ) ) {
1817
1850
IndexedMapEntry :: Occupied ( node_entry) => {
1818
- node_entry. into_mut ( ) . channels . push ( short_channel_id) ;
1851
+ let node = node_entry. into_mut ( ) ;
1852
+ node. channels . push ( short_channel_id) ;
1853
+ * * node_counter = node. node_counter ;
1819
1854
} ,
1820
1855
IndexedMapEntry :: Vacant ( node_entry) => {
1821
1856
let mut removed_node_counters = self . removed_node_counters . lock ( ) . unwrap ( ) ;
1822
- let node_counter = removed_node_counters. pop ( )
1857
+ * * node_counter = removed_node_counters. pop ( )
1823
1858
. unwrap_or ( self . next_node_counter . fetch_add ( 1 , Ordering :: Relaxed ) as u32 ) ;
1824
1859
node_entry. insert ( NodeInfo {
1825
1860
channels : vec ! ( short_channel_id) ,
1826
1861
announcement_info : None ,
1827
- node_counter,
1862
+ node_counter : * * node_counter ,
1828
1863
} ) ;
1829
1864
}
1830
1865
} ;
@@ -1915,6 +1950,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1915
1950
announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
1916
1951
{ full_msg. cloned ( ) } else { None } ,
1917
1952
announcement_received_time,
1953
+ node_one_counter : u32:: max_value ( ) ,
1954
+ node_two_counter : u32:: max_value ( ) ,
1918
1955
} ;
1919
1956
1920
1957
self . add_channel_between_nodes ( msg. short_channel_id , chan_info, utxo_value) ?;
@@ -3595,6 +3632,8 @@ pub(crate) mod tests {
3595
3632
capacity_sats : None ,
3596
3633
announcement_message : None ,
3597
3634
announcement_received_time : 87654 ,
3635
+ node_one_counter : 0 ,
3636
+ node_two_counter : 1 ,
3598
3637
} ;
3599
3638
3600
3639
let mut encoded_chan_info: Vec < u8 > = Vec :: new ( ) ;
@@ -3613,6 +3652,8 @@ pub(crate) mod tests {
3613
3652
capacity_sats : None ,
3614
3653
announcement_message : None ,
3615
3654
announcement_received_time : 87654 ,
3655
+ node_one_counter : 0 ,
3656
+ node_two_counter : 1 ,
3616
3657
} ;
3617
3658
3618
3659
let mut encoded_chan_info: Vec < u8 > = Vec :: new ( ) ;
0 commit comments