Skip to content
Open
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
13 changes: 13 additions & 0 deletions crates/module-system/module-implementations/sov-evm/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ pub(crate) fn from_recovered_with_block_context(
alloy_rpc_types::Transaction::from_transaction(tx.convert(), tx_info)
}

pub(crate) fn from_recovered_pending(
tx: Recovered<TransactionSigned>,
) -> alloy_rpc_types::Transaction {
let tx_info = TransactionInfo {
block_hash: None,
block_number: None,
index: None,
hash: None,
base_fee: None,
};
alloy_rpc_types::Transaction::from_transaction(tx.convert(), tx_info)
}

#[cfg(test)]
mod tests {
use alloy_primitives::{Address, U256};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::into_rpc_error;
use crate::rpc::error::ensure_success;
use alloy_consensus::ReceiptEnvelope;
use alloy_consensus::Transaction as TransactionTrait;
use alloy_eips::BlockId;
use alloy_primitives::{Address, U64};
use alloy_primitives::{Bytes, B256, U256};
Expand Down Expand Up @@ -153,16 +154,32 @@ where
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<U64> {
let mut state = self.resolve_state_for_block_id(block_id, state)?;

let ethereum_address: EthereumAddress = address.into();
let credential_id = ethereum_address.as_credential_id();

let nonce = self
.uniqueness_module
.next_nonce(&credential_id, state.deref_mut())
.unwrap_or_default();

let block_id = block_id.unwrap_or_else(BlockId::latest);
let is_pending = block_id.is_latest() || block_id.is_pending();

let nonce = {
let mut resolved_state = self.resolve_state_for_block_id(Some(block_id), state)?;
let ethereum_address: EthereumAddress = address.into();
let credential_id = ethereum_address.as_credential_id();
self.uniqueness_module
.next_nonce(&credential_id, resolved_state.deref_mut())
.unwrap_or_default()
};

let pending_nonce = if is_pending {
let pending_txs: Vec<_> = self.pending_transactions.collect_infallible(state);
pending_txs
.iter()
.filter(|pending| pending.transaction.signer == address)
.map(|pending| pending.transaction.signed_transaction.nonce())
.max()
.map(|nonce| nonce.saturating_add(1))
.unwrap_or(0)
} else {
0
};
Comment on lines +169 to +180
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential edge case with nonce gaps. If pending transactions have non-sequential nonces (e.g., nonces 5 and 10), this returns 11, but nonce 6-9 would still be valid. Standard Ethereum behavior tracks all pending nonces, not just the max.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/module-system/module-implementations/sov-evm/src/rpc/handlers.rs
Line: 169:180

Comment:
Potential edge case with nonce gaps. If pending transactions have non-sequential nonces (e.g., nonces 5 and 10), this returns 11, but nonce 6-9 would still be valid. Standard Ethereum behavior tracks all pending nonces, not just the max.

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a good point!


let nonce = nonce.max(pending_nonce);
trace!(%address, nonce, method = "eth_getTransactionCount", "EVM module JSON-RPC request");
Ok(U64::from(nonce))
}
Expand Down Expand Up @@ -217,7 +234,9 @@ where
hash: B256,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<Transaction>> {
let transaction = self.get_transaction(hash, state);
let transaction = self
.get_transaction(hash, state)
.or_else(|| self.get_pending_transaction(hash, state));
trace!(
%hash,
?transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::error::into_rpc_error;
use crate::evm::executor;
use crate::evm::primitive_types::{Receipt, TransactionSigned, TxSignedAndRecovered};
use crate::executor::get_cfg_env;
use crate::helpers::{from_recovered_with_block_context, prepare_call_env};
use crate::helpers::{from_recovered_pending, from_recovered_with_block_context, prepare_call_env};
pub use crate::primitive_types::MaybeSealedBlock;
use crate::{verify_contract_creation_allowlist, Evm, SealedBlock};
use alloy_consensus::{transaction::Recovered, Transaction as TransactionTrait, TxReceipt};
Expand Down Expand Up @@ -151,6 +151,18 @@ where
Some(tx)
}

fn get_pending_transaction(
&self,
hash: B256,
state: &mut ApiStateAccessor<S>,
) -> Option<Transaction> {
let pending_transactions: Vec<_> = self.pending_transactions.collect_infallible(state);
let pending = pending_transactions
.iter()
.find(|pending| *pending.transaction.signed_transaction.hash() == hash)?;
Some(from_recovered_pending(pending.transaction.clone().into()))
}

fn get_receipt_by_hash(
&self,
hash: B256,
Expand Down