Skip to content

Commit

Permalink
fix: l1 block receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Dec 11, 2024
1 parent d9192e2 commit 4e6b709
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 23 deletions.
8 changes: 7 additions & 1 deletion crates/edr_eth/src/receipt/factory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use auto_impl::auto_impl;
use revm_wiring::evm_wiring::HardforkTrait;

use crate::{
log::FilterLog,
Expand All @@ -9,14 +10,19 @@ use crate::{
/// Trait for constructing a receipt from a transaction receipt and the block it
/// was executed in.
#[auto_impl(&, Box, Arc)]
pub trait ReceiptFactory<ExecutionReceiptT: ExecutionReceipt<Log = FilterLog>, SignedTransactionT> {
pub trait ReceiptFactory<ExecutionReceiptT, HardforkT, SignedTransactionT>
where
ExecutionReceiptT: ExecutionReceipt<Log = FilterLog>,
HardforkT: HardforkTrait,
{
/// Type of the receipt that the factory constructs.
type Output: ExecutionReceipt<Log = FilterLog> + ReceiptTrait;

/// Constructs a new instance from a transaction receipt and the block it
/// was executed in.
fn create_receipt(
&self,
hardfork: HardforkT,
transaction: &SignedTransactionT,
transaction_receipt: TransactionReceipt<ExecutionReceiptT>,
block_hash: &B256,
Expand Down
19 changes: 16 additions & 3 deletions crates/edr_evm/src/block/builder/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use edr_eth::{
log::{ExecutionLog, FilterLog},
receipt::{BlockReceipt, ExecutionReceipt, TransactionReceipt},
result::{ExecutionResult, ResultAndState},
spec::HardforkTrait,
transaction::{ExecutableTransaction as _, Transaction as _},
trie::{ordered_trie_root, KECCAK_NULL_RLP},
withdrawal::Withdrawal,
Expand Down Expand Up @@ -334,6 +335,7 @@ where
mut self,
receipt_factory: impl ReceiptFactory<
ChainSpecT::ExecutionReceipt<FilterLog>,
ChainSpecT::Hardfork,
ChainSpecT::SignedTransaction,
Output = ChainSpecT::BlockReceipt,
>,
Expand Down Expand Up @@ -392,6 +394,7 @@ where
// TODO: handle ommers
let block = EthLocalBlockForChainSpec::<ChainSpecT>::new(
receipt_factory,
self.hardfork,
self.header,
self.transactions,
self.receipts,
Expand Down Expand Up @@ -502,19 +505,29 @@ pub struct EthBlockReceiptFactory<ExecutionReceiptT: ExecutionReceipt<Log = Filt
phantom: PhantomData<ExecutionReceiptT>,
}

impl<ExecutionReceiptT: ExecutionReceipt<Log = FilterLog>, SignedTransactionT>
ReceiptFactory<ExecutionReceiptT, SignedTransactionT>
impl<
ExecutionReceiptT: ExecutionReceipt<Log = FilterLog>,
HardforkT: HardforkTrait + Into<l1::SpecId>,
SignedTransactionT,
> ReceiptFactory<ExecutionReceiptT, HardforkT, SignedTransactionT>
for EthBlockReceiptFactory<ExecutionReceiptT>
{
type Output = BlockReceipt<ExecutionReceiptT>;

fn create_receipt(
&self,
hardfork: HardforkT,
_transaction: &SignedTransactionT,
transaction_receipt: TransactionReceipt<ExecutionReceiptT>,
mut transaction_receipt: TransactionReceipt<ExecutionReceiptT>,
block_hash: &B256,
block_number: u64,
) -> Self::Output {
// The JSON-RPC layer should not return the gas price as effective gas price for
// receipts in pre-London hardforks.
if hardfork.into() < l1::SpecId::LONDON {
transaction_receipt.effective_gas_price = None;
}

BlockReceipt {
inner: transaction_receipt,
block_hash: *block_hash,
Expand Down
3 changes: 3 additions & 0 deletions crates/edr_evm/src/block/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ impl<
pub fn new(
receipt_factory: impl ReceiptFactory<
<ExecutionReceiptHigherKindedT as HigherKinded<FilterLog>>::Type,
HardforkT,
SignedTransactionT,
Output = BlockReceiptT,
>,
hardfork: HardforkT,
partial_header: PartialHeader,
transactions: Vec<SignedTransactionT>,
transaction_receipts: Vec<
Expand Down Expand Up @@ -121,6 +123,7 @@ impl<
.zip(transactions.iter())
.map(|(transaction_receipt, transaction)| {
Arc::new(receipt_factory.create_receipt(
hardfork,
transaction,
transaction_receipt,
&hash,
Expand Down
1 change: 1 addition & 0 deletions crates/edr_evm/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub trait RuntimeSpec:
/// Type representing a factory for block receipts.
type BlockReceiptFactory: ReceiptFactory<
Self::ExecutionReceipt<FilterLog>,
Self::Hardfork,
Self::SignedTransaction,
Output = Self::BlockReceipt
>;
Expand Down
1 change: 1 addition & 0 deletions crates/edr_evm/tests/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ fn insert_dummy_block_with_transaction(
transaction::Signed,
>::new(
&receipt_factory,
blockchain.hardfork(),
header,
vec![transaction],
vec![transaction_receipt.clone()],
Expand Down
1 change: 0 additions & 1 deletion crates/edr_optimism/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl<'blockchain, BlockchainErrorT, DebugDataT, StateErrorT: Debug + Send>

fn block_receipt_factory(&self) -> BlockReceiptFactory {
BlockReceiptFactory {
hardfork: self.hardfork,
l1_block_info: self.l1_block_info.clone(),
}
}
Expand Down
13 changes: 9 additions & 4 deletions crates/edr_optimism/src/receipt/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ use crate::{eip2718::TypedEnvelope, receipt, transaction, OptimismSpecId};

/// Block receipt factory for Optimism.
pub struct BlockReceiptFactory {
pub(crate) hardfork: OptimismSpecId,
pub(crate) l1_block_info: L1BlockInfo,
}

impl ReceiptFactory<TypedEnvelope<receipt::Execution<FilterLog>>, transaction::Signed>
for BlockReceiptFactory
impl
ReceiptFactory<
TypedEnvelope<receipt::Execution<FilterLog>>,
OptimismSpecId,
transaction::Signed,
> for BlockReceiptFactory
{
type Output = receipt::Block;

fn create_receipt(
&self,
hardfork: OptimismSpecId,
transaction: &transaction::Signed,
transaction_receipt: TransactionReceipt<TypedEnvelope<receipt::Execution<FilterLog>>>,
block_hash: &B256,
block_number: u64,
) -> Self::Output {
let l1_block_info = to_rpc_l1_block_info(
self.hardfork,
hardfork,
&self.l1_block_info,
transaction,
&transaction_receipt,
Expand All @@ -36,6 +40,7 @@ impl ReceiptFactory<TypedEnvelope<receipt::Execution<FilterLog>>, transaction::S
let eth = {
let receipt_factory = EthBlockReceiptFactory::default();
receipt_factory.create_receipt(
hardfork,
transaction,
transaction_receipt,
block_hash,
Expand Down
13 changes: 6 additions & 7 deletions crates/edr_optimism/src/rpc/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@ mod tests {
use receipt::BlockReceiptFactory;

use super::*;
use crate::{L1BlockInfo, OptimismChainSpec, OptimismSpecId};
use crate::{L1BlockInfo, OptimismChainSpec};

impl_execution_receipt_tests! {
OptimismChainSpec, BlockReceiptFactory {
hardfork: OptimismSpecId::FJORD,
l1_block_info: L1BlockInfo {
l1_base_fee: U256::from(1234),
l1_fee_overhead: None,
Expand All @@ -180,7 +179,7 @@ mod tests {
l1_blob_base_fee_scalar: None,
}.into(),
} => {
legacy => TypedEnvelope::Legacy(receipt::Execution::Legacy(receipt::execution::Legacy {
legacy, OptimismSpecId::LATEST => TypedEnvelope::Legacy(receipt::Execution::Legacy(receipt::execution::Legacy {
root: B256::random(),
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -189,7 +188,7 @@ mod tests {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip2930 => TypedEnvelope::Eip2930(receipt::Execution::Eip658(receipt::execution::Eip658 {
eip658_eip2930, OptimismSpecId::LATEST => TypedEnvelope::Eip2930(receipt::Execution::Eip658(receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -198,7 +197,7 @@ mod tests {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip1559 => TypedEnvelope::Eip2930(receipt::Execution::Eip658(receipt::execution::Eip658 {
eip658_eip1559, OptimismSpecId::LATEST => TypedEnvelope::Eip2930(receipt::Execution::Eip658(receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -207,7 +206,7 @@ mod tests {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip4844 => TypedEnvelope::Eip4844(receipt::Execution::Eip658(receipt::execution::Eip658 {
eip658_eip4844, OptimismSpecId::LATEST => TypedEnvelope::Eip4844(receipt::Execution::Eip658(receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -216,7 +215,7 @@ mod tests {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
deposit => TypedEnvelope::Deposit(receipt::Execution::Deposit(receipt::execution::Deposit {
deposit, OptimismSpecId::LATEST => TypedEnvelope::Deposit(receipt::Execution::Deposit(receipt::execution::Deposit {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand Down
15 changes: 10 additions & 5 deletions crates/edr_rpc_eth/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ impl TryFrom<Block> for receipt::BlockReceipt<TypedEnvelope<receipt::Execution<F
#[cfg(test)]
mod test {
use assert_json_diff::assert_json_eq;
use edr_eth::{eips::eip2718::TypedEnvelope, l1::L1ChainSpec, log::ExecutionLog, Bloom, Bytes};
use edr_eth::{
eips::eip2718::TypedEnvelope,
l1::{self, L1ChainSpec},
log::ExecutionLog,
Bloom, Bytes,
};
use edr_evm::block::EthBlockReceiptFactory;
use serde_json::json;

Expand Down Expand Up @@ -238,7 +243,7 @@ mod test {

impl_execution_receipt_tests! {
L1ChainSpec, EthBlockReceiptFactory::default() => {
legacy => TypedEnvelope::Legacy(edr_eth::receipt::Execution::Legacy(edr_eth::receipt::execution::Legacy {
legacy, l1::SpecId::LATEST => TypedEnvelope::Legacy(edr_eth::receipt::Execution::Legacy(edr_eth::receipt::execution::Legacy {
root: B256::random(),
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -247,7 +252,7 @@ mod test {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip2930 => TypedEnvelope::Eip2930(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
eip658_eip2930, l1::SpecId::LATEST => TypedEnvelope::Eip2930(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -256,7 +261,7 @@ mod test {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip1559 => TypedEnvelope::Eip2930(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
eip658_eip1559, l1::SpecId::LATEST => TypedEnvelope::Eip2930(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand All @@ -265,7 +270,7 @@ mod test {
ExecutionLog::new_unchecked(Address::random(), Vec::new(), Bytes::from_static(b"test"))
],
})),
eip658_eip4844 => TypedEnvelope::Eip4844(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
eip658_eip4844, l1::SpecId::LATEST => TypedEnvelope::Eip4844(edr_eth::receipt::Execution::Eip658(edr_eth::receipt::execution::Eip658 {
status: true,
cumulative_gas_used: 0xffff,
logs_bloom: Bloom::random(),
Expand Down
4 changes: 2 additions & 2 deletions crates/edr_rpc_eth/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
macro_rules! impl_execution_receipt_tests {
($chain_spec:ty, $block_receipt_factory:expr => {
$(
$name:ident => $receipt:expr,
$name:ident, $hardfork:expr => $receipt:expr,
)+
}) => {
$(
Expand Down Expand Up @@ -59,7 +59,7 @@ macro_rules! impl_execution_receipt_tests {
let transaction = <$chain_spec as ChainSpec>::SignedTransaction::default();

let receipt_factory = $block_receipt_factory;
let block_receipt = receipt_factory.create_receipt(&transaction, transaction_receipt, &block_hash, block_number);
let block_receipt = receipt_factory.create_receipt($hardfork, &transaction, transaction_receipt, &block_hash, block_number);

let rpc_receipt = <$chain_spec as RpcSpec>::RpcReceipt::rpc_type_from(&block_receipt, Default::default());

Expand Down

0 comments on commit 4e6b709

Please sign in to comment.