Skip to content

Commit

Permalink
Avoid unnecessary PublicKey parsing in RGS application
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
TheBlueMatt committed Jan 31, 2025
1 parent 7e86255 commit c06b284
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 3 additions & 5 deletions lightning-rapid-gossip-sync/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use core::ops::Deref;
use core::sync::atomic::Ordering;

use bitcoin::constants::ChainHash;
use bitcoin::secp256k1::PublicKey;

use lightning::io;
use lightning::ln::msgs::{
Expand Down Expand Up @@ -117,7 +116,7 @@ where
};

let node_id_count: u32 = Readable::read(read_cursor)?;
let mut node_ids: Vec<PublicKey> = Vec::with_capacity(core::cmp::min(
let mut node_ids: Vec<NodeId> = Vec::with_capacity(core::cmp::min(
node_id_count,
MAX_INITIAL_NODE_ID_VECTOR_CAPACITY,
) as usize);
Expand Down Expand Up @@ -154,9 +153,8 @@ where
let key_parity = node_detail_flag & 0b_0000_0011;
pubkey_bytes[0] = key_parity;

let current_pubkey = PublicKey::from_slice(&pubkey_bytes)?;
let current_node_id = NodeId::from_pubkey(&current_pubkey);
node_ids.push(current_pubkey);
let current_node_id = NodeId::from_slice(&pubkey_bytes)?;
node_ids.push(current_node_id);

if is_reminder || has_address_details || feature_detail_marker > 0 {
let mut synthetic_node_announcement = UnsignedNodeAnnouncement {
Expand Down
8 changes: 3 additions & 5 deletions lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,7 @@ where
/// All other parameters as used in [`msgs::UnsignedChannelAnnouncement`] fields.
pub fn add_channel_from_partial_announcement(
&self, short_channel_id: u64, timestamp: u64, features: ChannelFeatures,
node_id_1: PublicKey, node_id_2: PublicKey,
node_id_1: NodeId, node_id_2: NodeId,
) -> Result<(), LightningError> {
if node_id_1 == node_id_2 {
return Err(LightningError {
Expand All @@ -2002,13 +2002,11 @@ where
});
};

let node_1 = NodeId::from_pubkey(&node_id_1);
let node_2 = NodeId::from_pubkey(&node_id_2);
let channel_info = ChannelInfo {
features,
node_one: node_1.clone(),
node_one: node_id_1,
one_to_two: None,
node_two: node_2.clone(),
node_two: node_id_2,
two_to_one: None,
capacity_sats: None,
announcement_message: None,
Expand Down

0 comments on commit c06b284

Please sign in to comment.