From 3d800d1a2ce0e6958e882b3b2b7facfc71146da5 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Thu, 14 Sep 2023 17:25:28 +0200 Subject: [PATCH 1/3] Makes async traits in the SDK be `Send` --- apps/src/lib/client/tx.rs | 2 +- apps/src/lib/node/ledger/shell/testing/node.rs | 2 +- benches/lib.rs | 4 ++-- shared/src/ledger/masp.rs | 2 +- shared/src/ledger/queries/mod.rs | 2 +- shared/src/ledger/queries/types.rs | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/src/lib/client/tx.rs b/apps/src/lib/client/tx.rs index c3a46d3fd7..d5814701c7 100644 --- a/apps/src/lib/client/tx.rs +++ b/apps/src/lib/client/tx.rs @@ -574,7 +574,7 @@ impl Default for CLIShieldedUtils { } } -#[async_trait(?Send)] +#[async_trait] impl masp::ShieldedUtils for CLIShieldedUtils { fn local_tx_prover(&self) -> LocalTxProver { if let Ok(params_dir) = env::var(masp::ENV_VAR_MASP_PARAMS_DIR) { diff --git a/apps/src/lib/node/ledger/shell/testing/node.rs b/apps/src/lib/node/ledger/shell/testing/node.rs index cebb970f62..6b7aa73d44 100644 --- a/apps/src/lib/node/ledger/shell/testing/node.rs +++ b/apps/src/lib/node/ledger/shell/testing/node.rs @@ -308,7 +308,7 @@ impl MockNode { } } -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl<'a> Client for &'a MockNode { type Error = Report; diff --git a/benches/lib.rs b/benches/lib.rs index 11042c84b3..59353c00e0 100644 --- a/benches/lib.rs +++ b/benches/lib.rs @@ -550,7 +550,7 @@ pub struct BenchShieldedUtils { context_dir: WrapperTempDir, } -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl ShieldedUtils for BenchShieldedUtils { fn local_tx_prover(&self) -> LocalTxProver { if let Ok(params_dir) = std::env::var(masp::ENV_VAR_MASP_PARAMS_DIR) { @@ -614,7 +614,7 @@ impl ShieldedUtils for BenchShieldedUtils { } } -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl Client for BenchShell { type Error = std::io::Error; diff --git a/shared/src/ledger/masp.rs b/shared/src/ledger/masp.rs index 0ddb7cb6a0..26b7629b1d 100644 --- a/shared/src/ledger/masp.rs +++ b/shared/src/ledger/masp.rs @@ -388,7 +388,7 @@ impl /// Abstracts platform specific details away from the logic of shielded pool /// operations. -#[async_trait(? Send)] +#[async_trait] pub trait ShieldedUtils: Sized + BorshDeserialize + BorshSerialize + Default + Clone { diff --git a/shared/src/ledger/queries/mod.rs b/shared/src/ledger/queries/mod.rs index aa87ce2903..6f3f16d0db 100644 --- a/shared/src/ledger/queries/mod.rs +++ b/shared/src/ledger/queries/mod.rs @@ -172,7 +172,7 @@ mod testing { } } - #[async_trait::async_trait(?Send)] + #[async_trait::async_trait] impl Client for TestClient where RPC: Router + Sync, diff --git a/shared/src/ledger/queries/types.rs b/shared/src/ledger/queries/types.rs index 78f136cad2..79854e7012 100644 --- a/shared/src/ledger/queries/types.rs +++ b/shared/src/ledger/queries/types.rs @@ -81,7 +81,7 @@ pub trait Router { /// A client with async request dispatcher method, which can be used to invoke /// type-safe methods from a root [`Router`], generated via `router!` macro. #[cfg(any(test, feature = "async-client"))] -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] pub trait Client { /// `std::io::Error` can happen in decoding with /// `BorshDeserialize::try_from_slice` @@ -306,7 +306,7 @@ pub enum Error { InvalidHeight(BlockHeight), } -#[async_trait::async_trait(?Send)] +#[async_trait::async_trait] impl Client for C { type Error = Error; From b2dcfa889b8be7b93bb8c8376032991379c5d593 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Tue, 19 Sep 2023 17:20:17 +0200 Subject: [PATCH 2/3] Conditionally requires async traits `Send`ness --- apps/src/lib/client/tx.rs | 4 ++-- apps/src/lib/node/ledger/shell/testing/node.rs | 3 ++- benches/lib.rs | 6 ++++-- shared/Cargo.toml | 4 ++++ shared/src/ledger/masp.rs | 4 ++-- shared/src/ledger/queries/mod.rs | 3 ++- shared/src/ledger/queries/types.rs | 6 ++++-- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/apps/src/lib/client/tx.rs b/apps/src/lib/client/tx.rs index d5814701c7..9e65e29283 100644 --- a/apps/src/lib/client/tx.rs +++ b/apps/src/lib/client/tx.rs @@ -4,7 +4,6 @@ use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; use std::path::PathBuf; -use async_trait::async_trait; use borsh::{BorshDeserialize, BorshSerialize}; use masp_proofs::prover::LocalTxProver; use namada::core::ledger::governance::cli::offline::{ @@ -574,7 +573,8 @@ impl Default for CLIShieldedUtils { } } -#[async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl masp::ShieldedUtils for CLIShieldedUtils { fn local_tx_prover(&self) -> LocalTxProver { if let Ok(params_dir) = env::var(masp::ENV_VAR_MASP_PARAMS_DIR) { diff --git a/apps/src/lib/node/ledger/shell/testing/node.rs b/apps/src/lib/node/ledger/shell/testing/node.rs index 6b7aa73d44..ac53044f48 100644 --- a/apps/src/lib/node/ledger/shell/testing/node.rs +++ b/apps/src/lib/node/ledger/shell/testing/node.rs @@ -308,7 +308,8 @@ impl MockNode { } } -#[async_trait::async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl<'a> Client for &'a MockNode { type Error = Report; diff --git a/benches/lib.rs b/benches/lib.rs index 59353c00e0..d53bd95772 100644 --- a/benches/lib.rs +++ b/benches/lib.rs @@ -550,7 +550,8 @@ pub struct BenchShieldedUtils { context_dir: WrapperTempDir, } -#[async_trait::async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl ShieldedUtils for BenchShieldedUtils { fn local_tx_prover(&self) -> LocalTxProver { if let Ok(params_dir) = std::env::var(masp::ENV_VAR_MASP_PARAMS_DIR) { @@ -614,7 +615,8 @@ impl ShieldedUtils for BenchShieldedUtils { } } -#[async_trait::async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl Client for BenchShell { type Error = std::io::Error; diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 783a349883..89ba43c616 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -42,6 +42,10 @@ wasm-runtime = [ async-client = [ "async-trait", ] + +# Requires async traits to be safe to send across threads +async-send = [] + # tendermint-rpc support tendermint-rpc = [ "async-client", diff --git a/shared/src/ledger/masp.rs b/shared/src/ledger/masp.rs index 26b7629b1d..2a7c86c3f4 100644 --- a/shared/src/ledger/masp.rs +++ b/shared/src/ledger/masp.rs @@ -7,7 +7,6 @@ use std::fmt::Debug; use std::ops::Deref; use std::path::PathBuf; -use async_trait::async_trait; // use async_std::io::prelude::WriteExt; // use async_std::io::{self}; use borsh::{BorshDeserialize, BorshSerialize}; @@ -388,7 +387,8 @@ impl /// Abstracts platform specific details away from the logic of shielded pool /// operations. -#[async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] pub trait ShieldedUtils: Sized + BorshDeserialize + BorshSerialize + Default + Clone { diff --git a/shared/src/ledger/queries/mod.rs b/shared/src/ledger/queries/mod.rs index 6f3f16d0db..1d636ec8bc 100644 --- a/shared/src/ledger/queries/mod.rs +++ b/shared/src/ledger/queries/mod.rs @@ -172,7 +172,8 @@ mod testing { } } - #[async_trait::async_trait] + #[cfg_attr(feature = "async-send", async_trait::async_trait)] + #[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl Client for TestClient where RPC: Router + Sync, diff --git a/shared/src/ledger/queries/types.rs b/shared/src/ledger/queries/types.rs index 79854e7012..4ec886ca55 100644 --- a/shared/src/ledger/queries/types.rs +++ b/shared/src/ledger/queries/types.rs @@ -81,7 +81,8 @@ pub trait Router { /// A client with async request dispatcher method, which can be used to invoke /// type-safe methods from a root [`Router`], generated via `router!` macro. #[cfg(any(test, feature = "async-client"))] -#[async_trait::async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] pub trait Client { /// `std::io::Error` can happen in decoding with /// `BorshDeserialize::try_from_slice` @@ -306,7 +307,8 @@ pub enum Error { InvalidHeight(BlockHeight), } -#[async_trait::async_trait] +#[cfg_attr(feature = "async-send", async_trait::async_trait)] +#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))] impl Client for C { type Error = Error; From d89c660e1b36c198745ef6b2adb8147fcd41fb60 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Thu, 14 Sep 2023 18:04:34 +0200 Subject: [PATCH 3/3] changelog: add #1894 --- .changelog/unreleased/SDK/1894-sdk-async-traits-send.md | 3 +++ .../unreleased/improvements/1894-sdk-async-traits-send.md | 2 ++ 2 files changed, 5 insertions(+) create mode 100644 .changelog/unreleased/SDK/1894-sdk-async-traits-send.md create mode 100644 .changelog/unreleased/improvements/1894-sdk-async-traits-send.md diff --git a/.changelog/unreleased/SDK/1894-sdk-async-traits-send.md b/.changelog/unreleased/SDK/1894-sdk-async-traits-send.md new file mode 100644 index 0000000000..613c567f1a --- /dev/null +++ b/.changelog/unreleased/SDK/1894-sdk-async-traits-send.md @@ -0,0 +1,3 @@ +- Added the `Send` bound to the `Client` and `ShieldedUtils` `async_trait`s'. + This allows the SDK to be used in environments which are both asynchronous and + multithread. ([\#1894](https://github.com/anoma/namada/pull/1894)) \ No newline at end of file diff --git a/.changelog/unreleased/improvements/1894-sdk-async-traits-send.md b/.changelog/unreleased/improvements/1894-sdk-async-traits-send.md new file mode 100644 index 0000000000..f90be910b3 --- /dev/null +++ b/.changelog/unreleased/improvements/1894-sdk-async-traits-send.md @@ -0,0 +1,2 @@ +- Forced the `async_trait`s' futures in the SDK to be `Send`. + ([\#1894](https://github.com/anoma/namada/pull/1894)) \ No newline at end of file