Skip to content

Commit

Permalink
chore: simplify get transaction nonce (#7392)
Browse files Browse the repository at this point in the history
* chore: simplify get transaction nonce

* chore: rm option

* chore: cleanup
  • Loading branch information
mattsse authored Mar 13, 2024
1 parent ed8dec5 commit bd03d2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,7 +2535,7 @@ impl EthApi {
}
}

let nonce = self.backend.get_nonce(address, Some(block_request)).await?;
let nonce = self.backend.get_nonce(address, block_request).await?;

Ok(nonce)
}
Expand Down
29 changes: 12 additions & 17 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,19 +1789,19 @@ impl Backend {
pub async fn get_nonce(
&self,
address: Address,
block_request: Option<BlockRequest>,
block_request: BlockRequest,
) -> Result<U256, BlockchainError> {
if let Some(BlockRequest::Pending(pool_transactions)) = block_request.as_ref() {
if let BlockRequest::Pending(pool_transactions) = &block_request {
if let Some(value) = get_pool_transactions_nonce(pool_transactions, address) {
return Ok(value);
}
}
let final_block_request = match block_request {
Some(BlockRequest::Pending(_)) => Some(BlockRequest::Number(self.best_number())),
Some(BlockRequest::Number(bn)) => Some(BlockRequest::Number(bn)),
None => None,
BlockRequest::Pending(_) => BlockRequest::Number(self.best_number()),
BlockRequest::Number(bn) => BlockRequest::Number(bn),
};
self.with_database_at(final_block_request, |db, _| {

self.with_database_at(Some(final_block_request), |db, _| {
trace!(target: "backend", "get nonce for {:?}", address);
Ok(U256::from(db.basic_ref(address)?.unwrap_or_default().nonce))
})
Expand Down Expand Up @@ -2276,19 +2276,14 @@ fn get_pool_transactions_nonce(
pool_transactions: &[Arc<PoolTransaction>],
address: Address,
) -> Option<U256> {
let highest_nonce_tx = pool_transactions
if let Some(highest_nonce) = pool_transactions
.iter()
.filter(|tx| *tx.pending_transaction.sender() == address)
.reduce(|accum, item| {
let nonce = item.pending_transaction.nonce();
if nonce > accum.pending_transaction.nonce() {
item
} else {
accum
}
});
if let Some(highest_nonce_tx) = highest_nonce_tx {
return Some(highest_nonce_tx.pending_transaction.nonce().saturating_add(U256::from(1)));
.map(|tx| tx.pending_transaction.nonce())
.max()
{
let tx_count = highest_nonce.saturating_add(U256::from(1));
return Some(tx_count)
}
None
}
Expand Down

0 comments on commit bd03d2b

Please sign in to comment.