Skip to content

Commit

Permalink
fix: content topic should accept strings (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Dec 8, 2023
1 parent dc32f22 commit 5aaafc7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/toy-chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn retrieve_history(
let peer = node_handle
.peers()?
.iter()
.find(|&peer| peer.peer_id() != &self_id)
.cloned()
.find(|peer| peer.peer_id() != &self_id)
.unwrap();

let result = node_handle.store_query(
Expand Down
2 changes: 1 addition & 1 deletion examples/toy-chat/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use prost::Message;
use waku_bindings::{Encoding, WakuContentTopic};

pub static TOY_CHAT_CONTENT_TOPIC: WakuContentTopic =
WakuContentTopic::new("toy-chat", 2, "huilong", Encoding::Proto);
WakuContentTopic::new("toy-chat", "2", "huilong", Encoding::Proto);

#[derive(Clone, Message)]
pub struct Chat2Message {
Expand Down
1 change: 1 addition & 0 deletions waku-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ smart-default = "0.6"
url = "2.3"
waku-sys = { version = "0.5.0", path = "../waku-sys" }
libc = "0.2"
serde-aux = "4.3.1"

[dev-dependencies]
futures = "0.3.25"
Expand Down
14 changes: 8 additions & 6 deletions waku-bindings/src/general/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use aes_gcm::{Aes256Gcm, Key};
use base64::Engine;
use secp256k1::{ecdsa::Signature, PublicKey, SecretKey};
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
use serde_aux::prelude::*;
use sscanf::{scanf, RegexRepresentation};
// internal
use crate::decrypt::{waku_decode_asymmetric, waku_decode_symmetric};
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct WakuMessage {
#[serde(default)]
version: WakuMessageVersion,
/// Unix timestamp in nanoseconds
#[serde(deserialize_with = "deserialize_number_from_string")]
timestamp: usize,
#[serde(with = "base64_serde", default = "Vec::new")]
meta: Vec<u8>,
Expand Down Expand Up @@ -442,21 +444,21 @@ impl RegexRepresentation for Encoding {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WakuContentTopic {
pub application_name: Cow<'static, str>,
pub version: usize,
pub version: Cow<'static, str>,
pub content_topic_name: Cow<'static, str>,
pub encoding: Encoding,
}

impl WakuContentTopic {
pub const fn new(
application_name: &'static str,
version: usize,
version: &'static str,
content_topic_name: &'static str,
encoding: Encoding,
) -> Self {
Self {
application_name: Cow::Borrowed(application_name),
version,
version: Cow::Borrowed(version),
content_topic_name: Cow::Borrowed(content_topic_name),
encoding,
}
Expand All @@ -468,11 +470,11 @@ impl FromStr for WakuContentTopic {

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if let Ok((application_name, version, content_topic_name, encoding)) =
scanf!(s, "/{}/{}/{}/{:/.+?/}", String, usize, String, Encoding)
scanf!(s, "/{}/{}/{}/{:/.+?/}", String, String, String, Encoding)
{
Ok(WakuContentTopic {
application_name: Cow::Owned(application_name),
version,
version: Cow::Owned(version),
content_topic_name: Cow::Owned(content_topic_name),
encoding,
})
Expand Down Expand Up @@ -600,7 +602,7 @@ mod tests {

#[test]
fn encode_decode() {
let content_topic = WakuContentTopic::new("hello", 2, "world", Encoding::Proto);
let content_topic = WakuContentTopic::new("hello", "2", "world", Encoding::Proto);
let message = WakuMessage::new(
"hello",
content_topic,
Expand Down
11 changes: 6 additions & 5 deletions waku-bindings/src/node/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ use crate::utils::{get_trampoline, handle_json_response, handle_no_response, han
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding)
pub fn waku_create_content_topic(
application_name: &str,
application_version: usize,
application_version: &str,
content_topic_name: &str,
encoding: Encoding,
) -> WakuContentTopic {
let application_name_ptr = CString::new(application_name)
.expect("Application name should always transform to CString")
.into_raw();
let application_version = application_version
.try_into()
.expect("Version should fit within an u32");
let application_version_ptr = CString::new(application_version)
.expect("Application version should always transform to CString")
.into_raw();
let content_topic_name_ptr = CString::new(content_topic_name)
.expect("Content topic should always transform to CString")
.into_raw();
Expand All @@ -39,14 +39,15 @@ pub fn waku_create_content_topic(
let cb = get_trampoline(&closure);
let out = waku_sys::waku_content_topic(
application_name_ptr,
application_version,
application_version_ptr,
content_topic_name_ptr,
encoding_ptr,
cb,
&mut closure as *mut _ as *mut c_void,
);

drop(CString::from_raw(application_name_ptr));
drop(CString::from_raw(application_version_ptr));
drop(CString::from_raw(content_topic_name_ptr));
drop(CString::from_raw(encoding_ptr));

Expand Down
4 changes: 2 additions & 2 deletions waku-bindings/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async fn discv5_echo() -> Result<(), String> {
// Subscribe to default channel.
let content_filter = ContentFilter::new(Some(waku_default_pubsub_topic()), vec![]);
node.relay_subscribe(&content_filter)?;
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_default_pubsub_topic();
Expand Down Expand Up @@ -218,7 +218,7 @@ async fn default_echo() -> Result<(), String> {
// subscribe to default channel
let content_filter = ContentFilter::new(Some(waku_default_pubsub_topic()), vec![]);
node.relay_subscribe(&content_filter)?;
let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto);
let content_topic = WakuContentTopic::new("toychat", "2", "huilong", Encoding::Proto);

let sleep = time::sleep(Duration::from_secs(ECHO_TIMEOUT));
tokio::pin!(sleep);
Expand Down
2 changes: 1 addition & 1 deletion waku-sys/vendor

0 comments on commit 5aaafc7

Please sign in to comment.