2022-10-09 16:50:40 +02:00
|
|
|
//! Node lifcycle [mangement](https://rfc.vac.dev/spec/36/#node-management) related methods
|
|
|
|
|
|
2022-10-03 15:21:19 +02:00
|
|
|
// std
|
2023-02-20 12:44:55 +01:00
|
|
|
use std::ffi::CString;
|
2022-10-03 15:21:19 +02:00
|
|
|
// crates
|
2024-02-12 20:13:49 -04:00
|
|
|
use libc::c_void;
|
2024-02-21 11:59:40 -04:00
|
|
|
use multiaddr::Multiaddr;
|
2022-10-03 15:21:19 +02:00
|
|
|
// internal
|
|
|
|
|
use super::config::WakuNodeConfig;
|
2024-02-08 17:16:22 -04:00
|
|
|
use crate::general::Result;
|
2024-02-26 11:13:30 -04:00
|
|
|
use crate::node::context::WakuNodeContext;
|
2023-11-02 13:59:41 -04:00
|
|
|
use crate::utils::{get_trampoline, handle_json_response, handle_no_response, handle_response};
|
2022-10-03 15:21:19 +02:00
|
|
|
|
|
|
|
|
/// Instantiates a Waku node
|
|
|
|
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
2024-02-26 11:13:30 -04:00
|
|
|
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeContext> {
|
2022-10-03 15:21:19 +02:00
|
|
|
let config = config.unwrap_or_default();
|
2023-02-14 18:30:08 +01:00
|
|
|
|
|
|
|
|
let config_ptr = CString::new(
|
|
|
|
|
serde_json::to_string(&config)
|
|
|
|
|
.expect("Serialization from properly built NodeConfig should never fail"),
|
|
|
|
|
)
|
|
|
|
|
.expect("CString should build properly from the config")
|
|
|
|
|
.into_raw();
|
|
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
let mut error: String = Default::default();
|
|
|
|
|
let error_cb = |v: &str| error = v.to_string();
|
2024-02-26 11:13:30 -04:00
|
|
|
let obj_ptr = unsafe {
|
2023-11-02 13:59:41 -04:00
|
|
|
let mut closure = error_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
|
|
|
|
let out = waku_sys::waku_new(config_ptr, cb, &mut closure as *mut _ as *mut c_void);
|
|
|
|
|
|
2023-02-14 18:30:08 +01:00
|
|
|
drop(CString::from_raw(config_ptr));
|
2023-11-02 13:59:41 -04:00
|
|
|
|
|
|
|
|
out
|
2023-02-14 18:30:08 +01:00
|
|
|
};
|
|
|
|
|
|
2024-02-21 14:07:42 -04:00
|
|
|
if !error.is_empty() {
|
2024-02-21 11:59:40 -04:00
|
|
|
Err(error)
|
|
|
|
|
} else {
|
2024-02-26 11:13:30 -04:00
|
|
|
Ok(WakuNodeContext { obj_ptr })
|
2024-02-13 16:18:16 -04:00
|
|
|
}
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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)
|
2024-02-26 11:13:30 -04:00
|
|
|
pub fn waku_start(ctx: &WakuNodeContext) -> Result<()> {
|
2023-11-02 13:59:41 -04:00
|
|
|
let mut error: String = Default::default();
|
|
|
|
|
let error_cb = |v: &str| error = v.to_string();
|
|
|
|
|
let code = unsafe {
|
|
|
|
|
let mut closure = error_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_sys::waku_start(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
2023-11-02 13:59:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_no_response(code, &error)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Stops a Waku node
|
|
|
|
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
2024-02-26 11:13:30 -04:00
|
|
|
pub fn waku_stop(ctx: &WakuNodeContext) -> Result<()> {
|
2023-11-02 13:59:41 -04:00
|
|
|
let mut error: String = Default::default();
|
|
|
|
|
let error_cb = |v: &str| error = v.to_string();
|
|
|
|
|
let code = unsafe {
|
|
|
|
|
let mut closure = error_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_sys::waku_stop(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
2023-11-02 13:59:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_no_response(code, &error)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-19 17:04:41 -04:00
|
|
|
/// nwaku version
|
|
|
|
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
2024-02-26 11:13:30 -04:00
|
|
|
pub fn waku_version(ctx: &WakuNodeContext) -> Result<String> {
|
2024-02-19 17:04:41 -04:00
|
|
|
let mut result: String = Default::default();
|
|
|
|
|
let result_cb = |v: &str| result = v.to_string();
|
|
|
|
|
let code = unsafe {
|
|
|
|
|
let mut closure = result_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_sys::waku_version(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
2024-02-19 17:04:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_response(code, &result)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-21 11:59:40 -04:00
|
|
|
/// Get the multiaddresses the Waku node is listening to
|
|
|
|
|
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
2024-02-26 11:13:30 -04:00
|
|
|
pub fn waku_listen_addresses(ctx: &WakuNodeContext) -> Result<Vec<Multiaddr>> {
|
2024-02-21 11:59:40 -04:00
|
|
|
let mut result: String = Default::default();
|
|
|
|
|
let result_cb = |v: &str| result = v.to_string();
|
|
|
|
|
let code = unsafe {
|
|
|
|
|
let mut closure = result_cb;
|
|
|
|
|
let cb = get_trampoline(&closure);
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_sys::waku_listen_addresses(ctx.obj_ptr, cb, &mut closure as *mut _ as *mut c_void)
|
2024-02-21 11:59:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_json_response(code, &result)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 15:21:19 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::waku_new;
|
2024-02-21 14:07:42 -04:00
|
|
|
use crate::node::management::{waku_listen_addresses, waku_start, waku_stop, waku_version};
|
2023-01-05 17:53:49 +02:00
|
|
|
use serial_test::serial;
|
2022-10-03 15:21:19 +02:00
|
|
|
|
|
|
|
|
#[test]
|
2023-01-05 17:53:49 +02:00
|
|
|
#[serial]
|
2022-10-03 15:21:19 +02:00
|
|
|
fn waku_flow() {
|
2024-02-19 17:04:41 -04:00
|
|
|
let node = waku_new(None).unwrap();
|
|
|
|
|
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_start(&node).unwrap();
|
2024-02-19 17:04:41 -04:00
|
|
|
|
2024-02-21 14:07:42 -04:00
|
|
|
// test addresses
|
2024-02-26 11:13:30 -04:00
|
|
|
let addresses = waku_listen_addresses(&node).unwrap();
|
2024-02-21 14:07:42 -04:00
|
|
|
dbg!(&addresses);
|
|
|
|
|
assert!(!addresses.is_empty());
|
2024-02-21 11:59:40 -04:00
|
|
|
|
2024-02-26 11:13:30 -04:00
|
|
|
waku_stop(&node).unwrap();
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
2024-02-19 17:04:41 -04:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[serial]
|
|
|
|
|
fn nwaku_version() {
|
|
|
|
|
let node = waku_new(None).unwrap();
|
2024-02-26 11:13:30 -04:00
|
|
|
let version = waku_version(&node).expect("should return the version");
|
2024-02-19 17:04:41 -04:00
|
|
|
assert!(!version.is_empty());
|
|
|
|
|
}
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|