From ca8c57e20fe66e123ed01c495f1ea7ab726448d5 Mon Sep 17 00:00:00 2001 From: mfahampshire Date: Tue, 3 Dec 2024 13:31:45 +0100 Subject: [PATCH] temp commit --- sdk/ffi/go/go-nym/bindings/bindings.go | 30 ++++++- sdk/ffi/go/go-nym/bindings/bindings.h | 1 + sdk/ffi/go/proxy_example.go | 2 +- sdk/ffi/go/src/bindings.udl | 2 +- sdk/ffi/go/src/lib.rs | 2 + sdk/ffi/shared/src/lib.rs | 86 +++++++++++-------- ...niffi-bindgen.rs => uniffi-bindgen.rs.bak} | 0 7 files changed, 84 insertions(+), 39 deletions(-) rename sdk/ffi/shared/{uniffi-bindgen.rs => uniffi-bindgen.rs.bak} (100%) diff --git a/sdk/ffi/go/go-nym/bindings/bindings.go b/sdk/ffi/go/go-nym/bindings/bindings.go index 100e4a5bf22..00ad0e3a826 100644 --- a/sdk/ffi/go/go-nym/bindings/bindings.go +++ b/sdk/ffi/go/go-nym/bindings/bindings.go @@ -383,7 +383,7 @@ func uniffiCheckChecksums() { checksum := rustCall(func(uniffiStatus *C.RustCallStatus) C.uint16_t { return C.uniffi_nym_go_ffi_checksum_func_new_proxy_client(uniffiStatus) }) - if checksum != 14386 { + if checksum != 38844 { // If this happens try cleaning and rebuilding your project panic("bindings: uniffi_nym_go_ffi_checksum_func_new_proxy_client: UniFFI API checksum mismatch") } @@ -453,6 +453,30 @@ func uniffiCheckChecksums() { } } +type FfiConverterUint8 struct{} + +var FfiConverterUint8INSTANCE = FfiConverterUint8{} + +func (FfiConverterUint8) Lower(value uint8) C.uint8_t { + return C.uint8_t(value) +} + +func (FfiConverterUint8) Write(writer io.Writer, value uint8) { + writeUint8(writer, value) +} + +func (FfiConverterUint8) Lift(value C.uint8_t) uint8 { + return uint8(value) +} + +func (FfiConverterUint8) Read(reader io.Reader) uint8 { + return readUint8(reader) +} + +type FfiDestroyerUint8 struct{} + +func (FfiDestroyerUint8) Destroy(_ uint8) {} + type FfiConverterUint64 struct{} var FfiConverterUint64INSTANCE = FfiConverterUint64{} @@ -963,9 +987,9 @@ func ListenForIncoming() (IncomingMessage, error) { } } -func NewProxyClient(serverAddress string, listenAddress string, listenPort string, closeTimeout uint64, env *string) error { +func NewProxyClient(serverAddress string, listenAddress string, listenPort string, closeTimeout uint64, env *string, poolSize uint8) error { _, _uniffiErr := rustCallWithError(FfiConverterTypeGoWrapError{}, func(_uniffiStatus *C.RustCallStatus) bool { - C.uniffi_nym_go_ffi_fn_func_new_proxy_client(FfiConverterStringINSTANCE.Lower(serverAddress), FfiConverterStringINSTANCE.Lower(listenAddress), FfiConverterStringINSTANCE.Lower(listenPort), FfiConverterUint64INSTANCE.Lower(closeTimeout), FfiConverterOptionalStringINSTANCE.Lower(env), _uniffiStatus) + C.uniffi_nym_go_ffi_fn_func_new_proxy_client(FfiConverterStringINSTANCE.Lower(serverAddress), FfiConverterStringINSTANCE.Lower(listenAddress), FfiConverterStringINSTANCE.Lower(listenPort), FfiConverterUint64INSTANCE.Lower(closeTimeout), FfiConverterOptionalStringINSTANCE.Lower(env), FfiConverterUint8INSTANCE.Lower(poolSize), _uniffiStatus) return false }) return _uniffiErr diff --git a/sdk/ffi/go/go-nym/bindings/bindings.h b/sdk/ffi/go/go-nym/bindings/bindings.h index 9e586da85e8..1a28b13dc47 100644 --- a/sdk/ffi/go/go-nym/bindings/bindings.h +++ b/sdk/ffi/go/go-nym/bindings/bindings.h @@ -90,6 +90,7 @@ void uniffi_nym_go_ffi_fn_func_new_proxy_client( RustBuffer listen_port, uint64_t close_timeout, RustBuffer env, + uint8_t pool_size, RustCallStatus* out_status ); diff --git a/sdk/ffi/go/proxy_example.go b/sdk/ffi/go/proxy_example.go index c9e0667562b..b979410f20c 100644 --- a/sdk/ffi/go/proxy_example.go +++ b/sdk/ffi/go/proxy_example.go @@ -118,7 +118,7 @@ func main() { go runProxyServer() // initialise a proxy client - build_err := bindings.NewProxyClient(proxyAddr, "127.0.0.1", clientPort, clientTimeout, &env_path) + build_err := bindings.NewProxyClient(proxyAddr, "127.0.0.1", clientPort, clientTimeout, &env_path, 2) if build_err != nil { fmt.Println(build_err) return diff --git a/sdk/ffi/go/src/bindings.udl b/sdk/ffi/go/src/bindings.udl index 030e75a9a68..ec429404c0f 100644 --- a/sdk/ffi/go/src/bindings.udl +++ b/sdk/ffi/go/src/bindings.udl @@ -30,7 +30,7 @@ namespace bindings { [Throws=GoWrapError] IncomingMessage listen_for_incoming(); [Throws=GoWrapError] - void new_proxy_client(string server_address, string listen_address, string listen_port, u64 close_timeout, string? env); + void new_proxy_client(string server_address, string listen_address, string listen_port, u64 close_timeout, string? env, u8 pool_size); [Throws=GoWrapError] void new_proxy_client_default(string server_address, string? env); [Throws=GoWrapError] diff --git a/sdk/ffi/go/src/lib.rs b/sdk/ffi/go/src/lib.rs index a0a690c6913..83656c1b76c 100644 --- a/sdk/ffi/go/src/lib.rs +++ b/sdk/ffi/go/src/lib.rs @@ -100,6 +100,7 @@ fn new_proxy_client( listen_port: String, close_timeout: u64, env: Option, + pool_size: u8, ) -> Result<(), GoWrapError> { let server_nym_addr = Recipient::try_from_base58_string(server_address).expect("couldn't create Recipient"); @@ -109,6 +110,7 @@ fn new_proxy_client( &listen_port, close_timeout, env, + pool_size as usize, ) { Ok(_) => Ok(()), Err(_) => Err(GoWrapError::ProxyInitError {}), diff --git a/sdk/ffi/shared/src/lib.rs b/sdk/ffi/shared/src/lib.rs index de25f90443a..4a09a63c8db 100644 --- a/sdk/ffi/shared/src/lib.rs +++ b/sdk/ffi/shared/src/lib.rs @@ -1,7 +1,7 @@ // Copyright 2023-2024 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 -use anyhow::{anyhow, bail}; +use anyhow::{anyhow, bail, Error, Result}; use lazy_static::lazy_static; use nym_sdk::mixnet::{ MixnetClient, MixnetClientBuilder, MixnetMessageSender, Recipient, ReconstructedMessage, @@ -13,11 +13,8 @@ use std::path::PathBuf; use std::sync::{Arc, Mutex}; use tokio::runtime::Runtime; -// NYM_CLIENT/PROXIES: Static reference (only init-ed once) to: -// - Arc: share ownership -// - Mutex: thread-safe way to share data between threads -// - Option: init-ed or not -// RUNTIME: Tokio runtime: no need to pass back to C and deal with raw pointers as it was previously +// NYM_CLIENT/PROXIES: Static thread-safe reference (init once) to Options. +// RUNTIME: Tokio runtime: no need to pass across FFI boundary and deal with raw pointers. lazy_static! { static ref NYM_PROXY_CLIENT: Arc>> = Arc::new(Mutex::new(None)); static ref NYM_PROXY_SERVER: Arc>> = Arc::new(Mutex::new(None)); @@ -25,7 +22,16 @@ lazy_static! { static ref RUNTIME: Runtime = Runtime::new().unwrap(); } -pub fn init_ephemeral_internal() -> anyhow::Result<(), anyhow::Error> { +// TODO create get_client() to use in fns and remove code repetition +fn get_client_as_ref() -> bool { + NYM_CLIENT.lock().unwrap().as_ref().is_some() +} + +fn get_client_guard() -> bool { + todo!() +} + +pub fn init_ephemeral_internal() -> Result<(), Error> { if NYM_CLIENT.lock().unwrap().as_ref().is_some() { bail!("client already exists"); } else { @@ -37,13 +43,27 @@ pub fn init_ephemeral_internal() -> anyhow::Result<(), anyhow::Error> { } else { return Err(anyhow!("couldnt lock ephemeral NYM_CLIENT")); } - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; } + // if get_client_as_ref() { + // RUNTIME.block_on(async move { + // let init_client = MixnetClient::connect_new().await?; + // let mut client = NYM_CLIENT.try_lock(); + // if let Ok(ref mut client) = client { + // **client = Some(init_client); + // } else { + // return Err(anyhow!("couldnt lock ephemeral NYM_CLIENT")); + // } + // Ok::<(), Error>(()) + // })?; + // } else { + // bail!("client already exists: no need to reinitialise"); + // } Ok(()) } -pub fn init_default_storage_internal(config_dir: PathBuf) -> anyhow::Result<(), anyhow::Error> { +pub fn init_default_storage_internal(config_dir: PathBuf) -> Result<(), Error> { if NYM_CLIENT.lock().unwrap().as_ref().is_some() { bail!("client already exists"); } else { @@ -60,13 +80,13 @@ pub fn init_default_storage_internal(config_dir: PathBuf) -> anyhow::Result<(), } else { return Err(anyhow!("couldnt lock NYM_CLIENT")); } - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; } Ok(()) } -pub fn get_self_address_internal() -> anyhow::Result { +pub fn get_self_address_internal() -> Result { let client = NYM_CLIENT.lock().expect("could not lock NYM_CLIENT"); if client.is_none() { bail!("Client is not yet initialised"); @@ -83,7 +103,8 @@ pub fn get_self_address_internal() -> anyhow::Result { pub fn send_message_internal( recipient: Recipient, message: &str, -) -> anyhow::Result<(), anyhow::Error> { + // TODO add Option, if Some(surb_amount) call send_message() instead with specified #, else send_plain_message as this uses the default +) -> Result<(), Error> { let client = NYM_CLIENT.lock().expect("could not lock NYM_CLIENT"); if client.is_none() { bail!("Client is not yet initialised"); @@ -94,17 +115,14 @@ pub fn send_message_internal( RUNTIME.block_on(async move { nym_client.send_plain_message(recipient, message).await?; - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; Ok(()) } // TODO send_raw_message_internal -pub fn reply_internal( - recipient: AnonymousSenderTag, - message: &str, -) -> anyhow::Result<(), anyhow::Error> { +pub fn reply_internal(recipient: AnonymousSenderTag, message: &str) -> Result<(), Error> { let client = NYM_CLIENT.lock().expect("could not lock NYM_CLIENT"); if client.is_none() { bail!("Client is not yet initialised"); @@ -115,12 +133,12 @@ pub fn reply_internal( RUNTIME.block_on(async move { nym_client.send_reply(recipient, message).await?; - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; Ok(()) } -pub fn listen_for_incoming_internal() -> anyhow::Result { +pub fn listen_for_incoming_internal() -> Result { let mut binding = NYM_CLIENT.lock().expect("could not lock NYM_CLIENT"); if binding.is_none() { bail!("recipient is null"); @@ -131,7 +149,7 @@ pub fn listen_for_incoming_internal() -> anyhow::Result(ReconstructedMessage { + Ok::(ReconstructedMessage { message: received.message, sender_tag: received.sender_tag, }) @@ -140,9 +158,7 @@ pub fn listen_for_incoming_internal() -> anyhow::Result anyhow::Result { +pub async fn wait_for_non_empty_message(client: &mut MixnetClient) -> Result { while let Some(mut new_message) = client.wait_for_messages().await { if !new_message.is_empty() { return new_message @@ -159,7 +175,8 @@ pub fn proxy_client_new_internal( listen_port: &str, close_timeout: u64, env: Option, -) -> anyhow::Result<(), anyhow::Error> { + pool_size: usize, +) -> Result<(), Error> { if NYM_PROXY_CLIENT.lock().unwrap().as_ref().is_some() { bail!("proxy client already exists"); } else { @@ -170,6 +187,7 @@ pub fn proxy_client_new_internal( listen_port, close_timeout, env, + pool_size, ) .await?; let mut client = NYM_PROXY_CLIENT.try_lock(); @@ -178,7 +196,7 @@ pub fn proxy_client_new_internal( } else { return Err(anyhow!("couldnt lock NYM_PROXY_CLIENT")); } - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; } Ok(()) @@ -187,7 +205,7 @@ pub fn proxy_client_new_internal( pub fn proxy_client_new_defaults_internal( server_address: Recipient, env: Option, -) -> anyhow::Result<(), anyhow::Error> { +) -> Result<(), Error> { if NYM_PROXY_CLIENT.lock().unwrap().as_ref().is_some() { bail!("proxy client already exists"); } else { @@ -199,13 +217,13 @@ pub fn proxy_client_new_defaults_internal( } else { return Err(anyhow!("couldn't lock PROXY_CLIENT")); } - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; } Ok(()) } -pub fn proxy_client_run_internal() -> anyhow::Result<(), anyhow::Error> { +pub fn proxy_client_run_internal() -> Result<(), Error> { let proxy_client = NYM_PROXY_CLIENT .lock() .expect("could not lock NYM_PROXY_CLIENT"); @@ -217,7 +235,7 @@ pub fn proxy_client_run_internal() -> anyhow::Result<(), anyhow::Error> { .ok_or_else(|| anyhow!("could not get proxy_client as_ref()"))?; RUNTIME.block_on(async move { proxy.run().await?; - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; Ok(()) } @@ -226,7 +244,7 @@ pub fn proxy_server_new_internal( upstream_address: &str, config_dir: &str, env: Option, -) -> anyhow::Result<(), anyhow::Error> { +) -> Result<(), Error> { if NYM_PROXY_SERVER.lock().unwrap().as_ref().is_some() { bail!("proxy client already exists"); } else { @@ -238,13 +256,13 @@ pub fn proxy_server_new_internal( } else { return Err(anyhow!("couldn't lock PROXY_SERVER")); } - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; } Ok(()) } -pub fn proxy_server_run_internal() -> anyhow::Result<(), anyhow::Error> { +pub fn proxy_server_run_internal() -> Result<(), Error> { let mut proxy_server = NYM_PROXY_SERVER .lock() .expect("could not lock NYM_PROXY_CLIENT"); @@ -256,12 +274,12 @@ pub fn proxy_server_run_internal() -> anyhow::Result<(), anyhow::Error> { .ok_or_else(|| anyhow!("could not get proxy_client as_ref()"))?; RUNTIME.block_on(async move { proxy.run_with_shutdown().await?; - Ok::<(), anyhow::Error>(()) + Ok::<(), Error>(()) })?; Ok(()) } -pub fn proxy_server_address_internal() -> anyhow::Result { +pub fn proxy_server_address_internal() -> Result { let mut proxy_server = NYM_PROXY_SERVER .lock() .expect("could not lock NYM_PROXY_CLIENT"); diff --git a/sdk/ffi/shared/uniffi-bindgen.rs b/sdk/ffi/shared/uniffi-bindgen.rs.bak similarity index 100% rename from sdk/ffi/shared/uniffi-bindgen.rs rename to sdk/ffi/shared/uniffi-bindgen.rs.bak