Skip to content

Commit

Permalink
Merge pull request #410 from contrun/fix-duplicated-p2p-postfix-in-an…
Browse files Browse the repository at this point in the history
…nounced-addresses

Fix duplicated p2p postfix in announced addresses
  • Loading branch information
quake authored Dec 20, 2024
2 parents a6b7e80 + d0ae2a9 commit c963248
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/fiber/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2917,9 +2917,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);
}
Expand Down
95 changes: 94 additions & 1 deletion src/fiber/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/fiber/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,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]
}
Expand Down

0 comments on commit c963248

Please sign in to comment.