@@ -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) )
@@ -850,7 +842,7 @@ impl Readable for ChannelUpdateInfo {
850
842
}
851
843
}
852
844
853
- #[ derive( Clone , Debug , PartialEq , Eq ) ]
845
+ #[ derive( Clone , Debug , Eq ) ]
854
846
/// Details about a channel (both directions).
855
847
/// Received within a channel announcement.
856
848
pub struct ChannelInfo {
@@ -875,6 +867,24 @@ pub struct ChannelInfo {
875
867
/// (which we can probably assume we are - no-std environments probably won't have a full
876
868
/// network graph in memory!).
877
869
announcement_received_time : u64 ,
870
+
871
+ /// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_one`].
872
+ pub ( crate ) node_one_counter : u32 ,
873
+ /// The [`NodeInfo::node_counter`] of the node pointed to by [`Self::node_two`].
874
+ pub ( crate ) node_two_counter : u32 ,
875
+ }
876
+
877
+ impl PartialEq for ChannelInfo {
878
+ fn eq ( & self , o : & ChannelInfo ) -> bool {
879
+ self . features == o. features &&
880
+ self . node_one == o. node_one &&
881
+ self . one_to_two == o. one_to_two &&
882
+ self . node_two == o. node_two &&
883
+ self . two_to_one == o. two_to_one &&
884
+ self . capacity_sats == o. capacity_sats &&
885
+ self . announcement_message == o. announcement_message &&
886
+ self . announcement_received_time == o. announcement_received_time
887
+ }
878
888
}
879
889
880
890
impl ChannelInfo {
@@ -995,6 +1005,8 @@ impl Readable for ChannelInfo {
995
1005
capacity_sats : _init_tlv_based_struct_field ! ( capacity_sats, required) ,
996
1006
announcement_message : _init_tlv_based_struct_field ! ( announcement_message, required) ,
997
1007
announcement_received_time : _init_tlv_based_struct_field ! ( announcement_received_time, ( default_value, 0 ) ) ,
1008
+ node_one_counter : u32:: max_value ( ) ,
1009
+ node_two_counter : u32:: max_value ( ) ,
998
1010
} )
999
1011
}
1000
1012
}
@@ -1470,7 +1482,7 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1470
1482
let mut channels = IndexedMap :: with_capacity ( cmp:: min ( channels_count as usize , 22500 ) ) ;
1471
1483
for _ in 0 ..channels_count {
1472
1484
let chan_id: u64 = Readable :: read ( reader) ?;
1473
- let chan_info = Readable :: read ( reader) ?;
1485
+ let chan_info: ChannelInfo = Readable :: read ( reader) ?;
1474
1486
channels. insert ( chan_id, chan_info) ;
1475
1487
}
1476
1488
let nodes_count: u64 = Readable :: read ( reader) ?;
@@ -1486,6 +1498,13 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1486
1498
nodes. insert ( node_id, node_info) ;
1487
1499
}
1488
1500
1501
+ for ( _, chan) in channels. unordered_iter_mut ( ) {
1502
+ chan. node_one_counter =
1503
+ nodes. get ( & chan. node_one ) . ok_or ( DecodeError :: InvalidValue ) ?. node_counter ;
1504
+ chan. node_two_counter =
1505
+ nodes. get ( & chan. node_two ) . ok_or ( DecodeError :: InvalidValue ) ?. node_counter ;
1506
+ }
1507
+
1489
1508
let mut last_rapid_gossip_sync_timestamp: Option < u32 > = None ;
1490
1509
read_tlv_fields ! ( reader, {
1491
1510
( 1 , last_rapid_gossip_sync_timestamp, option) ,
@@ -1555,6 +1574,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1555
1574
1556
1575
fn test_node_counter_consistency ( & self ) {
1557
1576
#[ cfg( debug_assertions) ] {
1577
+ let channels = self . channels . read ( ) . unwrap ( ) ;
1558
1578
let nodes = self . nodes . read ( ) . unwrap ( ) ;
1559
1579
let removed_node_counters = self . removed_node_counters . lock ( ) . unwrap ( ) ;
1560
1580
let next_counter = self . next_node_counter . load ( Ordering :: Acquire ) ;
@@ -1574,6 +1594,11 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1574
1594
assert_eq ! ( used_node_counters[ pos] & bit, 0 ) ;
1575
1595
used_node_counters[ pos] |= bit;
1576
1596
}
1597
+
1598
+ for ( _, chan) in channels. unordered_iter ( ) {
1599
+ assert_eq ! ( chan. node_one_counter, nodes. get( & chan. node_one) . unwrap( ) . node_counter) ;
1600
+ assert_eq ! ( chan. node_two_counter, nodes. get( & chan. node_two) . unwrap( ) . node_counter) ;
1601
+ }
1577
1602
}
1578
1603
}
1579
1604
@@ -1738,6 +1763,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1738
1763
capacity_sats : None ,
1739
1764
announcement_message : None ,
1740
1765
announcement_received_time : timestamp,
1766
+ node_one_counter : u32:: max_value ( ) ,
1767
+ node_two_counter : u32:: max_value ( ) ,
1741
1768
} ;
1742
1769
1743
1770
self . add_channel_between_nodes ( short_channel_id, channel_info, None )
@@ -1752,7 +1779,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1752
1779
1753
1780
log_gossip ! ( self . logger, "Adding channel {} between nodes {} and {}" , short_channel_id, node_id_a, node_id_b) ;
1754
1781
1755
- match channels. entry ( short_channel_id) {
1782
+ let channel_entry = channels. entry ( short_channel_id) ;
1783
+ let channel_info = match channel_entry {
1756
1784
IndexedMapEntry :: Occupied ( mut entry) => {
1757
1785
//TODO: because asking the blockchain if short_channel_id is valid is only optional
1758
1786
//in the blockchain API, we need to handle it smartly here, though it's unclear
@@ -1768,28 +1796,35 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1768
1796
// c) it's unclear how to do so without exposing ourselves to massive DoS risk.
1769
1797
self . remove_channel_in_nodes ( & mut nodes, & entry. get ( ) , short_channel_id) ;
1770
1798
* entry. get_mut ( ) = channel_info;
1799
+ entry. into_mut ( )
1771
1800
} else {
1772
1801
return Err ( LightningError { err : "Already have knowledge of channel" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1773
1802
}
1774
1803
} ,
1775
1804
IndexedMapEntry :: Vacant ( entry) => {
1776
- entry. insert ( channel_info) ;
1805
+ entry. insert ( channel_info)
1777
1806
}
1778
1807
} ;
1779
1808
1780
- for current_node_id in [ node_id_a, node_id_b] . iter ( ) {
1809
+ let mut node_counter_id = [
1810
+ ( & mut channel_info. node_one_counter , node_id_a) ,
1811
+ ( & mut channel_info. node_two_counter , node_id_b)
1812
+ ] ;
1813
+ for ( node_counter, current_node_id) in node_counter_id. iter_mut ( ) {
1781
1814
match nodes. entry ( current_node_id. clone ( ) ) {
1782
1815
IndexedMapEntry :: Occupied ( node_entry) => {
1783
- node_entry. into_mut ( ) . channels . push ( short_channel_id) ;
1816
+ let node = node_entry. into_mut ( ) ;
1817
+ node. channels . push ( short_channel_id) ;
1818
+ * * node_counter = node. node_counter ;
1784
1819
} ,
1785
1820
IndexedMapEntry :: Vacant ( node_entry) => {
1786
1821
let mut removed_node_counters = self . removed_node_counters . lock ( ) . unwrap ( ) ;
1787
- let node_counter = removed_node_counters. pop ( )
1822
+ * * node_counter = removed_node_counters. pop ( )
1788
1823
. unwrap_or ( self . next_node_counter . fetch_add ( 1 , Ordering :: Relaxed ) as u32 ) ;
1789
1824
node_entry. insert ( NodeInfo {
1790
1825
channels : vec ! ( short_channel_id) ,
1791
1826
announcement_info : None ,
1792
- node_counter,
1827
+ node_counter : * * node_counter ,
1793
1828
} ) ;
1794
1829
}
1795
1830
} ;
@@ -1880,6 +1915,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1880
1915
announcement_message : if msg. excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY
1881
1916
{ full_msg. cloned ( ) } else { None } ,
1882
1917
announcement_received_time,
1918
+ node_one_counter : u32:: max_value ( ) ,
1919
+ node_two_counter : u32:: max_value ( ) ,
1883
1920
} ;
1884
1921
1885
1922
self . add_channel_between_nodes ( msg. short_channel_id , chan_info, utxo_value) ?;
@@ -3565,6 +3602,8 @@ pub(crate) mod tests {
3565
3602
capacity_sats : None ,
3566
3603
announcement_message : None ,
3567
3604
announcement_received_time : 87654 ,
3605
+ node_one_counter : 0 ,
3606
+ node_two_counter : 1 ,
3568
3607
} ;
3569
3608
3570
3609
let mut encoded_chan_info: Vec < u8 > = Vec :: new ( ) ;
@@ -3583,6 +3622,8 @@ pub(crate) mod tests {
3583
3622
capacity_sats : None ,
3584
3623
announcement_message : None ,
3585
3624
announcement_received_time : 87654 ,
3625
+ node_one_counter : 0 ,
3626
+ node_two_counter : 1 ,
3586
3627
} ;
3587
3628
3588
3629
let mut encoded_chan_info: Vec < u8 > = Vec :: new ( ) ;
0 commit comments