reth_primitives_traits/header/
sealed.rspub use alloy_consensus::Header;
use core::mem;
use alloy_consensus::Sealed;
use alloy_eips::BlockNumHash;
use alloy_primitives::{keccak256, BlockHash, Sealable, B256};
use alloy_rlp::{Decodable, Encodable};
use bytes::BufMut;
use derive_more::{AsRef, Deref};
use crate::InMemorySize;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockWithParent {
pub parent: B256,
pub block: BlockNumHash,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, AsRef, Deref)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(rlp))]
pub struct SealedHeader<H = Header> {
hash: BlockHash,
#[as_ref]
#[deref]
header: H,
}
impl<H> SealedHeader<H> {
#[inline]
pub const fn new(header: H, hash: BlockHash) -> Self {
Self { header, hash }
}
#[inline]
pub const fn header(&self) -> &H {
&self.header
}
#[inline]
pub const fn hash(&self) -> BlockHash {
self.hash
}
pub fn unseal(self) -> H {
self.header
}
pub fn split(self) -> (H, BlockHash) {
(self.header, self.hash)
}
}
impl<H: Sealable> SealedHeader<H> {
pub fn seal(header: H) -> Self {
let hash = header.hash_slow();
Self::new(header, hash)
}
}
impl<H: alloy_consensus::BlockHeader> SealedHeader<H> {
pub fn num_hash(&self) -> BlockNumHash {
BlockNumHash::new(self.number(), self.hash)
}
}
impl InMemorySize for SealedHeader {
#[inline]
fn size(&self) -> usize {
self.header.size() + mem::size_of::<BlockHash>()
}
}
impl<H: Sealable + Default> Default for SealedHeader<H> {
fn default() -> Self {
Self::seal(H::default())
}
}
impl Encodable for SealedHeader {
fn encode(&self, out: &mut dyn BufMut) {
self.header.encode(out);
}
}
impl Decodable for SealedHeader {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let b = &mut &**buf;
let started_len = buf.len();
let header = Header::decode(b)?;
let consumed = started_len - b.len();
let hash = keccak256(&buf[..consumed]);
*buf = *b;
Ok(Self { header, hash })
}
}
#[cfg(any(test, feature = "test-utils"))]
impl SealedHeader {
pub fn set_header(&mut self, header: Header) {
self.header = header
}
pub fn set_hash(&mut self, hash: BlockHash) {
self.hash = hash
}
pub fn set_parent_hash(&mut self, hash: BlockHash) {
self.header.parent_hash = hash
}
pub fn set_block_number(&mut self, number: alloy_primitives::BlockNumber) {
self.header.number = number;
}
pub fn set_state_root(&mut self, state_root: alloy_primitives::B256) {
self.header.state_root = state_root;
}
pub fn set_difficulty(&mut self, difficulty: alloy_primitives::U256) {
self.header.difficulty = difficulty;
}
}
impl<H> From<SealedHeader<H>> for Sealed<H> {
fn from(value: SealedHeader<H>) -> Self {
Self::new_unchecked(value.header, value.hash)
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a, H> arbitrary::Arbitrary<'a> for SealedHeader<H>
where
H: for<'b> arbitrary::Arbitrary<'b> + Sealable,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let header = H::arbitrary(u)?;
Ok(Self::seal(header))
}
}
#[cfg(feature = "serde-bincode-compat")]
pub(super) mod serde_bincode_compat {
use alloy_consensus::serde_bincode_compat::Header;
use alloy_primitives::BlockHash;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs};
#[derive(Debug, Serialize, Deserialize)]
pub struct SealedHeader<'a> {
hash: BlockHash,
header: Header<'a>,
}
impl<'a> From<&'a super::SealedHeader> for SealedHeader<'a> {
fn from(value: &'a super::SealedHeader) -> Self {
Self { hash: value.hash, header: Header::from(&value.header) }
}
}
impl<'a> From<SealedHeader<'a>> for super::SealedHeader {
fn from(value: SealedHeader<'a>) -> Self {
Self { hash: value.hash, header: value.header.into() }
}
}
impl SerializeAs<super::SealedHeader> for SealedHeader<'_> {
fn serialize_as<S>(source: &super::SealedHeader, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
SealedHeader::from(source).serialize(serializer)
}
}
impl<'de> DeserializeAs<'de, super::SealedHeader> for SealedHeader<'de> {
fn deserialize_as<D>(deserializer: D) -> Result<super::SealedHeader, D::Error>
where
D: Deserializer<'de>,
{
SealedHeader::deserialize(deserializer).map(Into::into)
}
}
#[cfg(test)]
mod tests {
use super::super::{serde_bincode_compat, SealedHeader};
use arbitrary::Arbitrary;
use rand::Rng;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
#[test]
fn test_sealed_header_bincode_roundtrip() {
#[serde_as]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Data {
#[serde_as(as = "serde_bincode_compat::SealedHeader")]
transaction: SealedHeader,
}
let mut bytes = [0u8; 1024];
rand::thread_rng().fill(&mut bytes[..]);
let data = Data {
transaction: SealedHeader::arbitrary(&mut arbitrary::Unstructured::new(&bytes))
.unwrap(),
};
let encoded = bincode::serialize(&data).unwrap();
let decoded: Data = bincode::deserialize(&encoded).unwrap();
assert_eq!(decoded, data);
}
}
}