From 29c9cd9f6c7d936921e4111dcf3494cd13062d2f Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Sun, 9 Oct 2022 17:08:39 -0500 Subject: [PATCH] Fix peer id connect --- waku/src/node/mod.rs | 4 ++-- waku/src/node/peers.rs | 9 ++++++--- waku/tests/node.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 waku/tests/node.rs diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 9f86ff2..c482bda 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -73,7 +73,7 @@ impl WakuNodeHandle { /// Add a node multiaddress and protocol to the waku node’s peerstore /// /// wrapper around [`peers::waku_add_peers`] - pub fn add_peer(&self, address: Multiaddr, protocol_id: ProtocolId) -> Result { + pub fn add_peer(&self, address: &Multiaddr, protocol_id: ProtocolId) -> Result { peers::waku_add_peers(address, protocol_id) } } @@ -118,7 +118,7 @@ impl WakuNodeHandle { /// wrapper around [`peers::waku_connect_peer_with_address`] pub fn connect_peer_with_address( &self, - address: Multiaddr, + address: &Multiaddr, timeout: Option, ) -> Result<()> { peers::waku_connect_peer_with_address(address, timeout) diff --git a/waku/src/node/peers.rs b/waku/src/node/peers.rs index 3be8f94..b0be9be 100644 --- a/waku/src/node/peers.rs +++ b/waku/src/node/peers.rs @@ -11,7 +11,7 @@ use crate::general::{JsonResponse, PeerId, ProtocolId, Result}; /// Add a node multiaddress and protocol to the waku node’s peerstore. /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid) -pub fn waku_add_peers(address: Multiaddr, protocol_id: ProtocolId) -> Result { +pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result { let response = unsafe { CStr::from_ptr(waku_sys::waku_add_peer( CString::new(address.to_string()) @@ -36,7 +36,10 @@ pub fn waku_add_peers(address: Multiaddr, protocol_id: ProtocolId) -> Result) -> Result<()> { +pub fn waku_connect_peer_with_address( + address: &Multiaddr, + timeout: Option, +) -> Result<()> { let response = unsafe { CStr::from_ptr(waku_sys::waku_connect( CString::new(address.to_string()) @@ -63,7 +66,7 @@ pub fn waku_connect_peer_with_address(address: Multiaddr, timeout: Option) -> Result<()> { let response = unsafe { - CStr::from_ptr(waku_sys::waku_connect( + CStr::from_ptr(waku_sys::waku_connect_peerid( CString::new(peer_id) .expect("CString should build properly from peer id") .into_raw(), diff --git a/waku/tests/node.rs b/waku/tests/node.rs new file mode 100644 index 0000000..01cc773 --- /dev/null +++ b/waku/tests/node.rs @@ -0,0 +1,36 @@ +use multiaddr::Multiaddr; +use std::net::IpAddr; +use std::str::FromStr; +use waku::{waku_new, ProtocolId, WakuNodeConfig}; + +const NODES: &[&str] = &[ + "/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm", + "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ", + "/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS" +]; + +#[test] +pub fn main() -> Result<(), String> { + let config = WakuNodeConfig { + host: IpAddr::from_str("0.0.0.0").ok(), + port: None, + advertise_addr: None, + node_key: None, + keep_alive_interval: None, + relay: None, + min_peers_to_publish: None, + filter: None, + }; + let node = waku_new(Some(config))?; + let node = node.start()?; + println!("Node peer id: {}", node.peer_id()?); + + for node_address in NODES { + let address: Multiaddr = node_address.parse().unwrap(); + let peer_id = node.add_peer(&address, ProtocolId::Relay)?; + node.connect_peer_with_id(peer_id, None)?; + } + + node.stop()?; + Ok(()) +}