@@ -32,11 +32,11 @@ use crate::util::logger::{Logger, Level};
3232use crate :: util:: events:: { MessageSendEvent , MessageSendEventsProvider } ;
3333use crate :: util:: scid_utils:: { block_from_scid, scid_from_parts, MAX_SCID_BLOCK } ;
3434use crate :: util:: string:: PrintableString ;
35+ use crate :: util:: indexed_map:: { IndexedMap , Entry as IndexedMapEntry } ;
3536
3637use crate :: io;
3738use crate :: io_extras:: { copy, sink} ;
3839use crate :: prelude:: * ;
39- use alloc:: collections:: { BTreeMap , btree_map:: Entry as BtreeEntry } ;
4040use core:: { cmp, fmt} ;
4141use crate :: sync:: { RwLock , RwLockReadGuard } ;
4242#[ cfg( feature = "std" ) ]
@@ -133,8 +133,8 @@ pub struct NetworkGraph<L: Deref> where L::Target: Logger {
133133 genesis_hash : BlockHash ,
134134 logger : L ,
135135 // 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 > > ,
138138 // Lock order: removed_channels -> removed_nodes
139139 //
140140 // 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 {
158158
159159/// A read-only view of [`NetworkGraph`].
160160pub 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 > > ,
163163}
164164
165165/// 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 {
11301130 self . genesis_hash . write ( writer) ?;
11311131 let channels = self . channels . read ( ) . unwrap ( ) ;
11321132 ( 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 ( ) {
11341134 ( * chan_id) . write ( writer) ?;
11351135 chan_info. write ( writer) ?;
11361136 }
11371137 let nodes = self . nodes . read ( ) . unwrap ( ) ;
11381138 ( 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 ( ) {
11401140 node_id. write ( writer) ?;
11411141 node_info. write ( writer) ?;
11421142 }
@@ -1155,14 +1155,14 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
11551155
11561156 let genesis_hash: BlockHash = Readable :: read ( reader) ?;
11571157 let channels_count: u64 = Readable :: read ( reader) ?;
1158- let mut channels: BTreeMap < u64 , ChannelInfo > = BTreeMap :: new ( ) ;
1158+ let mut channels = IndexedMap :: new ( ) ;
11591159 for _ in 0 ..channels_count {
11601160 let chan_id: u64 = Readable :: read ( reader) ?;
11611161 let chan_info = Readable :: read ( reader) ?;
11621162 channels. insert ( chan_id, chan_info) ;
11631163 }
11641164 let nodes_count: u64 = Readable :: read ( reader) ?;
1165- let mut nodes: BTreeMap < NodeId , NodeInfo > = BTreeMap :: new ( ) ;
1165+ let mut nodes = IndexedMap :: new ( ) ;
11661166 for _ in 0 ..nodes_count {
11671167 let node_id = Readable :: read ( reader) ?;
11681168 let node_info = Readable :: read ( reader) ?;
@@ -1190,11 +1190,11 @@ impl<L: Deref> ReadableArgs<L> for NetworkGraph<L> where L::Target: Logger {
11901190impl < L : Deref > fmt:: Display for NetworkGraph < L > where L :: Target : Logger {
11911191 fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
11921192 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 ( ) {
11941194 writeln ! ( f, " {}: {}" , key, val) ?;
11951195 }
11961196 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 ( ) {
11981198 writeln ! ( f, " {}: {}" , log_bytes!( node_id. as_slice( ) ) , val) ?;
11991199 }
12001200 Ok ( ( ) )
@@ -1217,8 +1217,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12171217 secp_ctx : Secp256k1 :: verification_only ( ) ,
12181218 genesis_hash,
12191219 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 ( ) ) ,
12221222 last_rapid_gossip_sync_timestamp : Mutex :: new ( None ) ,
12231223 removed_channels : Mutex :: new ( HashMap :: new ( ) ) ,
12241224 removed_nodes : Mutex :: new ( HashMap :: new ( ) ) ,
@@ -1251,7 +1251,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12511251 /// purposes.
12521252 #[ cfg( test) ]
12531253 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 ( ) {
12551255 node. 1 . announcement_info = None ;
12561256 }
12571257 }
@@ -1381,7 +1381,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
13811381 let node_id_b = channel_info. node_two . clone ( ) ;
13821382
13831383 match channels. entry ( short_channel_id) {
1384- BtreeEntry :: Occupied ( mut entry) => {
1384+ IndexedMapEntry :: Occupied ( mut entry) => {
13851385 //TODO: because asking the blockchain if short_channel_id is valid is only optional
13861386 //in the blockchain API, we need to handle it smartly here, though it's unclear
13871387 //exactly how...
@@ -1400,17 +1400,17 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14001400 return Err ( LightningError { err : "Already have knowledge of channel" . to_owned ( ) , action : ErrorAction :: IgnoreDuplicateGossip } ) ;
14011401 }
14021402 } ,
1403- BtreeEntry :: Vacant ( entry) => {
1403+ IndexedMapEntry :: Vacant ( entry) => {
14041404 entry. insert ( channel_info) ;
14051405 }
14061406 } ;
14071407
14081408 for current_node_id in [ node_id_a, node_id_b] . iter ( ) {
14091409 match nodes. entry ( current_node_id. clone ( ) ) {
1410- BtreeEntry :: Occupied ( node_entry) => {
1410+ IndexedMapEntry :: Occupied ( node_entry) => {
14111411 node_entry. into_mut ( ) . channels . push ( short_channel_id) ;
14121412 } ,
1413- BtreeEntry :: Vacant ( node_entry) => {
1413+ IndexedMapEntry :: Vacant ( node_entry) => {
14141414 node_entry. insert ( NodeInfo {
14151415 channels : vec ! ( short_channel_id) ,
14161416 announcement_info : None ,
@@ -1584,7 +1584,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
15841584 for scid in node. channels . iter ( ) {
15851585 if let Some ( chan_info) = channels. remove ( scid) {
15861586 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) {
15881588 other_node_entry. get_mut ( ) . channels . retain ( |chan_id| {
15891589 * scid != * chan_id
15901590 } ) ;
@@ -1643,7 +1643,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
16431643 // Sadly BTreeMap::retain was only stabilized in 1.53 so we can't switch to it for some
16441644 // time.
16451645 let mut scids_to_remove = Vec :: new ( ) ;
1646- for ( scid, info) in channels. iter_mut ( ) {
1646+ for ( scid, info) in channels. unordered_iter_mut ( ) {
16471647 if info. one_to_two . is_some ( ) && info. one_to_two . as_ref ( ) . unwrap ( ) . last_update < min_time_unix {
16481648 info. one_to_two = None ;
16491649 }
@@ -1812,10 +1812,10 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18121812 Ok ( ( ) )
18131813 }
18141814
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 ) {
18161816 macro_rules! remove_from_node {
18171817 ( $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) {
18191819 entry. get_mut( ) . channels. retain( |chan_id| {
18201820 short_channel_id != * chan_id
18211821 } ) ;
@@ -1836,8 +1836,8 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
18361836impl ReadOnlyNetworkGraph < ' _ > {
18371837 /// Returns all known valid channels' short ids along with announced channel info.
18381838 ///
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 > {
18411841 & * self . channels
18421842 }
18431843
@@ -1854,8 +1854,8 @@ impl ReadOnlyNetworkGraph<'_> {
18541854
18551855 /// Returns all known nodes' public keys along with announced node info.
18561856 ///
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 > {
18591859 & * self . nodes
18601860 }
18611861
0 commit comments