From 53a9e6d837a565754896d8275d81af724a4e5f13 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 10 Oct 2024 14:29:52 +0200 Subject: [PATCH] AccountManager: implement `start_nonce_updater` (#1442) * AccountManager: implement start_nonce_updater * bump kakarot * Update src/pool/mempool.rs Co-authored-by: greged93 <82421016+greged93@users.noreply.github.com> --------- Co-authored-by: greged93 <82421016+greged93@users.noreply.github.com> --- Cargo.toml | 3 +- src/pool/mempool.rs | 29 +++++++++++++++++++ .../eth_provider/database/types/receipt.rs | 3 +- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71031faa4..6c6214693 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ alloy-rpc-types = { version = "0.4.2", features = [ "eth", "arbitrary", ], default-features = false } -alloy-consensus = { version = "0.4.2", default-features = false } +alloy-consensus = { version = "0.4.2", default-features = false, optional = true } alloy-rpc-types-txpool = { version = "0.4.2", default-features = false } alloy-rpc-types-trace = { version = "0.4.2", default-features = false } jsonrpsee = { version = "0.24", features = ["macros", "server"] } @@ -193,6 +193,7 @@ testing = [ "alloy-json-abi", "alloy-signer-local", "alloy-signer", + "alloy-consensus", "anyhow", "dep:arbitrary", "dojo-test-utils", diff --git a/src/pool/mempool.rs b/src/pool/mempool.rs index e1031634c..587725a8c 100644 --- a/src/pool/mempool.rs +++ b/src/pool/mempool.rs @@ -69,6 +69,10 @@ impl AccountM /// Starts the account manager task that periodically checks account balances and processes transactions. pub fn start(self) { let this = Arc::new(self); + + // Start the nonce updater in a separate task + this.clone().start_nonce_updater(); + tokio::spawn(async move { loop { // TODO: add a listener on the pool and only try to call [`best_transaction`] @@ -181,6 +185,31 @@ impl AccountM .await .map_err(Into::into) } + + /// Update the nonces for all accounts every minute. + pub fn start_nonce_updater(self: Arc) { + tokio::spawn(async move { + loop { + for (address, nonce_mutex) in &self.accounts { + // Query the updated nonce for the account from the provider + let new_nonce = self + .eth_client + .starknet_provider() + .get_nonce(starknet::core::types::BlockId::Tag(BlockTag::Pending), *address) + .await + .unwrap_or_default(); + + let mut nonce = nonce_mutex.lock().await; + *nonce = new_nonce; + + tracing::info!(target: "account_manager", ?address, ?new_nonce); + } + + // Sleep for 1 minute before the next update + tokio::time::sleep(Duration::from_secs(60)).await; + } + }); + } } #[derive(Default)] diff --git a/src/providers/eth_provider/database/types/receipt.rs b/src/providers/eth_provider/database/types/receipt.rs index 7068a6a6d..1ae864ebf 100644 --- a/src/providers/eth_provider/database/types/receipt.rs +++ b/src/providers/eth_provider/database/types/receipt.rs @@ -1,4 +1,3 @@ -use alloy_primitives::{Address, Bloom, B256}; use alloy_rpc_types::TransactionReceipt; #[cfg(any(test, feature = "arbitrary", feature = "testing"))] use reth_primitives::Receipt; @@ -20,6 +19,8 @@ impl From for TransactionReceipt { #[cfg(any(test, feature = "arbitrary", feature = "testing"))] impl<'a> arbitrary::Arbitrary<'a> for StoredTransactionReceipt { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + use alloy_primitives::{Address, Bloom, B256}; + let receipt = Receipt::arbitrary(u)?; let mut logs = Vec::new();