@@ -58,6 +58,12 @@ pub struct NetworkGraph {
58
58
nodes : RwLock < BTreeMap < PublicKey , NodeInfo > > ,
59
59
}
60
60
61
+ /// A read-only view of [`NetworkGraph`].
62
+ pub struct ReadOnlyNetworkGraph < ' a > {
63
+ channels : RwLockReadGuard < ' a , BTreeMap < u64 , ChannelInfo > > ,
64
+ nodes : RwLockReadGuard < ' a , BTreeMap < PublicKey , NodeInfo > > ,
65
+ }
66
+
61
67
/// Receives and validates network updates from peers,
62
68
/// stores authentic and relevant data as a network graph.
63
69
/// This network graph is then used for routing payments.
@@ -171,8 +177,8 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
171
177
172
178
fn get_next_channel_announcements ( & self , starting_point : u64 , batch_amount : u8 ) -> Vec < ( ChannelAnnouncement , Option < ChannelUpdate > , Option < ChannelUpdate > ) > {
173
179
let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
174
- let channels = self . network_graph . get_channels ( ) ;
175
- let mut iter = channels. range ( starting_point..) ;
180
+ let network_graph = self . network_graph . read_only ( ) ;
181
+ let mut iter = network_graph . channels ( ) . range ( starting_point..) ;
176
182
while result. len ( ) < batch_amount as usize {
177
183
if let Some ( ( _, ref chan) ) = iter. next ( ) {
178
184
if chan. announcement_message . is_some ( ) {
@@ -199,7 +205,8 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
199
205
200
206
fn get_next_node_announcements ( & self , starting_point : Option < & PublicKey > , batch_amount : u8 ) -> Vec < NodeAnnouncement > {
201
207
let mut result = Vec :: with_capacity ( batch_amount as usize ) ;
202
- let nodes = self . network_graph . get_nodes ( ) ;
208
+ let network_graph = self . network_graph . read_only ( ) ;
209
+ let nodes = network_graph. nodes ( ) ;
203
210
let mut iter = if let Some ( pubkey) = starting_point {
204
211
let mut iter = nodes. range ( ( * pubkey) ..) ;
205
212
iter. next ( ) ;
@@ -340,7 +347,8 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
340
347
// (has at least one update). A peer may still want to know the channel
341
348
// exists even if its not yet routable.
342
349
let mut batches: Vec < Vec < u64 > > = vec ! [ Vec :: with_capacity( MAX_SCIDS_PER_REPLY ) ] ;
343
- for ( _, ref chan) in self . network_graph . get_channels ( ) . range ( inclusive_start_scid. unwrap ( ) ..exclusive_end_scid. unwrap ( ) ) {
350
+ let network_graph = self . network_graph . read_only ( ) ;
351
+ for ( _, ref chan) in network_graph. channels ( ) . range ( inclusive_start_scid. unwrap ( ) ..exclusive_end_scid. unwrap ( ) ) {
344
352
if let Some ( chan_announcement) = & chan. announcement_message {
345
353
// Construct a new batch if last one is full
346
354
if batches. last ( ) . unwrap ( ) . len ( ) == batches. last ( ) . unwrap ( ) . capacity ( ) {
@@ -662,34 +670,6 @@ impl PartialEq for NetworkGraph {
662
670
}
663
671
664
672
impl NetworkGraph {
665
- /// Returns all known valid channels' short ids along with announced channel info.
666
- ///
667
- /// (C-not exported) because we have no mapping for `BTreeMap`s
668
- pub fn get_channels ( & self ) -> RwLockReadGuard < ' _ , BTreeMap < u64 , ChannelInfo > > {
669
- self . channels . read ( ) . unwrap ( )
670
- }
671
-
672
- /// Returns all known nodes' public keys along with announced node info.
673
- ///
674
- /// (C-not exported) because we have no mapping for `BTreeMap`s
675
- pub fn get_nodes ( & self ) -> RwLockReadGuard < ' _ , BTreeMap < PublicKey , NodeInfo > > {
676
- self . nodes . read ( ) . unwrap ( )
677
- }
678
-
679
- /// Get network addresses by node id.
680
- /// Returns None if the requested node is completely unknown,
681
- /// or if node announcement for the node was never received.
682
- ///
683
- /// (C-not exported) as there is no practical way to track lifetimes of returned values.
684
- pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < Vec < NetAddress > > {
685
- if let Some ( node) = self . nodes . read ( ) . unwrap ( ) . get ( pubkey) {
686
- if let Some ( node_info) = node. announcement_info . as_ref ( ) {
687
- return Some ( node_info. addresses . clone ( ) )
688
- }
689
- }
690
- None
691
- }
692
-
693
673
/// Creates a new, empty, network graph.
694
674
pub fn new ( genesis_hash : BlockHash ) -> NetworkGraph {
695
675
Self {
@@ -699,6 +679,16 @@ impl NetworkGraph {
699
679
}
700
680
}
701
681
682
+ /// Returns a read-only view of the network graph.
683
+ pub fn read_only ( & ' _ self ) -> ReadOnlyNetworkGraph < ' _ > {
684
+ let channels = self . channels . read ( ) . unwrap ( ) ;
685
+ let nodes = self . nodes . read ( ) . unwrap ( ) ;
686
+ ReadOnlyNetworkGraph {
687
+ channels,
688
+ nodes,
689
+ }
690
+ }
691
+
702
692
/// For an already known node (from channel announcements), update its stored properties from a
703
693
/// given node announcement.
704
694
///
@@ -1064,6 +1054,36 @@ impl NetworkGraph {
1064
1054
}
1065
1055
}
1066
1056
1057
+ impl ReadOnlyNetworkGraph < ' _ > {
1058
+ /// Returns all known valid channels' short ids along with announced channel info.
1059
+ ///
1060
+ /// (C-not exported) because we have no mapping for `BTreeMap`s
1061
+ pub fn channels ( & self ) -> & BTreeMap < u64 , ChannelInfo > {
1062
+ & * self . channels
1063
+ }
1064
+
1065
+ /// Returns all known nodes' public keys along with announced node info.
1066
+ ///
1067
+ /// (C-not exported) because we have no mapping for `BTreeMap`s
1068
+ pub fn nodes ( & self ) -> & BTreeMap < PublicKey , NodeInfo > {
1069
+ & * self . nodes
1070
+ }
1071
+
1072
+ /// Get network addresses by node id.
1073
+ /// Returns None if the requested node is completely unknown,
1074
+ /// or if node announcement for the node was never received.
1075
+ ///
1076
+ /// (C-not exported) as there is no practical way to track lifetimes of returned values.
1077
+ pub fn get_addresses ( & self , pubkey : & PublicKey ) -> Option < & Vec < NetAddress > > {
1078
+ if let Some ( node) = self . nodes . get ( pubkey) {
1079
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1080
+ return Some ( & node_info. addresses )
1081
+ }
1082
+ }
1083
+ None
1084
+ }
1085
+ }
1086
+
1067
1087
#[ cfg( test) ]
1068
1088
mod tests {
1069
1089
use chain;
@@ -1268,7 +1288,7 @@ mod tests {
1268
1288
1269
1289
{
1270
1290
let network = & net_graph_msg_handler. network_graph ;
1271
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1291
+ match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1272
1292
None => panic ! ( ) ,
1273
1293
Some ( _) => ( )
1274
1294
} ;
@@ -1320,7 +1340,7 @@ mod tests {
1320
1340
1321
1341
{
1322
1342
let network = & net_graph_msg_handler. network_graph ;
1323
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1343
+ match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1324
1344
None => panic ! ( ) ,
1325
1345
Some ( _) => ( )
1326
1346
} ;
@@ -1351,7 +1371,7 @@ mod tests {
1351
1371
} ;
1352
1372
{
1353
1373
let network = & net_graph_msg_handler. network_graph ;
1354
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1374
+ match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1355
1375
Some ( channel_entry) => {
1356
1376
assert_eq ! ( channel_entry. features, ChannelFeatures :: empty( ) ) ;
1357
1377
} ,
@@ -1481,7 +1501,7 @@ mod tests {
1481
1501
1482
1502
{
1483
1503
let network = & net_graph_msg_handler. network_graph ;
1484
- match network. get_channels ( ) . get ( & short_channel_id) {
1504
+ match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1485
1505
None => panic ! ( ) ,
1486
1506
Some ( channel_info) => {
1487
1507
assert_eq ! ( channel_info. one_to_two. as_ref( ) . unwrap( ) . cltv_expiry_delta, 144 ) ;
@@ -1587,7 +1607,7 @@ mod tests {
1587
1607
{
1588
1608
// There is no nodes in the table at the beginning.
1589
1609
let network = & net_graph_msg_handler. network_graph ;
1590
- assert_eq ! ( network. get_nodes ( ) . len( ) , 0 ) ;
1610
+ assert_eq ! ( network. read_only ( ) . nodes ( ) . len( ) , 0 ) ;
1591
1611
}
1592
1612
1593
1613
{
@@ -1643,7 +1663,7 @@ mod tests {
1643
1663
// Non-permanent closing just disables a channel
1644
1664
{
1645
1665
let network = & net_graph_msg_handler. network_graph ;
1646
- match network. get_channels ( ) . get ( & short_channel_id) {
1666
+ match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1647
1667
None => panic ! ( ) ,
1648
1668
Some ( channel_info) => {
1649
1669
assert ! ( channel_info. one_to_two. is_some( ) ) ;
@@ -1661,7 +1681,7 @@ mod tests {
1661
1681
// Non-permanent closing just disables a channel
1662
1682
{
1663
1683
let network = & net_graph_msg_handler. network_graph ;
1664
- match network. get_channels ( ) . get ( & short_channel_id) {
1684
+ match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1665
1685
None => panic ! ( ) ,
1666
1686
Some ( channel_info) => {
1667
1687
assert ! ( !channel_info. one_to_two. as_ref( ) . unwrap( ) . enabled) ;
@@ -1679,9 +1699,9 @@ mod tests {
1679
1699
// Permanent closing deletes a channel
1680
1700
{
1681
1701
let network = & net_graph_msg_handler. network_graph ;
1682
- assert_eq ! ( network. get_channels ( ) . len( ) , 0 ) ;
1702
+ assert_eq ! ( network. read_only ( ) . channels ( ) . len( ) , 0 ) ;
1683
1703
// Nodes are also deleted because there are no associated channels anymore
1684
- assert_eq ! ( network. get_nodes ( ) . len( ) , 0 ) ;
1704
+ assert_eq ! ( network. read_only ( ) . nodes ( ) . len( ) , 0 ) ;
1685
1705
}
1686
1706
// TODO: Test HTLCFailChannelUpdate::NodeFailure, which is not implemented yet.
1687
1707
}
@@ -1998,8 +2018,8 @@ mod tests {
1998
2018
1999
2019
let network = & net_graph_msg_handler. network_graph ;
2000
2020
let mut w = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
2001
- assert ! ( !network. get_nodes ( ) . is_empty( ) ) ;
2002
- assert ! ( !network. get_channels ( ) . is_empty( ) ) ;
2021
+ assert ! ( !network. read_only ( ) . nodes ( ) . is_empty( ) ) ;
2022
+ assert ! ( !network. read_only ( ) . channels ( ) . is_empty( ) ) ;
2003
2023
network. write ( & mut w) . unwrap ( ) ;
2004
2024
assert ! ( <NetworkGraph >:: read( & mut io:: Cursor :: new( & w. 0 ) ) . unwrap( ) == * network) ;
2005
2025
}
0 commit comments