From: Elias Rohrer Date: Wed, 4 Sep 2024 13:39:20 +0000 (+0200) Subject: Add node id to remaining `RoutingMessageHandler::handle_` methods X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f68b8b68346e066bae9b22e53c7b72491d1c22c6;p=rust-lightning Add node id to remaining `RoutingMessageHandler::handle_` methods Previously, some `RoutingMessageHandler::handle_` methods (in particular the ones handling node and channel announcements, as well as channel updates, omitted the `their_node_id` argument. This didn't allow implementors to discern *who* sent a particular method. Here, we add `their_node_id: Option<&PublicKey>` to have them learn who sent a message, and set `None` if our own node it the originator of a broadcast operation. --- diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index a5f4de470..3ca6552dd 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -660,16 +660,18 @@ mod tests { } impl RoutingMessageHandler for MsgHandler { fn handle_node_announcement( - &self, _msg: &NodeAnnouncement, + &self, _their_node_id: Option<&PublicKey>, _msg: &NodeAnnouncement, ) -> Result { Ok(false) } fn handle_channel_announcement( - &self, _msg: &ChannelAnnouncement, + &self, _their_node_id: Option<&PublicKey>, _msg: &ChannelAnnouncement, ) -> Result { Ok(false) } - fn handle_channel_update(&self, _msg: &ChannelUpdate) -> Result { + fn handle_channel_update( + &self, _their_node_id: Option<&PublicKey>, _msg: &ChannelUpdate, + ) -> Result { Ok(false) } fn get_next_channel_announcement( diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index c4bfac1dd..160a6e7a8 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -1945,10 +1945,11 @@ fn do_during_funding_monitor_fail(confirm_a_first: bool, restore_b_before_conf: let (channel_ready, channel_id) = create_chan_between_nodes_with_value_confirm_second(&nodes[0], &nodes[1]); (channel_id, create_chan_between_nodes_with_value_b(&nodes[1], &nodes[0], &channel_ready)) }; - for node in nodes.iter() { - assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap()); - node.gossip_sync.handle_channel_update(&as_update).unwrap(); - node.gossip_sync.handle_channel_update(&bs_update).unwrap(); + for (i, node) in nodes.iter().enumerate() { + let counterparty_node_id = nodes[(i + 1) % 2].node.get_our_node_id(); + assert!(node.gossip_sync.handle_channel_announcement(Some(&counterparty_node_id), &announcement).unwrap()); + node.gossip_sync.handle_channel_update(Some(&counterparty_node_id), &as_update).unwrap(); + node.gossip_sync.handle_channel_update(Some(&counterparty_node_id), &bs_update).unwrap(); } if !restore_b_before_lock { diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 6b0a8a231..96fbdedb4 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -1501,14 +1501,16 @@ pub fn create_unannounced_chan_between_nodes_with_value<'a, 'b, 'c, 'd>(nodes: & pub fn update_nodes_with_chan_announce<'a, 'b, 'c, 'd>(nodes: &'a Vec>, a: usize, b: usize, ann: &msgs::ChannelAnnouncement, upd_1: &msgs::ChannelUpdate, upd_2: &msgs::ChannelUpdate) { for node in nodes { - assert!(node.gossip_sync.handle_channel_announcement(ann).unwrap()); - node.gossip_sync.handle_channel_update(upd_1).unwrap(); - node.gossip_sync.handle_channel_update(upd_2).unwrap(); + let node_id_a = nodes[a].node.get_our_node_id(); + let node_id_b = nodes[b].node.get_our_node_id(); + assert!(node.gossip_sync.handle_channel_announcement(None, ann).unwrap()); + node.gossip_sync.handle_channel_update(Some(&node_id_a), upd_1).unwrap(); + node.gossip_sync.handle_channel_update(Some(&node_id_b), upd_2).unwrap(); // Note that channel_updates are also delivered to ChannelManagers to ensure we have // forwarding info for local channels even if its not accepted in the network graph. - node.node.handle_channel_update(&nodes[a].node.get_our_node_id(), &upd_1); - node.node.handle_channel_update(&nodes[b].node.get_our_node_id(), &upd_2); + node.node.handle_channel_update(&node_id_a, &upd_1); + node.node.handle_channel_update(&node_id_b, &upd_2); } } @@ -3512,9 +3514,10 @@ pub fn handle_announce_close_broadcast_events<'a, 'b, 'c>(nodes: &Vec Result; + /// + /// If `their_node_id` is `None`, the message was generated by our own local node. + fn handle_node_announcement(&self, their_node_id: Option<&PublicKey>, msg: &NodeAnnouncement) -> Result; /// Handle a `channel_announcement` message, returning `true` if it should be forwarded on, `false` /// or returning an `Err` otherwise. - fn handle_channel_announcement(&self, msg: &ChannelAnnouncement) -> Result; + /// + /// If `their_node_id` is `None`, the message was generated by our own local node. + fn handle_channel_announcement(&self, their_node_id: Option<&PublicKey>, msg: &ChannelAnnouncement) -> Result; /// Handle an incoming `channel_update` message, returning true if it should be forwarded on, /// `false` or returning an `Err` otherwise. - fn handle_channel_update(&self, msg: &ChannelUpdate) -> Result; + /// + /// If `their_node_id` is `None`, the message was generated by our own local node. + fn handle_channel_update(&self, their_node_id: Option<&PublicKey>, msg: &ChannelUpdate) -> Result; /// Gets channel announcements and updates required to dump our routing table to a remote node, /// starting at the `short_channel_id` indicated by `starting_point` and including announcements /// for a single channel. diff --git a/lightning/src/ln/offers_tests.rs b/lightning/src/ln/offers_tests.rs index 8bbf9a0ad..c45660b10 100644 --- a/lightning/src/ln/offers_tests.rs +++ b/lightning/src/ln/offers_tests.rs @@ -134,9 +134,10 @@ fn announce_node_address<'a, 'b, 'c>( contents: announcement }; - node.gossip_sync.handle_node_announcement(&msg).unwrap(); + let node_pubkey = node.node.get_our_node_id(); + node.gossip_sync.handle_node_announcement(None, &msg).unwrap(); for peer in peers { - peer.gossip_sync.handle_node_announcement(&msg).unwrap(); + peer.gossip_sync.handle_node_announcement(Some(&node_pubkey), &msg).unwrap(); } } diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index 80c9c7810..585d0a261 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -111,9 +111,9 @@ impl MessageSendEventsProvider for IgnoringMessageHandler { fn get_and_clear_pending_msg_events(&self) -> Vec { Vec::new() } } impl RoutingMessageHandler for IgnoringMessageHandler { - fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result { Ok(false) } - fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result { Ok(false) } - fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result { Ok(false) } + fn handle_node_announcement(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::NodeAnnouncement) -> Result { Ok(false) } + fn handle_channel_announcement(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::ChannelAnnouncement) -> Result { Ok(false) } + fn handle_channel_update(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::ChannelUpdate) -> Result { Ok(false) } fn get_next_channel_announcement(&self, _starting_point: u64) -> Option<(msgs::ChannelAnnouncement, Option, Option)> { None } fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option { None } @@ -1887,22 +1887,22 @@ impl { - if self.message_handler.route_handler.handle_channel_announcement(&msg) + if self.message_handler.route_handler.handle_channel_announcement(Some(their_node_id), &msg) .map_err(|e| -> MessageHandlingError { e.into() })? { should_forward = Some(wire::Message::ChannelAnnouncement(msg)); } self.update_gossip_backlogged(); }, wire::Message::NodeAnnouncement(msg) => { - if self.message_handler.route_handler.handle_node_announcement(&msg) + if self.message_handler.route_handler.handle_node_announcement(Some(their_node_id), &msg) .map_err(|e| -> MessageHandlingError { e.into() })? { should_forward = Some(wire::Message::NodeAnnouncement(msg)); } self.update_gossip_backlogged(); }, wire::Message::ChannelUpdate(msg) => { - self.message_handler.chan_handler.handle_channel_update(&their_node_id, &msg); - if self.message_handler.route_handler.handle_channel_update(&msg) + self.message_handler.chan_handler.handle_channel_update(their_node_id, &msg); + if self.message_handler.route_handler.handle_channel_update(Some(their_node_id), &msg) .map_err(|e| -> MessageHandlingError { e.into() })? { should_forward = Some(wire::Message::ChannelUpdate(msg)); } @@ -2285,13 +2285,13 @@ impl { log_debug!(self.logger, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id); - match self.message_handler.route_handler.handle_channel_announcement(&msg) { + match self.message_handler.route_handler.handle_channel_announcement(None, &msg) { Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) => self.forward_broadcast_msg(peers, &wire::Message::ChannelAnnouncement(msg), None), _ => {}, } if let Some(msg) = update_msg { - match self.message_handler.route_handler.handle_channel_update(&msg) { + match self.message_handler.route_handler.handle_channel_update(None, &msg) { Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) => self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None), _ => {}, @@ -2300,7 +2300,7 @@ impl { log_debug!(self.logger, "Handling BroadcastChannelUpdate event in peer_handler for contents {:?}", msg.contents); - match self.message_handler.route_handler.handle_channel_update(&msg) { + match self.message_handler.route_handler.handle_channel_update(None, &msg) { Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) => self.forward_broadcast_msg(peers, &wire::Message::ChannelUpdate(msg), None), _ => {}, @@ -2308,7 +2308,7 @@ impl { log_debug!(self.logger, "Handling BroadcastNodeAnnouncement event in peer_handler for node {}", msg.contents.node_id); - match self.message_handler.route_handler.handle_node_announcement(&msg) { + match self.message_handler.route_handler.handle_node_announcement(None, &msg) { Ok(_) | Err(LightningError { action: msgs::ErrorAction::IgnoreDuplicateGossip, .. }) => self.forward_broadcast_msg(peers, &wire::Message::NodeAnnouncement(msg), None), _ => {}, @@ -2674,7 +2674,7 @@ impl(msg: &ChannelAnnouncement, s impl>, U: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync where U::Target: UtxoLookup, L::Target: Logger { - fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result { + fn handle_node_announcement(&self, _their_node_id: Option<&PublicKey>, msg: &msgs::NodeAnnouncement) -> Result { self.network_graph.update_node_from_announcement(msg)?; Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY && msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY && msg.contents.excess_data.len() + msg.contents.excess_address_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY) } - fn handle_channel_announcement(&self, msg: &msgs::ChannelAnnouncement) -> Result { + fn handle_channel_announcement(&self, _their_node_id: Option<&PublicKey>, msg: &msgs::ChannelAnnouncement) -> Result { self.network_graph.update_channel_from_announcement(msg, &*self.utxo_lookup.read().unwrap())?; Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY) } - fn handle_channel_update(&self, msg: &msgs::ChannelUpdate) -> Result { + fn handle_channel_update(&self, _their_node_id: Option<&PublicKey>, msg: &msgs::ChannelUpdate) -> Result { self.network_graph.update_channel(msg)?; Ok(msg.contents.excess_data.len() <= MAX_EXCESS_BYTES_FOR_RELAY) } @@ -2568,11 +2568,12 @@ pub(crate) mod tests { let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let zero_hash = Sha256dHash::hash(&[0; 32]); let valid_announcement = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!("No existing channels for node_announcement", e.err) }; @@ -2580,7 +2581,7 @@ pub(crate) mod tests { { // Announce a channel to add a corresponding node. let valid_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() }; @@ -2588,6 +2589,7 @@ pub(crate) mod tests { let fake_msghash = hash_to_message!(zero_hash.as_byte_array()); match gossip_sync.handle_node_announcement( + Some(&node_1_pubkey), &NodeAnnouncement { signature: secp_ctx.sign_ecdsa(&fake_msghash, node_1_privkey), contents: valid_announcement.contents.clone() @@ -2596,7 +2598,7 @@ pub(crate) mod tests { Err(e) => assert_eq!(e.err, "Invalid signature on node_announcement message") }; - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), Err(_) => panic!() }; @@ -2606,7 +2608,7 @@ pub(crate) mod tests { unsigned_announcement.excess_data.resize(MAX_EXCESS_BYTES_FOR_RELAY + 1, 0); }, node_1_privkey, &secp_ctx); // Return false because contains excess data. - match gossip_sync.handle_node_announcement(&announcement_with_data) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement_with_data) { Ok(res) => assert!(!res), Err(_) => panic!() }; @@ -2616,7 +2618,7 @@ pub(crate) mod tests { let outdated_announcement = get_signed_node_announcement(|unsigned_announcement| { unsigned_announcement.timestamp += 1000 - 10; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&outdated_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &outdated_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Update older than last processed update") }; @@ -2628,6 +2630,7 @@ pub(crate) mod tests { let logger = test_utils::TestLogger::new(); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let good_script = get_channel_script(&secp_ctx); @@ -2636,7 +2639,7 @@ pub(crate) mod tests { // Test if the UTXO lookups were not supported let network_graph = NetworkGraph::new(Network::Testnet, &logger); let mut gossip_sync = P2PGossipSync::new(&network_graph, None, &logger); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() }; @@ -2650,7 +2653,7 @@ pub(crate) mod tests { // If we receive announcement for the same channel (with UTXO lookups disabled), // drop new one on the floor, since we can't see any changes. - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Already have non-chain-validated channel") }; @@ -2664,7 +2667,7 @@ pub(crate) mod tests { let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.short_channel_id += 1; }, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Channel announced without corresponding UTXO entry") }; @@ -2675,7 +2678,7 @@ pub(crate) mod tests { let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.short_channel_id += 2; }, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() }; @@ -2691,7 +2694,7 @@ pub(crate) mod tests { // chain, we simply ignore all new (duplicate) announcements. *chain_source.utxo_ret.lock().unwrap() = UtxoResult::Sync(Ok(TxOut { value: Amount::ZERO, script_pubkey: good_script })); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Already have chain-validated channel") }; @@ -2708,7 +2711,7 @@ pub(crate) mod tests { let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.short_channel_id += 3; }, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Channel with SCID 3 or one of its nodes was removed from our network graph recently") } @@ -2716,7 +2719,7 @@ pub(crate) mod tests { gossip_sync.network_graph().remove_stale_channels_and_tracking_with_time(tracking_time + REMOVED_ENTRIES_TRACKING_AGE_LIMIT_SECS); // The above channel announcement should be handled as per normal now. - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() } @@ -2729,19 +2732,19 @@ pub(crate) mod tests { let mut invalid_sig_announcement = valid_excess_data_announcement.clone(); invalid_sig_announcement.contents.excess_data = Vec::new(); - match gossip_sync.handle_channel_announcement(&invalid_sig_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &invalid_sig_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Invalid signature on channel_announcement message") }; // Don't relay valid channels with excess data - match gossip_sync.handle_channel_announcement(&valid_excess_data_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_excess_data_announcement) { Ok(res) => assert!(!res), _ => panic!() }; let channel_to_itself_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&channel_to_itself_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &channel_to_itself_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself") }; @@ -2751,7 +2754,7 @@ pub(crate) mod tests { let incorrect_chain_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.chain_hash = ChainHash::using_genesis_block(Network::Bitcoin); }, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&incorrect_chain_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &incorrect_chain_announcement) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Channel announcement chain hash does not match genesis hash") }; @@ -2766,6 +2769,7 @@ pub(crate) mod tests { let gossip_sync = P2PGossipSync::new(&network_graph, Some(&chain_source), &logger); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let amount_sats = Amount::from_sat(1000_000); @@ -2779,7 +2783,7 @@ pub(crate) mod tests { let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); short_channel_id = valid_channel_announcement.contents.short_channel_id; - match gossip_sync.handle_channel_announcement(&valid_channel_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_channel_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -2788,7 +2792,7 @@ pub(crate) mod tests { let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx); network_graph.verify_channel_update(&valid_channel_update).unwrap(); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(res) => assert!(res), _ => panic!(), }; @@ -2808,7 +2812,7 @@ pub(crate) mod tests { unsigned_channel_update.excess_data.resize(MAX_EXCESS_BYTES_FOR_RELAY + 1, 0); }, node_1_privkey, &secp_ctx); // Return false because contains excess data - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(res) => assert!(!res), _ => panic!() }; @@ -2817,7 +2821,7 @@ pub(crate) mod tests { unsigned_channel_update.timestamp += 110; unsigned_channel_update.short_channel_id += 1; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Couldn't find channel for update") }; @@ -2826,7 +2830,7 @@ pub(crate) mod tests { unsigned_channel_update.htlc_maximum_msat = MAX_VALUE_MSAT + 1; unsigned_channel_update.timestamp += 110; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than maximum possible msats") }; @@ -2835,7 +2839,7 @@ pub(crate) mod tests { unsigned_channel_update.htlc_maximum_msat = amount_sats.to_sat() * 1000 + 1; unsigned_channel_update.timestamp += 110; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "htlc_maximum_msat is larger than channel capacity or capacity is bogus") }; @@ -2845,7 +2849,7 @@ pub(crate) mod tests { let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| { unsigned_channel_update.timestamp += 100; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Update had same timestamp as last processed update") }; @@ -2856,7 +2860,7 @@ pub(crate) mod tests { let zero_hash = Sha256dHash::hash(&[0; 32]); let fake_msghash = hash_to_message!(zero_hash.as_byte_array()); invalid_sig_channel_update.signature = secp_ctx.sign_ecdsa(&fake_msghash, node_1_privkey); - match gossip_sync.handle_channel_update(&invalid_sig_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &invalid_sig_channel_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Invalid signature on channel_update message") }; @@ -2867,7 +2871,7 @@ pub(crate) mod tests { unsigned_channel_update.chain_hash = ChainHash::using_genesis_block(Network::Bitcoin); }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&incorrect_chain_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &incorrect_chain_update) { Ok(_) => panic!(), Err(e) => assert_eq!(e.err, "Channel update chain hash does not match genesis hash") }; @@ -2978,6 +2982,7 @@ pub(crate) mod tests { let secp_ctx = Secp256k1::new(); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); @@ -2988,11 +2993,11 @@ pub(crate) mod tests { // Submit two channel updates for each channel direction (update.flags bit). let valid_channel_update = get_signed_channel_update(|_| {}, node_1_privkey, &secp_ctx); - assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok()); + assert!(gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update).is_ok()); assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some()); let valid_channel_update_2 = get_signed_channel_update(|update| {update.channel_flags |=1;}, node_2_privkey, &secp_ctx); - gossip_sync.handle_channel_update(&valid_channel_update_2).unwrap(); + gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update_2).unwrap(); assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().two_to_one.is_some()); network_graph.remove_stale_channels_and_tracking_with_time(100 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS); @@ -3024,7 +3029,7 @@ pub(crate) mod tests { let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| { unsigned_channel_update.timestamp = (announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS) as u32; }, node_1_privkey, &secp_ctx); - assert!(gossip_sync.handle_channel_update(&valid_channel_update).is_ok()); + assert!(gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update).is_ok()); assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some()); network_graph.remove_stale_channels_and_tracking_with_time(announcement_time + 1 + STALE_CHANNEL_UPDATE_AGE_LIMIT_SECS); // Make sure removed channels are tracked. @@ -3107,6 +3112,7 @@ pub(crate) mod tests { let network_graph = create_network_graph(); let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); // Channels were not announced yet. @@ -3118,7 +3124,7 @@ pub(crate) mod tests { // Announce a channel we will update let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); short_channel_id = valid_channel_announcement.contents.short_channel_id; - match gossip_sync.handle_channel_announcement(&valid_channel_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_channel_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -3139,7 +3145,7 @@ pub(crate) mod tests { let valid_channel_update = get_signed_channel_update(|unsigned_channel_update| { unsigned_channel_update.timestamp = 101; }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => (), Err(_) => panic!() }; @@ -3161,7 +3167,7 @@ pub(crate) mod tests { unsigned_channel_update.timestamp = 102; unsigned_channel_update.excess_data = [1; MAX_EXCESS_BYTES_FOR_RELAY + 1].to_vec(); }, node_1_privkey, &secp_ctx); - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_1_pubkey), &valid_channel_update) { Ok(_) => (), Err(_) => panic!() }; @@ -3187,6 +3193,7 @@ pub(crate) mod tests { let network_graph = create_network_graph(); let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey)); @@ -3197,7 +3204,7 @@ pub(crate) mod tests { { // Announce a channel to add 2 nodes let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_channel_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_channel_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -3209,13 +3216,13 @@ pub(crate) mod tests { { let valid_announcement = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => (), Err(_) => panic!() }; let valid_announcement = get_signed_node_announcement(|_| {}, node_2_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -3234,7 +3241,7 @@ pub(crate) mod tests { unsigned_announcement.timestamp += 10; unsigned_announcement.excess_data = [1; MAX_EXCESS_BYTES_FOR_RELAY + 1].to_vec(); }, node_2_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(!res), Err(_) => panic!() }; @@ -3250,17 +3257,18 @@ pub(crate) mod tests { let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); // Announce a channel to add a corresponding node. let valid_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() }; let valid_announcement = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx); - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -3338,6 +3346,7 @@ pub(crate) mod tests { let chain_hash = ChainHash::using_genesis_block(Network::Testnet); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let node_id_2 = PublicKey::from_secret_key(&secp_ctx, node_2_privkey); @@ -3358,7 +3367,7 @@ pub(crate) mod tests { let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| { unsigned_announcement.short_channel_id = scid; }, node_1_privkey, node_2_privkey, &secp_ctx); - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(_) => (), _ => panic!() }; @@ -3816,11 +3825,12 @@ pub(crate) mod tests { let (secp_ctx, gossip_sync) = create_gossip_sync(&network_graph); let node_1_privkey = &SecretKey::from_slice(&[42; 32]).unwrap(); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); let node_2_privkey = &SecretKey::from_slice(&[41; 32]).unwrap(); let node_1_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey)); let announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx); - gossip_sync.handle_channel_announcement(&announcement).unwrap(); + gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &announcement).unwrap(); let tcp_ip_v4 = SocketAddress::TcpIpV4 { addr: [255, 254, 253, 252], @@ -3845,7 +3855,7 @@ pub(crate) mod tests { assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement(|_| {}, node_1_privkey, &secp_ctx); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3858,7 +3868,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3870,7 +3880,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3882,7 +3892,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3892,7 +3902,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3902,7 +3912,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); let announcement = get_signed_node_announcement( @@ -3912,7 +3922,7 @@ pub(crate) mod tests { }, node_1_privkey, &secp_ctx ); - gossip_sync.handle_node_announcement(&announcement).unwrap(); + gossip_sync.handle_node_announcement(Some(&node_1_pubkey), &announcement).unwrap(); assert!(!network_graph.read_only().node(&node_1_id).unwrap().is_tor_only()); } } diff --git a/lightning/src/routing/test_utils.rs b/lightning/src/routing/test_utils.rs index cf991ed0a..d38b505c7 100644 --- a/lightning/src/routing/test_utils.rs +++ b/lightning/src/routing/test_utils.rs @@ -32,7 +32,8 @@ pub(crate) fn add_channel( gossip_sync: &P2PGossipSync>>, Arc, Arc>, secp_ctx: &Secp256k1, node_1_privkey: &SecretKey, node_2_privkey: &SecretKey, features: ChannelFeatures, short_channel_id: u64 ) { - let node_id_1 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_1_privkey)); + let node_1_pubkey = PublicKey::from_secret_key(&secp_ctx, node_1_privkey); + let node_id_1 = NodeId::from_pubkey(&node_1_pubkey); let node_id_2 = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_2_privkey)); let unsigned_announcement = UnsignedChannelAnnouncement { @@ -54,7 +55,7 @@ pub(crate) fn add_channel( bitcoin_signature_2: secp_ctx.sign_ecdsa(&msghash, node_2_privkey), contents: unsigned_announcement.clone(), }; - match gossip_sync.handle_channel_announcement(&valid_announcement) { + match gossip_sync.handle_channel_announcement(Some(&node_1_pubkey), &valid_announcement) { Ok(res) => assert!(res), _ => panic!() }; @@ -64,7 +65,8 @@ pub(crate) fn add_or_update_node( gossip_sync: &P2PGossipSync>>, Arc, Arc>, secp_ctx: &Secp256k1, node_privkey: &SecretKey, features: NodeFeatures, timestamp: u32 ) { - let node_id = NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, node_privkey)); + let node_pubkey = PublicKey::from_secret_key(&secp_ctx, node_privkey); + let node_id = NodeId::from_pubkey(&node_pubkey); let unsigned_announcement = UnsignedNodeAnnouncement { features, timestamp, @@ -81,7 +83,7 @@ pub(crate) fn add_or_update_node( contents: unsigned_announcement.clone() }; - match gossip_sync.handle_node_announcement(&valid_announcement) { + match gossip_sync.handle_node_announcement(Some(&node_pubkey), &valid_announcement) { Ok(_) => (), Err(_) => panic!() }; @@ -91,13 +93,14 @@ pub(crate) fn update_channel( gossip_sync: &P2PGossipSync>>, Arc, Arc>, secp_ctx: &Secp256k1, node_privkey: &SecretKey, update: UnsignedChannelUpdate ) { + let node_pubkey = PublicKey::from_secret_key(&secp_ctx, node_privkey); let msghash = hash_to_message!(&Sha256dHash::hash(&update.encode()[..])[..]); let valid_channel_update = ChannelUpdate { signature: secp_ctx.sign_ecdsa(&msghash, node_privkey), contents: update.clone() }; - match gossip_sync.handle_channel_update(&valid_channel_update) { + match gossip_sync.handle_channel_update(Some(&node_pubkey), &valid_channel_update) { Ok(res) => assert!(res), Err(_) => panic!() }; diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 7252162e2..78ce627be 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -1023,14 +1023,14 @@ impl TestRoutingMessageHandler { } } impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { - fn handle_node_announcement(&self, _msg: &msgs::NodeAnnouncement) -> Result { + fn handle_node_announcement(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::NodeAnnouncement) -> Result { Err(msgs::LightningError { err: "".to_owned(), action: msgs::ErrorAction::IgnoreError }) } - fn handle_channel_announcement(&self, _msg: &msgs::ChannelAnnouncement) -> Result { + fn handle_channel_announcement(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::ChannelAnnouncement) -> Result { self.chan_anns_recvd.fetch_add(1, Ordering::AcqRel); Err(msgs::LightningError { err: "".to_owned(), action: msgs::ErrorAction::IgnoreError }) } - fn handle_channel_update(&self, _msg: &msgs::ChannelUpdate) -> Result { + fn handle_channel_update(&self, _their_node_id: Option<&PublicKey>, _msg: &msgs::ChannelUpdate) -> Result { self.chan_upds_recvd.fetch_add(1, Ordering::AcqRel); Err(msgs::LightningError { err: "".to_owned(), action: msgs::ErrorAction::IgnoreError }) }