Skip to content

Commit 0f73d6a

Browse files
committed
Move Secp256k1 context to NetworkGraph
P2PGossipSync has a Secp256k1 context field, which it only uses to pass to NetworkGraph methods. Move the field to NetworkGraph so other callers don't need to pass in a Secp256k1 context.
1 parent 0017bc8 commit 0f73d6a

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

lightning/src/routing/gossip.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ impl Readable for NodeId {
123123

124124
/// Represents the network as nodes and channels between them
125125
pub struct NetworkGraph {
126+
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
126127
last_rapid_gossip_sync_timestamp: Mutex<Option<u32>>,
127128
genesis_hash: BlockHash,
128129
// Lock order: channels -> nodes
@@ -136,6 +137,7 @@ impl Clone for NetworkGraph {
136137
let nodes = self.nodes.read().unwrap();
137138
let last_rapid_gossip_sync_timestamp = self.get_last_rapid_gossip_sync_timestamp();
138139
Self {
140+
secp_ctx: Secp256k1::verification_only(),
139141
genesis_hash: self.genesis_hash.clone(),
140142
channels: RwLock::new(channels.clone()),
141143
nodes: RwLock::new(nodes.clone()),
@@ -218,7 +220,6 @@ where C::Target: chain::Access, L::Target: Logger {
218220
pub struct P2PGossipSync<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref>
219221
where C::Target: chain::Access, L::Target: Logger
220222
{
221-
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
222223
network_graph: G,
223224
chain_access: Option<C>,
224225
full_syncs_requested: AtomicUsize,
@@ -236,7 +237,6 @@ where C::Target: chain::Access, L::Target: Logger
236237
/// channel owners' keys.
237238
pub fn new(network_graph: G, chain_access: Option<C>, logger: L) -> Self {
238239
P2PGossipSync {
239-
secp_ctx: Secp256k1::verification_only(),
240240
network_graph,
241241
full_syncs_requested: AtomicUsize::new(0),
242242
chain_access,
@@ -280,7 +280,7 @@ where C::Target: chain::Access, L::Target: Logger
280280
let is_enabled = msg.contents.flags & (1 << 1) != (1 << 1);
281281
let status = if is_enabled { "enabled" } else { "disabled" };
282282
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);
284284
},
285285
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
286286
let action = if is_permanent { "Removing" } else { "Disabling" };
@@ -320,20 +320,20 @@ impl<G: Deref<Target=NetworkGraph>, C: Deref, L: Deref> RoutingMessageHandler fo
320320
where C::Target: chain::Access, L::Target: Logger
321321
{
322322
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)?;
324324
Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
325325
msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY &&
326326
msg.contents.excess_data.len() + msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
327327
}
328328

329329
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)?;
331331
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 { "" });
332332
Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
333333
}
334334

335335
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)?;
337337
Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY)
338338
}
339339

@@ -1027,6 +1027,7 @@ impl Readable for NetworkGraph {
10271027
});
10281028

10291029
Ok(NetworkGraph {
1030+
secp_ctx: Secp256k1::verification_only(),
10301031
genesis_hash,
10311032
channels: RwLock::new(channels),
10321033
nodes: RwLock::new(nodes),
@@ -1061,6 +1062,7 @@ impl NetworkGraph {
10611062
/// Creates a new, empty, network graph.
10621063
pub fn new(genesis_hash: BlockHash) -> NetworkGraph {
10631064
Self {
1065+
secp_ctx: Secp256k1::verification_only(),
10641066
genesis_hash,
10651067
channels: RwLock::new(BTreeMap::new()),
10661068
nodes: RwLock::new(BTreeMap::new()),
@@ -1105,9 +1107,9 @@ impl NetworkGraph {
11051107
/// You probably don't want to call this directly, instead relying on a P2PGossipSync's
11061108
/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
11071109
/// 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> {
11091111
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");
11111113
self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
11121114
}
11131115

@@ -1160,17 +1162,17 @@ impl NetworkGraph {
11601162
///
11611163
/// If a `chain::Access` object is provided via `chain_access`, it will be called to verify
11621164
/// 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>,
11651167
) -> Result<(), LightningError>
11661168
where
11671169
C::Target: chain::Access,
11681170
{
11691171
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");
11741176
self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), chain_access)
11751177
}
11761178

@@ -1432,8 +1434,8 @@ impl NetworkGraph {
14321434
///
14331435
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
14341436
/// 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))
14371439
}
14381440

14391441
/// For an already known (from announcement) channel, update info about one of the directions
@@ -1443,10 +1445,10 @@ impl NetworkGraph {
14431445
/// If built with `no-std`, any updates with a timestamp more than two weeks in the past or
14441446
/// materially in the future will be rejected.
14451447
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)
14471449
}
14481450

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> {
14501452
let dest_node_id;
14511453
let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
14521454
let chan_was_enabled;
@@ -1527,8 +1529,8 @@ impl NetworkGraph {
15271529
if msg.flags & 1 == 1 {
15281530
dest_node_id = channel.node_one.clone();
15291531
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{
15321534
err: "Couldn't parse source node pubkey".to_owned(),
15331535
action: ErrorAction::IgnoreAndLog(Level::Debug)
15341536
})?, "channel_update");
@@ -1537,8 +1539,8 @@ impl NetworkGraph {
15371539
} else {
15381540
dest_node_id = channel.node_two.clone();
15391541
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{
15421544
err: "Couldn't parse destination node pubkey".to_owned(),
15431545
action: ErrorAction::IgnoreAndLog(Level::Debug)
15441546
})?, "channel_update");
@@ -2081,7 +2083,7 @@ mod tests {
20812083
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
20822084
short_channel_id = valid_channel_announcement.contents.short_channel_id;
20832085
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());
20852087
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
20862088

20872089
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);
@@ -2177,7 +2179,7 @@ mod tests {
21772179
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
21782180
let short_channel_id = valid_channel_announcement.contents.short_channel_id;
21792181
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());
21812183
assert!(network_graph.read_only().channels().get(&short_channel_id).is_some());
21822184

21832185
let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx);

lightning/src/routing/scoring.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,7 +1228,7 @@ mod tests {
12281228
};
12291229
let chain_source: Option<&::util::test_utils::TestChainSource> = None;
12301230
network_graph.update_channel_from_announcement(
1231-
&signed_announcement, &chain_source, &secp_ctx).unwrap();
1231+
&signed_announcement, &chain_source).unwrap();
12321232
update_channel(network_graph, short_channel_id, node_1_key, 0);
12331233
update_channel(network_graph, short_channel_id, node_2_key, 1);
12341234
}
@@ -1255,7 +1255,7 @@ mod tests {
12551255
signature: secp_ctx.sign_ecdsa(&msghash, &node_key),
12561256
contents: unsigned_update,
12571257
};
1258-
network_graph.update_channel(&signed_update, &secp_ctx).unwrap();
1258+
network_graph.update_channel(&signed_update).unwrap();
12591259
}
12601260

12611261
fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {

0 commit comments

Comments
 (0)