fix: content topic should accept strings (#84)

This commit is contained in:
richΛrd 2023-12-08 17:30:30 -04:00 committed by GitHub
parent dc32f22f4a
commit 5aaafc7c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 16 deletions

12
Cargo.lock generated
View File

@ -1265,6 +1265,17 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde-aux"
version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "184eba62ebddb71658697c8b08822edee89970bf318c5362189f0de27f85b498"
dependencies = [
"chrono",
"serde",
"serde_json",
]
[[package]]
name = "serde_derive"
version = "1.0.145"
@ -1687,6 +1698,7 @@ dependencies = [
"rand",
"secp256k1 0.26.0",
"serde",
"serde-aux",
"serde_json",
"serial_test",
"smart-default",

12
examples/Cargo.lock generated
View File

@ -1145,6 +1145,17 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde-aux"
version = "4.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "184eba62ebddb71658697c8b08822edee89970bf318c5362189f0de27f85b498"
dependencies = [
"chrono",
"serde",
"serde_json",
]
[[package]]
name = "serde_derive"
version = "1.0.151"
@ -1509,6 +1520,7 @@ dependencies = [
"rand",
"secp256k1 0.26.0",
"serde",
"serde-aux",
"serde_json",
"smart-default",
"sscanf",

View File

@ -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(

View File

@ -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 {

View File

@ -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"

View File

@ -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};
@ -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>,
@ -442,7 +444,7 @@ 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,
}
@ -450,13 +452,13 @@ pub struct WakuContentTopic {
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,
}
@ -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,
})
@ -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,

View File

@ -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();
@ -39,7 +39,7 @@ 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,
@ -47,6 +47,7 @@ pub fn waku_create_content_topic(
);
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));

View File

@ -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();
@ -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);

@ -1 +1 @@
Subproject commit 02f2800b046094f73d1011081daef7d897126687
Subproject commit e340337d64622d22cb94a969255efe4e36637df0