From 5cad83d6ffb7bd2054c3dd95d957e46e95699d44 Mon Sep 17 00:00:00 2001 From: yuunlimm Date: Tue, 3 Dec 2024 11:48:46 -0800 Subject: [PATCH] allow specifying account address at config level instead of random assignment --- .../move_fixtures/fa_double_transfer.yaml | 2 +- .../move_fixtures/fa_mint_transfer_burn.yaml | 2 +- .../move_fixtures/simple_user_script.yaml | 4 ++-- .../move_fixtures/simple_user_script2.yaml | 4 ++-- .../testing_accounts.yaml | 21 ++++++++++++------- .../src/accont_manager.rs | 20 ++++++++++-------- .../src/config.rs | 18 ++++++++++++++++ .../src/script_transaction_generator.rs | 15 ++++--------- 8 files changed, 52 insertions(+), 34 deletions(-) diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_double_transfer.yaml b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_double_transfer.yaml index 35871efc024e29..280ff6c451b8da 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_double_transfer.yaml +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_double_transfer.yaml @@ -1,5 +1,5 @@ transactions: - output_name: fa_double_transfer script_path: fa_double_transfer - sender_address: __ACCOUNT_A__ + sender_address: 7a2e070df14c96d0808ffab6b735591221241c34f6c4b4a9fa34051f945b5197 \ No newline at end of file diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_mint_transfer_burn.yaml b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_mint_transfer_burn.yaml index dbb41021a2079d..06c0b226724dbb 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_mint_transfer_burn.yaml +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/fa_mint_transfer_burn.yaml @@ -1,5 +1,5 @@ transactions: - output_name: fa_mint_transfer_burn script_path: fa_mint_transfer_burn - sender_address: __ACCOUNT_A__ + sender_address: d857c1f1dc5303ffcdc47205ebddc02d485c9dce619fd4d00055d86b58143363 \ No newline at end of file diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script.yaml b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script.yaml index 07b7428a361375..5c6bba9a0845b8 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script.yaml +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script.yaml @@ -1,8 +1,8 @@ transactions: - output_name: simple_user_script1 script_path: simple_user_script - sender_address: __ACCOUNT_A__ + sender_address: 765d8c8d4d5859f43a56e2756fbf5f3d2483dbaa14f3fb62872df820d6e64eff - output_name: simple_user_script2 script_path: simple_user_script2 - sender_address: __ACCOUNT_A__ + sender_address: 765d8c8d4d5859f43a56e2756fbf5f3d2483dbaa14f3fb62872df820d6e64eff \ No newline at end of file diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script2.yaml b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script2.yaml index 548e0a53f27ad0..f0aec47b7a052d 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script2.yaml +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/move_fixtures/simple_user_script2.yaml @@ -1,8 +1,8 @@ transactions: - output_name: simple_user_script3 script_path: simple_user_script - sender_address: __ACCOUNT_A__ + sender_address: 501b015c58f2a1a62a330a6da80dfee723f528f719d25a4232751986f9a9f43f - output_name: simple_user_script4 script_path: simple_user_script2 - sender_address: __ACCOUNT_B__ + sender_address: 8f0de18409d6fca18c72fac4062fc0f9baa6404296fed93a3ad0250fb671f8b3 \ No newline at end of file diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/testing_accounts.yaml b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/testing_accounts.yaml index f0cbcdd8cfe701..1908d54c85c0b9 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/testing_accounts.yaml +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/testing_accounts.yaml @@ -1,22 +1,27 @@ # This file serves the accounts to use during account generation. # These are generated in localnet and safe to share. accounts: - - private_key: "0x99978d48e7b2d50d0a7a3273db0929447ae59635e71118fa256af654c0ce56c9" + a531b7fdd7917f73ca216d89a8d9ce0cf7e7cfb9086ca6f6cbf9521532748d16: + private_key: "0x99978d48e7b2d50d0a7a3273db0929447ae59635e71118fa256af654c0ce56c9" public_key: "0x39b4acc85e026dc056464a5ea00b98f858260eaad2b74dd30b86ae0d4d94ddf5" - account: a531b7fdd7917f73ca216d89a8d9ce0cf7e7cfb9086ca6f6cbf9521532748d16 - - private_key: "0xe77498ac20ca67e8f642a6521077c8d5cba54853e7bed1e2c33b67e5a7b6c76e" + account: "a531b7fdd7917f73ca216d89a8d9ce0cf7e7cfb9086ca6f6cbf9521532748d16" + 501b015c58f2a1a62a330a6da80dfee723f528f719d25a4232751986f9a9f43f: + private_key: "0xe77498ac20ca67e8f642a6521077c8d5cba54853e7bed1e2c33b67e5a7b6c76e" public_key: "0xc92c8e7b4467e629ca8cd201a21564de39eea7cbe45b59bfd37f10b56e0a728c" account: 501b015c58f2a1a62a330a6da80dfee723f528f719d25a4232751986f9a9f43f - - private_key: "0x76a641118ffb5ca5f9de4fe414a7d216d89616e74bec2c445324a2f0ab609ab6" + 8f0de18409d6fca18c72fac4062fc0f9baa6404296fed93a3ad0250fb671f8b3: + private_key: "0x76a641118ffb5ca5f9de4fe414a7d216d89616e74bec2c445324a2f0ab609ab6" public_key: "0xef05bede15f422e16c0002e3cee8b4d4341518d99c4695352a1869b0779864fb" account: 8f0de18409d6fca18c72fac4062fc0f9baa6404296fed93a3ad0250fb671f8b3 - - private_key: "0xf24423f014e6f2fdd1914d6961b49a2cfc9eac59b88a57457e4cd9424fc140c8" + 765d8c8d4d5859f43a56e2756fbf5f3d2483dbaa14f3fb62872df820d6e64eff: + private_key: "0xf24423f014e6f2fdd1914d6961b49a2cfc9eac59b88a57457e4cd9424fc140c8" public_key: "0xb298975d27dbff3020e5ee7fdbbad8a969d4f2a2d5286e097d1db9760d04dd31" account: 765d8c8d4d5859f43a56e2756fbf5f3d2483dbaa14f3fb62872df820d6e64eff - - private_key: "0xd5dc637987a9ca85f52be2ff583a408ae5747a99f2fe85de416c9339e669a86b" + 7a2e070df14c96d0808ffab6b735591221241c34f6c4b4a9fa34051f945b5197: + private_key: "0xd5dc637987a9ca85f52be2ff583a408ae5747a99f2fe85de416c9339e669a86b" public_key: "0x5f776d9fcfb99f7d348ac0e1782d56c27b80eab5043f94a1b127ef8a90122d43" account: 7a2e070df14c96d0808ffab6b735591221241c34f6c4b4a9fa34051f945b5197 - - private_key: "0x9a2a409701f7ca41251cc2a4a0442ec44a450133b0d23564fc09191e763fd551" + d857c1f1dc5303ffcdc47205ebddc02d485c9dce619fd4d00055d86b58143363: + private_key: "0x9a2a409701f7ca41251cc2a4a0442ec44a450133b0d23564fc09191e763fd551" public_key: "0x294d3cca5f78884ad20a8e15845ead196418aaf49a2b52234c3ff88bfba83d22" account: d857c1f1dc5303ffcdc47205ebddc02d485c9dce619fd4d00055d86b58143363 - \ No newline at end of file diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/src/accont_manager.rs b/ecosystem/indexer-grpc/indexer-transaction-generator/src/accont_manager.rs index 393573a3e51cda..7680148359ef91 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/src/accont_manager.rs +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/src/accont_manager.rs @@ -3,17 +3,17 @@ use anyhow::Context; use serde::{Deserialize, Serialize}; -use std::path::Path; +use std::{collections::HashMap, path::Path}; const FAUCET_URL: &str = "http://localhost:8081"; const REST_URL: &str = "http://localhost:8080"; #[derive(Debug, Serialize, Deserialize)] pub struct AccountManager { - pub accounts: Vec, + pub accounts: HashMap, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct Account { pub public_key: String, pub private_key: String, @@ -66,15 +66,17 @@ impl AccountManager { ))?; let account_manager: AccountManager = serde_yaml::from_str(&file) .context("[Account Manager] Failed to parse account list file")?; + Ok(account_manager) } - pub fn allocate_account(&mut self) -> anyhow::Result { - match self.accounts.pop() { - Some(account) => Ok(account), - None => { - anyhow::bail!("[Account Manager] No more account to allocate; please add more.") - }, + pub fn get_account(&mut self, account_address: &str) -> anyhow::Result { + match self.accounts.get(account_address) { + Some(account) => Ok(account.clone()), + None => anyhow::bail!( + "[Account Manager] Account not found for address: {}", + account_address + ), } } } diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/src/config.rs b/ecosystem/indexer-grpc/indexer-transaction-generator/src/config.rs index 356e3502d7fb49..b674de63ccfbf7 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/src/config.rs +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/src/config.rs @@ -67,6 +67,7 @@ impl IndexerCliArgs { // Scan all yaml files in the move folder path. let mut script_transactions_vec: Vec<(String, ScriptTransactions)> = vec![]; let move_files = std::fs::read_dir(&move_folder_path)?; + let mut used_sender_addresses: HashSet = HashSet::new(); for entry in move_files { let entry = entry?; // entry has to be a file. @@ -79,6 +80,23 @@ impl IndexerCliArgs { let script_transactions_raw: String = tokio::fs::read_to_string(&path).await?; let script_transactions: ScriptTransactions = serde_yaml::from_str(&script_transactions_raw)?; + + let new_senders: HashSet = script_transactions + .transactions + .iter() + .map(|txn| txn.sender_address.clone()) + .collect(); + // Check if any new sender is already used + if new_senders + .iter() + .any(|sender| used_sender_addresses.contains(sender)) + { + return Err(anyhow::anyhow!( + "[Script Transaction Generator] Sender address in file `{}` is already being used", + file_name + )); + } + used_sender_addresses.extend(new_senders); script_transactions_vec.push((file_name.to_string(), script_transactions)); } } diff --git a/ecosystem/indexer-grpc/indexer-transaction-generator/src/script_transaction_generator.rs b/ecosystem/indexer-grpc/indexer-transaction-generator/src/script_transaction_generator.rs index 79d404f65ead09..7a8431236ca34e 100644 --- a/ecosystem/indexer-grpc/indexer-transaction-generator/src/script_transaction_generator.rs +++ b/ecosystem/indexer-grpc/indexer-transaction-generator/src/script_transaction_generator.rs @@ -35,21 +35,14 @@ impl ScriptTransactions { account_manager: &mut AccountManager, ) -> anyhow::Result<()> { // Get all accounts that'll be used in this run. - // TODO: improve this to support account address as argument. - let mut account_symbols: HashMap = HashMap::new(); - for transaction in &self.transactions { - account_symbols.insert( - transaction.sender_address.clone(), - account_manager.allocate_account()?, - ); - } let mut versions_to_capture = vec![]; for transaction in &self.transactions { - let sender_account = account_symbols - .get(transaction.sender_address.as_str()) + let sender_account = account_manager + .get_account(&transaction.sender_address) .unwrap(); + let version = self - .execute_script_transaction(move_folder_path, transaction, sender_account) + .execute_script_transaction(move_folder_path, transaction, &sender_account) .await?; if let Some(output_name) = &transaction.output_name { versions_to_capture.push((version, output_name.clone()));