Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes async traits in the SDK be Send #1894

Merged
merged 3 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/unreleased/SDK/1894-sdk-async-traits-send.md
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Forced the `async_trait`s' futures in the SDK to be `Send`.
([\#1894](https://github.com/anoma/namada/pull/1894))
4 changes: 2 additions & 2 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -574,7 +573,8 @@ impl Default for CLIShieldedUtils {
}
}

#[async_trait(?Send)]
#[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) {
Expand Down
3 changes: 2 additions & 1 deletion apps/src/lib/node/ledger/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ impl MockNode {
}
}

#[async_trait::async_trait(?Send)]
#[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;

Expand Down
6 changes: 4 additions & 2 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ pub struct BenchShieldedUtils {
context_dir: WrapperTempDir,
}

#[async_trait::async_trait(?Send)]
#[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) {
Expand Down Expand Up @@ -614,7 +615,8 @@ impl ShieldedUtils for BenchShieldedUtils {
}
}

#[async_trait::async_trait(?Send)]
#[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;

Expand Down
4 changes: 4 additions & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions shared/src/ledger/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -388,7 +387,8 @@ impl<P1, R1, N1>

/// Abstracts platform specific details away from the logic of shielded pool
/// operations.
#[async_trait(? Send)]
#[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
{
Expand Down
3 changes: 2 additions & 1 deletion shared/src/ledger/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ mod testing {
}
}

#[async_trait::async_trait(?Send)]
#[cfg_attr(feature = "async-send", async_trait::async_trait)]
#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))]
impl<RPC> Client for TestClient<RPC>
where
RPC: Router + Sync,
Expand Down
6 changes: 4 additions & 2 deletions shared/src/ledger/queries/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(?Send)]
#[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`
Expand Down Expand Up @@ -306,7 +307,8 @@ pub enum Error {
InvalidHeight(BlockHeight),
}

#[async_trait::async_trait(?Send)]
#[cfg_attr(feature = "async-send", async_trait::async_trait)]
#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))]
impl<C: tendermint_rpc::Client + std::marker::Sync> Client for C {
type Error = Error;

Expand Down
Loading