feat: add get_highest_tx_by_sender to pools (#11514)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
tedison
2024-10-07 07:42:56 -04:00
committed by GitHub
parent f5c9701e72
commit f8228482ac
5 changed files with 43 additions and 14 deletions

View File

@ -13,7 +13,7 @@ use reth_provider::{
};
use reth_rpc_eth_types::{EthApiError, EthStateCache, PendingBlockEnv, RpcInvalidTransactionError};
use reth_rpc_types_compat::proof::from_primitive_account_proof;
use reth_transaction_pool::{PoolTransaction, TransactionPool};
use reth_transaction_pool::TransactionPool;
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, SpecId};
use crate::{EthApiTypes, FromEthApiError};
@ -280,23 +280,23 @@ pub trait LoadState: EthApiTypes {
if block_id == Some(BlockId::pending()) {
// for pending tag we need to find the highest nonce in the pool
let address_txs = this.pool().get_transactions_by_sender(address);
if let Some(highest_pool_nonce) =
address_txs.iter().map(|item| item.transaction.nonce()).max()
if let Some(highest_pool_tx) =
this.pool().get_highest_transaction_by_sender(address)
{
// and the corresponding txcount is nonce + 1
let next_nonce =
nonce.max(highest_pool_nonce).checked_add(1).ok_or_else(|| {
Self::Error::from(EthApiError::InvalidTransaction(
RpcInvalidTransactionError::NonceMaxValue,
))
})?;
{
// and the corresponding txcount is nonce + 1
let next_nonce =
nonce.max(highest_pool_tx.nonce()).checked_add(1).ok_or_else(|| {
Self::Error::from(EthApiError::InvalidTransaction(
RpcInvalidTransactionError::NonceMaxValue,
))
})?;
let tx_count = nonce.max(next_nonce);
return Ok(U256::from(tx_count))
let tx_count = nonce.max(next_nonce);
return Ok(U256::from(tx_count));
}
}
}
Ok(U256::from(nonce))
})
}

View File

@ -489,6 +489,13 @@ where
self.pool.get_transactions_by_sender(sender)
}
fn get_highest_transaction_by_sender(
&self,
sender: Address,
) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.pool.get_highest_transaction_by_sender(sender)
}
fn get_transaction_by_sender_and_nonce(
&self,
sender: Address,

View File

@ -206,6 +206,13 @@ impl TransactionPool for NoopTransactionPool {
vec![]
}
fn get_highest_transaction_by_sender(
&self,
_sender: Address,
) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
None
}
fn get_transaction_by_sender_and_nonce(
&self,
_sender: Address,

View File

@ -744,6 +744,15 @@ where
self.get_pool_data().get_transactions_by_sender(sender_id)
}
/// Returns the highest transaction of the address
pub(crate) fn get_highest_transaction_by_sender(
&self,
sender: Address,
) -> Option<Arc<ValidPoolTransaction<T::Transaction>>> {
let sender_id = self.get_sender_id(sender);
self.get_pool_data().get_highest_transaction_by_sender(sender_id)
}
/// Returns all transactions that where submitted with the given [`TransactionOrigin`]
pub(crate) fn get_transactions_by_origin(
&self,

View File

@ -334,6 +334,12 @@ pub trait TransactionPool: Send + Sync + Clone {
sender: Address,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
/// Returns the highest transaction sent by a given user
fn get_highest_transaction_by_sender(
&self,
sender: Address,
) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
/// Returns a transaction sent by a given user and a nonce
fn get_transaction_by_sender_and_nonce(
&self,