mirror of
https://github.com/waku-org/waku-rust-bindings.git
synced 2025-01-13 08:04:34 +00:00
Add new changes from go-waku (#58)
* feat: add ephemeral and meta fields * feat: seenTTL * feat: expose list of topics
This commit is contained in:
parent
27597d76b5
commit
ce9f71a592
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -1674,7 +1674,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "waku-bindings"
|
name = "waku-bindings"
|
||||||
version = "0.1.0-rc.2"
|
version = "0.1.0-rc.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes-gcm",
|
"aes-gcm",
|
||||||
"base64 0.21.0",
|
"base64 0.21.0",
|
||||||
@ -1697,7 +1697,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "waku-sys"
|
name = "waku-sys"
|
||||||
version = "0.1.0-rc.2"
|
version = "0.1.0-rc.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
]
|
]
|
||||||
|
@ -193,12 +193,15 @@ fn run_app<B: Backend>(
|
|||||||
let message_content: String = app.input.drain(..).collect();
|
let message_content: String = app.input.drain(..).collect();
|
||||||
let message = Chat2Message::new(&app.nick, &message_content);
|
let message = Chat2Message::new(&app.nick, &message_content);
|
||||||
let mut buff = Vec::new();
|
let mut buff = Vec::new();
|
||||||
|
let meta = Vec::new();
|
||||||
Message::encode(&message, &mut buff)?;
|
Message::encode(&message, &mut buff)?;
|
||||||
let waku_message = WakuMessage::new(
|
let waku_message = WakuMessage::new(
|
||||||
buff,
|
buff,
|
||||||
TOY_CHAT_CONTENT_TOPIC.clone(),
|
TOY_CHAT_CONTENT_TOPIC.clone(),
|
||||||
1,
|
1,
|
||||||
Utc::now().timestamp_nanos() as usize,
|
Utc::now().timestamp_nanos() as usize,
|
||||||
|
meta,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
if let Err(e) =
|
if let Err(e) =
|
||||||
app.node_handle
|
app.node_handle
|
||||||
|
@ -89,25 +89,33 @@ pub struct WakuMessage {
|
|||||||
version: WakuMessageVersion,
|
version: WakuMessageVersion,
|
||||||
/// Unix timestamp in nanoseconds
|
/// Unix timestamp in nanoseconds
|
||||||
timestamp: usize,
|
timestamp: usize,
|
||||||
|
#[serde(with = "base64_serde")]
|
||||||
|
meta: Vec<u8>,
|
||||||
|
ephemeral: bool,
|
||||||
// TODO: implement RLN fields
|
// TODO: implement RLN fields
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
_extras: serde_json::Value,
|
_extras: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WakuMessage {
|
impl WakuMessage {
|
||||||
pub fn new<PAYLOAD: AsRef<[u8]>>(
|
pub fn new<PAYLOAD: AsRef<[u8]>, META: AsRef<[u8]>>(
|
||||||
payload: PAYLOAD,
|
payload: PAYLOAD,
|
||||||
content_topic: WakuContentTopic,
|
content_topic: WakuContentTopic,
|
||||||
version: WakuMessageVersion,
|
version: WakuMessageVersion,
|
||||||
timestamp: usize,
|
timestamp: usize,
|
||||||
|
meta: META,
|
||||||
|
ephemeral: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let payload = payload.as_ref().to_vec();
|
let payload = payload.as_ref().to_vec();
|
||||||
|
let meta = meta.as_ref().to_vec();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
payload,
|
payload,
|
||||||
content_topic,
|
content_topic,
|
||||||
version,
|
version,
|
||||||
timestamp,
|
timestamp,
|
||||||
|
meta,
|
||||||
|
ephemeral,
|
||||||
_extras: Default::default(),
|
_extras: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,6 +136,14 @@ impl WakuMessage {
|
|||||||
self.timestamp
|
self.timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn meta(&self) -> &[u8] {
|
||||||
|
&self.meta
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ephemeral(&self) -> bool {
|
||||||
|
self.ephemeral
|
||||||
|
}
|
||||||
|
|
||||||
/// Try decode the message with an expected symmetric key
|
/// Try decode the message with an expected symmetric key
|
||||||
///
|
///
|
||||||
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_symmetricchar-messagejson-char-symmetrickey)
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_symmetricchar-messagejson-char-symmetrickey)
|
||||||
@ -555,7 +571,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize_waku_message() {
|
fn deserialize_waku_message() {
|
||||||
let message = "{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660}";
|
let message = "{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660,\"ephemeral\":true,\"meta\":\"SGkgZnJvbSDwn6aAIQ==\"}";
|
||||||
let _: WakuMessage = serde_json::from_str(message).unwrap();
|
let _: WakuMessage = serde_json::from_str(message).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,6 +190,9 @@ pub struct GossipSubParams {
|
|||||||
/// the router may apply bahavioural penalties.
|
/// the router may apply bahavioural penalties.
|
||||||
#[serde(rename = "iwantFollowupTimeSeconds")]
|
#[serde(rename = "iwantFollowupTimeSeconds")]
|
||||||
pub iwant_followup_time_seconds: Option<i32>,
|
pub iwant_followup_time_seconds: Option<i32>,
|
||||||
|
// Time until a previously seen message ID can be forgotten about.
|
||||||
|
#[serde(rename = "seenMessagesTTLSeconds")]
|
||||||
|
pub seen_messages_ttl_seconds: Option<i32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
|
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
|
||||||
|
@ -212,6 +212,11 @@ impl WakuNodeHandle<Running> {
|
|||||||
relay::waku_relay_unsubscribe(pubsub_topic)
|
relay::waku_relay_unsubscribe(pubsub_topic)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the list of pubsub topics the node is subscribed to in Waku Relay
|
||||||
|
pub fn relay_topics(&self) -> Result<Vec<String>> {
|
||||||
|
relay::waku_relay_topics()
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieves historical messages on specific content topics. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`),
|
/// Retrieves historical messages on specific content topics. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`),
|
||||||
/// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`),
|
/// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`),
|
||||||
/// the node must return messages on a per-page basis and include [`PagingOptions`](`crate::general::PagingOptions`) in the response.
|
/// the node must return messages on a per-page basis and include [`PagingOptions`](`crate::general::PagingOptions`) in the response.
|
||||||
|
@ -97,6 +97,13 @@ pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic {
|
|||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the list of subscribed pubsub topics in Waku Relay.
|
||||||
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_topics)
|
||||||
|
pub fn waku_relay_topics() -> Result<Vec<String>> {
|
||||||
|
let result_ptr = unsafe { waku_sys::waku_relay_topics() };
|
||||||
|
decode_and_free_response(result_ptr)
|
||||||
|
}
|
||||||
|
|
||||||
/// Publish a message using Waku Relay
|
/// 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)
|
/// 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(
|
pub fn waku_relay_publish_message(
|
||||||
|
@ -10,9 +10,9 @@ use std::{collections::HashSet, str::from_utf8};
|
|||||||
use tokio::sync::mpsc::{self, Sender};
|
use tokio::sync::mpsc::{self, Sender};
|
||||||
use tokio::time;
|
use tokio::time;
|
||||||
use waku_bindings::{
|
use waku_bindings::{
|
||||||
waku_new, waku_set_event_callback, Encoding, Event, GossipSubParams, Key, MessageId,
|
waku_dafault_pubsub_topic, waku_new, waku_set_event_callback, Encoding, Event, GossipSubParams,
|
||||||
ProtocolId, Running, WakuContentTopic, WakuLogLevel, WakuMessage, WakuNodeConfig,
|
Key, MessageId, ProtocolId, Running, WakuContentTopic, WakuLogLevel, WakuMessage,
|
||||||
WakuNodeHandle,
|
WakuNodeConfig, WakuNodeHandle, WakuPubSubTopic,
|
||||||
};
|
};
|
||||||
|
|
||||||
const ECHO_TIMEOUT: u64 = 10;
|
const ECHO_TIMEOUT: u64 = 10;
|
||||||
@ -119,6 +119,8 @@ async fn test_echo_messages(
|
|||||||
.as_millis()
|
.as_millis()
|
||||||
.try_into()
|
.try_into()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
Vec::new(),
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
|
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
let (tx, mut rx) = mpsc::channel(1);
|
||||||
@ -177,6 +179,13 @@ async fn discv5_echo() -> Result<(), String> {
|
|||||||
node.relay_subscribe(None)?;
|
node.relay_subscribe(None)?;
|
||||||
let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto);
|
let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto);
|
||||||
|
|
||||||
|
let topics = node.relay_topics()?;
|
||||||
|
let default_topic = waku_dafault_pubsub_topic();
|
||||||
|
assert!(topics.len() == 1);
|
||||||
|
let topic: WakuPubSubTopic = topics[0].parse().unwrap();
|
||||||
|
|
||||||
|
assert!(topic == default_topic);
|
||||||
|
|
||||||
let sleep = time::sleep(Duration::from_secs(ECHO_TIMEOUT));
|
let sleep = time::sleep(Duration::from_secs(ECHO_TIMEOUT));
|
||||||
tokio::pin!(sleep);
|
tokio::pin!(sleep);
|
||||||
|
|
||||||
@ -281,6 +290,7 @@ fn gossipsub_config() -> Result<(), String> {
|
|||||||
max_ihave_length: Some(32),
|
max_ihave_length: Some(32),
|
||||||
max_ihave_messages: Some(8),
|
max_ihave_messages: Some(8),
|
||||||
iwant_followup_time_seconds: Some(120),
|
iwant_followup_time_seconds: Some(120),
|
||||||
|
seen_messages_ttl_seconds: Some(120),
|
||||||
};
|
};
|
||||||
|
|
||||||
let config = WakuNodeConfig {
|
let config = WakuNodeConfig {
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 3c4a863cb7c898a6c551a51288a6badf809fd6f5
|
Subproject commit d9a12bf079a8bbb59ab1df591062b9bb91d3804d
|
Loading…
x
Reference in New Issue
Block a user