Skip to content

Commit

Permalink
AccountManager: implement start_nonce_updater (#1442)
Browse files Browse the repository at this point in the history
* AccountManager: implement start_nonce_updater

* bump kakarot

* Update src/pool/mempool.rs

Co-authored-by: greged93 <[email protected]>

---------

Co-authored-by: greged93 <[email protected]>
  • Loading branch information
tcoratger and greged93 authored Oct 10, 2024
1 parent b2e9f23 commit 53a9e6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -193,6 +193,7 @@ testing = [
"alloy-json-abi",
"alloy-signer-local",
"alloy-signer",
"alloy-consensus",
"anyhow",
"dep:arbitrary",
"dojo-test-utils",
Expand Down
29 changes: 29 additions & 0 deletions src/pool/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> 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`]
Expand Down Expand Up @@ -181,6 +185,31 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
.await
.map_err(Into::into)
}

/// Update the nonces for all accounts every minute.
pub fn start_nonce_updater(self: Arc<Self>) {
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)]
Expand Down
3 changes: 2 additions & 1 deletion src/providers/eth_provider/database/types/receipt.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +19,8 @@ impl From<StoredTransactionReceipt> 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<Self> {
use alloy_primitives::{Address, Bloom, B256};

let receipt = Receipt::arbitrary(u)?;

let mut logs = Vec::new();
Expand Down

0 comments on commit 53a9e6d

Please sign in to comment.