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

chore(consensus): Upstream Receipt Constructor #165

Open
wants to merge 5 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
79 changes: 79 additions & 0 deletions crates/consensus/src/receipt/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{OpDepositReceipt, OpDepositReceiptWithBloom, OpTxType};
use alloc::vec::Vec;
use alloy_consensus::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718};
use alloy_primitives::{Bloom, Log};
Expand Down Expand Up @@ -44,6 +45,57 @@ pub enum OpReceiptEnvelope<T = Log> {
Deposit(OpDepositReceiptWithBloom<T>),
}

/// Compute the logs bloom filter for the given logs.
pub(crate) fn logs_bloom<'a>(logs: impl IntoIterator<Item = &'a Log>) -> Bloom {
let mut bloom = Bloom::ZERO;
for log in logs {
bloom.m3_2048(log.address.as_slice());
for topic in log.topics() {
bloom.m3_2048(topic.as_slice());
}
}
bloom
}
Comment on lines +48 to +58
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should actually just move this to alloy-primitives

fine here for now


impl OpReceiptEnvelope<Log> {
/// Creates a new [`OpReceiptEnvelope`] from the given parts.
pub fn from_parts<'a>(
status: bool,
cumulative_gas_used: u128,
logs: impl IntoIterator<Item = &'a Log>,
tx_type: OpTxType,
deposit_nonce: Option<u64>,
deposit_receipt_version: Option<u64>,
) -> Self {
let logs = logs.into_iter().cloned().collect::<Vec<_>>();
let logs_bloom = logs_bloom(&logs);
let inner_receipt =
Receipt { status: Eip658Value::Eip658(status), cumulative_gas_used, logs };
match tx_type {
OpTxType::Legacy => {
Self::Legacy(ReceiptWithBloom { receipt: inner_receipt, logs_bloom })
}
OpTxType::Eip2930 => {
Self::Eip2930(ReceiptWithBloom { receipt: inner_receipt, logs_bloom })
}
OpTxType::Eip1559 => {
Self::Eip1559(ReceiptWithBloom { receipt: inner_receipt, logs_bloom })
}
OpTxType::Deposit => {
let inner = OpDepositReceiptWithBloom {
receipt: OpDepositReceipt {
inner: inner_receipt,
deposit_nonce,
deposit_receipt_version,
},
logs_bloom,
};
Self::Deposit(inner)
}
}
}
}

impl<T> OpReceiptEnvelope<T> {
/// Return the [`OpTxType`] of the inner receipt.
pub const fn tx_type(&self) -> OpTxType {
Expand Down Expand Up @@ -259,3 +311,30 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn legacy_receipt_from_parts() {
let receipt =
OpReceiptEnvelope::from_parts(true, 100, vec![], OpTxType::Legacy, None, None);
assert!(receipt.status());
assert_eq!(receipt.cumulative_gas_used(), 100);
assert_eq!(receipt.logs().len(), 0);
assert_eq!(receipt.tx_type(), OpTxType::Legacy);
}

#[test]
fn deposit_receipt_from_parts() {
let receipt =
OpReceiptEnvelope::from_parts(true, 100, vec![], OpTxType::Deposit, Some(1), Some(2));
assert!(receipt.status());
assert_eq!(receipt.cumulative_gas_used(), 100);
assert_eq!(receipt.logs().len(), 0);
assert_eq!(receipt.tx_type(), OpTxType::Deposit);
assert_eq!(receipt.deposit_nonce(), Some(1));
assert_eq!(receipt.deposit_receipt_version(), Some(2));
}
}
Loading