From 592338b35bbc5d3b2346af8ea5211f02afea9af7 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Sun, 9 Oct 2022 16:32:18 -0500 Subject: [PATCH] Pipe protocol id --- waku/src/general/mod.rs | 29 +++++++++++++++++++++++++++++ waku/src/lib.rs | 2 +- waku/src/node/config.rs | 16 ++++++++-------- waku/src/node/mod.rs | 6 +++--- waku/src/node/peers.rs | 4 ++-- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index 80b08fb..1eb1368 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -18,6 +18,35 @@ pub type PeerId = String; /// Waku message id, hex encoded sha256 digest of the message pub type MessageId = String; +/// Protocol identifiers +#[non_exhaustive] +pub enum ProtocolId { + Store, + Lightpush, + Filter, + Relay, +} + +impl ProtocolId { + pub fn as_string_with_version(&self, version: &str) -> String { + format!("{}/{}", self, version) + } +} + +impl Display for ProtocolId { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let tag = match self { + ProtocolId::Store => "/vac/waku/store", + ProtocolId::Lightpush => "/vac/waku/lightpush", + ProtocolId::Filter => "/vac/waku/filter", + ProtocolId::Relay => "/vac/waku/relay", + #[allow(unreachable_patterns)] + _ => unreachable!(), + }; + write!(f, "{}", tag) + } +} + /// JsonResponse wrapper. /// `go-waku` ffi returns this type as a `char *` as per the [specification](https://rfc.vac.dev/spec/36/#jsonresponse-type) /// This is internal, as it is better to use rust plain `Result` type. diff --git a/waku/src/lib.rs b/waku/src/lib.rs index 2679160..da5191e 100644 --- a/waku/src/lib.rs +++ b/waku/src/lib.rs @@ -13,7 +13,7 @@ pub use node::{ pub use general::{ ContentFilter, DecodedPayload, Encoding, FilterSubscription, MessageId, MessageIndex, - PagingOptions, PeerId, StoreQuery, StoreResponse, WakuContentTopic, WakuMessage, + PagingOptions, PeerId, ProtocolId, StoreQuery, StoreResponse, WakuContentTopic, WakuMessage, WakuMessageVersion, WakuPubSubTopic, }; diff --git a/waku/src/node/config.rs b/waku/src/node/config.rs index 44d9705..e88d6a8 100644 --- a/waku/src/node/config.rs +++ b/waku/src/node/config.rs @@ -12,24 +12,24 @@ use serde::{Deserialize, Serialize}; #[serde(rename_all = "camelCase")] pub struct WakuNodeConfig { /// Listening IP address. Default `0.0.0.0` - host: Option, + pub host: Option, /// Libp2p TCP listening port. Default `60000`. Use `0` for **random** - port: Option, + pub port: Option, /// External address to advertise to other nodes. Can be ip4, ip6 or dns4, dns6. /// If null, the multiaddress(es) generated from the ip and port specified in the config (or default ones) will be used. /// Default: null - advertise_addr: Option, + pub advertise_addr: Option, /// Secp256k1 private key in Hex format (`0x123...abc`). Default random #[serde(with = "secret_key_serde")] - node_key: Option, + pub node_key: Option, /// Interval in seconds for pinging peers to keep the connection alive. Default `20` - keep_alive_interval: Option, + pub keep_alive_interval: Option, /// Enable relay protocol. Default `true` - relay: Option, + pub relay: Option, /// The minimum number of peers required on a topic to allow broadcasting a message. Default `0` - min_peers_to_publish: Option, + pub min_peers_to_publish: Option, /// Enable filter protocol. Default `false` - filter: Option, + pub filter: Option, } mod secret_key_serde { diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 217e1f7..9f86ff2 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -18,8 +18,8 @@ use std::time::Duration; // crates // internal use crate::general::{ - FilterSubscription, MessageId, PeerId, Result, StoreQuery, StoreResponse, WakuMessage, - WakuPubSubTopic, + FilterSubscription, MessageId, PeerId, ProtocolId, Result, StoreQuery, StoreResponse, + WakuMessage, WakuPubSubTopic, }; pub use config::WakuNodeConfig; @@ -73,7 +73,7 @@ impl WakuNodeHandle { /// Add a node multiaddress and protocol to the waku node’s peerstore /// /// wrapper around [`peers::waku_add_peers`] - pub fn add_peer(&self, address: Multiaddr, protocol_id: usize) -> Result { + pub fn add_peer(&self, address: Multiaddr, protocol_id: ProtocolId) -> Result { peers::waku_add_peers(address, protocol_id) } } diff --git a/waku/src/node/peers.rs b/waku/src/node/peers.rs index 094c1ef..3be8f94 100644 --- a/waku/src/node/peers.rs +++ b/waku/src/node/peers.rs @@ -7,11 +7,11 @@ use std::time::Duration; use multiaddr::Multiaddr; use serde::Deserialize; // internal -use crate::general::{JsonResponse, PeerId, Result}; +use crate::general::{JsonResponse, PeerId, ProtocolId, Result}; /// Add a node multiaddress and protocol to the waku node’s peerstore. /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid) -pub fn waku_add_peers(address: Multiaddr, protocol_id: usize) -> Result { +pub fn waku_add_peers(address: Multiaddr, protocol_id: ProtocolId) -> Result { let response = unsafe { CStr::from_ptr(waku_sys::waku_add_peer( CString::new(address.to_string())