@@ -62,36 +62,27 @@ pub struct NetworkGraph {
62
62
/// This network graph is then used for routing payments.
63
63
/// Provides interface to help with initial routing sync by
64
64
/// serving historical announcements.
65
- pub struct NetGraphMsgHandler < C : Deref , L : Deref > where C :: Target : chain:: Access , L :: Target : Logger {
65
+ pub struct NetGraphMsgHandler < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref >
66
+ where C :: Target : chain:: Access , L :: Target : Logger
67
+ {
66
68
secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
67
69
/// Representation of the payment channel network
68
- pub network_graph : NetworkGraph ,
70
+ pub network_graph : G ,
69
71
chain_access : Option < C > ,
70
72
full_syncs_requested : AtomicUsize ,
71
73
pending_events : Mutex < Vec < MessageSendEvent > > ,
72
74
logger : L ,
73
75
}
74
76
75
- impl < C : Deref , L : Deref > NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
77
+ impl < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref > NetGraphMsgHandler < G , C , L >
78
+ where C :: Target : chain:: Access , L :: Target : Logger
79
+ {
76
80
/// Creates a new tracker of the actual state of the network of channels and nodes,
77
81
/// assuming a fresh network graph.
78
82
/// Chain monitor is used to make sure announced channels exist on-chain,
79
83
/// channel data is correct, and that the announcement is signed with
80
84
/// channel owners' keys.
81
- pub fn new ( genesis_hash : BlockHash , chain_access : Option < C > , logger : L ) -> Self {
82
- NetGraphMsgHandler {
83
- secp_ctx : Secp256k1 :: verification_only ( ) ,
84
- network_graph : NetworkGraph :: new ( genesis_hash) ,
85
- full_syncs_requested : AtomicUsize :: new ( 0 ) ,
86
- chain_access,
87
- pending_events : Mutex :: new ( vec ! [ ] ) ,
88
- logger,
89
- }
90
- }
91
-
92
- /// Creates a new tracker of the actual state of the network of channels and nodes,
93
- /// assuming an existing Network Graph.
94
- pub fn from_net_graph ( chain_access : Option < C > , logger : L , network_graph : NetworkGraph ) -> Self {
85
+ pub fn new ( network_graph : G , chain_access : Option < C > , logger : L ) -> Self {
95
86
NetGraphMsgHandler {
96
87
secp_ctx : Secp256k1 :: verification_only ( ) ,
97
88
network_graph,
@@ -131,7 +122,9 @@ macro_rules! secp_verify_sig {
131
122
} ;
132
123
}
133
124
134
- impl < C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < C , L > where C :: Target : chain:: Access , L :: Target : Logger {
125
+ impl < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref > RoutingMessageHandler for NetGraphMsgHandler < G , C , L >
126
+ where C :: Target : chain:: Access , L :: Target : Logger
127
+ {
135
128
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
136
129
self . network_graph . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
137
130
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
@@ -411,7 +404,7 @@ impl<C: Deref , L: Deref > RoutingMessageHandler for NetGraphMsgHandler<C, L> wh
411
404
}
412
405
}
413
406
414
- impl < C : Deref , L : Deref > MessageSendEventsProvider for NetGraphMsgHandler < C , L >
407
+ impl < G : Deref < Target = NetworkGraph > , C : Deref , L : Deref > MessageSendEventsProvider for NetGraphMsgHandler < G , C , L >
415
408
where
416
409
C :: Target : chain:: Access ,
417
410
L :: Target : Logger ,
@@ -1094,11 +1087,12 @@ mod tests {
1094
1087
use prelude:: * ;
1095
1088
use sync:: Arc ;
1096
1089
1097
- fn create_net_graph_msg_handler ( ) -> ( Secp256k1 < All > , NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ) {
1090
+ fn create_net_graph_msg_handler ( ) -> ( Secp256k1 < All > , NetGraphMsgHandler < Arc < NetworkGraph > , Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ) {
1098
1091
let secp_ctx = Secp256k1 :: new ( ) ;
1099
1092
let logger = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1100
1093
let genesis_hash = genesis_block ( Network :: Testnet ) . header . block_hash ( ) ;
1101
- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_hash, None , Arc :: clone ( & logger) ) ;
1094
+ let network_graph = Arc :: new ( NetworkGraph :: new ( genesis_hash) ) ;
1095
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( network_graph, None , Arc :: clone ( & logger) ) ;
1102
1096
( secp_ctx, net_graph_msg_handler)
1103
1097
}
1104
1098
@@ -1259,15 +1253,15 @@ mod tests {
1259
1253
} ;
1260
1254
1261
1255
// Test if the UTXO lookups were not supported
1262
- let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , None , Arc :: clone ( & logger) ) ;
1256
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1257
+ let mut net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, None , Arc :: clone ( & logger) ) ;
1263
1258
match net_graph_msg_handler. handle_channel_announcement ( & valid_announcement) {
1264
1259
Ok ( res) => assert ! ( res) ,
1265
1260
_ => panic ! ( )
1266
1261
} ;
1267
1262
1268
1263
{
1269
- let network = & net_graph_msg_handler. network_graph ;
1270
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1264
+ match network_graph. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1271
1265
None => panic ! ( ) ,
1272
1266
Some ( _) => ( )
1273
1267
} ;
@@ -1283,7 +1277,8 @@ mod tests {
1283
1277
// Test if an associated transaction were not on-chain (or not confirmed).
1284
1278
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1285
1279
* chain_source. utxo_ret . lock ( ) . unwrap ( ) = Err ( chain:: AccessError :: UnknownTx ) ;
1286
- net_graph_msg_handler = NetGraphMsgHandler :: new ( chain_source. clone ( ) . genesis_hash , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1280
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1281
+ net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1287
1282
unsigned_announcement. short_channel_id += 1 ;
1288
1283
1289
1284
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
@@ -1318,8 +1313,7 @@ mod tests {
1318
1313
} ;
1319
1314
1320
1315
{
1321
- let network = & net_graph_msg_handler. network_graph ;
1322
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1316
+ match network_graph. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1323
1317
None => panic ! ( ) ,
1324
1318
Some ( _) => ( )
1325
1319
} ;
@@ -1349,8 +1343,7 @@ mod tests {
1349
1343
_ => panic ! ( )
1350
1344
} ;
1351
1345
{
1352
- let network = & net_graph_msg_handler. network_graph ;
1353
- match network. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1346
+ match network_graph. get_channels ( ) . get ( & unsigned_announcement. short_channel_id ) {
1354
1347
Some ( channel_entry) => {
1355
1348
assert_eq ! ( channel_entry. features, ChannelFeatures :: empty( ) ) ;
1356
1349
} ,
@@ -1407,7 +1400,8 @@ mod tests {
1407
1400
let secp_ctx = Secp256k1 :: new ( ) ;
1408
1401
let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1409
1402
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1410
- let net_graph_msg_handler = NetGraphMsgHandler :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) , Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1403
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
1404
+ let net_graph_msg_handler = NetGraphMsgHandler :: new ( & network_graph, Some ( chain_source. clone ( ) ) , Arc :: clone ( & logger) ) ;
1411
1405
1412
1406
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1413
1407
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
@@ -1479,8 +1473,7 @@ mod tests {
1479
1473
} ;
1480
1474
1481
1475
{
1482
- let network = & net_graph_msg_handler. network_graph ;
1483
- match network. get_channels ( ) . get ( & short_channel_id) {
1476
+ match network_graph. get_channels ( ) . get ( & short_channel_id) {
1484
1477
None => panic ! ( ) ,
1485
1478
Some ( channel_info) => {
1486
1479
assert_eq ! ( channel_info. one_to_two. as_ref( ) . unwrap( ) . cltv_expiry_delta, 144 ) ;
@@ -1995,7 +1988,7 @@ mod tests {
1995
1988
Err ( _) => panic ! ( )
1996
1989
} ;
1997
1990
1998
- let network = & net_graph_msg_handler. network_graph ;
1991
+ let network = net_graph_msg_handler. network_graph ;
1999
1992
let mut w = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
2000
1993
assert ! ( !network. get_nodes( ) . is_empty( ) ) ;
2001
1994
assert ! ( !network. get_channels( ) . is_empty( ) ) ;
@@ -2400,7 +2393,7 @@ mod tests {
2400
2393
}
2401
2394
2402
2395
fn do_handling_query_channel_range (
2403
- net_graph_msg_handler : & NetGraphMsgHandler < Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ,
2396
+ net_graph_msg_handler : & NetGraphMsgHandler < Arc < NetworkGraph > , Arc < test_utils:: TestChainSource > , Arc < test_utils:: TestLogger > > ,
2404
2397
test_node_id : & PublicKey ,
2405
2398
msg : QueryChannelRange ,
2406
2399
expected_ok : bool ,
0 commit comments