-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add AnyReceiptEnvelope * fix docs
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::ReceiptWithBloom; | ||
use alloy_eips::eip2718::{Decodable2718, Encodable2718}; | ||
use alloy_primitives::{bytes::BufMut, Log}; | ||
use alloy_rlp::{Decodable, Encodable}; | ||
|
||
/// Receipt envelope, as defined in [EIP-2718]. | ||
/// | ||
/// This enum distinguishes between tagged and untagged legacy receipts, as the | ||
/// in-protocol merkle tree may commit to EITHER 0-prefixed or raw. Therefore | ||
/// we must ensure that encoding returns the precise byte-array that was | ||
/// decoded, preserving the presence or absence of the `TransactionType` flag. | ||
/// | ||
/// Transaction receipt payloads are specified in their respective EIPs. | ||
/// | ||
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718 | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct AnyReceiptEnvelope<T = Log> { | ||
/// The receipt envelope. | ||
#[cfg_attr(feature = "serde", serde(flatten))] | ||
pub inner: ReceiptWithBloom<T>, | ||
/// The transaction type. | ||
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u8_hex"))] | ||
pub r#type: u8, | ||
} | ||
|
||
impl AnyReceiptEnvelope { | ||
/// Returns whether this is a legacy receipt (type 0) | ||
pub const fn is_legacy(&self) -> bool { | ||
self.r#type == 0 | ||
} | ||
|
||
/// Calculate the length of the rlp payload of the network encoded receipt. | ||
pub fn rlp_payload_length(&self) -> usize { | ||
let length = self.inner.length(); | ||
if self.is_legacy() { | ||
length | ||
} else { | ||
length + 1 | ||
} | ||
} | ||
} | ||
|
||
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]) -> alloy_rlp::Result<Self> { | ||
let receipt = Decodable::decode(buf)?; | ||
Ok(Self { inner: receipt, r#type: ty }) | ||
} | ||
|
||
fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> { | ||
Self::typed_decode(0, buf) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters