Skip to content

Commit

Permalink
Removed the lifetime in transact_async
Browse files Browse the repository at this point in the history
Addresses #248

There are probably further improvements I could make to the API of
workspace now that Client has a clone instance, but I wanted to start
with a PR with a small footprint and get feedback.
  • Loading branch information
DavidM-D committed Dec 2, 2022
1 parent 2a32573 commit 31da88f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
22 changes: 11 additions & 11 deletions workspaces/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::rpc::client::{
DEFAULT_CALL_FN_GAS,
};
use crate::rpc::query::{Query, ViewFunction};
use crate::rpc::BoxFuture;
use crate::types::{
AccessKey, AccountId, Balance, Gas, InMemorySigner, KeyType, PublicKey, SecretKey,
};
Expand All @@ -25,6 +24,7 @@ use near_primitives::views::FinalExecutionOutcomeView;
use std::convert::TryInto;
use std::fmt;
use std::future::IntoFuture;
use std::pin::Pin;
use std::task::Poll;

const MAX_GAS: Gas = 300_000_000_000_000;
Expand Down Expand Up @@ -248,7 +248,7 @@ impl<'a> Transaction<'a> {
/// of the transaction.
///
/// [`status`]: TransactionStatus::status
pub async fn transact_async(self) -> Result<TransactionStatus<'a>> {
pub async fn transact_async(self) -> Result<TransactionStatus> {
send_batch_tx_async_and_retry(self.client, &self.signer, &self.receiver_id, self.actions?)
.await
}
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a> CallTransaction<'a> {
/// of the transaction.
///
/// [`status`]: TransactionStatus::status
pub async fn transact_async(self) -> Result<TransactionStatus<'a>> {
pub async fn transact_async(self) -> Result<TransactionStatus> {
send_batch_tx_async_and_retry(
self.client,
&self.signer,
Expand Down Expand Up @@ -446,20 +446,20 @@ impl<'a, 'b> CreateAccountTransaction<'a, 'b> {
///
/// [`asynchronous transaction`]: https://docs.near.org/api/rpc/transactions#send-transaction-async
#[must_use]
pub struct TransactionStatus<'a> {
client: &'a Client,
pub struct TransactionStatus {
client: Client,
sender_id: AccountId,
hash: CryptoHash,
}

impl<'a> TransactionStatus<'a> {
impl TransactionStatus {
pub(crate) fn new(
client: &'a Client,
client: &Client,
id: AccountId,
hash: near_primitives::hash::CryptoHash,
) -> Self {
Self {
client,
client: client.clone(),
sender_id: id,
hash: CryptoHash(hash.0),
}
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<'a> TransactionStatus<'a> {
}
}

impl<'a> fmt::Debug for TransactionStatus<'a> {
impl<'a> fmt::Debug for TransactionStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TransactionStatus")
.field("sender_id", &self.sender_id)
Expand All @@ -521,9 +521,9 @@ impl<'a> fmt::Debug for TransactionStatus<'a> {
}
}

impl<'a> IntoFuture for TransactionStatus<'a> {
impl IntoFuture for TransactionStatus {
type Output = Result<ExecutionFinalResult>;
type IntoFuture = BoxFuture<'a, Self::Output>;
type IntoFuture = Pin<Box<dyn std::future::Future<Output = Self::Output>>>;

fn into_future(self) -> Self::IntoFuture {
Box::pin(async { self.wait().await })
Expand Down
25 changes: 14 additions & 11 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::RwLock;
Expand Down Expand Up @@ -36,11 +37,13 @@ pub(crate) const DEFAULT_CALL_DEPOSIT: Balance = 0;

/// A client that wraps around [`JsonRpcClient`], and provides more capabilities such
/// as retry w/ exponential backoff and utility functions for sending transactions.
#[derive(Clone)]
pub struct Client {
rpc_addr: String,
rpc_client: JsonRpcClient,
/// AccessKey nonces to reference when sending transactions.
pub(crate) access_key_nonces: RwLock<HashMap<(AccountId, near_crypto::PublicKey), AtomicU64>>,
pub(crate) access_key_nonces:
Arc<RwLock<HashMap<(AccountId, near_crypto::PublicKey), AtomicU64>>>,
}

impl Client {
Expand All @@ -51,7 +54,7 @@ impl Client {
Self {
rpc_client,
rpc_addr: rpc_addr.into(),
access_key_nonces: RwLock::new(HashMap::new()),
access_key_nonces: Arc::new(RwLock::new(HashMap::new())),
}
}

Expand Down Expand Up @@ -446,9 +449,9 @@ pub(crate) async fn send_batch_tx_and_retry(
let cache_key = (signer.account_id.clone(), signer.public_key());

retry(|| async {
let (block_hash, nonce) = fetch_tx_nonce(client, &cache_key).await?;
let (block_hash, nonce) = fetch_tx_nonce(&client, &cache_key).await?;
send_tx(
client,
&*client,
&cache_key,
SignedTransaction::from_actions(
nonce,
Expand All @@ -464,18 +467,18 @@ pub(crate) async fn send_batch_tx_and_retry(
.await
}

pub(crate) async fn send_batch_tx_async_and_retry<'a>(
client: &'a Client,
pub(crate) async fn send_batch_tx_async_and_retry(
client: &Client,
signer: &InMemorySigner,
receiver_id: &AccountId,
actions: Vec<Action>,
) -> Result<TransactionStatus<'a>> {
) -> Result<TransactionStatus> {
let signer = signer.inner();
let cache_key = (signer.account_id.clone(), signer.public_key());

retry(|| async {
let (block_hash, nonce) = fetch_tx_nonce(client, &cache_key).await?;
let hash = client
let (block_hash, nonce) = fetch_tx_nonce(&client, &cache_key).await?;
let hash = &client
.query(&methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
signed_transaction: SignedTransaction::from_actions(
nonce,
Expand All @@ -490,9 +493,9 @@ pub(crate) async fn send_batch_tx_async_and_retry<'a>(
.map_err(|e| RpcErrorCode::BroadcastTxFailure.custom(e))?;

Ok(TransactionStatus::new(
client,
&client.clone(),
signer.account_id.clone(),
hash,
*hash,
))
})
.await
Expand Down

0 comments on commit 31da88f

Please sign in to comment.