Adapt for nwaku v0.35 (#114)

* build.rs: initialize submodules the first time cargo build is invoked in waku-sys
* messagehash.rs only contains a String as the hex representation of msg hash
* events.rs adapt test to parse message event containing msg-hash in hex string format
* bump nwaku to v0.35.0
* waku-sys/build.rs use STATIC=1 instead of STATIC=true
This commit is contained in:
Ivan FB 2025-02-12 10:06:16 +01:00 committed by GitHub
parent 90577d1d58
commit 9c9900897e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 61 additions and 40 deletions

4
examples/Cargo.lock generated
View File

@ -4945,7 +4945,7 @@ checksum = "317211a0dc0ceedd78fb2ca9a44aed3d7b9b26f81870d485c07122b4350673b7"
[[package]] [[package]]
name = "waku-bindings" name = "waku-bindings"
version = "0.5.0" version = "1.0.0"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"base64 0.21.7", "base64 0.21.7",
@ -4972,7 +4972,7 @@ dependencies = [
[[package]] [[package]]
name = "waku-sys" name = "waku-sys"
version = "0.5.0" version = "1.0.0"
dependencies = [ dependencies = [
"bindgen", "bindgen",
"cc", "cc",

View File

@ -1,40 +1,18 @@
use crate::general::waku_decode::WakuDecode; use crate::general::waku_decode::WakuDecode;
use hex::FromHex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt; use std::fmt;
use std::fmt::Write;
use std::hash::Hash; use std::hash::Hash;
use std::str::FromStr; use std::str::FromStr;
/// Waku message hash, hex encoded sha256 digest of the message /// Waku message hash, hex encoded sha256 digest of the message
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Hash)] #[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Hash)]
pub struct MessageHash([u8; 32]); pub struct MessageHash(String);
impl MessageHash {
fn to_hex_string(&self) -> String {
self.0.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{b:02X}");
output
})
}
}
impl FromStr for MessageHash { impl FromStr for MessageHash {
type Err = String; type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let s = s.strip_prefix("0x").unwrap_or(s); Ok(MessageHash(s.to_string()))
// Decode the hexadecimal string to a Vec<u8>
// We expect a string format like: d38220de82fbcf2df865b680692fce98c36600fdd1d954b8a71e916dc4222b8e
let bytes = Vec::from_hex(s).map_err(|e| format!("Hex decode error MessageHash: {}", e))?;
// Ensure the length is exactly 32 bytes
let res = bytes
.try_into()
.map_err(|_| "Hex string must represent exactly 32 bytes".to_string())?;
Ok(MessageHash(res))
} }
} }
@ -47,6 +25,6 @@ impl WakuDecode for MessageHash {
// Implement the Display trait // Implement the Display trait
impl fmt::Display for MessageHash { impl fmt::Display for MessageHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_hex_string()) write!(f, "{}", self.0)
} }
} }

View File

