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

chore: simplify get transaction nonce #7392

Merged
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
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),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is always Some now?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ye, would just do the Some in the call to with_database_at as a nit

Copy link
Member Author

@mattsse mattsse Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, we don't need the option here at all, removed

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
Loading