From 21b9c15acbecbc707c48aa6d62447d4be03f6654 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Thu, 27 Oct 2022 15:59:35 -0700 Subject: [PATCH] Added test for nonce --- workspaces/src/operations.rs | 2 +- workspaces/src/rpc/client.rs | 4 ++-- workspaces/tests/parallel_transactions.rs | 23 ++++++++++++++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/workspaces/src/operations.rs b/workspaces/src/operations.rs index e9b9f654..a329aa6c 100644 --- a/workspaces/src/operations.rs +++ b/workspaces/src/operations.rs @@ -349,7 +349,7 @@ impl<'a> CallTransaction<'a> { &self.contract_id, vec![FunctionCallAction { args: self.function.args?, - method_name: self.function.name.into(), + method_name: self.function.name, gas: self.function.gas, deposit: self.function.deposit, } diff --git a/workspaces/src/rpc/client.rs b/workspaces/src/rpc/client.rs index f3a24544..a5f345f3 100644 --- a/workspaces/src/rpc/client.rs +++ b/workspaces/src/rpc/client.rs @@ -40,7 +40,7 @@ pub struct Client { rpc_addr: String, rpc_client: JsonRpcClient, /// AccessKey nonces to reference when sending transactions. - access_key_nonces: RwLock>, + pub(crate) access_key_nonces: RwLock>, } impl Client { @@ -331,7 +331,7 @@ impl Client { } } -async fn access_key( +pub(crate) async fn access_key( client: &Client, account_id: near_primitives::account::id::AccountId, public_key: near_crypto::PublicKey, diff --git a/workspaces/tests/parallel_transactions.rs b/workspaces/tests/parallel_transactions.rs index c96717f3..94c8d2a3 100644 --- a/workspaces/tests/parallel_transactions.rs +++ b/workspaces/tests/parallel_transactions.rs @@ -48,9 +48,16 @@ async fn test_parallel_async() -> anyhow::Result<()> { let contract = worker.dev_deploy(STATUS_MSG_CONTRACT).await?; let account = worker.dev_create_account().await?; + // nonce of access key before any transactions occured. + let nonce_start = worker + .view_access_key(account.id(), &account.secret_key().public_key()) + .await? + .nonce; + // Create a queue statuses we can check the status of later. let mut statuses = VecDeque::new(); - for msg in ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"] { + let messages = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + for msg in messages { let status = account .call(contract.id(), "set_status") .args_json(json!({ @@ -64,8 +71,10 @@ async fn test_parallel_async() -> anyhow::Result<()> { // Retry checking the statuses of all transactions until the queue is empty // with all transactions completed. while let Some(status) = statuses.pop_front() { - if matches!(status.status().await, TransactionPoll::Pending) { - statuses.push_back(status); + match status.status().await { + TransactionPoll::Ready(_) => (), + TransactionPoll::Pending => statuses.push_back(status), + TransactionPoll::Error(err) => Err(err)?, } } @@ -78,5 +87,13 @@ async fn test_parallel_async() -> anyhow::Result<()> { .json::()?; assert_eq!(final_set_msg, "j"); + let nonce_end = worker + .view_access_key(account.id(), &account.secret_key().public_key()) + .await? + .nonce; + + // The amount of transactions should equal the increase in nonce: + assert!(nonce_end - nonce_start == messages.len() as u64); + Ok(()) }