@@ -32,11 +32,11 @@ use crate::util::logger::{Logger, Level};
32
32
use crate :: util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
33
33
use crate :: util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
34
34
use crate :: util:: string:: PrintableString ;
35
+ use crate :: util:: indexed_map:: { IndexedMap , Entry as IndexedMapEntry } ;
35
36
36
37
use crate :: io;
37
38
use crate :: io_extras:: { copy, sink} ;
38
39
use crate :: prelude:: * ;
39
- use alloc:: collections:: { BTreeMap , btree_map:: Entry as BtreeEntry } ;
40
40
use core:: { cmp, fmt} ;
41
41
use crate :: sync:: { RwLock , RwLockReadGuard } ;
42
42
#[ cfg( feature = "std" ) ]
@@ -133,8 +133,8 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
133
133
genesis_hash : BlockHash ,
134
134
logger : L ,
135
135
// Lock order: channels -> nodes
136
- channels : RwLock < BTreeMap < u64 , ChannelInfo > > ,
137
- nodes : RwLock < BTreeMap < NodeId , NodeInfo > > ,
136
+ channels : RwLock < IndexedMap < u64 , ChannelInfo > > ,
137
+ nodes : RwLock < IndexedMap < NodeId , NodeInfo > > ,
138
138
// Lock order: removed_channels -> removed_nodes
139
139
//
140
140
// NOTE: In the following `removed_*` maps, we use seconds since UNIX epoch to track time instead
@@ -158,8 +158,8 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
158
158
159
159
/// A read-only view of [`NetworkGraph`].
160
160
pub struct ReadOnlyNetworkGraph < ' a > {
161
- channels : RwLockReadGuard < ' a , BTreeMap < u64 , ChannelInfo > > ,
162
- nodes : RwLockReadGuard < ' a , BTreeMap < NodeId , NodeInfo > > ,
161
+ channels : RwLockReadGuard < ' a , IndexedMap < u64 , ChannelInfo > > ,
162
+ nodes : RwLockReadGuard < ' a , IndexedMap < NodeId , NodeInfo > > ,
163
163
}
164
164
165
165
/// Update to the [`NetworkGraph`] based on payment failure information conveyed via the Onion
@@ -1131,13 +1131,13 @@ impl<L: Deref> Writeable for NetworkGraph<L> where L::Target: Logger {
1131
1131
self . genesis_hash . write ( writer) ?;
1132
1132
let channels = self . channels . read ( ) . unwrap ( ) ;
1133
1133
( channels. len ( ) as u64 ) . write ( writer) ?;
1134
- for ( ref chan_id, ref chan_info) in channels. iter ( ) {
1134
+ for ( ref chan_id, ref chan_info) in channels. unordered_iter ( ) {
1135
1135
( * chan_id) . write ( writer) ?;
1136
1136
chan_info. write ( writer) ?;
1137
1137
}
1138
1138
let nodes = self . nodes . read ( ) . unwrap ( ) ;
1139
1139
( nodes. len ( ) as u64 ) . write ( writer) ?;
1140
- for ( ref node_id, ref node_info) in nodes. iter ( ) {
1140
+ for ( ref node_id, ref node_info) in nodes. unordered_iter ( ) {
1141
1141
node_id. write ( writer) ?;
1142
1142
node_info. write ( writer) ?;
1143
1143
}
@@ -1156,14 +1156,14 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1156
1156
1157
1157
let genesis_hash: BlockHash = Readable :: read ( reader) ?;
1158
1158
let channels_count: u64 = Readable :: read ( reader) ?;
1159
- let mut channels: BTreeMap < u64 , ChannelInfo > = BTreeMap :: new ( ) ;
1159
+ let mut channels = IndexedMap :: new ( ) ;
1160
1160
for _ in 0 ..channels_count {
1161
1161
let chan_id: u64 = Readable :: read ( reader) ?;
1162
1162
let chan_info = Readable :: read ( reader) ?;
1163
1163
channels. insert ( chan_id, chan_info) ;
1164
1164
}
1165
1165
let nodes_count: u64 = Readable :: read ( reader) ?;
1166
- let mut nodes: BTreeMap < NodeId , NodeInfo > = BTreeMap :: new ( ) ;
1166
+ let mut nodes = IndexedMap :: new ( ) ;
1167
1167
for _ in 0 ..nodes_count {
1168
1168
let node_id = Readable :: read ( reader) ?;
1169
1169
let node_info = Readable :: read ( reader) ?;
@@ -1191,11 +1191,11 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1191
1191
impl < L : Deref > fmt:: Display for NetworkGraph < L > where L :: Target : Logger {
1192
1192
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
1193
1193
writeln ! ( f, "Network map\n [Channels]" ) ?;
1194
- for ( key, val) in self . channels . read ( ) . unwrap ( ) . iter ( ) {
1194
+ for ( key, val) in self . channels . read ( ) . unwrap ( ) . unordered_iter ( ) {
1195
1195
writeln ! ( f, " {}: {}" , key, val) ?;
1196
1196
}
1197
1197
writeln ! ( f, "[Nodes]" ) ?;
1198
- for ( & node_id, val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
1198
+ for ( & node_id, val) in self . nodes . read ( ) . unwrap ( ) . unordered_iter ( ) {
1199
1199
writeln ! ( f, " {}: {}" , log_bytes!( node_id. as_slice( ) ) , val) ?;
1200
1200
}
1201
1201
Ok ( ( ) )
@@ -1218,8 +1218,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1218
1218
secp_ctx : Secp256k1 :: verification_only ( ) ,
1219
1219
genesis_hash,
1220
1220
logger,
1221
- channels : RwLock :: new ( BTreeMap :: new ( ) ) ,
1222
- nodes : RwLock :: new ( BTreeMap :: new ( ) ) ,
1221
+ channels : RwLock :: new ( IndexedMap :: new ( ) ) ,
1222
+ nodes : RwLock :: new ( IndexedMap :: new ( ) ) ,
1223
1223
last_rapid_gossip_sync_timestamp : Mutex :: new ( None ) ,
1224
1224
removed_channels : Mutex :: new ( HashMap :: new ( ) ) ,
1225
1225
removed_nodes : Mutex :: new ( HashMap :: new ( ) ) ,
@@ -1252,7 +1252,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1252
1252
/// purposes.
1253
1253
#[ cfg( test) ]
1254
1254
pub fn clear_nodes_announcement_info ( & self ) {
1255
- for node in self . nodes . write ( ) . unwrap ( ) . iter_mut ( ) {
1255
+ for node in self . nodes . write ( ) . unwrap ( ) . unordered_iter_mut ( ) {
1256
1256
node. 1 . announcement_info = None ;
1257
1257
}
1258
1258
}
@@ -1382,7 +1382,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1382
1382
let node_id_b = channel_info. node_two . clone ( ) ;
1383
1383
1384
1384
match channels. entry ( short_channel_id) {
1385
- BtreeEntry :: Occupied ( mut entry) => {
1385
+ IndexedMapEntry :: Occupied ( mut entry) => {
1386
1386
//TODO: because asking the blockchain if short_channel_id is valid is only optional
1387
1387
//in the blockchain API, we need to handle it smartly here, though it's unclear
1388
1388
//exactly how...
@@ -1401,17 +1401,17 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1401
1401
return Err ( LightningError { err : "Already have knowledge of channel" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1402
1402
}
1403
1403
} ,
1404
- BtreeEntry :: Vacant ( entry) => {
1404
+ IndexedMapEntry :: Vacant ( entry) => {
1405
1405
entry. insert ( channel_info) ;
1406
1406
}
1407
1407
} ;
1408
1408
1409
1409
for current_node_id in [ node_id_a, node_id_b] . iter ( ) {
1410
1410
match nodes. entry ( current_node_id. clone ( ) ) {
1411
- BtreeEntry :: Occupied ( node_entry) => {
1411
+ IndexedMapEntry :: Occupied ( node_entry) => {
1412
1412
node_entry. into_mut ( ) . channels . push ( short_channel_id) ;
1413
1413
} ,
1414
- BtreeEntry :: Vacant ( node_entry) => {
1414
+ IndexedMapEntry :: Vacant ( node_entry) => {
1415
1415
node_entry. insert ( NodeInfo {
1416
1416
channels : vec ! ( short_channel_id) ,
1417
1417
announcement_info : None ,
@@ -1585,7 +1585,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1585
1585
for scid in node. channels . iter ( ) {
1586
1586
if let Some ( chan_info) = channels. remove ( scid) {
1587
1587
let other_node_id = if node_id == chan_info. node_one { chan_info. node_two } else { chan_info. node_one } ;
1588
- if let BtreeEntry :: Occupied ( mut other_node_entry) = nodes. entry ( other_node_id) {
1588
+ if let IndexedMapEntry :: Occupied ( mut other_node_entry) = nodes. entry ( other_node_id) {
1589
1589
other_node_entry. get_mut ( ) . channels . retain ( |chan_id| {
1590
1590
* scid != * chan_id
1591
1591
} ) ;
@@ -1644,7 +1644,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1644
1644
// Sadly BTreeMap::retain was only stabilized in 1.53 so we can't switch to it for some
1645
1645
// time.
1646
1646
let mut scids_to_remove = Vec :: new ( ) ;
1647
- for ( scid, info) in channels. iter_mut ( ) {
1647
+ for ( scid, info) in channels. unordered_iter_mut ( ) {
1648
1648
if info. one_to_two . is_some ( ) && info. one_to_two . as_ref ( ) . unwrap ( ) . last_update < min_time_unix {
1649
1649
info. one_to_two = None ;
1650
1650
}
@@ -1813,10 +1813,10 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1813
1813
Ok ( ( ) )
1814
1814
}
1815
1815
1816
- fn remove_channel_in_nodes ( nodes : & mut BTreeMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1816
+ fn remove_channel_in_nodes ( nodes : & mut IndexedMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1817
1817
macro_rules! remove_from_node {
1818
1818
( $node_id: expr) => {
1819
- if let BtreeEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
1819
+ if let IndexedMapEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
1820
1820
entry. get_mut( ) . channels. retain( |chan_id| {
1821
1821
short_channel_id != * chan_id
1822
1822
} ) ;
@@ -1837,8 +1837,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1837
1837
impl ReadOnlyNetworkGraph < ' _ > {
1838
1838
/// Returns all known valid channels' short ids along with announced channel info.
1839
1839
///
1840
- /// (C-not exported) because we have no mapping for `BTreeMap`s
1841
- pub fn channels ( & self ) -> & BTreeMap < u64 , ChannelInfo > {
1840
+ /// (C-not exported) because we don't want to return lifetime'd references
1841
+ pub fn channels ( & self ) -> & IndexedMap < u64 , ChannelInfo > {
1842
1842
& * self . channels
1843
1843
}
1844
1844
@@ -1850,13 +1850,13 @@ impl ReadOnlyNetworkGraph<'_> {
1850
1850
#[ cfg( c_bindings) ] // Non-bindings users should use `channels`
1851
1851
/// Returns the list of channels in the graph
1852
1852
pub fn list_channels ( & self ) -> Vec < u64 > {
1853
- self . channels . keys ( ) . map ( |c| * c) . collect ( )
1853
+ self . channels . unordered_keys ( ) . map ( |c| * c) . collect ( )
1854
1854
}
1855
1855
1856
1856
/// Returns all known nodes' public keys along with announced node info.
1857
1857
///
1858
- /// (C-not exported) because we have no mapping for `BTreeMap`s
1859
- pub fn nodes ( & self ) -> & BTreeMap < NodeId , NodeInfo > {
1858
+ /// (C-not exported) because we don't want to return lifetime'd references
1859
+ pub fn nodes ( & self ) -> & IndexedMap < NodeId , NodeInfo > {
1860
1860
& * self . nodes
1861
1861
}
1862
1862
@@ -1868,7 +1868,7 @@ impl ReadOnlyNetworkGraph<'_> {
1868
1868
#[ cfg( c_bindings) ] // Non-bindings users should use `nodes`
1869
1869
/// Returns the list of nodes in the graph
1870
1870
pub fn list_nodes ( & self ) -> Vec < NodeId > {
1871
- self . nodes . keys ( ) . map ( |n| * n) . collect ( )
1871
+ self . nodes . unordered_keys ( ) . map ( |n| * n) . collect ( )
1872
1872
}
1873
1873
1874
1874
/// Get network addresses by node id.
0 commit comments