@@ -123,6 +123,7 @@ impl Readable for NodeId {
123
123
124
124
/// Represents the network as nodes and channels between them
125
125
pub struct NetworkGraph {
126
+ secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
126
127
last_rapid_gossip_sync_timestamp : Mutex < Option < u32 > > ,
127
128
genesis_hash : BlockHash ,
128
129
// Lock order: channels -> nodes
@@ -136,6 +137,7 @@ impl Clone for NetworkGraph {
136
137
let nodes = self . nodes . read ( ) . unwrap ( ) ;
137
138
let last_rapid_gossip_sync_timestamp = self . get_last_rapid_gossip_sync_timestamp ( ) ;
138
139
Self {
140
+ secp_ctx : Secp256k1 :: verification_only ( ) ,
139
141
genesis_hash : self . genesis_hash . clone ( ) ,
140
142
channels : RwLock :: new ( channels. clone ( ) ) ,
141
143
nodes : RwLock :: new ( nodes. clone ( ) ) ,
@@ -218,7 +220,6 @@ where C::Target: chain::Access, L::Target: Logger {
218
220
pub struct P2PGossipSync < G : Deref < Target =NetworkGraph > , C : Deref , L : Deref >
219
221
where C :: Target : chain:: Access , L :: Target : Logger
220
222
{
221
- secp_ctx : Secp256k1 < secp256k1:: VerifyOnly > ,
222
223
network_graph : G ,
223
224
chain_access : Option < C > ,
224
225
full_syncs_requested : AtomicUsize ,
@@ -236,7 +237,6 @@ where C::Target: chain::Access, L::Target: Logger
236
237
/// channel owners' keys.
237
238
pub fn new ( network_graph : G , chain_access : Option < C > , logger : L ) -> Self {
238
239
P2PGossipSync {
239
- secp_ctx : Secp256k1 :: verification_only ( ) ,
240
240
network_graph,
241
241
full_syncs_requested : AtomicUsize :: new ( 0 ) ,
242
242
chain_access,
@@ -280,7 +280,7 @@ where C::Target: chain::Access, L::Target: Logger
280
280
let is_enabled = msg. contents . flags & ( 1 << 1 ) != ( 1 << 1 ) ;
281
281
let status = if is_enabled { "enabled" } else { "disabled" } ;
282
282
log_debug ! ( self . logger, "Updating channel with channel_update from a payment failure. Channel {} is {}." , short_channel_id, status) ;
283
- let _ = self . network_graph . update_channel ( msg, & self . secp_ctx ) ;
283
+ let _ = self . network_graph . update_channel ( msg) ;
284
284
} ,
285
285
NetworkUpdate :: ChannelFailure { short_channel_id, is_permanent } => {
286
286
let action = if is_permanent { "Removing" } else { "Disabling" } ;
@@ -320,20 +320,20 @@ impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> RoutingMessageHandler fo
320
320
where C :: Target : chain:: Access , L :: Target : Logger
321
321
{
322
322
fn handle_node_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < bool , LightningError > {
323
- self . network_graph . update_node_from_announcement ( msg, & self . secp_ctx ) ?;
323
+ self . network_graph . update_node_from_announcement ( msg) ?;
324
324
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
325
325
msg. contents . excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY &&
326
326
msg. contents . excess_data . len ( ) + msg. contents . excess_address_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
327
327
}
328
328
329
329
fn handle_channel_announcement ( & self , msg : & msgs:: ChannelAnnouncement ) -> Result < bool , LightningError > {
330
- self . network_graph . update_channel_from_announcement ( msg, & self . chain_access , & self . secp_ctx ) ?;
330
+ self . network_graph . update_channel_from_announcement ( msg, & self . chain_access ) ?;
331
331
log_gossip ! ( self . logger, "Added channel_announcement for {}{}" , msg. contents. short_channel_id, if !msg. contents. excess_data. is_empty( ) { " with excess uninterpreted data!" } else { "" } ) ;
332
332
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
333
333
}
334
334
335
335
fn handle_channel_update ( & self , msg : & msgs:: ChannelUpdate ) -> Result < bool , LightningError > {
336
- self . network_graph . update_channel ( msg, & self . secp_ctx ) ?;
336
+ self . network_graph . update_channel ( msg) ?;
337
337
Ok ( msg. contents . excess_data . len ( ) <= MAX_EXCESS_BYTES_FOR_RELAY )
338
338
}
339
339
@@ -1027,6 +1027,7 @@ impl Readable for NetworkGraph {
1027
1027
} ) ;
1028
1028
1029
1029
Ok ( NetworkGraph {
1030
+ secp_ctx : Secp256k1 :: verification_only ( ) ,
1030
1031
genesis_hash,
1031
1032
channels : RwLock :: new ( channels) ,
1032
1033
nodes : RwLock :: new ( nodes) ,
@@ -1061,6 +1062,7 @@ impl NetworkGraph {
1061
1062
/// Creates a new, empty, network graph.
1062
1063
pub fn new ( genesis_hash : BlockHash ) -> NetworkGraph {
1063
1064
Self {
1065
+ secp_ctx : Secp256k1 :: verification_only ( ) ,
1064
1066
genesis_hash,
1065
1067
channels : RwLock :: new ( BTreeMap :: new ( ) ) ,
1066
1068
nodes : RwLock :: new ( BTreeMap :: new ( ) ) ,
@@ -1105,9 +1107,9 @@ impl NetworkGraph {
1105
1107
/// You probably don't want to call this directly, instead relying on a P2PGossipSync's
1106
1108
/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
1107
1109
/// routing messages from a source using a protocol other than the lightning P2P protocol.
1108
- pub fn update_node_from_announcement < T : secp256k1 :: Verification > ( & self , msg : & msgs:: NodeAnnouncement , secp_ctx : & Secp256k1 < T > ) -> Result < ( ) , LightningError > {
1110
+ pub fn update_node_from_announcement ( & self , msg : & msgs:: NodeAnnouncement ) -> Result < ( ) , LightningError > {
1109
1111
let msg_hash = hash_to_message ! ( & Sha256dHash :: hash( & msg. contents. encode( ) [ ..] ) [ ..] ) ;
1110
- secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. signature, & msg. contents. node_id, "node_announcement" ) ;
1112
+ secp_verify_sig ! ( self . secp_ctx, & msg_hash, & msg. signature, & msg. contents. node_id, "node_announcement" ) ;
1111
1113
self . update_node_from_announcement_intern ( & msg. contents , Some ( & msg) )
1112
1114
}
1113
1115
@@ -1160,17 +1162,17 @@ impl NetworkGraph {
1160
1162
///
1161
1163
/// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
1162
1164
/// the corresponding UTXO exists on chain and is correctly-formatted.
1163
- pub fn update_channel_from_announcement < T : secp256k1 :: Verification , C : Deref > (
1164
- & self , msg : & msgs:: ChannelAnnouncement , chain_access : & Option < C > , secp_ctx : & Secp256k1 < T >
1165
+ pub fn update_channel_from_announcement < C : Deref > (
1166
+ & self , msg : & msgs:: ChannelAnnouncement , chain_access : & Option < C > ,
1165
1167
) -> Result < ( ) , LightningError >
1166
1168
where
1167
1169
C :: Target : chain:: Access ,
1168
1170
{
1169
1171
let msg_hash = hash_to_message ! ( & Sha256dHash :: hash( & msg. contents. encode( ) [ ..] ) [ ..] ) ;
1170
- secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. node_signature_1, & msg. contents. node_id_1, "channel_announcement" ) ;
1171
- secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. node_signature_2, & msg. contents. node_id_2, "channel_announcement" ) ;
1172
- secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. bitcoin_signature_1, & msg. contents. bitcoin_key_1, "channel_announcement" ) ;
1173
- secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. bitcoin_signature_2, & msg. contents. bitcoin_key_2, "channel_announcement" ) ;
1172
+ secp_verify_sig ! ( self . secp_ctx, & msg_hash, & msg. node_signature_1, & msg. contents. node_id_1, "channel_announcement" ) ;
1173
+ secp_verify_sig ! ( self . secp_ctx, & msg_hash, & msg. node_signature_2, & msg. contents. node_id_2, "channel_announcement" ) ;
1174
+ secp_verify_sig ! ( self . secp_ctx, & msg_hash, & msg. bitcoin_signature_1, & msg. contents. bitcoin_key_1, "channel_announcement" ) ;
1175
+ secp_verify_sig ! ( self . secp_ctx, & msg_hash, & msg. bitcoin_signature_2, & msg. contents. bitcoin_key_2, "channel_announcement" ) ;
1174
1176
self . update_channel_from_unsigned_announcement_intern ( & msg. contents , Some ( msg) , chain_access)
1175
1177
}
1176
1178
@@ -1432,8 +1434,8 @@ impl NetworkGraph {
1432
1434
///
1433
1435
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
1434
1436
/// materially in the future will be rejected.
1435
- pub fn update_channel < T : secp256k1 :: Verification > ( & self , msg : & msgs:: ChannelUpdate , secp_ctx : & Secp256k1 < T > ) -> Result < ( ) , LightningError > {
1436
- self . update_channel_intern ( & msg. contents , Some ( & msg) , Some ( ( & msg. signature , secp_ctx ) ) )
1437
+ pub fn update_channel ( & self , msg : & msgs:: ChannelUpdate ) -> Result < ( ) , LightningError > {
1438
+ self . update_channel_intern ( & msg. contents , Some ( & msg) , Some ( & msg. signature ) )
1437
1439
}
1438
1440
1439
1441
/// For an already known (from announcement) channel, update info about one of the directions
@@ -1443,10 +1445,10 @@ impl NetworkGraph {
1443
1445
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
1444
1446
/// materially in the future will be rejected.
1445
1447
pub fn update_channel_unsigned ( & self , msg : & msgs:: UnsignedChannelUpdate ) -> Result < ( ) , LightningError > {
1446
- self . update_channel_intern ( msg, None , None :: < ( & secp256k1 :: ecdsa :: Signature , & Secp256k1 < secp256k1 :: VerifyOnly > ) > )
1448
+ self . update_channel_intern ( msg, None , None )
1447
1449
}
1448
1450
1449
- fn update_channel_intern < T : secp256k1 :: Verification > ( & self , msg : & msgs:: UnsignedChannelUpdate , full_msg : Option < & msgs:: ChannelUpdate > , sig_info : Option < ( & secp256k1:: ecdsa:: Signature , & Secp256k1 < T > ) > ) -> Result < ( ) , LightningError > {
1451
+ fn update_channel_intern ( & self , msg : & msgs:: UnsignedChannelUpdate , full_msg : Option < & msgs:: ChannelUpdate > , sig : Option < & secp256k1:: ecdsa:: Signature > ) -> Result < ( ) , LightningError > {
1450
1452
let dest_node_id;
1451
1453
let chan_enabled = msg. flags & ( 1 << 1 ) != ( 1 << 1 ) ;
1452
1454
let chan_was_enabled;
@@ -1527,8 +1529,8 @@ impl NetworkGraph {
1527
1529
if msg. flags & 1 == 1 {
1528
1530
dest_node_id = channel. node_one . clone ( ) ;
1529
1531
check_update_latest ! ( channel. two_to_one) ;
1530
- if let Some ( ( sig, ctx ) ) = sig_info {
1531
- secp_verify_sig ! ( ctx , & msg_hash, & sig, & PublicKey :: from_slice( channel. node_two. as_slice( ) ) . map_err( |_| LightningError {
1532
+ if let Some ( sig) = sig {
1533
+ secp_verify_sig ! ( self . secp_ctx , & msg_hash, & sig, & PublicKey :: from_slice( channel. node_two. as_slice( ) ) . map_err( |_| LightningError {
1532
1534
err: "Couldn't parse source node pubkey" . to_owned( ) ,
1533
1535
action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1534
1536
} ) ?, "channel_update" ) ;
@@ -1537,8 +1539,8 @@ impl NetworkGraph {
1537
1539
} else {
1538
1540
dest_node_id = channel. node_two . clone ( ) ;
1539
1541
check_update_latest ! ( channel. one_to_two) ;
1540
- if let Some ( ( sig, ctx ) ) = sig_info {
1541
- secp_verify_sig ! ( ctx , & msg_hash, & sig, & PublicKey :: from_slice( channel. node_one. as_slice( ) ) . map_err( |_| LightningError {
1542
+ if let Some ( sig) = sig {
1543
+ secp_verify_sig ! ( self . secp_ctx , & msg_hash, & sig, & PublicKey :: from_slice( channel. node_one. as_slice( ) ) . map_err( |_| LightningError {
1542
1544
err: "Couldn't parse destination node pubkey" . to_owned( ) ,
1543
1545
action: ErrorAction :: IgnoreAndLog ( Level :: Debug )
1544
1546
} ) ?, "channel_update" ) ;
@@ -2081,7 +2083,7 @@ mod tests {
2081
2083
let valid_channel_announcement = get_signed_channel_announcement ( |_| { } , node_1_privkey, node_2_privkey, & secp_ctx) ;
2082
2084
short_channel_id = valid_channel_announcement. contents . short_channel_id ;
2083
2085
let chain_source: Option < & test_utils:: TestChainSource > = None ;
2084
- assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source, & secp_ctx ) . is_ok( ) ) ;
2086
+ assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source) . is_ok( ) ) ;
2085
2087
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
2086
2088
2087
2089
let valid_channel_update = get_signed_channel_update ( |_| { } , node_1_privkey, & secp_ctx) ;
@@ -2177,7 +2179,7 @@ mod tests {
2177
2179
let valid_channel_announcement = get_signed_channel_announcement ( |_| { } , node_1_privkey, node_2_privkey, & secp_ctx) ;
2178
2180
let short_channel_id = valid_channel_announcement. contents . short_channel_id ;
2179
2181
let chain_source: Option < & test_utils:: TestChainSource > = None ;
2180
- assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source, & secp_ctx ) . is_ok( ) ) ;
2182
+ assert ! ( network_graph. update_channel_from_announcement( & valid_channel_announcement, & chain_source) . is_ok( ) ) ;
2181
2183
assert ! ( network_graph. read_only( ) . channels( ) . get( & short_channel_id) . is_some( ) ) ;
2182
2184
2183
2185
let valid_channel_update = get_signed_channel_update ( |_| { } , node_1_privkey, & secp_ctx) ;
0 commit comments