Skip to content
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ This changelog covers Firehose-specific changes only. For upstream reth changes,

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## reth-v2.11.0-fh3.1-1

### Fixed

- On Amsterdam, a transaction whose root call sends value and runs out of gas in its runtime gas
phase no longer reports a `REASON_TRANSFER` debit of the sender and credit of the recipient.
The value never moves, and go-ethereum reports no transfer. This happens when the recipient is
empty and the transaction cannot pay its new-account state gas, or when the recipient is an
EIP-7702 delegated account and the transaction cannot pay to access the delegate.
`reth-v2.11.0-fh3.1` reported the transfer.

## reth-v2.9.0-fh3.1-not-a-real-version-alpha.2

Tests the release made by sfreleaser, no Firehose changes.
Expand Down
43 changes: 39 additions & 4 deletions crates/firehose/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use alloy_primitives::{Address, Bytes, Log as AlloyLog, B256, KECCAK256_EMPTY, U256};
use evm2::{
bytecode::Bytecode,
evm::{AccountInfo, DynDatabase as _, JournalEntry, SYSTEM_MAX_SSTORES_PER_CALL},
interpreter::{InstrStop, Interpreter, Message, MessageKind, MessageResult, Word},
version::GasId,
Evm, EvmTypes, Inspector,
Evm, EvmFeatures, EvmTypes, Inspector,
};
use firehose_tracer::{
pb::sf::ethereum::r#type::v2::balance_change::Reason,
Expand Down Expand Up @@ -184,7 +185,9 @@ struct InspectorState<G> {
/// Value transfer `(from, to, value)` of the current non-self call, captured at call enter
/// and cleared once the scan emitted it. If it survives to `call_end`, the call failed and
/// evm2 rolled the transfer back before any hook saw it (a precompile that failed); Geth
/// records the transfer that happened before the revert, so `call_end` re-emits it.
/// records the transfer that happened before the revert, so `call_end` re-emits it. A root
/// call halted by the runtime gas phase never transferred, see
/// [`halted_in_runtime_gas_phase`].
pending_value_transfer: Option<(Address, Address, U256)>,

/// Contracts that executed SELFDESTRUCT this transaction. A candidate whose frame reverted
Expand Down Expand Up @@ -704,9 +707,11 @@ impl<G: DerefMut<Target = firehose_tracer::Tracer>> InspectorState<G> {
}

// A transfer still pending was rolled back before any hook could see it. It happened
// unless the call aborted before transferring.
// unless the call aborted before transferring, or the transaction's runtime gas phase
// halted its root call.
if let Some((from, to, value)) = self.pending_value_transfer.take() &&
!matches!(result.stop, InstrStop::OutOfFunds | InstrStop::CallTooDeep)
!matches!(result.stop, InstrStop::OutOfFunds | InstrStop::CallTooDeep) &&
!(depth == 0 && halted_in_runtime_gas_phase(host, message, result.stop))
{
let from_balance = account_info(host, &from).balance;
let to_balance = account_info(host, &to).balance;
Expand Down Expand Up @@ -1002,6 +1007,36 @@ fn frame_depth<T: EvmTypes>(host: &Evm<'_, T>, message: &Message<T>) -> i32 {
i32::from(message.depth) - i32::from(host.initial_call_depth())
}

/// Whether the EIP-2780 runtime gas phase halted `message`, a transaction's root call that sends
/// value and stopped with `stop`. The phase runs out of gas before the value moves, and evm2 still
/// runs the root frame's hooks without saying it never ran.
///
/// Must stay in sync with the out-of-gas halts of evm2's `ethereum::prepare_initial_frame`: the
/// new-account state gas of value sent to an empty recipient, and the delegation access gas of an
/// EIP-7702 delegated recipient.
fn halted_in_runtime_gas_phase<T: EvmTypes>(
host: &mut Evm<'_, T>,
message: &Message<T>,
stop: InstrStop,
) -> bool {
if !matches!(stop, InstrStop::OutOfGas) || !host.feature(EvmFeatures::EIP2780) {
return false;
}
// The recipient reads as the runtime gas phase saw it: a halt never touched it, and a call that
// ran out of gas later rolled its transfer back.
let recipient = account_info(host, &message.call_target);
// A call to an empty account runs no code, and a precompile that runs out of gas stops with
// `PrecompileOOG`, so only the new-account charge stops it with `OutOfGas`.
if recipient.is_empty() {
return true;
}
// A resolved delegation runs the delegate's code, the halted frame keeps the recipient as its
// code address.
message.code_address == message.call_target &&
Bytecode::new_raw_checked(load_code(host, &recipient))
.is_ok_and(|code| code.is_eip7702())
}

/// The memory range the KECCAK256 about to run hashes, when its preimage is small enough to
/// record. The offset and size are still on the stack; the range is read back in `step_end`.
fn keccak256_range<T: EvmTypes>(interp: &Interpreter<'_, '_, T>) -> Option<(usize, usize)> {
Expand Down
75 changes: 74 additions & 1 deletion crates/firehose/src/inspector/tests/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{scenario::*, support::*};
use crate::{inspector::*, PreTxAdjust};
use alloy_eips::eip7702::SignedAuthorization;
use alloy_rlp::Decodable;
use evm2::{ethereum::LazyAuthorization, SpecId};
use evm2::{bytecode::Bytecode, ethereum::LazyAuthorization, SpecId, Version};
use firehose_tracer::pb;

/// Block beneficiary of the priced transactions below.
Expand Down Expand Up @@ -472,3 +472,76 @@ fn root_balance_reason_override_is_single_shot() {
.collect();
assert_eq!(root_reasons, [Reason::IncreaseMint, Reason::GasBuy]);
}

/// The `Transfer` balance changes of `trx`, all calls together, as `(address, old, new)`.
fn transfers(trx: &pb::sf::ethereum::r#type::v2::TransactionTrace) -> Vec<(Address, U256, U256)> {
trx.calls
.iter()
.flat_map(|call| &call.balance_changes)
.filter(|change| change.reason == Reason::Transfer as i32)
.map(|change| {
(Address::from_slice(&change.address), big(&change.old_value), big(&change.new_value))
})
.collect()
}

/// An Amsterdam transaction sending value to an empty account without the gas for its EIP-2780
/// new-account charge runs out of gas before the value moves, so it reports no transfer.
#[test]
fn root_call_halted_on_new_account_gas_reports_no_transfer() {
let block = drive_txs(
SpecId::AMSTERDAM,
&[(SENDER, balance_account(SENDER_BALANCE))],
&[DriveTx::call(RECIPIENT, 3).with_gas(24_000)],
);
let trx = &block.transaction_traces[0];

assert!(trx.calls[0].status_failed, "the root call runs out of gas");
assert_eq!(trx.gas_used, 24_000);
assert_eq!(transfers(trx), []);
}

/// An Amsterdam transaction sending value to an EIP-7702 delegated account without the gas to
/// access the delegate runs out of gas before the value moves, so it reports no transfer.
#[test]
fn root_call_halted_on_delegation_access_gas_reports_no_transfer() {
let block = drive_txs(
SpecId::AMSTERDAM,
&[
(SENDER, balance_account(SENDER_BALANCE)),
(RECIPIENT, AccountInfo::default().with_code(Bytecode::new_eip7702(DELEGATE))),
],
&[DriveTx::call(RECIPIENT, 3).with_gas(21_050)],
);
let trx = &block.transaction_traces[0];

assert!(trx.calls[0].status_failed, "the root call runs out of gas");
assert_eq!(trx.gas_used, 21_050);
assert_eq!(transfers(trx), []);
}

/// An Amsterdam transaction that pays the new-account charge of an empty precompile, then runs
/// out of gas in the precompile, moved its value before the rollback, so it reports the
/// transfer as go-ethereum does.
#[test]
fn root_call_to_precompile_out_of_gas_reports_the_transfer() {
const ECRECOVER: Address = Address::with_last_byte(1);

let new_account_gas = Version::new(SpecId::AMSTERDAM).gas_params.new_account_state_gas();
let block = drive_txs(
SpecId::AMSTERDAM,
&[(SENDER, balance_account(SENDER_BALANCE))],
&[DriveTx::call(ECRECOVER, 3).with_gas(21_000 + new_account_gas + 100)],
);
let trx = &block.transaction_traces[0];
assert!(trx.calls[0].status_failed, "ecrecover runs out of gas");

let sender_balance = U256::from(SENDER_BALANCE);
assert_eq!(
transfers(trx),
[
(SENDER, sender_balance, sender_balance - U256::from(3)),
(ECRECOVER, U256::ZERO, U256::from(3)),
]
);
}
Loading