Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sequence numbers for local account pool in transaction generator #15229

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ impl SubmissionWorker {
)
.await;

self.txn_generator
.update_sequence_numbers(&latest_fetched_counts);

for account in self.accounts.iter_mut() {
update_account_seq_num(
Arc::get_mut(account).unwrap(),
Expand Down
42 changes: 35 additions & 7 deletions crates/transaction-generator-lib/src/call_custom_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ use crate::{
};
use aptos_logger::{error, info};
use aptos_sdk::{
move_types::account_address::AccountAddress,
transaction_builder::TransactionFactory,
types::{transaction::SignedTransaction, LocalAccount},
};
use async_trait::async_trait;
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use std::{borrow::Borrow, sync::Arc};
use std::{borrow::Borrow, collections::HashMap, sync::Arc};

// Fn + Send + Sync, as it will be called from multiple threads simultaneously
// if you need any coordination, use Arc<RwLock<X>> fields
Expand All @@ -27,6 +28,8 @@ pub type TransactionGeneratorWorker = dyn Fn(
+ Send
+ Sync;

pub type SequenceNumberUpdateWorker = dyn Fn(&HashMap<AccountAddress, u64>) + Send + Sync;

#[async_trait]
pub trait UserModuleTransactionGenerator: Sync + Send {
/// Called for each instance of the module we publish,
Expand Down Expand Up @@ -54,13 +57,16 @@ pub trait UserModuleTransactionGenerator: Sync + Send {
txn_executor: &dyn ReliableTransactionSubmitter,
rng: &mut StdRng,
) -> Arc<TransactionGeneratorWorker>;

async fn create_sequence_number_update_fn(&self) -> Option<Arc<SequenceNumberUpdateWorker>>;
}

pub struct CustomModulesDelegationGenerator {
rng: StdRng,
txn_factory: TransactionFactory,
packages: Arc<Vec<(Package, LocalAccount)>>,
txn_generator: Arc<TransactionGeneratorWorker>,
sequence_number_updater: Option<Arc<SequenceNumberUpdateWorker>>,
}

impl CustomModulesDelegationGenerator {
Expand All @@ -69,12 +75,14 @@ impl CustomModulesDelegationGenerator {
txn_factory: TransactionFactory,
packages: Arc<Vec<(Package, LocalAccount)>>,
txn_generator: Arc<TransactionGeneratorWorker>,
sequence_number_updater: Option<Arc<SequenceNumberUpdateWorker>>,
) -> Self {
Self {
rng,
txn_factory,
packages,
txn_generator,
sequence_number_updater,
}
}
}
Expand Down Expand Up @@ -102,12 +110,22 @@ impl TransactionGenerator for CustomModulesDelegationGenerator {
}
requests
}

fn update_sequence_numbers(
&mut self,
latest_fetched_sequence_numbers: &HashMap<AccountAddress, u64>,
) {
if let Some(sequence_number_updater) = &self.sequence_number_updater {
(sequence_number_updater)(latest_fetched_sequence_numbers);
}
}
}

pub struct CustomModulesDelegationGeneratorCreator {
txn_factory: TransactionFactory,
packages: Arc<Vec<(Package, LocalAccount)>>,
txn_generator: Arc<TransactionGeneratorWorker>,
sequence_number_updater: Option<Arc<SequenceNumberUpdateWorker>>,
}

impl CustomModulesDelegationGeneratorCreator {
Expand All @@ -116,11 +134,13 @@ impl CustomModulesDelegationGeneratorCreator {
txn_factory: TransactionFactory,
packages: Arc<Vec<(Package, LocalAccount)>>,
txn_generator: Arc<TransactionGeneratorWorker>,
sequence_number_updater: Option<Arc<SequenceNumberUpdateWorker>>,
) -> Self {
Self {
txn_factory,
packages,
txn_generator,
sequence_number_updater,
}
}

Expand All @@ -142,7 +162,7 @@ impl CustomModulesDelegationGeneratorCreator {
None,
)
.await;
let worker = Self::create_worker(
let (txn_generator_worker, sequence_number_update_worker) = Self::create_worker(
init_txn_factory,
root_account,
txn_executor,
Expand All @@ -153,7 +173,8 @@ impl CustomModulesDelegationGeneratorCreator {
Self {
txn_factory,
packages: Arc::new(packages),
txn_generator: worker,
txn_generator: txn_generator_worker,
sequence_number_updater: sequence_number_update_worker,
}
}

Expand All @@ -163,7 +184,10 @@ impl CustomModulesDelegationGeneratorCreator {
txn_executor: &dyn ReliableTransactionSubmitter,
packages: &mut [(Package, LocalAccount)],
workload: &mut dyn UserModuleTransactionGenerator,
) -> Arc<TransactionGeneratorWorker> {
) -> (
Arc<TransactionGeneratorWorker>,
Option<Arc<SequenceNumberUpdateWorker>>,
) {
let mut rng = StdRng::from_entropy();
let mut requests_initialize = Vec::with_capacity(packages.len());

Expand All @@ -189,9 +213,12 @@ impl CustomModulesDelegationGeneratorCreator {

info!("Done preparing workload for {} packages", packages.len());

workload
.create_generator_fn(root_account, &init_txn_factory, txn_executor, &mut rng)
.await
(
workload
.create_generator_fn(root_account, &init_txn_factory, txn_executor, &mut rng)
.await,
workload.create_sequence_number_update_fn().await,
)
}

pub async fn publish_package(
Expand Down Expand Up @@ -273,6 +300,7 @@ impl TransactionGeneratorCreator for CustomModulesDelegationGeneratorCreator {
self.txn_factory.clone(),
self.packages.clone(),
self.txn_generator.clone(),
self.sequence_number_updater.clone(),
))
}
}
8 changes: 7 additions & 1 deletion crates/transaction-generator-lib/src/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use super::{
ReliableTransactionSubmitter,
};
use crate::{
call_custom_modules::{TransactionGeneratorWorker, UserModuleTransactionGenerator},
call_custom_modules::{
SequenceNumberUpdateWorker, TransactionGeneratorWorker, UserModuleTransactionGenerator,
},
create_account_transaction,
publishing::module_simple::MultiSigConfig,
RootAccountHandle,
Expand Down Expand Up @@ -113,4 +115,8 @@ impl UserModuleTransactionGenerator for EntryPointTransactionGenerator {
})
})
}

