From fa56c3540bc0eca7f7815a76b05454390e5b0ae3 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Fri, 7 Oct 2022 11:41:06 +0200 Subject: [PATCH] Implemented store methods wrappers --- waku/src/node/filter.rs | 4 ++-- waku/src/node/mod.rs | 1 + waku/src/node/store.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 waku/src/node/store.rs diff --git a/waku/src/node/filter.rs b/waku/src/node/filter.rs index 7b763b5..c9562e2 100644 --- a/waku/src/node/filter.rs +++ b/waku/src/node/filter.rs @@ -7,7 +7,7 @@ use std::time::Duration; use crate::general::Result; use crate::general::{FilterSubscription, JsonResponse, MessageId, PeerId}; -/// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`PubSubTopic`] +/// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms) pub fn waku_filter_subscribe( filter_subscription: &FilterSubscription, @@ -40,7 +40,7 @@ pub fn waku_filter_subscribe( Result::from(response).map(|_| ()) } -/// Removes subscriptions in a light node matching a content filter and, optionally, a [`PubSubTopic`] +/// Removes subscriptions in a light node matching a content filter and, optionally, a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_unsubscribechar-filterjson-int-timeoutms) pub fn waku_filter_unsubscribe( filter_subscription: &FilterSubscription, diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 4cb4b19..89d0c9c 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -4,6 +4,7 @@ mod lightpush; mod management; mod peers; mod relay; +mod store; // std use aes_gcm::{Aes256Gcm, Key}; diff --git a/waku/src/node/store.rs b/waku/src/node/store.rs new file mode 100644 index 0000000..90c5d95 --- /dev/null +++ b/waku/src/node/store.rs @@ -0,0 +1,40 @@ +// std +use std::ffi::{CStr, CString}; +use std::time::Duration; +// crates +// internal +use crate::general::{JsonResponse, PeerId, Result, StoreQuery, StoreResponse}; + +/// 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`), +/// the node must return messages on a per-page basis and include [`PagingOptions`](`crate::general::PagingOptions`) in the response. +/// These [`PagingOptions`](`crate::general::PagingOptions`) must contain a cursor pointing to the Index from which a new page can be requested +pub fn waku_store_query( + query: &StoreQuery, + peer_id: PeerId, + timeout: Duration, +) -> Result { + let result = unsafe { + CStr::from_ptr(waku_sys::waku_store_query( + CString::new( + serde_json::to_string(query) + .expect("StoreQuery should always be able to be serialized"), + ) + .expect("CString should build properly from the serialized filter subscription") + .into_raw(), + CString::new(peer_id) + .expect("CString should build properly from peer id") + .into_raw(), + timeout + .as_millis() + .try_into() + .expect("Duration as milliseconds should fit in a i32"), + )) + } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let response: JsonResponse = + serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + response.into() +}