ci: fix nonce manager test (#2014)

* Use Anvil instead of Goerli to test the nonce manager

* restore comment

* fix: explicitly assign `inner.max_priority_fee_per_gas`

Co-authored-by: Andrea Simeoni <>
This commit is contained in:
Andrea Simeoni 2023-01-06 10:29:46 +01:00 committed by GitHub
parent 7e6c3ba983
commit 5917e842d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 26 deletions

View File

@ -1,28 +1,22 @@
#![cfg(all(not(target_arch = "wasm32"), not(feature = "celo")))] #![cfg(all(not(target_arch = "wasm32"), not(feature = "celo")))]
use ethers_core::types::*; use ethers_core::{types::*, utils::Anvil};
use ethers_middleware::{nonce_manager::NonceManagerMiddleware, signer::SignerMiddleware}; use ethers_middleware::MiddlewareBuilder;
use ethers_providers::Middleware; use ethers_providers::{Http, Middleware, Provider};
use ethers_signers::{LocalWallet, Signer};
use std::time::Duration;
#[tokio::test] #[tokio::test]
async fn nonce_manager() { async fn nonce_manager() {
let provider = ethers_providers::GOERLI.provider().interval(Duration::from_millis(2000)); let anvil = Anvil::new().spawn();
let chain_id = provider.get_chainid().await.unwrap().as_u64(); let endpoint = anvil.endpoint();
let wallet = std::env::var("GOERLI_PRIVATE_KEY") let provider = Provider::<Http>::try_from(endpoint).unwrap();
.expect("GOERLI_PRIVATE_KEY is not defined") let accounts = provider.get_accounts().await.unwrap();
.parse::<LocalWallet>() let address = accounts[0];
.unwrap() let to = accounts[1];
.with_chain_id(chain_id);
let address = wallet.address();
let provider = SignerMiddleware::new(provider, wallet);
// the nonce manager must be over the Client so that it overrides the nonce // the nonce manager must be over the Client so that it overrides the nonce
// before the client gets it // before the client gets it
let provider = NonceManagerMiddleware::new(provider, address); let provider = provider.nonce_manager(address);
let nonce = provider let nonce = provider
.get_transaction_count(address, Some(BlockNumber::Pending.into())) .get_transaction_count(address, Some(BlockNumber::Pending.into()))
@ -34,22 +28,16 @@ async fn nonce_manager() {
let mut tx_hashes = Vec::with_capacity(num_tx); let mut tx_hashes = Vec::with_capacity(num_tx);
for _ in 0..num_tx { for _ in 0..num_tx {
let tx = provider let tx = provider
.send_transaction( .send_transaction(TransactionRequest::new().from(address).to(to).value(100u64), None)
Eip1559TransactionRequest::new().to(address).value(100u64).chain_id(chain_id),
None,
)
.await .await
.unwrap(); .unwrap();
tx_hashes.push(*tx); tx_hashes.push(*tx);
} }
// sleep a bit to ensure there's no flakiness in the test
tokio::time::sleep(Duration::from_secs(10)).await;
let mut nonces = Vec::with_capacity(num_tx); let mut nonces = Vec::with_capacity(num_tx);
for tx_hash in tx_hashes { for tx_hash in tx_hashes {
nonces.push(provider.get_transaction(tx_hash).await.unwrap().unwrap().nonce.as_u64()); nonces.push(provider.get_transaction(tx_hash).await.unwrap().unwrap().nonce.as_u64());
} }
assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::<Vec<_>>()) assert_eq!(nonces, (nonce..nonce + num_tx as u64).collect::<Vec<_>>());
} }

View File

@ -359,10 +359,10 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
// - first: if set, set to the min(current value, MFPG) // - first: if set, set to the min(current value, MFPG)
// - second, if still unset, use the RPC estimated amount // - second, if still unset, use the RPC estimated amount
let mfpg = inner.max_fee_per_gas.get_or_insert(max_fee_per_gas); let mfpg = inner.max_fee_per_gas.get_or_insert(max_fee_per_gas);
inner inner.max_priority_fee_per_gas = inner
.max_priority_fee_per_gas .max_priority_fee_per_gas
.map(|tip| std::cmp::min(tip, *mfpg)) .map(|tip| std::cmp::min(tip, *mfpg))
.get_or_insert(max_priority_fee_per_gas); .or(Some(max_priority_fee_per_gas));
}; };
} }
} }