Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
feat: batch nonce relayers requests (#1491)
Browse files Browse the repository at this point in the history
* use batch requests for the nonce of the relayers

Signed-off-by: Gregory Edison <[email protected]>

* fix latest to pending

Signed-off-by: Gregory Edison <[email protected]>

---------

Signed-off-by: Gregory Edison <[email protected]>
  • Loading branch information
greged93 authored Oct 25, 2024
1 parent dd15346 commit f005ea1
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/pool/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use reth_transaction_pool::{
TransactionOrigin, TransactionPool, TransactionPoolExt,
};
use starknet::{
core::types::{BlockTag, Felt},
providers::{jsonrpc::HttpTransport, JsonRpcClient},
core::types::{requests::GetNonceRequest, BlockId, BlockTag, Felt},
providers::{jsonrpc::HttpTransport, JsonRpcClient, ProviderRequestData, ProviderResponseData},
};
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio::sync::Mutex;
Expand Down Expand Up @@ -208,19 +208,33 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
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);
// Convert the account addresses into batch requests
let requests = self
.accounts
.keys()
.map(|add| {
ProviderRequestData::GetNonce(GetNonceRequest {
contract_address: *add,
block_id: BlockId::Tag(BlockTag::Pending),
})
})
.collect::<Vec<_>>();

// Try to make the request to the provider. If it fails, display error and retry 1 minute later.
let maybe_new_nonces = self.eth_client.starknet_provider().batch_requests(requests).await;
if maybe_new_nonces.is_err() {
tracing::error!(target: "account_manager", err = ?maybe_new_nonces.unwrap_err(), "failed to get nonces");
// Sleep for 1 minute before the next update
tokio::time::sleep(Duration::from_secs(60)).await;
continue;
}
let new_nonces = maybe_new_nonces.expect("not error");

for ((address, old_nonce), new_nonce) in self.accounts.iter().zip(new_nonces) {
if let ProviderResponseData::GetNonce(new_nonce) = new_nonce {
*old_nonce.lock().await = new_nonce;
tracing::info!(target: "account_manager", ?address, ?new_nonce);
};
}

// Sleep for 1 minute before the next update
Expand Down

0 comments on commit f005ea1

Please sign in to comment.