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
|
|
|
|
|
use multiaddr::Multiaddr;
|
2023-02-20 12:44:55 +01:00
|
|
|
use std::ffi::CString;
|
2022-10-03 15:21:19 +02:00
|
|
|
// crates
|
2023-11-02 13:59:41 -04:00
|
|
|
use libc::*;
|
2022-10-03 15:21:19 +02:00
|
|
|
// internal
|
|
|
|
|
use super::config::WakuNodeConfig;
|
2023-02-20 12:44:55 +01:00
|
|
|
use crate::general::{PeerId, Result};
|
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)
|
2023-11-02 13:59:41 -04:00
|
|
|
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<()> {
|
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();
|
|
|
|
|
let code = unsafe {
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
handle_no_response(code, &error)
|
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)
|
2023-11-02 13:59:41 -04:00
|
|
|
pub fn waku_start() -> Result<()> {
|
|
|
|
|
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);
|
|
|
|
|
waku_sys::waku_start(cb, &mut closure as *mut _ as *mut c_void)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
2023-11-02 13:59:41 -04:00
|
|
|
pub fn waku_stop() -> Result<()> {
|
|
|
|
|
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);
|
|
|
|
|
waku_sys::waku_stop(cb, &mut closure as *mut _ as *mut c_void)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_no_response(code, &error)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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)
|
2022-10-06 15:51:00 +02:00
|
|
|
pub fn waku_peer_id() -> Result<PeerId> {
|
2023-11-02 13:59: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);
|
|
|
|
|
waku_sys::waku_peerid(cb, &mut closure as *mut _ as *mut c_void)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_response(code, &result)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the multiaddresses the Waku node is listening to
|
|
|
|
|
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
2022-10-06 15:51:00 +02:00
|
|
|
pub fn waku_listen_addresses() -> Result<Vec<Multiaddr>> {
|
2023-11-02 13:59: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);
|
|
|
|
|
waku_sys::waku_listen_addresses(cb, &mut closure as *mut _ as *mut c_void)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handle_json_response(code, &result)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::waku_new;
|
2022-10-06 15:51:00 +02:00
|
|
|
use crate::node::management::{waku_listen_addresses, waku_peer_id, waku_start, waku_stop};
|
2023-10-01 15:40:33 -04:00
|
|
|
use crate::node::peers::waku_peer_count;
|
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() {
|
|
|
|
|
waku_new(None).unwrap();
|
|
|
|
|
waku_start().unwrap();
|
|
|
|
|
// test peer id call, since we cannot start different instances of the node
|
|
|
|
|
let id = waku_peer_id().unwrap();
|
|
|
|
|
dbg!(&id);
|
|
|
|
|
assert!(!id.is_empty());
|
|
|
|
|
|
2023-10-01 15:40:33 -04:00
|
|
|
let peer_cnt = waku_peer_count().unwrap();
|
|
|
|
|
dbg!(peer_cnt);
|
|
|
|
|
|
2022-10-03 15:21:19 +02:00
|
|
|
// test addresses, since we cannot start different instances of the node
|
2022-10-06 15:51:00 +02:00
|
|
|
let addresses = waku_listen_addresses().unwrap();
|
2022-10-03 15:21:19 +02:00
|
|
|
dbg!(&addresses);
|
|
|
|
|
assert!(!addresses.is_empty());
|
|
|
|
|
|
|
|
|
|
waku_stop().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|