@@ -69,36 +69,27 @@ pub struct ReadOnlyNetworkGraph<'a> {
6969/// This network graph is then used for routing payments.
7070/// Provides interface to help with initial routing sync by
7171/// serving historical announcements.
72- pub struct NetGraphMsgHandler < C : Deref , L : Deref > where C :: Target : chain:: Access , L :: Target : Logger {
72+ pub struct NetGraphMsgHandler < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref >
73+ where C :: Target : chain:: Access , L :: Target : Logger
74+ {
7375 secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
7476 /// Representation of the payment channel network
75- pub network_graph : NetworkGraph ,
77+ pub network_graph : G ,
7678 chain_access : Option < C > ,
7779 full_syncs_requested : AtomicUsize ,
7880 pending_events : Mutex < Vec < MessageSendEvent > > ,
7981 logger : L ,
8082}
8183
82- impl < C : Deref , L : Deref > NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
84+ impl < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref > NetGraphMsgHandler < G , C , L >
85+ where C :: Target : chain:: Access , L :: Target : Logger
86+ {
8387 /// Creates a new tracker of the actual state of the network of channels and nodes,
8488 /// assuming a fresh network graph.
8589 /// Chain monitor is used to make sure announced channels exist on-chain,
8690 /// channel data is correct, and that the announcement is signed with
8791 /// channel owners' keys.
88- pub fn new ( genesis_hash : BlockHash , chain_access : Option < C > , logger : L ) -> Self {
89- NetGraphMsgHandler {
90- secp_ctx : Secp256k1 :: verification_only ( ) ,
91- network_graph : NetworkGraph :: new ( genesis_hash) ,
92- full_syncs_requested : AtomicUsize :: new ( 0 ) ,
93- chain_access,
94- pending_events : Mutex :: new ( vec ! [ ] ) ,
95- logger,
96- }
97- }
98-
99- /// Creates a new tracker of the actual state of the network of channels and nodes,
100- /// assuming an existing Network Graph.
101- pub fn from_net_graph ( chain_access : Option < C > , logger : L , network_graph : NetworkGraph ) -> Self {
92+ pub fn new ( network_graph : G , chain_access : Option < C > , logger : L ) -> Self {
10293 NetGraphMsgHandler {
10394 secp_ctx : Secp256k1 :: verification_only ( ) ,
10495 network_graph,
@@ -138,7 +129,9 @@ macro_rules! secp_verify_sig {
138129 } ;
139130}
140131
141- impl < C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
132+ impl < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < G , C , L >
133+ where C :: Target : chain:: Access , L :: Target : Logger
134+ {
142135 fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
143136 self . network_graph . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
144137 Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
@@ -420,7 +413,7 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
420413 }
421414}
422415
423- impl < C : Deref , L : Deref > MessageSendEventsProvider for NetGraphMsgHandler < C , L >
416+ impl < G : Deref < Target = NetworkGraph > , C : Deref , L : Deref > MessageSendEventsProvider for NetGraphMsgHandler < G , C , L >
424417where
425418 C :: Target : chain:: Access ,
426419 L :: Target : Logger ,
@@ -1115,11 +1108,12 @@ mod tests {
11151108 use prelude:: * ;
11161109 use sync:: Arc ;
11171110
1118- fn create_net_graph_msg_handler ( ) -> ( Secp256k1 < All > , NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ) {
1111+ fn create_net_graph_msg_handler ( ) -> ( Secp256k1 < All > , NetGraphMsgHandler < Arc < NetworkGraph > , Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ) {
11191112 let secp_ctx = Secp256k1 :: new ( ) ;
11201113 let logger = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
11211114 let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1122- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_hash, None , Arc :: clone ( & logger) ) ;
1115+ let network_graph = Arc :: new ( NetworkGraph :: new ( genesis_hash) ) ;
1116+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, None , Arc :: clone ( & logger) ) ;
11231117 ( secp_ctx, net_graph_msg_handler)
11241118 }
11251119
@@ -1280,15 +1274,15 @@ mod tests {
12801274 } ;
12811275
12821276 // Test if the UTXO lookups were not supported
1283- let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , None , Arc :: clone ( & logger) ) ;
1277+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1278+ let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, None , Arc :: clone ( & logger) ) ;
12841279 match net_graph_msg_handler. handle_channel_announcement ( & valid_announcement) {
12851280 Ok ( res) => assert ! ( res) ,
12861281 _ => panic ! ( )
12871282 } ;
12881283
12891284 {
1290- let network = & net_graph_msg_handler. network_graph ;
1291- match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1285+ match network_graph. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
12921286 None => panic ! ( ) ,
12931287 Some ( _) => ( )
12941288 } ;
@@ -1304,7 +1298,8 @@ mod tests {
13041298 // Test if an associated transaction were not on-chain (or not confirmed).
13051299 let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
13061300 * chain_source. utxo_ret . lock ( ) . unwrap ( ) = Err ( chain:: AccessError :: UnknownTx ) ;
1307- net_graph_msg_handler = NetGraphMsgHandler :: new ( chain_source. clone ( ) . genesis_hash , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1301+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1302+ net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
13081303 unsigned_announcement. short_channel_id += 1 ;
13091304
13101305 msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
@@ -1339,8 +1334,7 @@ mod tests {
13391334 } ;
13401335
13411336 {
1342- let network = & net_graph_msg_handler. network_graph ;
1343- match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1337+ match network_graph. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
13441338 None => panic ! ( ) ,
13451339 Some ( _) => ( )
13461340 } ;
@@ -1370,8 +1364,7 @@ mod tests {
13701364 _ => panic ! ( )
13711365 } ;
13721366 {
1373- let network = & net_graph_msg_handler. network_graph ;
1374- match network. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1367+ match network_graph. read_only ( ) . channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
13751368 Some ( channel_entry) => {
13761369 assert_eq ! ( channel_entry. features, ChannelFeatures :: empty( ) ) ;
13771370 } ,
@@ -1428,7 +1421,8 @@ mod tests {
14281421 let secp_ctx = Secp256k1 :: new ( ) ;
14291422 let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
14301423 let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1431- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1424+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1425+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
14321426
14331427 let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
14341428 let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
@@ -1500,8 +1494,7 @@ mod tests {
15001494 } ;
15011495
15021496 {
1503- let network = & net_graph_msg_handler. network_graph ;
1504- match network. read_only ( ) . channels ( ) . get ( & short_channel_id) {
1497+ match network_graph. read_only ( ) . channels ( ) . get ( & short_channel_id) {
15051498 None => panic ! ( ) ,
15061499 Some ( channel_info) => {
15071500 assert_eq ! ( channel_info. one_to_two. as_ref( ) . unwrap( ) . cltv_expiry_delta, 144 ) ;
@@ -2016,7 +2009,7 @@ mod tests {
20162009 Err ( _) => panic ! ( )
20172010 } ;
20182011
2019- let network = & net_graph_msg_handler. network_graph ;
2012+ let network = net_graph_msg_handler. network_graph ;
20202013 let mut w = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
20212014 assert ! ( !network. read_only( ) . nodes( ) . is_empty( ) ) ;
20222015 assert ! ( !network. read_only( ) . channels( ) . is_empty( ) ) ;
@@ -2421,7 +2414,7 @@ mod tests {
24212414 }
24222415
24232416 fn do_handling_query_channel_range (
2424- net_graph_msg_handler : & NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ,
2417+ net_graph_msg_handler : & NetGraphMsgHandler < Arc < NetworkGraph > , Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ,
24252418 test_node_id : & PublicKey ,
24262419 msg : QueryChannelRange ,
24272420 expected_ok : bool ,
0 commit comments