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

feat: fix nonce deadlock #408

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
56 changes: 50 additions & 6 deletions crates/pevm/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use alloy_primitives::TxKind;
use alloy_rpc_types::Receipt;
use hashbrown::HashMap;
Expand Down Expand Up @@ -376,13 +378,42 @@ impl<'a, S: Storage, C: PevmChain> Database for VmDb<'a, S, C> {
if location_hash == self.from_hash
&& self.tx.nonce.is_some_and(|nonce| nonce != account.nonce)
{
if self.tx_idx > 0 {
// TODO: Better retry strategy -- immediately, to the
// closest sender tx, to the missing sender tx, etc.
return Err(ReadError::Blocking(self.tx_idx - 1));
} else {
return Err(ReadError::InvalidNonce(self.tx_idx));
let get_parent = |tx_idx: TxIdx| {
let parent_tx_idx = unsafe { *self.vm.parent_tx_idxs.get_unchecked(tx_idx) };
if parent_tx_idx == tx_idx {
None
} else {
Some(parent_tx_idx)
}
};

// BEGIN: nonce check
let mut current_parent: Option<TxIdx> = get_parent(self.tx_idx);
for read_origin in read_origins.iter() {
match read_origin {
ReadOrigin::MvMemory(tx_version) => {
if let Some(parent) = current_parent {
match parent.cmp(&tx_version.tx_idx) {
Ordering::Less => {}
Ordering::Equal => {
current_parent = get_parent(parent);
}
Ordering::Greater => {
return Err(ReadError::Blocking(parent));
}
}
}
}
ReadOrigin::Storage => {
if let Some(parent) = current_parent {
return Err(ReadError::Blocking(parent));
}
}
}
}
// END: nonce check

return Err(ReadError::InvalidNonce(self.tx_idx));
}

// Fully evaluate the account and register it to read cache
Expand Down Expand Up @@ -500,6 +531,7 @@ pub(crate) struct Vm<'a, S: Storage, C: PevmChain> {
spec_id: SpecId,
beneficiary_location_hash: MemoryLocationHash,
reward_policy: RewardPolicy,
parent_tx_idxs: Vec<TxIdx>, // parent_tx_idxs[i] = i if i does not have parent
}

impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> {
Expand All @@ -511,6 +543,17 @@ impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> {
txs: &'a [TxEnv],
spec_id: SpecId,
) -> Self {
let mut parent_tx_idxs: Vec<_> = txs.iter().enumerate().map(|(index, _)| index).collect();
let mut address_to_tx_idx =
HashMap::with_capacity_and_hasher(txs.len(), BuildSuffixHasher::default());

for (tx_idx, tx) in txs.iter().enumerate() {
// We use the fact that `insert` returns the previous value.
if let Some(parent_tx_idx) = address_to_tx_idx.insert(tx.caller, tx_idx) {
parent_tx_idxs[tx_idx] = parent_tx_idx;
}
}

Self {
storage,
mv_memory,
Expand All @@ -522,6 +565,7 @@ impl<'a, S: Storage, C: PevmChain> Vm<'a, S, C> {
block_env.coinbase,
)),
reward_policy: chain.get_reward_policy(),
parent_tx_idxs,
}
}

Expand Down