From 35d4056424337f62bf949cc00be6d2cf23c7ab3e Mon Sep 17 00:00:00 2001 From: angieyth Date: Fri, 10 Feb 2023 10:34:19 -0800 Subject: [PATCH] refactor root_account and make it async base on #5894 --- api/src/tests/accounts_test.rs | 16 +++--- api/src/tests/resource_groups.rs | 7 ++- api/src/tests/state_test.rs | 6 +-- api/src/tests/string_resource_test.rs | 4 +- api/src/tests/transactions_test.rs | 70 +++++++++++++-------------- api/src/tests/view_function.rs | 8 +-- api/test-context/src/test_context.rs | 41 +++++++++++----- 7 files changed, 85 insertions(+), 67 deletions(-) diff --git a/api/src/tests/accounts_test.rs b/api/src/tests/accounts_test.rs index 066554107ddad..ba9ffcbbd43fd 100644 --- a/api/src/tests/accounts_test.rs +++ b/api/src/tests/accounts_test.rs @@ -111,12 +111,12 @@ async fn test_account_modules_structs() { async fn test_get_account_resources_by_ledger_version() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn.clone()]).await; let ledger_version_1_resources = context .get(&account_resources( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), )) .await; let root_account = find_value(&ledger_version_1_resources, |f| { @@ -126,7 +126,7 @@ async fn test_get_account_resources_by_ledger_version() { let ledger_version_0_resources = context .get(&account_resources_with_ledger_version( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), 0, )) .await; @@ -142,7 +142,7 @@ async fn test_get_account_resources_by_too_large_ledger_version() { let resp = context .expect_status_code(404) .get(&account_resources_with_ledger_version( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), 1000000000000000000, )) .await; @@ -155,7 +155,7 @@ async fn test_get_account_resources_by_invalid_ledger_version() { let resp = context .expect_status_code(400) .get(&account_resources_with_ledger_version( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), -1, )) .await; @@ -168,7 +168,7 @@ async fn test_get_account_resources_by_invalid_ledger_version() { async fn test_get_account_modules_by_ledger_version() { let mut context = new_test_context(current_function_name!()); let code = "a11ceb0b0300000006010002030205050703070a0c0816100c260900000001000100000102084d794d6f64756c650269640000000000000000000000000b1e55ed00010000000231010200"; - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let txn = root_account.sign_with_transaction_builder( context .transaction_factory() @@ -177,7 +177,7 @@ async fn test_get_account_modules_by_ledger_version() { context.commit_block(&vec![txn.clone()]).await; let modules = context .get(&account_modules( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), )) .await; @@ -185,7 +185,7 @@ async fn test_get_account_modules_by_ledger_version() { let modules = context .get(&account_modules_with_ledger_version( - &context.root_account().address().to_hex_literal(), + &context.root_account().await.address().to_hex_literal(), 0, )) .await; diff --git a/api/src/tests/resource_groups.rs b/api/src/tests/resource_groups.rs index ddf29f468114b..575669266ddc2 100644 --- a/api/src/tests/resource_groups.rs +++ b/api/src/tests/resource_groups.rs @@ -22,10 +22,9 @@ async fn test_read_resource_group() { let mut context = new_test_context(current_function_name!()); // Prepare accounts - let mut root = context.root_account(); - let mut admin0 = context.create_account(&mut root).await; - let mut admin1 = context.create_account(&mut root).await; - let mut user = context.create_account(&mut root).await; + let mut admin0 = context.create_account().await; + let mut admin1 = context.create_account().await; + let mut user = context.create_account().await; // Publish packages let named_addresses = vec![ diff --git a/api/src/tests/state_test.rs b/api/src/tests/state_test.rs index 51a7fbed9f321..24aca3cca66a8 100644 --- a/api/src/tests/state_test.rs +++ b/api/src/tests/state_test.rs @@ -129,7 +129,7 @@ async fn test_merkle_leaves_with_nft_transfer() { let ctx = &mut context; let creator = &mut ctx.gen_account(); let owner = &mut ctx.gen_account(); - let txn1 = ctx.mint_user_account(creator); + let txn1 = ctx.mint_user_account(creator).await; let txn2 = ctx.account_transfer(creator, owner, 100_000); let collection_name = "collection name".to_owned().into_bytes(); @@ -222,14 +222,14 @@ async fn test_get_table_item() { let ctx = &mut context; let mut account = ctx.gen_account(); let acc = &mut account; - let txn = ctx.create_user_account(acc); + let txn = ctx.create_user_account(acc).await; ctx.commit_block(&vec![txn.clone()]).await; make_test_tables(ctx, acc).await; // get the TestTables instance let tt = ctx .api_get_account_resource( - acc, + acc.address(), &acc.address().to_hex_literal(), "TableTestData", "TestTables", diff --git a/api/src/tests/string_resource_test.rs b/api/src/tests/string_resource_test.rs index 30ac3ffeb33c5..9949c8a4df5bd 100644 --- a/api/src/tests/string_resource_test.rs +++ b/api/src/tests/string_resource_test.rs @@ -14,7 +14,7 @@ use std::convert::TryInto; async fn test_renders_move_acsii_string_into_utf8_string() { let mut context = new_test_context(current_function_name!()); let mut account = init_test_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn]).await; // module 0x87342d91af60c3a883a2812c9294c2f8::Message { @@ -45,7 +45,7 @@ async fn test_renders_move_acsii_string_into_utf8_string() { let message = context .api_get_account_resource( - &account, + account.address(), &account.address().to_hex_literal(), "Message", "MessageHolder", diff --git a/api/src/tests/transactions_test.rs b/api/src/tests/transactions_test.rs index c246d69e9964f..f4db0b0bfa29f 100644 --- a/api/src/tests/transactions_test.rs +++ b/api/src/tests/transactions_test.rs @@ -48,7 +48,7 @@ async fn test_get_transactions_output_genesis_transaction() { async fn test_get_transactions_returns_last_page_when_start_version_is_not_specified() { let mut context = new_test_context(current_function_name!()); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; for _i in 0..20 { let account = context.gen_account(); let txn = context.create_user_account_by(&mut root_account, &account); @@ -112,7 +112,7 @@ async fn test_get_transactions_param_limit_exceeds_limit() { async fn test_get_transactions_output_user_transaction_with_entry_function_payload() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn.clone()]).await; let txns = context.get("/transactions?start=1").await; @@ -126,7 +126,7 @@ async fn test_get_transactions_output_user_transaction_with_entry_function_paylo async fn test_get_transactions_output_user_transaction_with_module_payload() { let mut context = new_test_context(current_function_name!()); let code = "a11ceb0b0300000006010002030205050703070a0c0816100c260900000001000100000102084d794d6f64756c650269640000000000000000000000000b1e55ed00010000000231010200"; - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let txn = root_account.sign_with_transaction_builder( context .transaction_factory() @@ -172,7 +172,7 @@ async fn test_get_transactions_output_user_transaction_with_module_payload() { async fn test_post_bcs_format_transaction() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; let body = bcs::to_bytes(&txn).unwrap(); let resp = context .expect_status_code(202) @@ -201,7 +201,7 @@ async fn test_post_invalid_bcs_format_transaction() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_post_invalid_signature_transaction() { let mut context = new_test_context(current_function_name!()); - let txn = context.create_invalid_signature_transaction(); + let txn = context.create_invalid_signature_transaction().await; let body = bcs::to_bytes(&txn).unwrap(); let resp = context .expect_status_code(400) @@ -215,8 +215,8 @@ async fn test_post_transaction_rejected_by_mempool() { let mut context = new_test_context(current_function_name!()); let account1 = context.gen_account(); let account2 = context.gen_account(); - let txn1 = context.create_user_account(&account1); - let txn2 = context.create_user_account(&account2); + let txn1 = context.create_user_account(&account1).await; + let txn2 = context.create_user_account(&account2).await; context .expect_status_code(202) @@ -236,7 +236,7 @@ async fn test_multi_agent_signed_transaction() { let account = context.gen_account(); let secondary = context.gen_account(); let factory = context.transaction_factory(); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; // Create secondary signer account context @@ -305,7 +305,7 @@ async fn test_multi_ed25519_signed_transaction() { let auth_key = AuthenticationKey::multi_ed25519(&public_key); let factory = context.transaction_factory(); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; // TODO: migrate once multi-ed25519 is supported let create_account_txn = root_account.sign_with_transaction_builder( factory.create_user_account(&Ed25519PrivateKey::generate_for_testing().public_key()), @@ -367,7 +367,7 @@ async fn test_multi_ed25519_signed_transaction() { async fn test_get_transaction_by_hash() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn.clone()]).await; let txns = context.get("/transactions?start=2&limit=1").await; @@ -419,7 +419,7 @@ async fn test_get_transaction_by_version_not_found() { async fn test_get_transaction_by_version() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn.clone()]).await; let txns = context.get("/transactions?start=2&limit=1").await; @@ -433,7 +433,7 @@ async fn test_get_transaction_by_version() { async fn test_get_pending_transaction_by_hash() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; let body = bcs::to_bytes(&txn).unwrap(); let pending_txn = context .expect_status_code(202) @@ -468,7 +468,7 @@ async fn test_get_pending_transaction_by_hash() { async fn test_signing_message_with_entry_function_payload() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; let payload = json!({ "type": "entry_function_payload", "function": "0x1::aptos_account::create_account", @@ -485,7 +485,7 @@ async fn test_signing_message_with_payload( txn: SignedTransaction, payload: serde_json::Value, ) { - let sender = context.root_account(); + let sender = context.root_account().await; let mut body = json!({ "sender": sender.address().to_hex_literal(), "sequence_number": sender.sequence_number().to_string(), @@ -517,6 +517,7 @@ async fn test_signing_message_with_payload( let sig = context .root_account() + .await .private_key() .sign_arbitrary_message(signing_msg.inner()); let expected_sig = match txn.authenticator() { @@ -550,14 +551,14 @@ async fn test_signing_message_with_payload( async fn test_get_account_transactions() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn]).await; let txns = context .get( format!( "/accounts/{}/transactions", - context.root_account().address() + context.root_account().await.address() ) .as_str(), ) @@ -571,14 +572,14 @@ async fn test_get_account_transactions() { async fn test_get_account_transactions_filter_transactions_by_start_sequence_number() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn]).await; let txns = context .get( format!( "/accounts/{}/transactions?start=1", - context.root_account().address() + context.root_account().await.address() ) .as_str(), ) @@ -590,14 +591,14 @@ async fn test_get_account_transactions_filter_transactions_by_start_sequence_num async fn test_get_account_transactions_filter_transactions_by_start_sequence_number_is_too_large() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); - let txn = context.create_user_account(&account); + let txn = context.create_user_account(&account).await; context.commit_block(&vec![txn]).await; let txns = context .get( format!( "/accounts/{}/transactions?start=1000", - context.root_account().address() + context.root_account().await.address() ) .as_str(), ) @@ -608,7 +609,7 @@ async fn test_get_account_transactions_filter_transactions_by_start_sequence_num #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_account_transactions_filter_transactions_by_limit() { let mut context = new_test_context(current_function_name!()); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let account1 = context.gen_account(); let txn1 = context.create_user_account_by(&mut root_account, &account1); let account2 = context.gen_account(); @@ -619,7 +620,7 @@ async fn test_get_account_transactions_filter_transactions_by_limit() { .get( format!( "/accounts/{}/transactions?start=0&limit=1", - context.root_account().address() + context.root_account().await.address() ) .as_str(), ) @@ -630,7 +631,7 @@ async fn test_get_account_transactions_filter_transactions_by_limit() { .get( format!( "/accounts/{}/transactions?start=0&limit=2", - context.root_account().address() + context.root_account().await.address() ) .as_str(), ) @@ -641,7 +642,7 @@ async fn test_get_account_transactions_filter_transactions_by_limit() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_invalid_script_payload_bytecode() { let context = new_test_context(current_function_name!()); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let invalid_bytecode = hex::decode("a11ceb0b030000").unwrap(); let txn = root_account.sign_with_transaction_builder( context @@ -655,7 +656,7 @@ async fn test_get_txn_execute_failed_by_invalid_script_payload_bytecode() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_invalid_entry_function_address() { let context = new_test_context(current_function_name!()); - let account = context.root_account(); + let account = context.root_account().await; test_get_txn_execute_failed_by_invalid_entry_function( context, account, @@ -674,7 +675,7 @@ async fn test_get_txn_execute_failed_by_invalid_entry_function_address() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_invalid_entry_function_module_name() { let context = new_test_context(current_function_name!()); - let account = context.root_account(); + let account = context.root_account().await; test_get_txn_execute_failed_by_invalid_entry_function( context, account, @@ -693,7 +694,7 @@ async fn test_get_txn_execute_failed_by_invalid_entry_function_module_name() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_invalid_entry_function_name() { let context = new_test_context(current_function_name!()); - let account = context.root_account(); + let account = context.root_account().await; test_get_txn_execute_failed_by_invalid_entry_function( context, account, @@ -712,7 +713,7 @@ async fn test_get_txn_execute_failed_by_invalid_entry_function_name() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_invalid_entry_function_arguments() { let context = new_test_context(current_function_name!()); - let account = context.root_account(); + let account = context.root_account().await; test_get_txn_execute_failed_by_invalid_entry_function( context, account, @@ -731,7 +732,7 @@ async fn test_get_txn_execute_failed_by_invalid_entry_function_arguments() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_missing_entry_function_arguments() { let context = new_test_context(current_function_name!()); - let account = context.root_account(); + let account = context.root_account().await; test_get_txn_execute_failed_by_invalid_entry_function( context, account, @@ -752,7 +753,7 @@ async fn test_get_txn_execute_failed_by_entry_function_validation() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); context - .commit_block(&vec![context.create_user_account(&account)]) + .commit_block(&vec![context.create_user_account(&account).await]) .await; test_get_txn_execute_failed_by_invalid_entry_function( @@ -775,7 +776,7 @@ async fn test_get_txn_execute_failed_by_entry_function_invalid_module_name() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); context - .commit_block(&vec![context.create_user_account(&account)]) + .commit_block(&vec![context.create_user_account(&account).await]) .await; test_submit_entry_function_api_validation( @@ -798,7 +799,7 @@ async fn test_get_txn_execute_failed_by_entry_function_invalid_function_name() { let mut context = new_test_context(current_function_name!()); let account = context.gen_account(); context - .commit_block(&vec![context.create_user_account(&account)]) + .commit_block(&vec![context.create_user_account(&account).await]) .await; test_submit_entry_function_api_validation( @@ -819,8 +820,7 @@ async fn test_get_txn_execute_failed_by_entry_function_invalid_function_name() { #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_get_txn_execute_failed_by_entry_function_execution_failure() { let mut context = new_test_context(current_function_name!()); - let mut root = context.root_account(); - let mut admin = context.create_account(&mut root).await; + let mut admin = context.create_account().await; let named_addresses = vec![ ("entry_func_fail".to_string(), admin.address()), @@ -864,7 +864,7 @@ async fn test_get_txn_execute_failed_by_script_execution_failure() { let script = hex::decode("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102") .unwrap(); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let txn = root_account.sign_with_transaction_builder( context .transaction_factory() diff --git a/api/src/tests/view_function.rs b/api/src/tests/view_function.rs index 7a0193c3f8b33..9aeda2c8f101f 100644 --- a/api/src/tests/view_function.rs +++ b/api/src/tests/view_function.rs @@ -10,7 +10,7 @@ async fn test_simple_view() { let mut context = new_test_context(current_function_name!()); let creator = &mut context.gen_account(); let owner = &mut context.gen_account(); - let txn1 = context.mint_user_account(creator); + let txn1 = context.mint_user_account(creator).await; let txn2 = context.account_transfer(creator, owner, 100_000); context.commit_block(&vec![txn1, txn2]).await; @@ -34,7 +34,7 @@ async fn test_simple_view_invalid() { let mut context = new_test_context(current_function_name!()); let creator = &mut context.gen_account(); let owner = &mut context.gen_account(); - let txn1 = context.mint_user_account(creator); + let txn1 = context.mint_user_account(creator).await; let txn2 = context.account_transfer(creator, owner, 100_000); context.commit_block(&vec![txn1, txn2]).await; @@ -60,7 +60,7 @@ async fn test_versioned_simple_view() { let mut context = new_test_context(current_function_name!()); let creator = &mut context.gen_account(); let owner = &mut context.gen_account(); - let txn1 = context.mint_user_account(creator); + let txn1 = context.mint_user_account(creator).await; let txn2 = context.account_transfer(creator, owner, 100_000); let txn3 = context.account_transfer(creator, owner, 100_000); @@ -94,7 +94,7 @@ async fn test_view_tuple() { // } // } let tuple_module = hex::decode("a11ceb0b0500000006010002030205050704070b1b0826200c461900000001000100000203030d5461626c6554657374446174610c72657475726e5f7475706c65000000000000000000000000000000000000000000000000000000000a550c180001000000030601000000000000000602000000000000000200").unwrap(); - let mut root_account = context.root_account(); + let mut root_account = context.root_account().await; let module_txn = root_account .sign_with_transaction_builder(context.transaction_factory().module(tuple_module)); diff --git a/api/test-context/src/test_context.rs b/api/test-context/src/test_context.rs index 0ef37f50b5826..2ca84c808c43a 100644 --- a/api/test-context/src/test_context.rs +++ b/api/test-context/src/test_context.rs @@ -295,8 +295,15 @@ impl TestContext { TransactionFactory::new(self.context.chain_id()) } - pub fn root_account(&self) -> LocalAccount { - LocalAccount::new(aptos_test_root_address(), self.root_key.private_key(), 0) + pub async fn root_account(&self) -> LocalAccount { + // Fetch the actual root account's sequence number in case it has been used to sign + // transactions before. + let root_sequence_number = self.get_sequence_number(aptos_test_root_address()).await; + LocalAccount::new( + aptos_test_root_address(), + self.root_key.private_key(), + root_sequence_number, + ) } pub fn latest_state_view(&self) -> DbStateView { @@ -309,7 +316,8 @@ impl TestContext { LocalAccount::generate(self.rng()) } - pub async fn create_account(&mut self, root: &mut LocalAccount) -> LocalAccount { + pub async fn create_account(&mut self) -> LocalAccount { + let mut root = self.root_account().await; let account = self.gen_account(); let factory = self.transaction_factory(); let txn = root.sign_with_transaction_builder( @@ -325,13 +333,13 @@ impl TestContext { self.commit_mempool_txns(1).await; account } - pub fn create_user_account(&self, account: &LocalAccount) -> SignedTransaction { - let mut tc = self.root_account(); + pub async fn create_user_account(&self, account: &LocalAccount) -> SignedTransaction { + let mut tc = self.root_account().await; self.create_user_account_by(&mut tc, account) } - pub fn mint_user_account(&self, account: &LocalAccount) -> SignedTransaction { - let mut tc = self.root_account(); + pub async fn mint_user_account(&self, account: &LocalAccount) -> SignedTransaction { + let mut tc = self.root_account().await; let factory = self.transaction_factory(); tc.sign_with_transaction_builder( factory @@ -367,9 +375,9 @@ impl TestContext { ) } - pub fn create_invalid_signature_transaction(&mut self) -> SignedTransaction { + pub async fn create_invalid_signature_transaction(&mut self) -> SignedTransaction { let factory = self.transaction_factory(); - let root_account = self.root_account(); + let root_account = self.root_account().await; let txn = factory .transfer(root_account.address(), 1) .sender(root_account.address()) @@ -504,10 +512,21 @@ impl TestContext { .unwrap(); } + pub async fn get_sequence_number(&self, account: AccountAddress) -> u64 { + let account_resource = self + .api_get_account_resource(account, "0x1", "account", "Account") + .await; + account_resource["data"]["sequence_number"] + .as_str() + .unwrap() + .parse::() + .unwrap() + } + // TODO: Add support for generic_type_params if necessary. pub async fn api_get_account_resource( &self, - account: &LocalAccount, + account: AccountAddress, resource_account_address: &str, module: &str, name: &str, @@ -515,7 +534,7 @@ impl TestContext { let resources = self .get(&format!( "/accounts/{}/resources", - account.address().to_hex_literal() + account.to_hex_literal() )) .await; let vals: Vec = serde_json::from_value(resources).unwrap();