From 9c696e6097e6fb75d46552cfb88cb46490b619b3 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 19 Feb 2024 17:04:41 -0400 Subject: [PATCH] feat: expose waku_version --- Cargo.lock | 188 ++------------------------- waku-bindings/src/lib.rs | 4 +- waku-bindings/src/node/config.rs | 16 --- waku-bindings/src/node/events.rs | 11 -- waku-bindings/src/node/management.rs | 40 ++++-- waku-bindings/src/node/mod.rs | 7 +- waku-bindings/src/node/relay.rs | 14 -- waku-bindings/tests/node.rs | 13 +- waku-sys/Cargo.toml | 3 +- waku-sys/vendor | 2 +- 10 files changed, 52 insertions(+), 246 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1cea34a..7d284b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -93,12 +93,6 @@ dependencies = [ "libc", ] -[[package]] -name = "anyhow" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" - [[package]] name = "ark-bn254" version = "0.4.0" @@ -375,6 +369,14 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" +[[package]] +name = "basic" +version = "0.1.0" +dependencies = [ + "signal-hook", + "waku-bindings", +] + [[package]] name = "bindgen" version = "0.64.0" @@ -485,12 +487,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" -[[package]] -name = "cassowary" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" - [[package]] name = "cast" version = "0.3.0" @@ -525,11 +521,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ "iana-time-zone", - "js-sys", "num-integer", "num-traits", - "time", - "wasm-bindgen", "winapi", ] @@ -794,31 +787,6 @@ version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" -[[package]] -name = "crossterm" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" -dependencies = [ - "bitflags 1.3.2", - "crossterm_winapi", - "libc", - "mio", - "parking_lot 0.12.1", - "signal-hook", - "signal-hook-mio", - "winapi", -] - -[[package]] -name = "crossterm_winapi" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c" -dependencies = [ - "winapi", -] - [[package]] name = "crunchy" version = "0.2.2" @@ -1315,7 +1283,7 @@ checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", ] [[package]] @@ -1695,18 +1663,6 @@ dependencies = [ "adler", ] -[[package]] -name = "mio" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" -dependencies = [ - "libc", - "log", - "wasi 0.11.0+wasi-snapshot-preview1", - "windows-sys 0.36.1", -] - [[package]] name = "more-asserts" version = "0.2.2" @@ -2086,29 +2042,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "prost" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "399c3c31cdec40583bb68f0b18403400d01ec4289c383aa047560439952c4dd7" -dependencies = [ - "bytes", - "prost-derive", -] - -[[package]] -name = "prost-derive" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7345d5f0e08c0536d7ac7229952590239e77abf0a0100a1b1d890add6ea96364" -dependencies = [ - "anyhow", - "itertools", - "proc-macro2", - "quote", - "syn 1.0.100", -] - [[package]] name = "ptr_meta" version = "0.1.4" @@ -2596,21 +2529,11 @@ version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a253b5e89e2698464fc26b545c9edceb338e18a89effeeecfea192c3025be29d" dependencies = [ + "cc", "libc", "signal-hook-registry", ] -[[package]] -name = "signal-hook-mio" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" -dependencies = [ - "libc", - "mio", - "signal-hook", -] - [[package]] name = "signal-hook-registry" version = "1.4.0" @@ -2844,17 +2767,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", -] - [[package]] name = "tiny-keccak" version = "2.0.2" @@ -2921,18 +2833,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toy-chat" -version = "0.1.0" -dependencies = [ - "chrono", - "crossterm", - "prost", - "tui", - "unicode-width", - "waku-bindings", -] - [[package]] name = "tracing" version = "0.1.40" @@ -2996,19 +2896,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tui" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccdd26cbd674007e649a272da4475fb666d3aa0ad0531da7136db6fab0e5bad1" -dependencies = [ - "bitflags 1.3.2", - "cassowary", - "crossterm", - "unicode-segmentation", - "unicode-width", -] - [[package]] name = "typenum" version = "1.15.0" @@ -3036,12 +2923,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fdbf052a0783de01e944a6ce7a8cb939e295b1e7be835a1112c3b9a7f047a5a" - [[package]] name = "unicode-width" version = "0.1.10" @@ -3143,12 +3024,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3541,19 +3416,6 @@ dependencies = [ "windows_x86_64_msvc 0.33.0", ] -[[package]] -name = "windows-sys" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" -dependencies = [ - "windows_aarch64_msvc 0.36.1", - "windows_i686_gnu 0.36.1", - "windows_i686_msvc 0.36.1", - "windows_x86_64_gnu 0.36.1", - "windows_x86_64_msvc 0.36.1", -] - [[package]] name = "windows-sys" version = "0.42.0" @@ -3611,12 +3473,6 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd761fd3eb9ab8cc1ed81e56e567f02dd82c4c837e48ac3b2181b9ffc5060807" -[[package]] -name = "windows_aarch64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" - [[package]] name = "windows_aarch64_msvc" version = "0.42.0" @@ -3635,12 +3491,6 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cab0cf703a96bab2dc0c02c0fa748491294bf9b7feb27e1f4f96340f208ada0e" -[[package]] -name = "windows_i686_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" - [[package]] name = "windows_i686_gnu" version = "0.42.0" @@ -3659,12 +3509,6 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cfdbe89cc9ad7ce618ba34abc34bbb6c36d99e96cae2245b7943cd75ee773d0" -[[package]] -name = "windows_i686_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" - [[package]] name = "windows_i686_msvc" version = "0.42.0" @@ -3683,12 +3527,6 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4dd9b0c0e9ece7bb22e84d70d01b71c6d6248b81a3c60d11869451b4cb24784" -[[package]] -name = "windows_x86_64_gnu" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" - [[package]] name = "windows_x86_64_gnu" version = "0.42.0" @@ -3719,12 +3557,6 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff1e4aa646495048ec7f3ffddc411e1d829c026a2ec62b39da15c1055e406eaa" -[[package]] -name = "windows_x86_64_msvc" -version = "0.36.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" - [[package]] name = "windows_x86_64_msvc" version = "0.42.0" diff --git a/waku-bindings/src/lib.rs b/waku-bindings/src/lib.rs index 0a2e7be..e295340 100644 --- a/waku-bindings/src/lib.rs +++ b/waku-bindings/src/lib.rs @@ -12,8 +12,8 @@ mod utils; use rln; pub use node::{ - waku_create_content_topic, waku_default_pubsub_topic, waku_new, Event, Key, Multiaddr, - PublicKey, SecretKey, WakuMessageEvent, WakuNodeConfig, WakuNodeHandle, + waku_create_content_topic, waku_new, Event, Key, Multiaddr, PublicKey, SecretKey, + WakuMessageEvent, WakuNodeConfig, WakuNodeHandle, }; pub use general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage, WakuMessageVersion}; diff --git a/waku-bindings/src/node/config.rs b/waku-bindings/src/node/config.rs index 8a508ca..a243e78 100644 --- a/waku-bindings/src/node/config.rs +++ b/waku-bindings/src/node/config.rs @@ -18,10 +18,6 @@ pub struct WakuNodeConfig { /// Libp2p TCP listening port. Default `60000`. Use `0` for **random** #[default(Some(60000))] pub port: Option, - /// External address to advertise to other nodes. Can be ip4, ip6 or dns4, dns6. - /// If null, the multiaddress(es) generated from the ip and port specified in the config (or default ones) will be used. - /// Default: null - pub advertise_addr: Option, /// Secp256k1 private key in Hex format (`0x123...abc`). Default random #[serde(with = "secret_key_serde", rename = "key")] pub node_key: Option, @@ -29,18 +25,6 @@ pub struct WakuNodeConfig { #[default(Some(true))] pub relay: Option, pub relay_topics: Vec, - // /// Enable store protocol to persist message history - // #[default(Some(false))] - // pub store: Option, - // /// Url connection string. Accepts SQLite and PostgreSQL connection strings - // #[default(Some("sqlite3://store.db".to_string()))] - // pub database_url: Option, - // /// Max number of messages to store in the databas - // #[default(Some(1000))] - // pub store_retention_max_messages: Option, - // /// Max number of seconds that a message will be persisted in the database, default 1 day - // #[default(Some(86400))] - // pub store_retention_max_seconds: Option, } mod secret_key_serde { diff --git a/waku-bindings/src/node/events.rs b/waku-bindings/src/node/events.rs index 30e92d2..b302873 100644 --- a/waku-bindings/src/node/events.rs +++ b/waku-bindings/src/node/events.rs @@ -12,9 +12,6 @@ use serde::{Deserialize, Serialize}; use crate::general::WakuMessage; use crate::utils::get_trampoline; use crate::MessageId; -use core::str::FromStr; - -use waku_sys::WakuCallBack; /// Waku event /// For now just WakuMessage is supported @@ -58,9 +55,7 @@ impl WakuMessageEvent { pub fn waku_set_event_callback(ctx: *mut c_void, mut f: F) { let cb = |v: &str| { let data: Event = serde_json::from_str(v).expect("Parsing event to succeed"); - println!("EXEC CALLBACK"); f(data); - println!("SUCCESS!"); }; unsafe { @@ -75,12 +70,6 @@ pub fn waku_set_event_callback(ctx: *mut c_void, mod tests { use crate::Event; - // TODO: how to actually send an event and check if the callback is run? - //#[test] - /*fn set_callback() { - callback(|_event| {}); - }*/ - #[test] fn deserialize_message_event() { let s = "{\"eventType\":\"message\",\"messageId\":\"0x26ff3d7fbc950ea2158ce62fd76fd745eee0323c9eac23d0713843b0f04ea27c\",\"pubsubTopic\":\"/waku/2/default-waku/proto\",\"wakuMessage\":{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660}}"; diff --git a/waku-bindings/src/node/management.rs b/waku-bindings/src/node/management.rs index 2d43dee..8bc53df 100644 --- a/waku-bindings/src/node/management.rs +++ b/waku-bindings/src/node/management.rs @@ -70,29 +70,41 @@ pub fn waku_stop(ctx: *mut c_void) -> Result<()> { handle_no_response(code, &error) } +/// nwaku version +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub fn waku_version(ctx: *mut c_void) -> Result { + 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_version(ctx, cb, &mut closure as *mut _ as *mut c_void) + }; + + handle_response(code, &result) +} + #[cfg(test)] mod test { use super::waku_new; - use crate::node::management::{waku_start, waku_stop}; - use crate::WakuNodeConfig; - use secp256k1::SecretKey; + use crate::node::management::{waku_start, waku_stop, waku_version}; use serial_test::serial; - use std::str::FromStr; #[test] #[serial] fn waku_flow() { - let node = waku_new(Some(WakuNodeConfig { - node_key: Some( - SecretKey::from_str( - "05f381866cc21f6c1e2e80e07fa732008e36d942dce3206ad6dcd6793c98d609", - ) - .unwrap(), - ), // TODO: consider making this optional - ..Default::default() - })) - .unwrap(); + let node = waku_new(None).unwrap(); + waku_start(node).unwrap(); + waku_stop(node).unwrap(); } + + #[test] + #[serial] + fn nwaku_version() { + let node = waku_new(None).unwrap(); + let version = waku_version(node).expect("should return the version"); + assert!(!version.is_empty()); + } } diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index cd27dd7..cf5c2c7 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -19,7 +19,7 @@ use crate::general::{MessageId, Result, WakuMessage}; pub use config::WakuNodeConfig; pub use events::{Event, WakuMessageEvent}; -pub use relay::{waku_create_content_topic, waku_default_pubsub_topic}; +pub use relay::waku_create_content_topic; /// Handle to the underliying waku node pub struct WakuNodeHandle { @@ -39,6 +39,11 @@ impl WakuNodeHandle { management::waku_stop(self.ctx) } + /// Get the nwaku version + pub fn version(&self) -> Result { + management::waku_version(self.ctx) + } + /// 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. diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index 9339e50..fa15a7b 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -55,20 +55,6 @@ pub fn waku_create_content_topic( .expect("&str from result should always be extracted") } -/// Default pubsub topic used for exchanging waku messages defined in [RFC 10](https://rfc.vac.dev/spec/10/) -#[allow(clippy::not_unsafe_ptr_arg_deref)] -pub fn waku_default_pubsub_topic(ctx: *mut c_void) -> String { - 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_default_pubsub_topic(ctx, cb, &mut closure as *mut _ as *mut c_void) - }; - - handle_response(code, &result).expect("&str from result should always be extracted") -} - /// Publish a message using Waku Relay /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms) pub fn waku_relay_publish_message( diff --git a/waku-bindings/tests/node.rs b/waku-bindings/tests/node.rs index e9e0f21..ccd7b0a 100644 --- a/waku-bindings/tests/node.rs +++ b/waku-bindings/tests/node.rs @@ -13,6 +13,7 @@ use waku_bindings::{ const ECHO_TIMEOUT: u64 = 10; const ECHO_MESSAGE: &str = "Hi from 🦀!"; +const TEST_PUBSUBTOPIC: &str = "test"; const NODES: &[&str] = &["/dns4/node-01.do-ams3.status.prod.statusim.net/tcp/30303/p2p/16Uiu2HAm6HZZr7aToTvEBPpiys4UxajCTU97zj5v7RNR2gbniy1D"]; @@ -20,7 +21,7 @@ fn try_publish_relay_messages( node: &WakuNodeHandle, msg: &WakuMessage, ) -> Result, String> { - let topic = "test".to_string(); + let topic = TEST_PUBSUBTOPIC.to_string(); node.relay_publish_message(msg, &topic, None)?; node.relay_publish_message(msg, &topic, None)?; @@ -42,15 +43,11 @@ fn set_callback(node: &WakuNodeHandle, tx: Sender) { let message = message.waku_message(); let payload = message.payload().to_vec(); - println!("==============="); - println!("ID: {}", id); - println!("Sending to channel...."); tx.send(Response { id: id.to_string(), payload, }) .expect("send response to the receiver"); - println!("Sent!"); } }); } @@ -98,7 +95,7 @@ async fn default_echo() -> Result<(), String> { node_key: Some( SecretKey::from_str("05f381866cc21f6c1e2e80e07fa732008e36d942dce3206ad6dcd6793c98d609") .unwrap(), - ), // TODO: consider making this optional + ), ..Default::default() }; @@ -112,7 +109,7 @@ async fn default_echo() -> Result<(), String> { } // subscribe to default channel - let topic = "test".to_string(); + let topic = TEST_PUBSUBTOPIC.to_string(); node.relay_subscribe(&topic)?; @@ -140,7 +137,7 @@ fn node_restart() { node_key: Some( SecretKey::from_str("05f381866cc21f6c1e2e80e07fa732008e36d942dce3206ad6dcd6793c98d609") .unwrap(), - ), // TODO: consider making this optional + ), ..Default::default() }; diff --git a/waku-sys/Cargo.toml b/waku-sys/Cargo.toml index cfde744..07fe377 100644 --- a/waku-sys/Cargo.toml +++ b/waku-sys/Cargo.toml @@ -3,7 +3,8 @@ name = "waku-sys" version = "0.5.0" edition = "2021" authors = [ - "Daniel Sanchez Quiros " + "Daniel Sanchez Quiros ", + "Richard Ramos " ] description = "Waku networking library generated bindings" license = "MIT OR Apache-2.0" diff --git a/waku-sys/vendor b/waku-sys/vendor index 9d96222..d01585e 160000 --- a/waku-sys/vendor +++ b/waku-sys/vendor @@ -1 +1 @@ -Subproject commit 9d9622265b7382e9f1a2b9f7f1b68c6beb93a0af +Subproject commit d01585e9facafcdf3b53b394ee94753982269c0f