Skip to content

Commit 2f2d167

Browse files
committed
Swap PublicKey for NodeId in UnsignedNodeAnnouncement
Also swaps `PublicKey` for `NodeId` in `get_next_node_announcement` and `InitSyncTracker` to avoid unnecessary deserialization that came from changing `UnsignedNodeAnnouncement`.
1 parent 3fecacc commit 2f2d167

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

lightning-net-tokio/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ mod tests {
584584
use lightning::ln::msgs::*;
585585
use lightning::ln::peer_handler::{MessageHandler, PeerManager};
586586
use lightning::ln::features::NodeFeatures;
587+
use lightning::routing::gossip::NodeId;
587588
use lightning::util::events::*;
588589
use lightning::util::test_utils::TestNodeSigner;
589590
use bitcoin::secp256k1::{Secp256k1, SecretKey, PublicKey};
@@ -614,7 +615,7 @@ mod tests {
614615
fn handle_channel_announcement(&self, _msg: &ChannelAnnouncement) -> Result<bool, LightningError> { Ok(false) }
615616
fn handle_channel_update(&self, _msg: &ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
616617
fn get_next_channel_announcement(&self, _starting_point: u64) -> Option<(ChannelAnnouncement, Option<ChannelUpdate>, Option<ChannelUpdate>)> { None }
617-
fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> { None }
618+
fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option<NodeAnnouncement> { None }
618619
fn peer_connected(&self, _their_node_id: &PublicKey, _init_msg: &Init) -> Result<(), ()> { Ok(()) }
619620
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
620621
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }

lightning/src/ln/msgs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ pub struct UnsignedNodeAnnouncement {
665665
pub timestamp: u32,
666666
/// The `node_id` this announcement originated from (don't rebroadcast the `node_announcement` back
667667
/// to this node).
668-
pub node_id: PublicKey,
668+
pub node_id: NodeId,
669669
/// An RGB color for UI purposes
670670
pub rgb: [u8; 3],
671671
/// An alias, for UI purposes.
@@ -1057,7 +1057,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider {
10571057
/// the node *after* the provided pubkey and including up to one announcement immediately
10581058
/// higher (as defined by `<PublicKey as Ord>::cmp`) than `starting_point`.
10591059
/// If `None` is provided for `starting_point`, we start at the first node.
1060-
fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement>;
1060+
fn get_next_node_announcement(&self, starting_point: Option<&NodeId>) -> Option<NodeAnnouncement>;
10611061
/// Called when a connection is established with a peer. This can be used to
10621062
/// perform routing table synchronization using a strategy defined by the
10631063
/// implementor.
@@ -1845,7 +1845,7 @@ impl Readable for UnsignedNodeAnnouncement {
18451845
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
18461846
let features: NodeFeatures = Readable::read(r)?;
18471847
let timestamp: u32 = Readable::read(r)?;
1848-
let node_id: PublicKey = Readable::read(r)?;
1848+
let node_id: NodeId = Readable::read(r)?;
18491849
let mut rgb = [0; 3];
18501850
r.read_exact(&mut rgb)?;
18511851
let alias: [u8; 32] = Readable::read(r)?;
@@ -2248,7 +2248,7 @@ mod tests {
22482248
let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
22492249
features,
22502250
timestamp: 20190119,
2251-
node_id: pubkey_1,
2251+
node_id: NodeId::from_pubkey(&pubkey_1),
22522252
rgb: [32; 3],
22532253
alias: [16;32],
22542254
addresses,

lightning/src/ln/peer_handler.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl RoutingMessageHandler for IgnoringMessageHandler {
7171
fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result<bool, LightningError> { Ok(false) }
7272
fn get_next_channel_announcement(&self, _starting_point: u64) ->
7373
Option<(msgs::ChannelAnnouncement, Option<msgs::ChannelUpdate>, Option<msgs::ChannelUpdate>)> { None }
74-
fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option<msgs::NodeAnnouncement> { None }
74+
fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option<msgs::NodeAnnouncement> { None }
7575
fn peer_connected(&self, _their_node_id: &PublicKey, _init: &msgs::Init) -> Result<(), ()> { Ok(()) }
7676
fn handle_reply_channel_range(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyChannelRange) -> Result<(), LightningError> { Ok(()) }
7777
fn handle_reply_short_channel_ids_end(&self, _their_node_id: &PublicKey, _msg: msgs::ReplyShortChannelIdsEnd) -> Result<(), LightningError> { Ok(()) }
@@ -345,7 +345,7 @@ impl error::Error for PeerHandleError {
345345
enum InitSyncTracker{
346346
NoSyncRequested,
347347
ChannelsSyncing(u64),
348-
NodesSyncing(PublicKey),
348+
NodesSyncing(NodeId),
349349
}
350350

351351
/// The ratio between buffer sizes at which we stop sending initial sync messages vs when we stop
@@ -434,15 +434,15 @@ impl Peer {
434434
}
435435

436436
/// Similar to the above, but for node announcements indexed by node_id.
437-
fn should_forward_node_announcement(&self, node_id: PublicKey) -> bool {
437+
fn should_forward_node_announcement(&self, node_id: NodeId) -> bool {
438438
if self.their_features.as_ref().unwrap().supports_gossip_queries() &&
439439
!self.sent_gossip_timestamp_filter {
440440
return false;
441441
}
442442
match self.sync_status {
443443
InitSyncTracker::NoSyncRequested => true,
444444
InitSyncTracker::ChannelsSyncing(_) => false,
445-
InitSyncTracker::NodesSyncing(pk) => pk < node_id,
445+
InitSyncTracker::NodesSyncing(sync_node_id) => sync_node_id.as_slice() < node_id.as_slice(),
446446
}
447447
}
448448

@@ -889,8 +889,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
889889
}
890890
},
891891
InitSyncTracker::ChannelsSyncing(_) => unreachable!(),
892-
InitSyncTracker::NodesSyncing(key) => {
893-
if let Some(msg) = self.message_handler.route_handler.get_next_node_announcement(Some(&key)) {
892+
InitSyncTracker::NodesSyncing(sync_node_id) => {
893+
if let Some(msg) = self.message_handler.route_handler.get_next_node_announcement(Some(&sync_node_id)) {
894894
self.enqueue_message(peer, &msg);
895895
peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
896896
} else {
@@ -1493,8 +1493,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
14931493
log_gossip!(self.logger, "Skipping broadcast message to {:?} as its outbound buffer is full", peer.their_node_id);
14941494
continue;
14951495
}
1496-
if peer.their_node_id.as_ref() == Some(&msg.contents.node_id) {
1497-
continue;
1496+
if let Some(their_node_id) = peer.their_node_id {
1497+
if NodeId::from_pubkey(&their_node_id) == msg.contents.node_id {
1498+
continue;
1499+
}
14981500
}
14991501
if except_node.is_some() && peer.their_node_id.as_ref() == except_node {
15001502
continue;
@@ -2023,7 +2025,7 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
20232025
let announcement = msgs::UnsignedNodeAnnouncement {
20242026
features,
20252027
timestamp: self.last_node_announcement_serial.fetch_add(1, Ordering::AcqRel),
2026-
node_id: self.node_signer.get_node_id(Recipient::Node).unwrap(),
2028+
node_id: NodeId::from_pubkey(&self.node_signer.get_node_id(Recipient::Node).unwrap()),
20272029
rgb, alias, addresses,
20282030
excess_address_data: Vec::new(),
20292031
excess_data: Vec::new(),

lightning/src/routing/gossip.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,10 @@ where C::Target: chain::Access, L::Target: Logger
385385
None
386386
}
387387

388-
fn get_next_node_announcement(&self, starting_point: Option<&PublicKey>) -> Option<NodeAnnouncement> {
388+
fn get_next_node_announcement(&self, starting_point: Option<&NodeId>) -> Option<NodeAnnouncement> {
389389
let nodes = self.network_graph.nodes.read().unwrap();
390-
let iter = if let Some(pubkey) = starting_point {
391-
nodes.range((Bound::Excluded(NodeId::from_pubkey(pubkey)), Bound::Unbounded))
390+
let iter = if let Some(node_id) = starting_point {
391+
nodes.range((Bound::Excluded(node_id), Bound::Unbounded))
392392
} else {
393393
nodes.range(..)
394394
};
@@ -1286,7 +1286,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12861286
/// routing messages from a source using a protocol other than the lightning P2P protocol.
12871287
pub fn update_node_from_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), LightningError> {
12881288
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
1289-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id, "node_announcement");
1289+
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &get_pubkey_from_node_id!(msg.contents.node_id, "node_announcement"), "node_announcement");
12901290
self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
12911291
}
12921292

@@ -1299,7 +1299,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
12991299
}
13001300

13011301
fn update_node_from_announcement_intern(&self, msg: &msgs::UnsignedNodeAnnouncement, full_msg: Option<&msgs::NodeAnnouncement>) -> Result<(), LightningError> {
1302-
match self.nodes.write().unwrap().get_mut(&NodeId::from_pubkey(&msg.node_id)) {
1302+
match self.nodes.write().unwrap().get_mut(&msg.node_id) {
13031303
None => Err(LightningError{err: "No existing channels for node_announcement".to_owned(), action: ErrorAction::IgnoreError}),
13041304
Some(node) => {
13051305
if let Some(node_info) = node.announcement_info.as_ref() {
@@ -1971,11 +1971,11 @@ mod tests {
19711971
}
19721972

19731973
fn get_signed_node_announcement<F: Fn(&mut UnsignedNodeAnnouncement)>(f: F, node_key: &SecretKey, secp_ctx: &Secp256k1<secp256k1::All>) -> NodeAnnouncement {
1974-
let node_id = PublicKey::from_secret_key(&secp_ctx, node_key);
1974+
let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_key));
19751975
let mut unsigned_announcement = UnsignedNodeAnnouncement {
19761976
features: channelmanager::provided_node_features(&UserConfig::default()),
19771977
timestamp: 100,
1978-
node_id: node_id,
1978+
node_id,
19791979
rgb: [0; 3],
19801980
alias: [0; 32],
19811981
addresses: Vec::new(),
@@ -2652,7 +2652,7 @@ mod tests {
26522652
let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph);
26532653
let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap();
26542654
let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap();
2655-
let node_id_1 = PublicKey::from_secret_key(&secp_ctx, node_1_privkey);
2655+
let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey));
26562656

26572657
// No nodes yet.
26582658
let next_announcements = gossip_sync.get_next_node_announcement(None);

lightning/src/routing/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) fn add_or_update_node(
6666
gossip_sync: &P2PGossipSync<Arc<NetworkGraph<Arc<test_utils::TestLogger>>>, Arc<test_utils::TestChainSource>, Arc<test_utils::TestLogger>>,
6767
secp_ctx: &Secp256k1<All>, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32
6868
) {
69-
let node_id = PublicKey::from_secret_key(&secp_ctx, node_privkey);
69+
let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_privkey));
7070
let unsigned_announcement = UnsignedNodeAnnouncement {
7171
features,
7272
timestamp,

lightning/src/util/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler {
511511
Some((chan_ann, Some(chan_upd_1), Some(chan_upd_2)))
512512
}
513513

514-
fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option<msgs::NodeAnnouncement> {
514+
fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option<msgs::NodeAnnouncement> {
515515
None
516516
}
517517

0 commit comments

Comments
 (0)