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

[Feature] Receipt trait in alloy-consensus #477

Merged
merged 8 commits into from
Apr 23, 2024
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
28 changes: 25 additions & 3 deletions crates/consensus/src/receipt/any.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ReceiptWithBloom;
use crate::{ReceiptWithBloom, TxReceipt};
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{bytes::BufMut, Bloom, Log};
use alloy_rlp::{Decodable, Encodable};
Expand Down Expand Up @@ -55,6 +55,11 @@ impl<T> AnyReceiptEnvelope<T> {
self.inner.receipt.status
}

/// Return the receipt's bloom.
pub const fn bloom(&self) -> Bloom {
self.inner.logs_bloom
}

/// Returns the cumulative gas used at this receipt.
pub const fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
Expand All @@ -64,10 +69,27 @@ impl<T> AnyReceiptEnvelope<T> {
pub fn logs(&self) -> &[T] {
&self.inner.receipt.logs
}
}

impl<T> TxReceipt<T> for AnyReceiptEnvelope<T> {
/// Returns the success status of the receipt's transaction.
fn status(&self) -> bool {
self.inner.receipt.status
}

/// Return the receipt's bloom.
pub const fn logs_bloom(&self) -> &Bloom {
&self.inner.logs_bloom
fn bloom(&self) -> Bloom {
self.inner.logs_bloom
}

/// Returns the cumulative gas used at this receipt.
fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
}

/// Return the receipt logs.
fn logs(&self) -> &[T] {
&self.inner.receipt.logs
}
}

Expand Down
27 changes: 26 additions & 1 deletion crates/consensus/src/receipt/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Receipt, ReceiptWithBloom, TxType};
use crate::{Receipt, ReceiptWithBloom, TxReceipt, TxType};
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};
Expand Down Expand Up @@ -93,6 +93,31 @@ impl<T> ReceiptEnvelope<T> {
}
}

impl<T> TxReceipt<T> for ReceiptEnvelope<T> {
fn status(&self) -> bool {
self.as_receipt().unwrap().status
}

/// Return the receipt's bloom.
fn bloom(&self) -> Bloom {
self.as_receipt_with_bloom().unwrap().logs_bloom
}

fn bloom_cheap(&self) -> Option<Bloom> {
Some(self.bloom())
}

/// Returns the cumulative gas used at this receipt.
fn cumulative_gas_used(&self) -> u128 {
self.as_receipt().unwrap().cumulative_gas_used
}

/// Return the receipt logs.
fn logs(&self) -> &[T] {
&self.as_receipt().unwrap().logs
}
}

impl ReceiptEnvelope {
/// Get the length of the inner receipt in the 2718 encoding.
pub fn inner_length(&self) -> usize {
Expand Down
6 changes: 3 additions & 3 deletions crates/consensus/src/receipt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod receipts;
pub use receipts::{Receipt, ReceiptWithBloom};

/// Receipt is the result of a transaction execution.
pub trait TxReceipt {
pub trait TxReceipt<T = Log> {
/// Returns true if the transaction was successful.
fn success(&self) -> bool;
fn status(&self) -> bool;

/// Returns the bloom filter for the logs in the receipt. This operation
/// may be expensive.
Expand All @@ -28,7 +28,7 @@ pub trait TxReceipt {
fn cumulative_gas_used(&self) -> u128;

/// Returns the logs emitted by this transaction.
fn logs(&self) -> &[Log];
fn logs(&self) -> &[T];
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/receipt/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
}

impl TxReceipt for Receipt {
Copy link
Member

Choose a reason for hiding this comment

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

followup work:

  • Make this impl generic over

fn success(&self) -> bool {
fn status(&self) -> bool {
self.status
}

Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct ReceiptWithBloom<T = Log> {
}

impl TxReceipt for ReceiptWithBloom {
fn success(&self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

followup work:

  • Make this impl generic over

fn status(&self) -> bool {
self.receipt.status
}

Expand Down
3 changes: 2 additions & 1 deletion crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy_consensus::TxReceipt;
use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error};
use alloy_json_rpc::RpcObject;
use alloy_primitives::Address;
Expand Down Expand Up @@ -75,7 +76,7 @@ pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static {
type UnsignedTx: From<Self::TxEnvelope>;

/// The network receipt envelope type.
type ReceiptEnvelope: Eip2718Envelope;
type ReceiptEnvelope: Eip2718Envelope + TxReceipt;
developeruche marked this conversation as resolved.
Show resolved Hide resolved

/// The network header type.
type Header;
Expand Down
Loading