Skip to content

Commit

Permalink
chore: relax build_receipt (#13486)
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored Dec 22, 2024
1 parent de477ba commit 05382ec
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions crates/rpc/rpc-eth-types/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
//! RPC receipt response builder, extends a layer one receipt with layer two data.
use super::{EthApiError, EthResult};
use alloy_consensus::{ReceiptEnvelope, Transaction};
use alloy_consensus::{ReceiptEnvelope, TxReceipt};
use alloy_primitives::{Address, TxKind};
use alloy_rpc_types_eth::{Log, ReceiptWithBloom, TransactionReceipt};
use reth_primitives::{Receipt, TransactionMeta, TransactionSigned, TxType};
use reth_primitives_traits::SignedTransaction;
use revm_primitives::calc_blob_gasprice;

/// Builds an [`TransactionReceipt`] obtaining the inner receipt envelope from the given closure.
pub fn build_receipt<T>(
transaction: &TransactionSigned,
pub fn build_receipt<R, T, E>(
transaction: &T,
meta: TransactionMeta,
receipt: &Receipt,
all_receipts: &[Receipt],
build_envelope: impl FnOnce(ReceiptWithBloom<alloy_consensus::Receipt<Log>>) -> T,
) -> EthResult<TransactionReceipt<T>> {
receipt: &R,
all_receipts: &[R],
build_envelope: impl FnOnce(ReceiptWithBloom<alloy_consensus::Receipt<Log>>) -> E,
) -> EthResult<TransactionReceipt<E>>
where
R: TxReceipt<Log = alloy_primitives::Log>,
T: SignedTransaction,
{
// Note: we assume this transaction is valid, because it's mined (or part of pending block)
// and we don't need to check for pre EIP-2
let from =
transaction.recover_signer_unchecked().ok_or(EthApiError::InvalidTransactionSignature)?;

// get the previous transaction cumulative gas used
let gas_used = if meta.index == 0 {
receipt.cumulative_gas_used
receipt.cumulative_gas_used()
} else {
let prev_tx_idx = (meta.index - 1) as usize;
all_receipts
.get(prev_tx_idx)
.map(|prev_receipt| receipt.cumulative_gas_used - prev_receipt.cumulative_gas_used)
.map(|prev_receipt| receipt.cumulative_gas_used() - prev_receipt.cumulative_gas_used())
.unwrap_or_default()
};

let blob_gas_used = transaction.transaction.blob_gas_used();
let blob_gas_used = transaction.blob_gas_used();
// Blob gas price should only be present if the transaction is a blob transaction
let blob_gas_price = blob_gas_used.and_then(|_| meta.excess_blob_gas.map(calc_blob_gasprice));
let logs_bloom = receipt.bloom_slow();
let logs_bloom = receipt.bloom();

// get number of logs in the block
let mut num_logs = 0;
for prev_receipt in all_receipts.iter().take(meta.index as usize) {
num_logs += prev_receipt.logs.len();
num_logs += prev_receipt.logs().len();
}

let logs: Vec<Log> = receipt
.logs
.logs()
.iter()
.enumerate()
.map(|(tx_log_idx, log)| Log {
Expand All @@ -60,13 +64,13 @@ pub fn build_receipt<T>(
.collect();

let rpc_receipt = alloy_rpc_types_eth::Receipt {
status: receipt.success.into(),
cumulative_gas_used: receipt.cumulative_gas_used as u128,
status: receipt.status_or_post_state(),
cumulative_gas_used: receipt.cumulative_gas_used(),
logs,
};

let (contract_address, to) = match transaction.transaction.kind() {
TxKind::Create => (Some(from.create(transaction.transaction.nonce())), None),
let (contract_address, to) = match transaction.kind() {
TxKind::Create => (Some(from.create(transaction.nonce())), None),
TxKind::Call(addr) => (None, Some(Address(*addr))),
};

Expand All @@ -78,7 +82,7 @@ pub fn build_receipt<T>(
block_number: Some(meta.block_number),
from,
to,
gas_used: gas_used as u128,
gas_used,
contract_address,
effective_gas_price: transaction.effective_gas_price(meta.base_fee),
// EIP-4844 fields
Expand Down

0 comments on commit 05382ec

Please sign in to comment.