@@ -69,36 +69,27 @@ pub struct ReadOnlyNetworkGraph<'a> {
69
69
/// This network graph is then used for routing payments.
70
70
/// Provides interface to help with initial routing sync by
71
71
/// 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
+ {
73
75
secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
74
76
/// Representation of the payment channel network
75
- pub network_graph : NetworkGraph ,
77
+ pub network_graph : G ,
76
78
chain_access : Option < C > ,
77
79
full_syncs_requested : AtomicUsize ,
78
80
pending_events : Mutex < Vec < MessageSendEvent > > ,
79
81
logger : L ,
80
82
}
81
83
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
+ {
83
87
/// Creates a new tracker of the actual state of the network of channels and nodes,
84
88
/// assuming a fresh network graph.
85
89
/// Chain monitor is used to make sure announced channels exist on-chain,
86
90
/// channel data is correct, and that the announcement is signed with
87
91
/// 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 {
102
93
NetGraphMsgHandler {
103
94
secp_ctx : Secp256k1 :: verification_only ( ) ,
104
95
network_graph,
@@ -138,7 +129,9 @@ macro_rules! secp_verify_sig {
138
129
} ;
139
130
}
140
131
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
+ {
142
135
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
143
136
self . network_graph . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
144
137
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
420
413
}
421
414
}
422
415
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 >
424
417
where
425
418
C :: Target : chain:: Access ,
426
419
L :: Target : Logger ,
@@ -1115,11 +1108,12 @@ mod tests {
1115
1108
use prelude:: * ;
1116
1109
use sync:: Arc ;
1117
1110
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 > > ) {
1119
1112
let secp_ctx = Secp256k1 :: new ( ) ;
1120
1113
let logger = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1121
1114
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) ) ;
1123
1117
( secp_ctx, net_graph_msg_handler)
1124
1118
}
1125
1119
@@ -1280,15 +1274,15 @@ mod tests {
1280
1274
} ;
1281
1275
1282
1276
// 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) ) ;
1284
1279
match net_graph_msg_handler. handle_channel_announcement ( & valid_announcement) {
1285
1280
Ok ( res) => assert ! ( res) ,
1286
1281
_ => panic ! ( )
1287
1282
} ;
1288
1283
1289
1284
{
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 ) {
1292
1286
None => panic ! ( ) ,
1293
1287
Some ( _) => ( )
1294
1288
} ;
@@ -1304,7 +1298,8 @@ mod tests {
1304
1298
// Test if an associated transaction were not on-chain (or not confirmed).
1305
1299
let chain_source = Arc :: new ( test_utils:: TestChainSource :: new ( Network :: Testnet ) ) ;
1306
1300
* 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) ) ;
1308
1303
unsigned_announcement. short_channel_id += 1 ;
1309
1304
1310
1305
msghash = hash_to_message ! ( & Sha256dHash :: hash( & unsigned_announcement. encode( ) [ ..] ) [ ..] ) ;
@@ -1339,8 +1334,7 @@ mod tests {
1339
1334
} ;
1340
1335
1341
1336
{
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 ) {
1344
1338
None => panic ! ( ) ,
1345
1339
Some ( _) => ( )
1346
1340
} ;
@@ -1370,8 +1364,7 @@ mod tests {
1370
1364
_ => panic ! ( )
1371
1365
} ;
1372
1366
{
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 ) {
1375
1368
Some ( channel_entry) => {
1376
1369
assert_eq ! ( channel_entry. features, ChannelFeatures :: empty( ) ) ;
1377
1370
} ,
@@ -1428,7 +1421,8 @@ mod tests {
1428
1421
let secp_ctx = Secp256k1 :: new ( ) ;
1429
1422
let logger: Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
1430
1423
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) ) ;
1432
1426
1433
1427
let node_1_privkey = & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ;
1434
1428
let node_2_privkey = & SecretKey :: from_slice ( & [ 41 ; 32 ] ) . unwrap ( ) ;
@@ -1500,8 +1494,7 @@ mod tests {
1500
1494
} ;
1501
1495
1502
1496
{
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) {
1505
1498
None => panic ! ( ) ,
1506
1499
Some ( channel_info) => {
1507
1500
assert_eq ! ( channel_info. one_to_two. as_ref( ) . unwrap( ) . cltv_expiry_delta, 144 ) ;
@@ -2016,7 +2009,7 @@ mod tests {
2016
2009
Err ( _) => panic ! ( )
2017
2010
} ;
2018
2011
2019
- let network = & net_graph_msg_handler. network_graph ;
2012
+ let network = net_graph_msg_handler. network_graph ;
2020
2013
let mut w = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
2021
2014
assert ! ( !network. read_only( ) . nodes( ) . is_empty( ) ) ;
2022
2015
assert ! ( !network. read_only( ) . channels( ) . is_empty( ) ) ;
@@ -2421,7 +2414,7 @@ mod tests {
2421
2414
}
2422
2415
2423
2416
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 > > ,
2425
2418
test_node_id : & PublicKey ,
2426
2419
msg : QueryChannelRange ,
2427
2420
expected_ok : bool ,
0 commit comments