@ -69,7 +69,7 @@ mod tests {
#[test] #[test]
fn deserialize_message_event() { fn deserialize_message_event() {
let s = "{\"eventType\":\"message\",\"messageHash\":[91, 70, 26, 8, 141, 232, 150, 200, 26, 206, 224, 175, 249, 74, 61, 140, 231, 126, 224, 160, 91, 80, 162, 65, 250, 171, 84, 149, 133, 110, 214, 101],\"pubsubTopic\":\"/waku/2/default-waku/proto\",\"wakuMessage\":{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660}}"; let s = "{\"eventType\":\"message\",\"messageHash\":\"0xd40aa51bbb4867fe40329a255575cfc9ef4000358cc7321b2668b008cba94b30\",\"pubsubTopic\":\"/waku/2/default-waku/proto\",\"wakuMessage\":{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660}}";
let evt: WakuEvent = serde_json::from_str(s).unwrap(); let evt: WakuEvent = serde_json::from_str(s).unwrap();
assert!(matches!(evt, WakuEvent::WakuMessage(_))); assert!(matches!(evt, WakuEvent::WakuMessage(_)));
} }

View File

@ -5,13 +5,55 @@ use std::process::Command;
extern crate cc; extern crate cc;
fn build_nwaku_lib(project_dir: &Path) { fn submodules_init(project_dir: &Path) {
let vendor_path = project_dir.join("vendor"); let mark_file_path = ".submodules-initialized";
set_current_dir(vendor_path).expect("Moving to vendor dir"); // Check if the mark file exists
if !Path::new(mark_file_path).exists() {
// If mark file doesn't exist, initialize submodule
if Command::new("git")
.args(["submodule", "init"])
.status()
.expect("Failed to execute 'git submodule init'")
.success()
&& Command::new("git")
.args(["submodule", "update", "--recursive"])
.status()
.expect("Failed to execute 'git submodule update --recursive'")
.success()
{
// Now, inside nwaku folder, run 'make update' to get nwaku's vendors
let nwaku_path = project_dir.join("vendor");
set_current_dir(nwaku_path).expect("Moving to vendor dir");
if Command::new("make")
.args(["update"])
.status()
.expect("Failed to execute 'make update'")
.success()
{
std::fs::File::create(mark_file_path).expect("Failed to create mark file");
} else {
panic!("Failed to run 'make update' within nwaku folder.");
}
set_current_dir(project_dir).expect("Going back to project dir");
println!("Git submodules initialized and updated successfully.");
} else {
panic!("Failed to initialize or update git submodules.");
}
} else {
println!("Mark file '{mark_file_path}' exists. Skipping git submodule initialization.");
}
}
fn build_nwaku_lib(project_dir: &Path) {
let nwaku_path = project_dir.join("vendor");
set_current_dir(nwaku_path).expect("Moving to vendor dir");
let mut cmd = Command::new("make"); let mut cmd = Command::new("make");
cmd.arg("libwaku").arg("STATIC=true"); cmd.arg("libwaku").arg("STATIC=1");
cmd.status() cmd.status()
.map_err(|e| println!("cargo:warning=make build failed due to: {e}")) .map_err(|e| println!("cargo:warning=make build failed due to: {e}"))
.unwrap(); .unwrap();
@ -20,12 +62,12 @@ fn build_nwaku_lib(project_dir: &Path) {
} }
fn generate_bindgen_code(project_dir: &Path) { fn generate_bindgen_code(project_dir: &Path) {
let vendor_path = project_dir.join("vendor"); let nwaku_path = project_dir.join("vendor");
let header_path = vendor_path.join("library/libwaku.h"); let header_path = nwaku_path.join("library/libwaku.h");
cc::Build::new() cc::Build::new()
.object( .object(
vendor_path nwaku_path
.join("vendor/nim-libbacktrace/libbacktrace_wrapper.o") .join("vendor/nim-libbacktrace/libbacktrace_wrapper.o")
.display() .display()
.to_string(), .to_string(),
@ -35,13 +77,13 @@ fn generate_bindgen_code(project_dir: &Path) {
println!("cargo:rerun-if-changed={}", header_path.display()); println!("cargo:rerun-if-changed={}", header_path.display());
println!( println!(
"cargo:rustc-link-search={}", "cargo:rustc-link-search={}",
vendor_path.join("build").display() nwaku_path.join("build").display()
); );
println!("cargo:rustc-link-lib=static=waku"); println!("cargo:rustc-link-lib=static=waku");
println!( println!(
"cargo:rustc-link-search={}", "cargo:rustc-link-search={}",
vendor_path nwaku_path
.join("vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc/build") .join("vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc/build")
.display() .display()
); );
@ -49,7 +91,7 @@ fn generate_bindgen_code(project_dir: &Path) {
println!( println!(
"cargo:rustc-link-search={}", "cargo:rustc-link-search={}",
vendor_path nwaku_path
.join("vendor/nim-nat-traversal/vendor/libnatpmp-upstream") .join("vendor/nim-nat-traversal/vendor/libnatpmp-upstream")
.display() .display()
); );
@ -60,7 +102,7 @@ fn generate_bindgen_code(project_dir: &Path) {
println!( println!(
"cargo:rustc-link-search=native={}", "cargo:rustc-link-search=native={}",
vendor_path nwaku_path
.join("vendor/nim-libbacktrace/install/usr/lib") .join("vendor/nim-libbacktrace/install/usr/lib")
.display() .display()
); );
@ -95,6 +137,7 @@ fn generate_bindgen_code(project_dir: &Path) {
fn main() { fn main() {
let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let project_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
submodules_init(&project_dir);
build_nwaku_lib(&project_dir); build_nwaku_lib(&project_dir);
generate_bindgen_code(&project_dir); generate_bindgen_code(&project_dir);
} }

@ -1 +1 @@
Subproject commit 625c8ee51bc3e065da0e2e8d3a53d3634589f548 Subproject commit 4117449b9af6c0304a6115dd4bc0d1d745159685