Skip to content

Commit

Permalink
Merge branch 'origin/fraccaman+murisi/with-async' (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Nov 30, 2023
2 parents 6b21bca + 0451fb4 commit 336e9d2
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/SDK/2235-with-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added Send trait support to the SDK to allow its use in more multithreaded
contexts. ([\#2235](https://github.com/anoma/namada/pull/2235))
74 changes: 43 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ index-set = {git = "https://github.com/heliaxdev/index-set", tag = "v0.8.0", fea
itertools = "0.10.0"
k256 = { version = "0.13.0", default-features = false, features = ["ecdsa", "pkcs8", "precomputed-tables", "serde", "std"]}
lazy_static = "1.4.0"
ledger-namada-rs = { git = "https://github.com/Zondax/ledger-namada", tag = "v0.0.11" }
ledger-namada-rs = { git = "https://github.com/Zondax/ledger-namada", tag = "v0.0.12" }
ledger-transport-hid = "0.10.0"
libc = "0.2.97"
libloading = "0.7.2"
Expand Down
2 changes: 2 additions & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ async-client = [
"async-trait",
]

async-send = []

# for integration tests and test utilies
testing = [
"namada_core/testing",
Expand Down
9 changes: 5 additions & 4 deletions sdk/src/eth_bridge/bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ use crate::rpc::{query_storage_value, query_wasm_code_hash, validate_amount};
use crate::signing::aux_signing_data;
use crate::tx::prepare_tx;
use crate::{
args, display, display_line, edisplay_line, Namada, SigningTxData,
args, display, display_line, edisplay_line, MaybeSync, Namada,
SigningTxData,
};

/// Craft a transaction that adds a transfer to the Ethereum bridge pool.
Expand Down Expand Up @@ -419,7 +420,7 @@ pub async fn query_relay_progress(
/// bridge pool.
async fn construct_bridge_pool_proof(
client: &(impl Client + Sync),
io: &impl Io,
io: &(impl Io + MaybeSync),
args: GenBridgePoolProofReq<'_, '_>,
) -> Result<GenBridgePoolProofRsp, Error> {
let in_progress = RPC
Expand Down Expand Up @@ -515,7 +516,7 @@ struct BridgePoolProofResponse {
/// to relaying it to ethereum).
pub async fn construct_proof(
client: &(impl Client + Sync),
io: &impl Io,
io: &(impl Io + MaybeSync),
args: args::BridgePoolProof,
) -> Result<(), Error> {
let GenBridgePoolProofRsp {
Expand Down Expand Up @@ -565,7 +566,7 @@ pub async fn construct_proof(
pub async fn relay_bridge_pool_proof<E>(
eth_client: Arc<E>,
client: &(impl Client + Sync),
io: &impl Io,
io: &(impl Io + MaybeSync),
args: args::RelayBridgePoolProof,
) -> Result<(), Error>
where
Expand Down
20 changes: 15 additions & 5 deletions sdk/src/io.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Traits for implementing IO handlers. This is to enable
//! generic IO. The defaults are the obvious Rust native
//! functions.
use crate::{MaybeSend, MaybeSync};

/// A trait that abstracts out I/O operations
#[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 Io {
/// Print the given string
fn print(&self, output: impl AsRef<str>) {
Expand Down Expand Up @@ -57,7 +59,10 @@ pub trait Io {
}

/// Display the given prompt and return the string input
async fn prompt(&self, question: impl AsRef<str>) -> String {
async fn prompt(
&self,
question: impl AsRef<str> + MaybeSync + MaybeSend,
) -> String {
#[cfg(not(target_family = "wasm"))]
{
prompt_aux(
Expand All @@ -81,13 +86,15 @@ pub trait Io {
/// Rust native I/O handling.
pub struct StdIo;

#[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 Io for StdIo {}

/// Ignores all I/O operations.
pub struct NullIo;

#[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 Io for NullIo {
fn print(&self, _output: impl AsRef<str>) {}

Expand Down Expand Up @@ -117,7 +124,10 @@ impl Io for NullIo {
panic!("Unsupported operation")
}

async fn prompt(&self, _question: impl AsRef<str>) -> String {
async fn prompt(
&self,
_question: impl AsRef<str> + MaybeSend + MaybeSync,
) -> String {
panic!("Unsupported operation")
}
}
Expand Down
49 changes: 35 additions & 14 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ pub mod queries;
pub mod wallet;

use std::collections::HashSet;
#[cfg(feature = "async-send")]
pub use std::marker::Send as MaybeSend;
#[cfg(feature = "async-send")]
pub use std::marker::Sync as MaybeSync;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -63,18 +67,28 @@ use crate::tx::{
};
use crate::wallet::{Wallet, WalletIo, WalletStorage};

#[async_trait::async_trait(?Send)]
#[cfg(not(feature = "async-send"))]
pub trait MaybeSync {}
#[cfg(not(feature = "async-send"))]
impl<T> MaybeSync for T where T: ?Sized {}
#[cfg(not(feature = "async-send"))]
pub trait MaybeSend {}
#[cfg(not(feature = "async-send"))]
impl<T> MaybeSend for T where T: ?Sized {}

#[cfg_attr(feature = "async-send", async_trait::async_trait)]
#[cfg_attr(not(feature = "async-send"), async_trait::async_trait(?Send))]
/// An interface for high-level interaction with the Namada SDK
pub trait Namada: Sized {
pub trait Namada: Sized + MaybeSync + MaybeSend {
/// A client with async request dispatcher method
type Client: queries::Client + Sync;
type Client: queries::Client + MaybeSend + Sync;
/// Captures the interactive parts of the wallet's functioning
type WalletUtils: WalletIo + WalletStorage;
type WalletUtils: WalletIo + WalletStorage + MaybeSend + MaybeSync;
/// Abstracts platform specific details away from the logic of shielded pool
/// operations.
type ShieldedUtils: ShieldedUtils;
type ShieldedUtils: ShieldedUtils + MaybeSend + MaybeSync;
/// Captures the input/output streams used by this object
type Io: Io;
type Io: Io + MaybeSend + MaybeSync;

/// Obtain the client for communicating with the ledger
fn client(&self) -> &Self::Client;
Expand Down Expand Up @@ -521,12 +535,18 @@ pub trait Namada: Sized {
}

/// Sign the given transaction using the given signing data
async fn sign<F: std::future::Future<Output = crate::error::Result<Tx>>>(
async fn sign<
F: MaybeSend
+ MaybeSync
+ std::future::Future<Output = crate::error::Result<Tx>>,
>(
&self,
tx: &mut Tx,
args: &args::Tx,
signing_data: SigningTxData,
with: impl Fn(Tx, common::PublicKey, HashSet<signing::Signable>) -> F,
with: impl MaybeSend
+ MaybeSync
+ Fn(Tx, common::PublicKey, HashSet<signing::Signable>) -> F,
) -> crate::error::Result<()> {
signing::sign_tx(self, args, tx, signing_data, with).await
}
Expand Down Expand Up @@ -564,7 +584,7 @@ pub trait Namada: Sized {
/// Provides convenience methods for common Namada interactions
pub struct NamadaImpl<C, U, V, I>
where
C: queries::Client + Sync,
C: queries::Client,
U: WalletIo,
V: ShieldedUtils,
I: Io,
Expand Down Expand Up @@ -650,13 +670,14 @@ where
}
}

#[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, U, V, I> Namada for NamadaImpl<C, U, V, I>
where
C: queries::Client + Sync,
U: WalletIo + WalletStorage,
V: ShieldedUtils,
I: Io,
C: queries::Client + MaybeSend + Sync,
U: WalletIo + WalletStorage + MaybeSync + MaybeSend,
V: ShieldedUtils + MaybeSend + MaybeSync,
I: Io + MaybeSend + MaybeSync,
{
type Client = C;
type Io = I;
Expand Down
Loading

0 comments on commit 336e9d2

Please sign in to comment.