alloy_consensus/receipt/
any.rsuse crate::{Eip658Value, ReceiptWithBloom, TxReceipt};
use alloy_eips::eip2718::{Decodable2718, Eip2718Result, Encodable2718};
use alloy_primitives::{bytes::BufMut, Bloom, Log};
use alloy_rlp::{Decodable, Encodable};
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[doc(alias = "AnyTransactionReceiptEnvelope", alias = "AnyTxReceiptEnvelope")]
pub struct AnyReceiptEnvelope<T = Log> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub inner: ReceiptWithBloom<T>,
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub r#type: u8,
}
impl<T> AnyReceiptEnvelope<T>
where
T: Encodable,
{
pub fn rlp_payload_length(&self) -> usize {
let length = self.inner.length();
if self.is_legacy() {
length
} else {
length + 1
}
}
}
impl<T> AnyReceiptEnvelope<T> {
pub const fn is_legacy(&self) -> bool {
self.r#type == 0
}
pub const fn is_success(&self) -> bool {
self.status()
}
pub const fn status(&self) -> bool {
matches!(self.inner.receipt.status, Eip658Value::Eip658(true) | Eip658Value::PostState(_))
}
pub const fn bloom(&self) -> Bloom {
self.inner.logs_bloom
}
pub const fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
}
pub fn logs(&self) -> &[T] {
&self.inner.receipt.logs
}
}
impl<T> TxReceipt<T> for AnyReceiptEnvelope<T>
where
T: Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
{
fn status_or_post_state(&self) -> Eip658Value {
self.inner.status_or_post_state()
}
fn status(&self) -> bool {
self.inner.status()
}
fn bloom(&self) -> Bloom {
self.inner.logs_bloom
}
fn cumulative_gas_used(&self) -> u128 {
self.inner.receipt.cumulative_gas_used
}
fn logs(&self) -> &[T] {
&self.inner.receipt.logs
}
}
impl Encodable2718 for AnyReceiptEnvelope {
fn type_flag(&self) -> Option<u8> {
match self.r#type {
0 => None,
ty => Some(ty),
}
}
fn encode_2718_len(&self) -> usize {
self.inner.length() + !self.is_legacy() as usize
}
fn encode_2718(&self, out: &mut dyn BufMut) {
match self.type_flag() {
None => {}
Some(ty) => out.put_u8(ty),
}
self.inner.encode(out);
}
}
impl Decodable2718 for AnyReceiptEnvelope {
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
let receipt = Decodable::decode(buf)?;
Ok(Self { inner: receipt, r#type: ty })
}
fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
Self::typed_decode(0, buf)
}
}