Skip to content

Commit 7806256

Browse files
committed
Cache our node ID in ChannelManager
While its not necessarily a common operation on a running node, `get_our_node_id()` is used incredibly heavily in tests, and there is no reason to not eat the extra ~64 bytes to just cache it.
1 parent 5927920 commit 7806256

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
434434
#[cfg(not(any(test, feature = "_test_utils")))]
435435
channel_state: Mutex<ChannelHolder<Signer>>,
436436
our_network_key: SecretKey,
437+
our_network_pubkey: PublicKey,
437438

438439
/// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
439440
/// value increases strictly since we don't assume access to a time source.
@@ -822,7 +823,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
822823

823824
latest_block_height: AtomicUsize::new(params.latest_height),
824825
last_block_hash: RwLock::new(params.latest_hash),
825-
secp_ctx,
826826

827827
channel_state: Mutex::new(ChannelHolder{
828828
by_id: HashMap::new(),
@@ -832,6 +832,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
832832
pending_msg_events: Vec::new(),
833833
}),
834834
our_network_key: keys_manager.get_node_secret(),
835+
our_network_pubkey: PublicKey::from_secret_key(&secp_ctx, &keys_manager.get_node_secret()),
836+
secp_ctx,
835837

836838
last_node_announcement_serial: AtomicUsize::new(0),
837839

@@ -2315,7 +2317,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23152317

23162318
/// Gets the node_id held by this ChannelManager
23172319
pub fn get_our_node_id(&self) -> PublicKey {
2318-
PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key)
2320+
self.our_network_pubkey.clone()
23192321
}
23202322

23212323
/// Restores a single, given channel to normal operation after a
@@ -4318,7 +4320,6 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
43184320

43194321
latest_block_height: AtomicUsize::new(latest_block_height as usize),
43204322
last_block_hash: RwLock::new(last_block_hash),
4321-
secp_ctx,
43224323

43234324
channel_state: Mutex::new(ChannelHolder {
43244325
by_id,
@@ -4328,6 +4329,8 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
43284329
pending_msg_events: Vec::new(),
43294330
}),
43304331
our_network_key: args.keys_manager.get_node_secret(),
4332+
our_network_pubkey: PublicKey::from_secret_key(&secp_ctx, &args.keys_manager.get_node_secret()),
4333+
secp_ctx,
43314334

43324335
last_node_announcement_serial: AtomicUsize::new(last_node_announcement_serial as usize),
43334336

0 commit comments

Comments
 (0)