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

randomize relayers round #1450

Merged
merged 2 commits into from
Oct 11, 2024
Merged
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
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
Eikix marked this conversation as resolved.
Show resolved Hide resolved
if balance < U256::from(ONE_TENTH_ETH) {
accounts.remove(account_address);
accounts.swap_remove(index);
continue;
}

Expand Down
Loading