From d0ae2a9a7d0a164f27690ed7bcfa0f2541a6ca75 Mon Sep 17 00:00:00 2001 From: YI Date: Fri, 20 Dec 2024 14:08:46 +0800 Subject: [PATCH] Fix duplicated p2p postfix in announced addresses --- src/fiber/network.rs | 20 +++++++- src/fiber/tests/network.rs | 95 ++++++++++++++++++++++++++++++++++- src/fiber/tests/test_utils.rs | 4 ++ 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/fiber/network.rs b/src/fiber/network.rs index 8db727b43..fab557177 100644 --- a/src/fiber/network.rs +++ b/src/fiber/network.rs @@ -2889,9 +2889,25 @@ where if config.announce_listening_addr() { announced_addrs.push(listening_addr.clone()); } - for listen_addr in &config.announced_addrs { + for announced_addr in &config.announced_addrs { let mut multiaddr = - MultiAddr::from_str(listen_addr.as_str()).expect("valid announced listen addr"); + MultiAddr::from_str(announced_addr.as_str()).expect("valid announced listen addr"); + match multiaddr.pop() { + Some(Protocol::P2P(c)) => { + // If the announced listen addr has a peer id, it must match our peer id. + if c.as_ref() != my_peer_id.as_bytes() { + panic!("Announced listen addr is using invalid peer id: announced addr {}, actual peer id {:?}", announced_addr, my_peer_id); + } + } + Some(component) => { + // Push this unrecognized component back to the multiaddr. + multiaddr.push(component); + } + None => { + // Should never happen + } + } + // Push our peer id to the multiaddr. multiaddr.push(Protocol::P2P(Cow::Owned(my_peer_id.clone().into_bytes()))); announced_addrs.push(multiaddr); } diff --git a/src/fiber/tests/network.rs b/src/fiber/tests/network.rs index 980bdd1de..c2c114d37 100644 --- a/src/fiber/tests/network.rs +++ b/src/fiber/tests/network.rs @@ -29,7 +29,7 @@ use ckb_types::{ use musig2::PartialSignature; use std::{borrow::Cow, str::FromStr, time::Duration}; use tentacle::{ - multiaddr::{MultiAddr, Protocol}, + multiaddr::{MultiAddr, Multiaddr, Protocol}, secio::PeerId, }; @@ -109,6 +109,99 @@ fn create_fake_node_announcement_mesage() -> NodeAnnouncement { create_node_announcement_mesage_with_priv_key(&priv_key) } +#[tokio::test] +async fn test_save_our_own_node_announcement_to_graph() { + let mut node = NetworkNode::new().await; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + node.stop().await; + let nodes = node.get_network_graph_nodes().await; + assert_eq!(nodes.len(), 1); + assert_eq!(nodes[0].node_id, node.get_public_key()); +} + +#[tokio::test] +#[should_panic] +async fn test_set_announced_addrs_with_invalid_peer_id() { + let mut node = NetworkNode::new_with_config( + NetworkNodeConfigBuilder::new() + .fiber_config_updater(|config| { + config.announced_addrs = vec![ + "/ip4/1.1.1.1/tcp/8346/p2p/QmaFDJb9CkMrXy7nhTWBY5y9mvuykre3EzzRsCJUAVXprZ" + .to_string(), + ]; + }) + .build(), + ) + .await; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + node.stop().await; + let nodes = node.get_network_graph_nodes().await; + assert_eq!(nodes.len(), 1); + assert_eq!(nodes[0].node_id, node.get_public_key()); +} + +#[tokio::test] +async fn test_set_announced_addrs_with_valid_peer_id() { + let mut node = NetworkNode::new().await; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + node.stop().await; + + let peer_id = node.get_peer_id(); + let addr = format!("/ip4/1.1.1.1/tcp/8346/p2p/{}", peer_id); + let multiaddr = Multiaddr::from_str(&addr).expect("valid multiaddr"); + let mut node = NetworkNode::new_with_config( + NetworkNodeConfigBuilder::new() + .base_dir(node.base_dir.clone()) + .fiber_config_updater(move |config| { + config.announced_addrs = vec![addr.clone()]; + }) + .build(), + ) + .await; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + node.stop().await; + let nodes = node.get_network_graph_nodes().await; + assert_eq!(nodes.len(), 1); + assert_eq!(nodes[0].node_id, node.get_public_key()); + assert_eq!( + nodes[0].addresses.iter().find(|x| *x == &multiaddr), + Some(&multiaddr) + ); +} + +#[tokio::test] +async fn test_set_announced_addrs_without_p2p() { + let addr = "/ip4/1.1.1.1/tcp/8346".to_string(); + let cloned_addr = addr.clone(); + let mut node = NetworkNode::new_with_config( + NetworkNodeConfigBuilder::new() + .fiber_config_updater(move |config| { + config.announced_addrs = vec![cloned_addr]; + }) + .build(), + ) + .await; + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; + node.stop().await; + let peer_id = node.get_peer_id(); + let peer_id_bytes = peer_id.clone().into_bytes(); + let multiaddr = + Multiaddr::from_str(&format!("{}/p2p/{}", addr, peer_id)).expect("valid multiaddr"); + let nodes = node.get_network_graph_nodes().await; + assert_eq!(nodes.len(), 1); + assert_eq!(nodes[0].node_id, node.get_public_key()); + assert!(nodes[0].addresses.clone().iter_mut().all(|multiaddr| { + match multiaddr.pop() { + Some(Protocol::P2P(peer_id)) => peer_id.as_ref() == peer_id_bytes.as_slice(), + _ => false, + } + })); + assert_eq!( + nodes[0].addresses.iter().find(|x| *x == &multiaddr), + Some(&multiaddr) + ); +} + #[tokio::test] async fn test_sync_channel_announcement_on_startup() { init_tracing(); diff --git a/src/fiber/tests/test_utils.rs b/src/fiber/tests/test_utils.rs index e685619fb..59b3d2313 100644 --- a/src/fiber/tests/test_utils.rs +++ b/src/fiber/tests/test_utils.rs @@ -466,6 +466,10 @@ impl NetworkNode { self.private_key.pubkey() } + pub fn get_peer_id(&self) -> PeerId { + self.private_key.pubkey().tentacle_peer_id() + } + pub fn get_node_address(&self) -> &MultiAddr { &self.listening_addrs[0] }