From 26cab6a022821a0059e8f5cf0245d0f5ced03993 Mon Sep 17 00:00:00 2001 From: Daniel McCartney Date: Tue, 30 May 2023 12:48:16 -0700 Subject: [PATCH] feat: expose batch_query on xmtp_networking and bindings_swift --- .../xmtp_rust_swift/xmtp_rust_swift.h | 26 ++++++++++++ bindings_swift/src/lib.rs | 40 ++++++++++++++++++- bindings_swift/src/types.rs | 30 +++++++++++++- xmtp_networking/src/grpc_api_helper.rs | 19 ++++++++- xmtp_networking/src/lib.rs | 11 +++++ 5 files changed, 121 insertions(+), 5 deletions(-) diff --git a/bindings_swift/include/Generated/xmtp_rust_swift/xmtp_rust_swift.h b/bindings_swift/include/Generated/xmtp_rust_swift/xmtp_rust_swift.h index ca8d5ff76..80c4a5aa6 100644 --- a/bindings_swift/include/Generated/xmtp_rust_swift/xmtp_rust_swift.h +++ b/bindings_swift/include/Generated/xmtp_rust_swift/xmtp_rust_swift.h @@ -41,6 +41,18 @@ void* __swift_bridge__$Vec_RustSubscription$get_mut(void* vec_ptr, uintptr_t ind uintptr_t __swift_bridge__$Vec_RustSubscription$len(void* vec_ptr); void* __swift_bridge__$Vec_RustSubscription$as_ptr(void* vec_ptr); +typedef struct QueryRequest QueryRequest; +void __swift_bridge__$QueryRequest$_free(void* self); + +void* __swift_bridge__$Vec_QueryRequest$new(void); +void __swift_bridge__$Vec_QueryRequest$drop(void* vec_ptr); +void __swift_bridge__$Vec_QueryRequest$push(void* vec_ptr, void* item_ptr); +void* __swift_bridge__$Vec_QueryRequest$pop(void* vec_ptr); +void* __swift_bridge__$Vec_QueryRequest$get(void* vec_ptr, uintptr_t index); +void* __swift_bridge__$Vec_QueryRequest$get_mut(void* vec_ptr, uintptr_t index); +uintptr_t __swift_bridge__$Vec_QueryRequest$len(void* vec_ptr); +void* __swift_bridge__$Vec_QueryRequest$as_ptr(void* vec_ptr); + typedef struct QueryResponse QueryResponse; void __swift_bridge__$QueryResponse$_free(void* self); @@ -53,6 +65,18 @@ void* __swift_bridge__$Vec_QueryResponse$get_mut(void* vec_ptr, uintptr_t index) uintptr_t __swift_bridge__$Vec_QueryResponse$len(void* vec_ptr); void* __swift_bridge__$Vec_QueryResponse$as_ptr(void* vec_ptr); +typedef struct BatchQueryResponse BatchQueryResponse; +void __swift_bridge__$BatchQueryResponse$_free(void* self); + +void* __swift_bridge__$Vec_BatchQueryResponse$new(void); +void __swift_bridge__$Vec_BatchQueryResponse$drop(void* vec_ptr); +void __swift_bridge__$Vec_BatchQueryResponse$push(void* vec_ptr, void* item_ptr); +void* __swift_bridge__$Vec_BatchQueryResponse$pop(void* vec_ptr); +void* __swift_bridge__$Vec_BatchQueryResponse$get(void* vec_ptr, uintptr_t index); +void* __swift_bridge__$Vec_BatchQueryResponse$get_mut(void* vec_ptr, uintptr_t index); +uintptr_t __swift_bridge__$Vec_BatchQueryResponse$len(void* vec_ptr); +void* __swift_bridge__$Vec_BatchQueryResponse$as_ptr(void* vec_ptr); + typedef struct RustClient RustClient; void __swift_bridge__$RustClient$_free(void* self); @@ -73,7 +97,9 @@ struct __private__ResultPtrAndPtr __swift_bridge__$RustSubscription$get_messages void __swift_bridge__$RustSubscription$close(void* self); void* __swift_bridge__$QueryResponse$envelopes(void* self); struct __swift_bridge__$Option$PagingInfo __swift_bridge__$QueryResponse$paging_info(void* self); +void* __swift_bridge__$BatchQueryResponse$responses(void* self); void __swift_bridge__$create_client(void* callback_wrapper, void __swift_bridge__$create_client$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* host, bool is_secure); +void __swift_bridge__$RustClient$batch_query(void* callback_wrapper, void __swift_bridge__$RustClient$batch_query$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* requests); void __swift_bridge__$RustClient$query(void* callback_wrapper, void __swift_bridge__$RustClient$query$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* topic, struct __private__OptionU64 start_time_ns, struct __private__OptionU64 end_time_ns, struct __swift_bridge__$Option$PagingInfo paging_info); void __swift_bridge__$RustClient$publish(void* callback_wrapper, void __swift_bridge__$RustClient$publish$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* token, void* envelopes); void __swift_bridge__$RustClient$subscribe(void* callback_wrapper, void __swift_bridge__$RustClient$subscribe$async(void* callback_wrapper, struct __private__ResultPtrAndPtr ret), void* self, void* topics); diff --git a/bindings_swift/src/lib.rs b/bindings_swift/src/lib.rs index cdf73b3a6..d2726687e 100644 --- a/bindings_swift/src/lib.rs +++ b/bindings_swift/src/lib.rs @@ -1,7 +1,8 @@ +use types::BatchQueryResponse; use types::QueryResponse; use xmtp_crypto::{hashes, k256_helper}; use xmtp_networking::grpc_api_helper; -use xmtp_proto::xmtp::message_api::v1::{Envelope as EnvelopeProto, PagingInfo}; +use xmtp_proto::xmtp::message_api::v1::{Envelope as EnvelopeProto, PagingInfo, QueryRequest}; pub mod types; #[swift_bridge::bridge] @@ -42,6 +43,10 @@ mod ffi { fn close(&mut self); } + extern "Rust" { + type QueryRequest; + } + extern "Rust" { type QueryResponse; @@ -50,11 +55,22 @@ mod ffi { } + extern "Rust" { + type BatchQueryResponse; + + fn responses(&self) -> Vec; + } + extern "Rust" { type RustClient; async fn create_client(host: String, is_secure: bool) -> Result; + async fn batch_query( + &mut self, + requests: Vec, + ) -> Result; + async fn query( &mut self, topic: String, @@ -110,6 +126,19 @@ async fn create_client(host: String, is_secure: bool) -> Result, + ) -> Result { + let result = self + .client + .batch_query(requests) + .await + .map_err(|e| format!("{}", e))?; + + Ok(BatchQueryResponse::from(result)) + } + async fn query( &mut self, topic: String, @@ -307,6 +336,15 @@ mod tests { } } + #[tokio::test] + async fn test_batch_query() { + let mut client = super::create_client(ADDRESS.to_string(), false) + .await + .unwrap(); + let result = client.batch_query(vec![]).await.unwrap(); + assert_eq!(result.responses().len(), 0); + } + // Try a query on a test topic, and make sure we get a response #[tokio::test] async fn test_publish_query() { diff --git a/bindings_swift/src/types.rs b/bindings_swift/src/types.rs index 808a7153d..15dbb9f88 100644 --- a/bindings_swift/src/types.rs +++ b/bindings_swift/src/types.rs @@ -1,6 +1,7 @@ use xmtp_proto::xmtp::message_api::v1::{ - cursor as cursor_proto, Cursor, Envelope as EnvelopeProto, IndexCursor, PagingInfo, - QueryResponse as ProtoQueryResponse, SortDirection, + cursor as cursor_proto, BatchQueryResponse as ProtoBatchQueryResponse, Cursor, + Envelope as EnvelopeProto, IndexCursor, PagingInfo, QueryResponse as ProtoQueryResponse, + SortDirection, }; impl From for SortDirection { @@ -87,6 +88,7 @@ impl From for crate::Envelope { } } +#[derive(Clone)] pub struct QueryResponse { _envelopes: Vec, _paging_info: Option, @@ -119,3 +121,27 @@ impl From for QueryResponse { } } } + +pub struct BatchQueryResponse { + _responses: Vec, +} + +impl BatchQueryResponse { + pub fn responses(&self) -> Vec { + self._responses.clone() + } +} + +impl From for BatchQueryResponse { + fn from(batch_query_response: ProtoBatchQueryResponse) -> Self { + let responses = batch_query_response + .responses + .into_iter() + .map(crate::QueryResponse::from) + .collect(); + + BatchQueryResponse { + _responses: responses, + } + } +} diff --git a/xmtp_networking/src/grpc_api_helper.rs b/xmtp_networking/src/grpc_api_helper.rs index 0370a2c49..7dff762ed 100644 --- a/xmtp_networking/src/grpc_api_helper.rs +++ b/xmtp_networking/src/grpc_api_helper.rs @@ -9,8 +9,8 @@ use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore}; use tonic::Status; use tonic::{metadata::MetadataValue, transport::Channel, Request, Streaming}; use xmtp_proto::xmtp::message_api::v1::{ - message_api_client::MessageApiClient, Envelope, PagingInfo, PublishRequest, PublishResponse, - QueryRequest, QueryResponse, SubscribeRequest, + message_api_client::MessageApiClient, BatchQueryRequest, BatchQueryResponse, Envelope, + PagingInfo, PublishRequest, PublishResponse, QueryRequest, QueryResponse, SubscribeRequest, }; fn tls_config() -> Result { @@ -173,6 +173,21 @@ impl Client { Err(e) => Err(e), } } + + pub async fn batch_query( + &self, + requests: Vec, + ) -> Result { + let request = BatchQueryRequest { requests }; + let res = match &self.client { + InnerApiClient::Plain(c) => c.clone().batch_query(request).await, + InnerApiClient::Tls(c) => c.clone().batch_query(request).await, + }; + match res { + Ok(response) => Ok(response.into_inner()), + Err(e) => Err(e), + } + } } pub struct Subscription { diff --git a/xmtp_networking/src/lib.rs b/xmtp_networking/src/lib.rs index 8d3785700..da68168f8 100644 --- a/xmtp_networking/src/lib.rs +++ b/xmtp_networking/src/lib.rs @@ -33,6 +33,17 @@ mod tests { assert_eq!(result.envelopes.len(), 0); } + #[tokio::test] + async fn grpc_batch_query_test() { + let client = Client::create("http://localhost:5556".to_string(), false) + .await + .unwrap(); + + let result = client.batch_query(vec![]).await.unwrap(); + + assert_eq!(result.responses.len(), 0); + } + #[tokio::test] async fn publish_test() { let client = Client::create("http://localhost:5556".to_string(), false)