Skip to content

Commit

Permalink
Added test for nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest committed Oct 31, 2022
1 parent da16342 commit 21b9c15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion workspaces/src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Client {
rpc_addr: String,
rpc_client: JsonRpcClient,
/// AccessKey nonces to reference when sending transactions.
access_key_nonces: RwLock<HashMap<AccountId, AtomicU64>>,
pub(crate) access_key_nonces: RwLock<HashMap<AccountId, AtomicU64>>,
}

impl Client {
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions workspaces/tests/parallel_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!({
Expand All @@ -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)?,
}
}

Expand All @@ -78,5 +87,13 @@ async fn test_parallel_async() -> anyhow::Result<()> {
.json::<String>()?;
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(())
}

0 comments on commit 21b9c15

Please sign in to comment.