Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
fix deposit tx fail receipts
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Sep 18, 2023
1 parent 61709e3 commit 7477805
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 24 deletions.
58 changes: 47 additions & 11 deletions crates/payload/basic/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ where
Err(err) => {
if sequencer_tx.is_deposit() {
// Manually bump the nonce and include a receipt for the deposit transaction.
// let sender = sequencer_tx.signer();
// fail_deposit_tx!(
// db,
// sender,
// block_number,
// sequencer_tx,
// &mut post_state,
// &mut cumulative_gas_used,
// is_regolith,
// PayloadBuilderError::AccountLoadFailed(sender)
// );
let tx_signer = sequencer_tx.signer();
fail_deposit_tx!(
db,
tx_signer,
block.number,
sequencer_tx,
&mut receipts,
&mut cumulative_gas_used,
is_regolith,
PayloadBuilderError::AccountLoadFailed(tx_signer)
);
executed_txs.push(sequencer_tx.into_signed());
continue
}
Expand Down Expand Up @@ -467,3 +467,39 @@ where
optimism_payload_builder(args)
}
}

/// If the Deposited transaction failed, the deposit must still be included. In this case, we need
/// to increment the sender nonce and disregard the state changes. The transaction is also recorded
/// as using all gas unless it is a system transaction.
#[macro_export]
macro_rules! fail_deposit_tx {
(
$db:expr,
$sender:ident,
$block_number:expr,
$transaction:ident,
$receipts:expr,
$cumulative_gas_used:expr,
$is_regolith:ident,
$error:expr
) => {
let mut sender_account = $db.basic($sender).ok().flatten().ok_or($error)?;
let old_nonce = sender_account.nonce;
sender_account.nonce += 1;

$db.insert_account($sender, sender_account);

if $is_regolith || !$transaction.is_system_transaction() {
*$cumulative_gas_used += $transaction.gas_limit();
}

$receipts.push(Some(Receipt {
tx_type: $transaction.tx_type(),
success: false,
cumulative_gas_used: *$cumulative_gas_used,
logs: vec![],
// Deposit nonces are only recorded after Regolith
deposit_nonce: $is_regolith.then_some(old_nonce),
}));
};
}
63 changes: 52 additions & 11 deletions crates/revm/src/optimism/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ impl<'a> EVMProcessor<'a> {
// disregard the state changes. The transaction is also recorded
// as using all gas unless it is a system transaction.
if transaction.is_deposit() {
fail_deposit_tx!(
self.db_mut(),
sender,
block.number,
transaction,
&mut receipts,
&mut cumulative_gas_used,
is_regolith,
BlockExecutionError::ProviderError
);
let mut sender_account = self
.db_mut()
.database
Expand All @@ -157,7 +167,7 @@ impl<'a> EVMProcessor<'a> {
))?;
let deposit_nonce = sender_account.nonce;
sender_account.nonce += 1;
// db.insert_account(sender, sender_account);
self.db_mut().insert_account(sender, sender_account);

if is_regolith || transaction.is_system_transaction() {
cumulative_gas_used += transaction.gas_limit();
Expand Down Expand Up @@ -208,22 +218,15 @@ impl<'a> EVMProcessor<'a> {
{
// Manually bump the nonce if the transaction was a contract creation.
if transaction.to().is_none() {
let sender_account = self
let mut sender_account = self
.db_mut()
.basic(sender)
.map_err(|_| BlockExecutionError::ProviderError)?
.ok_or(BlockExecutionError::Validation(
BlockValidationError::SenderRecoveryError,
))?;
let mut new_sender_account = sender_account.clone();
new_sender_account.nonce += 1;
// post_state.change_account(
// block.number,
// sender,
// to_reth_acc(&sender_account.info),
// to_reth_acc(&new_sender_account.info),
// );
// self.db().insert_account_info(sender, sender_account.info);
sender_account.nonce += 1;
self.db_mut().insert_account(sender, sender_account);
}

cumulative_gas_used += transaction.gas_limit();
Expand Down Expand Up @@ -296,3 +299,41 @@ impl<'a> EVMProcessor<'a> {
Ok((receipts, cumulative_gas_used))
}
}

/// If the Deposited transaction failed, the deposit must still be included. In this case, we need
/// to increment the sender nonce and disregard the state changes. The transaction is also recorded
/// as using all gas unless it is a system transaction.
#[macro_export]
macro_rules! fail_deposit_tx {
(
$db:expr,
$sender:ident,
$block_number:expr,
$transaction:ident,
$receipts:expr,
$cumulative_gas_used:expr,
$is_regolith:ident,
$error:expr
) => {
let mut sender_account = $db.basic($sender).ok().flatten().ok_or($error)?;
let old_nonce = sender_account.nonce;
sender_account.nonce += 1;

$db.insert_account($sender, sender_account);

if $is_regolith || !$transaction.is_system_transaction() {
*$cumulative_gas_used += $transaction.gas_limit();
}

$receipts.push(Receipt {
tx_type: $transaction.tx_type(),
success: false,
cumulative_gas_used: *$cumulative_gas_used,
logs: vec![],
// Deposit nonces are only recorded after Regolith
deposit_nonce: $is_regolith.then_some(old_nonce),
});
};
}

pub use fail_deposit_tx;
2 changes: 1 addition & 1 deletion crates/storage/provider/src/test_utils/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use parking_lot::Mutex;
use reth_interfaces::executor::BlockExecutionError;
use reth_primitives::{Address, Block, BlockNumber, ChainSpec, PruneModes, Receipt, U256};
use reth_primitives::{Address, Block, BlockNumber, ChainSpec, PruneModes, U256};
use std::sync::Arc;
/// Test executor with mocked result.
pub struct TestExecutor(pub Option<BundleStateWithReceipts>);
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/src/traits/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{bundle_state::BundleStateWithReceipts, StateProvider};
use reth_interfaces::executor::BlockExecutionError;
use reth_primitives::{Address, Block, BlockNumber, ChainSpec, PruneModes, Receipt, U256};
use reth_primitives::{Address, Block, BlockNumber, ChainSpec, PruneModes, U256};
use std::time::Duration;
use tracing::info;

Expand Down

0 comments on commit 7477805

Please sign in to comment.