@@ -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
@@ -1130,13 +1130,13 @@ impl<L: Deref> Writeable for NetworkGraph<L> where L::Target: Logger {
1130
1130
self . genesis_hash . write ( writer) ?;
1131
1131
let channels = self . channels . read ( ) . unwrap ( ) ;
1132
1132
( channels. len ( ) as u64 ) . write ( writer) ?;
1133
- for ( ref chan_id, ref chan_info) in channels. iter ( ) {
1133
+ for ( ref chan_id, ref chan_info) in channels. unordered_iter ( ) {
1134
1134
( * chan_id) . write ( writer) ?;
1135
1135
chan_info. write ( writer) ?;
1136
1136
}
1137
1137
let nodes = self . nodes . read ( ) . unwrap ( ) ;
1138
1138
( nodes. len ( ) as u64 ) . write ( writer) ?;
1139
- for ( ref node_id, ref node_info) in nodes. iter ( ) {
1139
+ for ( ref node_id, ref node_info) in nodes. unordered_iter ( ) {
1140
1140
node_id. write ( writer) ?;
1141
1141
node_info. write ( writer) ?;
1142
1142
}
@@ -1155,14 +1155,14 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1155
1155
1156
1156
let genesis_hash: BlockHash = Readable :: read ( reader) ?;
1157
1157
let channels_count: u64 = Readable :: read ( reader) ?;
1158
- let mut channels: BTreeMap < u64 , ChannelInfo > = BTreeMap :: new ( ) ;
1158
+ let mut channels = IndexedMap :: new ( ) ;
1159
1159
for _ in 0 ..channels_count {
1160
1160
let chan_id: u64 = Readable :: read ( reader) ?;
1161
1161
let chan_info = Readable :: read ( reader) ?;
1162
1162
channels. insert ( chan_id, chan_info) ;
1163
1163
}
1164
1164
let nodes_count: u64 = Readable :: read ( reader) ?;
1165
- let mut nodes: BTreeMap < NodeId , NodeInfo > = BTreeMap :: new ( ) ;
1165
+ let mut nodes = IndexedMap :: new ( ) ;
1166
1166
for _ in 0 ..nodes_count {
1167
1167
let node_id = Readable :: read ( reader) ?;
1168
1168
let node_info = Readable :: read ( reader) ?;
@@ -1190,11 +1190,11 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
1190
1190
impl < L : Deref > fmt:: Display for NetworkGraph < L > where L :: Target : Logger {
1191
1191
fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
1192
1192
writeln ! ( f, "Network map\n [Channels]" ) ?;
1193
- for ( key, val) in self . channels . read ( ) . unwrap ( ) . iter ( ) {
1193
+ for ( key, val) in self . channels . read ( ) . unwrap ( ) . unordered_iter ( ) {
1194
1194
writeln ! ( f, " {}: {}" , key, val) ?;
1195
1195
}
1196
1196
writeln ! ( f, "[Nodes]" ) ?;
1197
- for ( & node_id, val) in self . nodes . read ( ) . unwrap ( ) . iter ( ) {
1197
+ for ( & node_id, val) in self . nodes . read ( ) . unwrap ( ) . unordered_iter ( ) {
1198
1198
writeln ! ( f, " {}: {}" , log_bytes!( node_id. as_slice( ) ) , val) ?;
1199
1199
}
1200
1200
Ok ( ( ) )
@@ -1217,8 +1217,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1217
1217
secp_ctx : Secp256k1 :: verification_only ( ) ,
1218
1218
genesis_hash,
1219
1219
logger,
1220
- channels : RwLock :: new ( BTreeMap :: new ( ) ) ,
1221
- nodes : RwLock :: new ( BTreeMap :: new ( ) ) ,
1220
+ channels : RwLock :: new ( IndexedMap :: new ( ) ) ,
1221
+ nodes : RwLock :: new ( IndexedMap :: new ( ) ) ,
1222
1222
last_rapid_gossip_sync_timestamp : Mutex :: new ( None ) ,
1223
1223
removed_channels : Mutex :: new ( HashMap :: new ( ) ) ,
1224
1224
removed_nodes : Mutex :: new ( HashMap :: new ( ) ) ,
@@ -1251,7 +1251,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1251
1251
/// purposes.
1252
1252
#[ cfg( test) ]
1253
1253
pub fn clear_nodes_announcement_info ( & self ) {
1254
- for node in self . nodes . write ( ) . unwrap ( ) . iter_mut ( ) {
1254
+ for node in self . nodes . write ( ) . unwrap ( ) . unordered_iter_mut ( ) {
1255
1255
node. 1 . announcement_info = None ;
1256
1256
}
1257
1257
}
@@ -1381,7 +1381,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1381
1381
let node_id_b = channel_info. node_two . clone ( ) ;
1382
1382
1383
1383
match channels. entry ( short_channel_id) {
1384
- BtreeEntry :: Occupied ( mut entry) => {
1384
+ IndexedMapEntry :: Occupied ( mut entry) => {
1385
1385
//TODO: because asking the blockchain if short_channel_id is valid is only optional
1386
1386
//in the blockchain API, we need to handle it smartly here, though it's unclear
1387
1387
//exactly how...
@@ -1400,17 +1400,17 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1400
1400
return Err ( LightningError { err : "Already have knowledge of channel" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
1401
1401
}
1402
1402
} ,
1403
- BtreeEntry :: Vacant ( entry) => {
1403
+ IndexedMapEntry :: Vacant ( entry) => {
1404
1404
entry. insert ( channel_info) ;
1405
1405
}
1406
1406
} ;
1407
1407
1408
1408
for current_node_id in [ node_id_a, node_id_b] . iter ( ) {
1409
1409
match nodes. entry ( current_node_id. clone ( ) ) {
1410
- BtreeEntry :: Occupied ( node_entry) => {
1410
+ IndexedMapEntry :: Occupied ( node_entry) => {
1411
1411
node_entry. into_mut ( ) . channels . push ( short_channel_id) ;
1412
1412
} ,
1413
- BtreeEntry :: Vacant ( node_entry) => {
1413
+ IndexedMapEntry :: Vacant ( node_entry) => {
1414
1414
node_entry. insert ( NodeInfo {
1415
1415
channels : vec ! ( short_channel_id) ,
1416
1416
announcement_info : None ,
@@ -1584,7 +1584,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1584
1584
for scid in node. channels . iter ( ) {
1585
1585
if let Some ( chan_info) = channels. remove ( scid) {
1586
1586
let other_node_id = if node_id == chan_info. node_one { chan_info. node_two } else { chan_info. node_one } ;
1587
- if let BtreeEntry :: Occupied ( mut other_node_entry) = nodes. entry ( other_node_id) {
1587
+ if let IndexedMapEntry :: Occupied ( mut other_node_entry) = nodes. entry ( other_node_id) {
1588
1588
other_node_entry. get_mut ( ) . channels . retain ( |chan_id| {
1589
1589
* scid != * chan_id
1590
1590
} ) ;
@@ -1643,7 +1643,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1643
1643
// Sadly BTreeMap::retain was only stabilized in 1.53 so we can't switch to it for some
1644
1644
// time.
1645
1645
let mut scids_to_remove = Vec :: new ( ) ;
1646
- for ( scid, info) in channels. iter_mut ( ) {
1646
+ for ( scid, info) in channels. unordered_iter_mut ( ) {
1647
1647
if info. one_to_two . is_some ( ) && info. one_to_two . as_ref ( ) . unwrap ( ) . last_update < min_time_unix {
1648
1648
info. one_to_two = None ;
1649
1649
}
@@ -1812,10 +1812,10 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1812
1812
Ok ( ( ) )
1813
1813
}
1814
1814
1815
- fn remove_channel_in_nodes ( nodes : & mut BTreeMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1815
+ fn remove_channel_in_nodes ( nodes : & mut IndexedMap < NodeId , NodeInfo > , chan : & ChannelInfo , short_channel_id : u64 ) {
1816
1816
macro_rules! remove_from_node {
1817
1817
( $node_id: expr) => {
1818
- if let BtreeEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
1818
+ if let IndexedMapEntry :: Occupied ( mut entry) = nodes. entry( $node_id) {
1819
1819
entry. get_mut( ) . channels. retain( |chan_id| {
1820
1820
short_channel_id != * chan_id
1821
1821
} ) ;
@@ -1836,8 +1836,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1836
1836
impl ReadOnlyNetworkGraph < ' _ > {
1837
1837
/// Returns all known valid channels' short ids along with announced channel info.
1838
1838
///
1839
- /// (C-not exported) because we have no mapping for `BTreeMap`s
1840
- pub fn channels ( & self ) -> & BTreeMap < u64 , ChannelInfo > {
1839
+ /// (C-not exported) because we don't want to return lifetime'd references
1840
+ pub fn channels ( & self ) -> & IndexedMap < u64 , ChannelInfo > {
1841
1841
& * self . channels
1842
1842
}
1843
1843
@@ -1854,8 +1854,8 @@ impl ReadOnlyNetworkGraph<'_> {
1854
1854
1855
1855
/// Returns all known nodes' public keys along with announced node info.
1856
1856
///
1857
- /// (C-not exported) because we have no mapping for `BTreeMap`s
1858
- pub fn nodes ( & self ) -> & BTreeMap < NodeId , NodeInfo > {
1857
+ /// (C-not exported) because we don't want to return lifetime'd references
1858
+ pub fn nodes ( & self ) -> & IndexedMap < NodeId , NodeInfo > {
1859
1859
& * self . nodes
1860
1860
}
1861
1861
0 commit comments