alloy_consensus/receipt/
envelope.rsuse crate::{Eip658Value, Receipt, ReceiptWithBloom, TxReceipt, TxType};
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718};
use alloy_primitives::{Bloom, Log};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
#[non_exhaustive]
#[doc(alias = "TransactionReceiptEnvelope", alias = "TxReceiptEnvelope")]
pub enum ReceiptEnvelope<T = Log> {
#[cfg_attr(feature = "serde", serde(rename = "0x0", alias = "0x00"))]
Legacy(ReceiptWithBloom<T>),
#[cfg_attr(feature = "serde", serde(rename = "0x1", alias = "0x01"))]
Eip2930(ReceiptWithBloom<T>),
#[cfg_attr(feature = "serde", serde(rename = "0x2", alias = "0x02"))]
Eip1559(ReceiptWithBloom<T>),
#[cfg_attr(feature = "serde", serde(rename = "0x3", alias = "0x03"))]
Eip4844(ReceiptWithBloom<T>),
#[cfg_attr(feature = "serde", serde(rename = "0x4", alias = "0x04"))]
Eip7702(ReceiptWithBloom<T>),
}
impl<T> ReceiptEnvelope<T> {
#[doc(alias = "transaction_type")]
pub const fn tx_type(&self) -> TxType {
match self {
Self::Legacy(_) => TxType::Legacy,
Self::Eip2930(_) => TxType::Eip2930,
Self::Eip1559(_) => TxType::Eip1559,
Self::Eip4844(_) => TxType::Eip4844,
Self::Eip7702(_) => TxType::Eip7702,
}
}
pub fn is_success(&self) -> bool {
self.status()
}
pub fn status(&self) -> bool {
self.as_receipt().unwrap().status.coerce_status()
}
pub fn cumulative_gas_used(&self) -> u128 {
self.as_receipt().unwrap().cumulative_gas_used
}
pub fn logs(&self) -> &[T] {
&self.as_receipt().unwrap().logs
}
pub fn logs_bloom(&self) -> &Bloom {
&self.as_receipt_with_bloom().unwrap().logs_bloom
}
pub const fn as_receipt_with_bloom(&self) -> Option<&ReceiptWithBloom<T>> {
match self {
Self::Legacy(t)
| Self::Eip2930(t)
| Self::Eip1559(t)
| Self::Eip4844(t)
| Self::Eip7702(t) => Some(t),
}
}
pub const fn as_receipt(&self) -> Option<&Receipt<T>> {
match self {
Self::Legacy(t)
| Self::Eip2930(t)
| Self::Eip1559(t)
| Self::Eip4844(t)
| Self::Eip7702(t) => Some(&t.receipt),
}
}
}
impl<T> TxReceipt<T> for ReceiptEnvelope<T>
where
T: Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
{
fn status_or_post_state(&self) -> Eip658Value {
self.as_receipt().unwrap().status
}
fn status(&self) -> bool {
self.as_receipt().unwrap().status.coerce_status()
}
fn bloom(&self) -> Bloom {
self.as_receipt_with_bloom().unwrap().logs_bloom
}
fn bloom_cheap(&self) -> Option<Bloom> {
Some(self.bloom())
}
fn cumulative_gas_used(&self) -> u128 {
self.as_receipt().unwrap().cumulative_gas_used
}
fn logs(&self) -> &[T] {
&self.as_receipt().unwrap().logs
}
}
impl ReceiptEnvelope {
pub fn inner_length(&self) -> usize {
self.as_receipt_with_bloom().unwrap().length()
}
pub fn rlp_payload_length(&self) -> usize {
let length = self.as_receipt_with_bloom().unwrap().length();
match self {
Self::Legacy(_) => length,
_ => length + 1,
}
}
}
impl Encodable for ReceiptEnvelope {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.network_encode(out)
}
fn length(&self) -> usize {
self.network_len()
}
}
impl Decodable for ReceiptEnvelope {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::network_decode(buf)
.map_or_else(|_| Err(alloy_rlp::Error::Custom("Unexpected type")), Ok)
}
}
impl Encodable2718 for ReceiptEnvelope {
fn type_flag(&self) -> Option<u8> {
match self {
Self::Legacy(_) => None,
Self::Eip2930(_) => Some(TxType::Eip2930 as u8),
Self::Eip1559(_) => Some(TxType::Eip1559 as u8),
Self::Eip4844(_) => Some(TxType::Eip4844 as u8),
Self::Eip7702(_) => Some(TxType::Eip7702 as u8),
}
}
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.as_receipt_with_bloom().unwrap().encode(out);
}
}
impl Decodable2718 for ReceiptEnvelope {
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
let receipt = Decodable::decode(buf)?;
match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? {
TxType::Eip2930 => Ok(Self::Eip2930(receipt)),
TxType::Eip1559 => Ok(Self::Eip1559(receipt)),
TxType::Eip4844 => Ok(Self::Eip4844(receipt)),
TxType::Eip7702 => Ok(Self::Eip7702(receipt)),
TxType::Legacy => Err(Eip2718Error::UnexpectedType(0)),
}
}
fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
Ok(Self::Legacy(Decodable::decode(buf)?))
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a, T> arbitrary::Arbitrary<'a> for ReceiptEnvelope<T>
where
T: arbitrary::Arbitrary<'a>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let receipt = ReceiptWithBloom::<T>::arbitrary(u)?;
match u.int_in_range(0..=3)? {
0 => Ok(Self::Legacy(receipt)),
1 => Ok(Self::Eip2930(receipt)),
2 => Ok(Self::Eip1559(receipt)),
3 => Ok(Self::Eip4844(receipt)),
4 => Ok(Self::Eip7702(receipt)),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod test {
#[cfg(feature = "serde")]
#[test]
fn deser_pre658_receipt_envelope() {
use alloy_primitives::b256;
let receipt = super::ReceiptWithBloom::<()> {
receipt: super::Receipt {
status: super::Eip658Value::PostState(b256!(
"284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10"
)),
cumulative_gas_used: 0,
logs: Default::default(),
},
logs_bloom: Default::default(),
};
let json = serde_json::to_string(&receipt).unwrap();
println!("Serialized {}", json);
let receipt: super::ReceiptWithBloom<()> = serde_json::from_str(&json).unwrap();
assert_eq!(
receipt.receipt.status,
super::Eip658Value::PostState(b256!(
"284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10"
))
);
}
}