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

AccountManager: implement start_nonce_updater #1442

Merged
merged 3 commits into from
Oct 10, 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
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
Loading