diff --git a/src/pool/mempool.rs b/src/pool/mempool.rs index ec6b10db3..de5ad2818 100644 --- a/src/pool/mempool.rs +++ b/src/pool/mempool.rs @@ -150,25 +150,31 @@ impl AccountM // 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")); } - // Shuffle the accounts randomly before iterating - accounts.shuffle(&mut rng); - + // Create the future locks with indices for more efficient removal // use [`select_all`] to poll an iterator over impl Future)> // 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.retain(|(addr, _)| *addr != account_address); + accounts.swap_remove(index); continue; }