mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-07 00:13:10 +00:00
refactor: node handle constructor and messageId on publish
This commit is contained in:
parent
a10a5c2d22
commit
cdfac7fdfb
@ -12,7 +12,7 @@ mod utils;
|
||||
use rln;
|
||||
|
||||
pub use node::{
|
||||
waku_create_content_topic, waku_new, Event, Key, Multiaddr, PublicKey, SecretKey,
|
||||
waku_create_content_topic, Event, Key, Multiaddr, PublicKey, SecretKey,
|
||||
WakuMessageEvent, WakuNodeConfig, WakuNodeHandle,
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ pub use multiaddr::Multiaddr;
|
||||
pub use secp256k1::{PublicKey, SecretKey};
|
||||
use std::time::Duration;
|
||||
// internal
|
||||
use crate::general::{Result, WakuMessage};
|
||||
use crate::general::{MessageId, Result, WakuMessage};
|
||||
use context::WakuNodeContext;
|
||||
|
||||
pub use config::WakuNodeConfig;
|
||||
@ -26,6 +26,15 @@ pub struct WakuNodeHandle {
|
||||
}
|
||||
|
||||
impl WakuNodeHandle {
|
||||
/// Spawn a new Waku node with the given configuration (default configuration if `None` provided)
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
||||
pub fn new(config: Option<WakuNodeConfig>) -> Result<WakuNodeHandle> {
|
||||
Ok(WakuNodeHandle {
|
||||
ctx: management::waku_new(config)?,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/// 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 start(&self) -> Result<()> {
|
||||
@ -66,7 +75,7 @@ impl WakuNodeHandle {
|
||||
message: &WakuMessage,
|
||||
pubsub_topic: &String,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<()> {
|
||||
) -> Result<MessageId> {
|
||||
relay::waku_relay_publish_message(&self.ctx, message, pubsub_topic, timeout)
|
||||
}
|
||||
|
||||
@ -84,11 +93,3 @@ impl WakuNodeHandle {
|
||||
events::waku_set_event_callback(&self.ctx, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a new Waku node with the given configuration (default configuration if `None` provided)
|
||||
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
||||
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeHandle> {
|
||||
Ok(WakuNodeHandle {
|
||||
ctx: management::waku_new(config)?,
|
||||
})
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ use std::time::Duration;
|
||||
// crates
|
||||
use libc::*;
|
||||
// internal
|
||||
use crate::general::{Encoding, Result, WakuContentTopic, WakuMessage};
|
||||
use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage};
|
||||
use crate::node::context::WakuNodeContext;
|
||||
use crate::utils::{get_trampoline, handle_no_response, handle_response, LibwakuResponse};
|
||||
|
||||
@ -62,7 +62,7 @@ pub fn waku_relay_publish_message(
|
||||
message: &WakuMessage,
|
||||
pubsub_topic: &String,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<()> {
|
||||
) -> Result<MessageId> {
|
||||
let pubsub_topic = pubsub_topic.to_string();
|
||||
|
||||
let message_ptr = CString::new(
|
||||
@ -102,7 +102,7 @@ pub fn waku_relay_publish_message(
|
||||
out
|
||||
};
|
||||
|
||||
handle_no_response(code, result)
|
||||
handle_response(code, result)
|
||||
}
|
||||
|
||||
pub fn waku_relay_subscribe(ctx: &WakuNodeContext, pubsub_topic: &String) -> Result<()> {
|
||||
|
||||
@ -2,12 +2,12 @@ use secp256k1::SecretKey;
|
||||
use serial_test::serial;
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{str::from_utf8};
|
||||
use std::{collections::HashSet, str::from_utf8};
|
||||
use tokio::sync::broadcast::{self, Sender};
|
||||
use tokio::time;
|
||||
use tokio::time::sleep;
|
||||
use waku_bindings::{
|
||||
waku_new, Encoding, Event, MessageId, WakuContentTopic, WakuMessage, WakuNodeConfig,
|
||||
Encoding, Event, MessageId, WakuContentTopic, WakuMessage, WakuNodeConfig,
|
||||
WakuNodeHandle,
|
||||
};
|
||||
const ECHO_TIMEOUT: u64 = 10;
|
||||
@ -17,10 +17,11 @@ const TEST_PUBSUBTOPIC: &str = "test";
|
||||
fn try_publish_relay_messages(
|
||||
node: &WakuNodeHandle,
|
||||
msg: &WakuMessage,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<HashSet<MessageId>, String> {
|
||||
let topic = TEST_PUBSUBTOPIC.to_string();
|
||||
Ok(node.relay_publish_message(msg, &topic, None)?)
|
||||
}
|
||||
Ok(HashSet::from([
|
||||
node.relay_publish_message(msg, &topic, None)?
|
||||
]))}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Response {
|
||||
@ -69,22 +70,27 @@ async fn test_echo_messages(
|
||||
let (tx, mut rx) = broadcast::channel(1);
|
||||
set_callback(node2, tx);
|
||||
|
||||
try_publish_relay_messages(node1, &message).expect("send relay messages");
|
||||
|
||||
let mut ids = try_publish_relay_messages(node1, &message).expect("send relay messages");
|
||||
while let Ok(res) = rx.recv().await {
|
||||
assert!(!res.id.is_empty());
|
||||
from_utf8(&res.payload).expect("should be valid message");
|
||||
if ids.take(&res.id).is_some() {
|
||||
let msg = from_utf8(&res.payload).expect("should be valid message");
|
||||
assert_eq!(content, msg);
|
||||
}
|
||||
|
||||
if ids.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn default_echo() -> Result<(), String> {
|
||||
let node1 = waku_new(Some(WakuNodeConfig {
|
||||
let node1 = WakuNodeHandle::new(Some(WakuNodeConfig {
|
||||
port: Some(60010),
|
||||
..Default::default()
|
||||
}))?;
|
||||
let node2 = waku_new(Some(WakuNodeConfig {
|
||||
let node2 = WakuNodeHandle::new(Some(WakuNodeConfig {
|
||||
port: Some(60020),
|
||||
..Default::default()
|
||||
}))?;
|
||||
@ -134,7 +140,7 @@ fn node_restart() {
|
||||
};
|
||||
|
||||
for _ in 0..3 {
|
||||
let node = waku_new(config.clone().into()).expect("default config should be valid");
|
||||
let node = WakuNodeHandle::new(config.clone().into()).expect("default config should be valid");
|
||||
|
||||
node.start().expect("node should start with valid config");
|
||||
|
||||
|
||||
@ -1 +1 @@
|
||||
Subproject commit 7aea145efe37dd3b8e5d623807016b1c82e97695
|
||||
Subproject commit d8e379be6a0483e865f360e8104bba00cb1a1549
|
||||
Loading…
x
Reference in New Issue
Block a user