Skip to content

Commit

Permalink
allow specifying account address at config level instead of random as…
Browse files Browse the repository at this point in the history
…signment (#15465)

## Description
This is to change the behavior of assigning the account used for scripted txn. Previously we were randomly assigning available account to scripted txn, which causes inconsistency within the same scripted txn whenever it gets regenerated


## How Has This Been Tested?
<img width="953" alt="Screenshot 2024-12-03 at 11 40 46 AM" src="https://github.com/user-attachments/assets/fc669ef1-f4f3-4542-9798-2c21349a7fa1">


## Key Areas to Review
<!--
- Identify any critical parts of the code that require special attention or understanding. Explain why these parts are crucial to the functionality or architecture of the project.
- Point out any areas where complex logic has been implemented. Provide a brief explanation of the logic and your approach to make it easier for reviewers to follow.
- Highlight any areas where you are particularly concerned or unsure about the code's impact on the change. This can include potential performance or security issues, or compatibility with existing features.
-->

## Type of Change
- [ ] New feature
- [ X] Bug fix
- [ ] Breaking change
- [ ] Performance improvement
- [X ] Refactoring
- [ ] Dependency update
- [ ] Documentation update
- [ ] Tests

## Which Components or Systems Does This Change Impact?
- [ ] Validator Node
- [ ] Full Node (API, Indexer, etc.)
- [ ] Move/Aptos Virtual Machine
- [ ] Aptos Framework
- [ ] Aptos CLI/SDK
- [ ] Developer Infrastructure
- [ ] Move Compiler
- [ ] Other (specify)

## Checklist
- [ ] I have read and followed the [CONTRIBUTING](https://github.com/aptos-labs/aptos-core/blob/main/CONTRIBUTING.md) doc
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I identified and added all stakeholders and component owners affected by this change as reviewers
- [ ] I tested both happy and unhappy path of the functionality
- [ ] I have made corresponding changes to the documentation

<!-- Thank you for your contribution! -->
  • Loading branch information
yuunlimm authored Jan 13, 2025
1 parent d8a5642 commit 9f54e05
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
transactions:
- output_name: fa_double_transfer
script_path: fa_double_transfer
sender_address: __ACCOUNT_A__
sender_address: 7a2e070df14c96d0808ffab6b735591221241c34f6c4b4a9fa34051f945b5197

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
transactions:
- output_name: fa_mint_transfer_burn
script_path: fa_mint_transfer_burn
sender_address: __ACCOUNT_A__
sender_address: d857c1f1dc5303ffcdc47205ebddc02d485c9dce619fd4d00055d86b58143363

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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<Account>,
pub accounts: HashMap<String, Account>,
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Account {
pub public_key: String,
pub private_key: String,
Expand Down Expand Up @@ -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<Account> {
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<Account> {
match self.accounts.get(account_address) {
Some(account) => Ok(account.clone()),
None => anyhow::bail!(
"[Account Manager] Account not found for address: {}",
account_address
),
}
}
}
Expand All @@ -90,10 +92,12 @@ mod tests {
let _ = tokio::fs::create_dir(&testing_folder_root_path).await;
// Example content of the file.
let content = r#"accounts:
- private_key: "0x99978d48e7b2d50d0a7a3273db0929447ae59635e71118fa256af654c0ce56c9"
a531b7fdd7917f73ca216d89a8d9ce0cf7e7cfb9086ca6f6cbf9521532748d16:
private_key: "0x99978d48e7b2d50d0a7a3273db0929447ae59635e71118fa256af654c0ce56c9"
public_key: "0x39b4acc85e026dc056464a5ea00b98f858260eaad2b74dd30b86ae0d4d94ddf5"
account: a531b7fdd7917f73ca216d89a8d9ce0cf7e7cfb9086ca6f6cbf9521532748d16
- private_key: "0xe77498ac20ca67e8f642a6521077c8d5cba54853e7bed1e2c33b67e5a7b6c76e"
501b015c58f2a1a62a330a6da80dfee723f528f719d25a4232751986f9a9f43f:
private_key: "0xe77498ac20ca67e8f642a6521077c8d5cba54853e7bed1e2c33b67e5a7b6c76e"
public_key: "0xc92c8e7b4467e629ca8cd201a21564de39eea7cbe45b59bfd37f10b56e0a728c"
account: 501b015c58f2a1a62a330a6da80dfee723f528f719d25a4232751986f9a9f43f
"#;
Expand Down
18 changes: 18 additions & 0 deletions ecosystem/indexer-grpc/indexer-transaction-generator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = HashSet::new();
for entry in move_files {
let entry = entry?;
// entry has to be a file.
Expand All @@ -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<String> = 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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Account> = 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()));
Expand Down

0 comments on commit 9f54e05

Please sign in to comment.