Add docs to node methods

This commit is contained in:
Daniel Sanchez Quiros 2022-10-05 15:39:46 +02:00
parent e5ad24ec8b
commit 7e5d060032
3 changed files with 71 additions and 14 deletions

View File

@ -4,7 +4,7 @@ use std::ffi::{CStr, CString};
// crates
// internal
use super::config::WakuNodeConfig;
use crate::general::{JsonResponse, Result};
use crate::general::{JsonResponse, PeerId, Result};
/// Instantiates a Waku node
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
@ -52,7 +52,7 @@ pub fn waku_stop() -> Result<bool> {
/// If the execution is successful, the result is the peer ID as a string (base58 encoded)
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
pub fn waku_peer_id() -> Result<String> {
pub fn waku_peer_id() -> Result<PeerId> {
let response = unsafe { CStr::from_ptr(waku_sys::waku_peerid()) }
.to_str()
.expect("Response should always succeed to load to a &str");
@ -65,7 +65,7 @@ pub fn waku_peer_id() -> Result<String> {
/// Get the multiaddresses the Waku node is listening to
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
pub fn waku_listen_addressses() -> Result<Vec<Multiaddr>> {
pub fn waku_listen_addresses() -> Result<Vec<Multiaddr>> {
let response = unsafe { CStr::from_ptr(waku_sys::waku_listen_addresses()) }
.to_str()
.expect("Response should always succeed to load to a &str");
@ -79,7 +79,7 @@ pub fn waku_listen_addressses() -> Result<Vec<Multiaddr>> {
#[cfg(test)]
mod test {
use super::waku_new;
use crate::node::management::{waku_listen_addressses, waku_peer_id, waku_start, waku_stop};
use crate::node::management::{waku_listen_addresses, waku_peer_id, waku_start, waku_stop};
#[test]
fn waku_flow() {
@ -91,7 +91,7 @@ mod test {
assert!(!id.is_empty());
// test addresses, since we cannot start different instances of the node
let addresses = waku_listen_addressses().unwrap();
let addresses = waku_listen_addresses().unwrap();
dbg!(&addresses);
assert!(!addresses.is_empty());

View File

@ -36,18 +36,28 @@ impl WakuNodeState for Running {}
pub struct WakuNodeHandle<State: WakuNodeState>(PhantomData<State>);
impl<State: WakuNodeState> WakuNodeHandle<State> {
pub fn peer_id(&self) -> Result<String> {
/// If the execution is successful, the result is the peer ID as a string (base58 encoded)
///
/// wrapper around [`management::waku_peer_id`]
pub fn peer_id(&self) -> Result<PeerId> {
management::waku_peer_id()
}
/// Get the multiaddresses the Waku node is listening to
///
/// wrapper around [`management::waku_listen_addresses`]
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>> {
management::waku_listen_addressses()
management::waku_listen_addresses()
}
/// Add a node multiaddress and protocol to the waku nodes peerstore
///
/// wrapper around [`peers::waku_add_peers`]
pub fn add_peer(&mut self, address: Multiaddr, protocol_id: usize) -> Result<PeerId> {
peers::waku_add_peers(address, protocol_id)
}
}
fn stop_node() -> Result<()> {
let mut node_initialized = WAKU_NODE_INITIALIZED
.lock()
@ -57,20 +67,35 @@ fn stop_node() -> Result<()> {
}
impl WakuNodeHandle<Initialized> {
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation
///
/// wrapper around [`management::waku_start`]
pub fn start(self) -> Result<WakuNodeHandle<Running>> {
management::waku_start().map(|_| WakuNodeHandle(Default::default()))
}
/// Stops a Waku node
///
/// internally uses [`management::waku_stop`]
pub fn stop(self) -> Result<()> {
stop_node()
}
}
impl WakuNodeHandle<Running> {
/// Stops a Waku node
///
/// internally uses [`management::waku_stop`]
pub fn stop(self) -> Result<()> {
stop_node()
}
/// Dial peer using a multiaddress
/// If `timeout` as milliseconds doesn't fit into a `i32` it is clamped to [`i32::MAX`]
/// If the function execution takes longer than `timeout` value, the execution will be canceled and an error returned.
/// Use 0 for no timeout
///
/// wrapper around [`peers::waku_connect_peer_with_address`]
pub fn connect_peer_with_address(
&mut self,
address: Multiaddr,
@ -79,6 +104,9 @@ impl WakuNodeHandle<Running> {
peers::waku_connect_peer_with_address(address, timeout)
}
/// Dial peer using its peer ID
///
/// wrapper around [`peers::waku_connect_peer_with_id`]
pub fn connect_peer_with_id(
&mut self,
peer_id: PeerId,
@ -87,19 +115,31 @@ impl WakuNodeHandle<Running> {
peers::waku_connect_peer_with_id(peer_id, timeout)
}
/// Disconnect a peer using its peerID
///
/// wrapper around [`peers::waku_disconnect_peer_with_id`]
pub fn disconnect_peer_with_id(&mut self, peer_id: PeerId) -> Result<()> {
peers::waku_disconnect_peer_with_id(peer_id)
}
/// Get number of connected peers
///
/// wrapper around [`peers::waku_peer_count`]
pub fn peer_count(&self) -> Result<usize> {
peers::waku_peer_count()
}
/// Retrieve the list of peers known by the Waku node
///
/// wrapper around [`peers::waku_peers`]
pub fn peers(&self) -> Result<WakuPeers> {
peers::waku_peers()
}
pub fn publish_message(
/// Publish a message using Waku Relay
///
/// wrapper around [`relay::waku_relay_publish_message`]
pub fn relay_publish_message(
&mut self,
message: &WakuMessage,
pubsub_topic: Option<WakuPubSubTopic>,
@ -108,7 +148,10 @@ impl WakuNodeHandle<Running> {
relay::waku_relay_publish_message(message, pubsub_topic, timeout)
}
pub fn publish_encrypt_asymmetric(
/// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay
///
/// wrapper around [`relay::waku_relay_publish_encrypt_asymmetric`]
pub fn relay_publish_encrypt_asymmetric(
&mut self,
message: &WakuMessage,
pubsub_topic: Option<WakuPubSubTopic>,
@ -125,7 +168,10 @@ impl WakuNodeHandle<Running> {
)
}
pub fn publish_encrypt_symmetric(
/// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Relay
///
/// wrapper around [`relay::waku_relay_publish_encrypt_symmetric`]
pub fn relay_publish_encrypt_symmetric(
&mut self,
message: &WakuMessage,
pubsub_topic: Option<WakuPubSubTopic>,
@ -142,19 +188,30 @@ impl WakuNodeHandle<Running> {
)
}
pub fn enough_peers(&self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool> {
/// Determine if there are enough peers to publish a message on a given pubsub topic
///
/// wrapper around [`relay::waku_enough_peers`]
pub fn relay_enough_peers(&self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool> {
relay::waku_enough_peers(pubsub_topic)
}
pub fn subscribe(&mut self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {
/// Subscribe to a Waku Relay pubsub topic to receive messages
///
/// wrapper around [`relay::waku_relay_subscribe`]
pub fn relay_subscribe(&mut self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {
relay::waku_relay_subscribe(pubsub_topic)
}
pub fn unsubscribe(&mut self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {
/// Closes the pubsub subscription to a pubsub topic. No more messages will be received from this pubsub topic
///
/// wrapper around [`relay::waku_relay_unsubscribe`]
pub fn relay_unsubscribe(&mut self, pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {
relay::waku_relay_unsubscribe(pubsub_topic)
}
}
/// Spawn a new Waku node with the givent configuration (default configuration if `None` provided)
/// Internally uses [`management::waku_new`]
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeHandle<Initialized>> {
let mut node_initialized = WAKU_NODE_INITIALIZED
.lock()

View File

@ -57,7 +57,7 @@ pub fn waku_connect_peer_with_address(address: Multiaddr, timeout: Option<Durati
/// Dial peer using a peer id
/// If `timeout` as milliseconds doesn't fit into a `i32` it is clamped to [`i32::MAX`]
/// The peer must be already known.
/// It must have been added before with [`waku_add_peer`] or previously dialed with [`waku_connect_peer_with_address`]
/// It must have been added before with [`waku_add_peers`] or previously dialed with [`waku_connect_peer_with_address`]
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peeridchar-peerid-int-timeoutms)
pub fn waku_connect_peer_with_id(peer_id: PeerId, timeout: Option<Duration>) -> Result<()> {
let response = unsafe {