From 710db3aa079f6bfb4178455ed935630f8ffc8f92 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Thu, 29 Sep 2022 18:46:05 +0200 Subject: [PATCH] Implemented waku management functions --- waku/src/general/mod.rs | 6 +- waku/src/node_management/node.rs | 116 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index edf2a50..43e1b27 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -18,10 +18,10 @@ pub(crate) enum JsonResponse { } /// Waku response, just a `Result` with an `String` error. -/// Convenient we can transform a [`JsonResponse`] into a [`Response`] (`Result`) -type Response = Result; +/// Convenient we can transform a [`JsonResponse`] into a [`std::result::Result`] +pub type Result = std::result::Result; -impl From> for Response { +impl From> for Result { fn from(response: JsonResponse) -> Self { match response { JsonResponse::Result(t) => Ok(t), diff --git a/waku/src/node_management/node.rs b/waku/src/node_management/node.rs index e69de29..7366e1a 100644 --- a/waku/src/node_management/node.rs +++ b/waku/src/node_management/node.rs @@ -0,0 +1,116 @@ +// std +use multiaddr::Multiaddr; +use std::ffi::{CStr, CString}; +// crates +// internal +use super::config::NodeConfig; +use crate::general::{JsonResponse, Result}; + +/// Instantiates a Waku node +/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig) +pub fn waku_new(config: &NodeConfig) -> Result { + let s_config = serde_json::to_string(config) + .expect("Serialization from properly built NodeConfig should never fail"); + let result: &str = unsafe { + CStr::from_ptr(waku_sys::waku_new( + CString::new(s_config) + .expect("CString should build properly from the serialized node config") + .into_raw(), + )) + } + .to_str() + .expect("Response should always succeed to load to a &str"); + let json_response: JsonResponse = + serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + json_response.into() +} + +/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation. +/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start) +pub fn waku_start() -> Result { + let response = unsafe { CStr::from_ptr(waku_sys::waku_start()) } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let json_response: JsonResponse = + serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + json_response.into() +} + +/// Stops a Waku node +/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) +pub fn waku_stop() -> Result { + let response = unsafe { CStr::from_ptr(waku_sys::waku_start()) } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let json_response: JsonResponse = + serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + json_response.into() +} + +/// 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 { + let response = unsafe { CStr::from_ptr(waku_sys::waku_peerid()) } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let json_response: JsonResponse = + serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + + json_response.into() +} + +/// 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> { + let response = unsafe { CStr::from_ptr(waku_sys::waku_listen_addresses()) } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let json_response: JsonResponse> = + serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + + json_response.into() +} + +#[cfg(test)] +mod test { + use super::waku_new; + use crate::node_management::node::{ + waku_listen_addressses, waku_peer_id, waku_start, waku_stop, + }; + + #[test] + fn waku_new_default() { + let config = Default::default(); + assert!(waku_new(&config).unwrap()); + } + + #[test] + fn waku_flow() { + let config = Default::default(); + waku_new(&config).unwrap(); + waku_start().unwrap(); + waku_stop().unwrap(); + } + + #[test] + fn waku_id() { + let config = Default::default(); + waku_new(&config).unwrap(); + let id = waku_peer_id().unwrap(); + dbg!(&id); + assert!(!id.is_empty()); + } + + #[test] + fn waku_address() { + let config = Default::default(); + waku_new(&config).unwrap(); + let addresses = waku_listen_addressses().unwrap(); + dbg!(&addresses); + assert!(!addresses.is_empty()); + } +}