Skip to content

Commit c1bb1df

Browse files
committed
Avoid unnecessary PublicKey parsing in RGS application
`PublicKey` parsing is relatively expensive as we have to check if the point is actually on the curve. To avoid it, our `NetworkGraph` uses `NodeId`s which don't have the validity requirement. Here, we take advantage of that in RGS application to avoid parsing `PublicKey`s, improving performance.
1 parent eb5198a commit c1bb1df

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

lightning-rapid-gossip-sync/src/processing.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use core::ops::Deref;
33
use core::sync::atomic::Ordering;
44

55
use bitcoin::constants::ChainHash;
6-
use bitcoin::secp256k1::PublicKey;
76

87
use lightning::io;
98
use lightning::ln::msgs::{
@@ -117,7 +116,7 @@ where
117116
};
118117

119118
let node_id_count: u32 = Readable::read(read_cursor)?;
120-
let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
119+
let mut node_ids: Vec<NodeId> = Vec::with_capacity(core::cmp::min(
121120
node_id_count,
122121
MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
123122
) as usize);
@@ -154,9 +153,8 @@ where
154153
let key_parity = node_detail_flag & 0b_0000_0011;
155154
pubkey_bytes[0] = key_parity;
156155

157-
let current_pubkey = PublicKey::from_slice(&pubkey_bytes)?;
158-
let current_node_id = NodeId::from_pubkey(&current_pubkey);
159-
node_ids.push(current_pubkey);
156+
let current_node_id = NodeId::from_slice(&pubkey_bytes)?;
157+
node_ids.push(current_node_id);
160158

161159
if is_reminder || has_address_details || feature_detail_marker > 0 {
162160
let mut synthetic_node_announcement = UnsignedNodeAnnouncement {

lightning/src/routing/gossip.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1992,8 +1992,8 @@ where
19921992
///
19931993
/// All other parameters as used in [`msgs::UnsignedChannelAnnouncement`] fields.
19941994
pub fn add_channel_from_partial_announcement(
1995-
&self, short_channel_id: u64, timestamp: u64, features: ChannelFeatures,
1996-
node_id_1: PublicKey, node_id_2: PublicKey,
1995+
&self, short_channel_id: u64, timestamp: u64, features: ChannelFeatures, node_id_1: NodeId,
1996+
node_id_2: NodeId,
19971997
) -> Result<(), LightningError> {
19981998
if node_id_1 == node_id_2 {
19991999
return Err(LightningError {
@@ -2002,13 +2002,11 @@ where
20022002
});
20032003
};
20042004

2005-
let node_1 = NodeId::from_pubkey(&node_id_1);
2006-
let node_2 = NodeId::from_pubkey(&node_id_2);
20072005
let channel_info = ChannelInfo {
20082006
features,
2009-
node_one: node_1.clone(),
2007+
node_one: node_id_1,
20102008
one_to_two: None,
2011-
node_two: node_2.clone(),
2009+
node_two: node_id_2,
20122010
two_to_one: None,
20132011
capacity_sats: None,
20142012
announcement_message: None,

0 commit comments

Comments
 (0)