Skip to content

Commit

Permalink
randomize relayers round (#1450)
Browse files Browse the repository at this point in the history
* randomize relayers round

* fix comments
  • Loading branch information
tcoratger authored Oct 11, 2024
1 parent 07e914b commit 1455145
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ alloy-eips = { version = "0.4.2", default-features = false }
anyhow = { version = "1", default-features = false, optional = true }
arbitrary = { version = "1", features = ["derive"], optional = true }
foundry-config = { git = "https://github.com/foundry-rs/foundry", branch = "master", optional = true }
rand = { version = "0.8", default-features = false, optional = true }
rand = { version = "0.8", default-features = false }
rayon = { version = "1", default-features = false, optional = true }
rstest = { version = "0.21", default-features = false, optional = true }
serde_with = { version = "3.9", default-features = false, optional = true }
Expand Down Expand Up @@ -204,7 +204,6 @@ testing = [
"foundry-config",
"katana-primitives",
"mockall",
"rand",
"rayon",
"reth-testing-utils",
"rstest",
Expand All @@ -222,7 +221,7 @@ testing = [
]
binaries = ["clap"]
hive = []
arbitrary = ["rand", "dep:arbitrary"]
arbitrary = ["dep:arbitrary"]

[[bin]]
name = "katana_genesis"
Expand Down
26 changes: 21 additions & 5 deletions src/pool/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};
use alloy_primitives::{Address, U256};
use futures::future::select_all;
use rand::{seq::SliceRandom, SeedableRng};
use reth_chainspec::ChainSpec;
use reth_execution_types::ChangedAccount;
use reth_primitives::BlockNumberOrTag;
Expand Down Expand Up @@ -143,22 +144,37 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
where
SP: starknet::providers::Provider + Send + Sync + Clone + 'static,
{
let mut accounts = self.accounts.iter().collect::<HashMap<_, _>>();
// Use `StdRng` instead of `ThreadRng` as it is `Send`
let mut rng = rand::rngs::StdRng::from_entropy();

// Collect the accounts into a vector for shuffling
let mut accounts: Vec<_> = self.accounts.iter().collect();

// Shuffle the accounts randomly before iterating
accounts.shuffle(&mut rng);

loop {
if accounts.is_empty() {
return Err(eyre::eyre!("failed to fetch funded account"));
}

// Create the future locks with indices for more efficient removal
// use [`select_all`] to poll an iterator over impl Future<Output = (Felt, MutexGuard<Felt>)>
// We use Box::pin because this Future doesn't implement `Unpin`.
let fut_locks = accounts.iter().map(|(address, nonce)| Box::pin(async { (*address, nonce.lock().await) }));
let ((account_address, guard), _, _) = select_all(fut_locks).await;
let fut_locks = accounts
.iter()
.enumerate()
.map(|(index, (address, nonce))| Box::pin(async move { (index, *address, nonce.lock().await) }));

// Select the first account that gets unlocked
let ((index, account_address, guard), _, _) = select_all(fut_locks).await;

// Fetch the balance of the selected account
let balance = self.get_balance(*account_address).await?;

// If the balance is lower than the threshold, continue
// If the balance is lower than the threshold, remove the account using swap_remove
if balance < U256::from(ONE_TENTH_ETH) {
accounts.remove(account_address);
accounts.swap_remove(index);
continue;
}

Expand Down

0 comments on commit 1455145

Please sign in to comment.