async fn create_sequence_number_update_fn(&self) -> Option<Arc<SequenceNumberUpdateWorker>> {
None
}
}
10 changes: 10 additions & 0 deletions crates/transaction-generator-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ pub trait TransactionGenerator: Sync + Send {
account: &LocalAccount,
num_to_create: usize,
) -> Vec<SignedTransaction>;

// The transaction emitter calls this function when it fetches the latest sequence numbers for the senders of
// the transactions it has sent. If the transaction generator is maintaining a local pool of accounts (usually happens
// in workflow transaction type), the transaction generator should update the sequence numbers of those accounts in the pool.
fn update_sequence_numbers(
&mut self,
_latest_fetched_sequence_numbers: &HashMap<AccountAddress, u64>,
) {
// Default implementation does nothing
}
}

#[async_trait]
Expand Down
48 changes: 26 additions & 22 deletions crates/transaction-generator-lib/src/workflow_delegator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,28 @@ impl WorkflowTxnGeneratorCreator {
)
.await;

let mint_worker = CustomModulesDelegationGeneratorCreator::create_worker(
init_txn_factory.clone(),
root_account,
txn_executor,
&mut packages,
&mut EntryPointTransactionGenerator {
entry_point: mint_entry_point,
},
)
.await;
let burn_worker = CustomModulesDelegationGeneratorCreator::create_worker(
init_txn_factory.clone(),
root_account,
txn_executor,
&mut packages,
&mut EntryPointTransactionGenerator {
entry_point: burn_entry_point,
},
)
.await;
let (mint_txn_generator_worker, mint_update_seq_num_worker) =
CustomModulesDelegationGeneratorCreator::create_worker(
init_txn_factory.clone(),
root_account,
txn_executor,
&mut packages,
&mut EntryPointTransactionGenerator {
entry_point: mint_entry_point,
},
)
.await;
let (burn_txn_generator_worker, burn_update_seq_num_worker) =
CustomModulesDelegationGeneratorCreator::create_worker(
init_txn_factory.clone(),
root_account,
txn_executor,
&mut packages,
&mut EntryPointTransactionGenerator {
entry_point: burn_entry_point,
},
)
.await;

let packages = Arc::new(packages);

Expand All @@ -328,7 +330,8 @@ impl WorkflowTxnGeneratorCreator {
Box::new(CustomModulesDelegationGeneratorCreator::new_raw(
txn_factory.clone(),
packages.clone(),
mint_worker,
mint_txn_generator_worker,
mint_update_seq_num_worker,
)),
created_pool.clone(),
Some(minted_pool.clone()),
Expand All @@ -337,7 +340,8 @@ impl WorkflowTxnGeneratorCreator {
Box::new(CustomModulesDelegationGeneratorCreator::new_raw(
txn_factory.clone(),
packages.clone(),
burn_worker,
burn_txn_generator_worker,
burn_update_seq_num_worker,
)),
minted_pool.clone(),
Some(burnt_pool.clone()),
Expand Down
Loading