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

chore(sdk): SealedBlock generic over header and body #11430

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 29 additions & 5 deletions crates/primitives-traits/src/header/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,48 @@ use serde::{Deserialize, Serialize};

/// A [`Header`] that is sealed at a precalculated hash, use [`SealedHeader::unseal()`] if you want
/// to modify header.
#[derive(Debug, Clone, PartialEq, Eq, Hash, AsRef, Deref, Serialize, Deserialize, Compact)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, AsRef, Deref, Serialize, Deserialize)]
#[add_arbitrary_tests(rlp, compact)]
pub struct SealedHeader {
pub struct SealedHeader<H = Header> {
/// Locked Header hash.
hash: BlockHash,
/// Locked Header fields.
#[as_ref]
#[deref]
header: Header,
header: H,
}

impl SealedHeader {
impl<H: Compact> Compact for SealedHeader<H> {
emhane marked this conversation as resolved.
Show resolved Hide resolved
fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
let mut buffer = Vec::new();
self.hash.to_compact(&mut buffer);
self.header.to_compact(&mut buffer);
buf.put(&buffer[..]);
buffer.len()
}

fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
let (hash, new_buf) = BlockHash::from_compact(buf, buf.len());
buf = new_buf;
let (header, new_buf) = H::from_compact(buf, buf.len());
buf = new_buf;
let sealed_header = Self { hash, header };
(sealed_header, buf)
}
}

impl<H> SealedHeader<H> {
/// Creates the sealed header with the corresponding block hash.
#[inline]
pub const fn new(header: Header, hash: BlockHash) -> Self {
pub const fn new(header: H, hash: BlockHash) -> Self {
Self { header, hash }
}
}

impl SealedHeader {
/// Returns the sealed Header fields.
#[inline]
pub const fn header(&self) -> &Header {
Expand Down
17 changes: 13 additions & 4 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,25 @@ impl BlockWithSenders {
/// Withdrawals can be optionally included at the end of the RLP encoded message.
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(rlp, 32))]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Deref, DerefMut)]
pub struct SealedBlock {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Deref, DerefMut)]
pub struct SealedBlock<H = Header, B = BlockBody> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

having to generics here is likely not convenient, ideally this should use the Block trait

Copy link
Member Author

Choose a reason for hiding this comment

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

SealedBlock is a return type in signature of Block trait

/// Locked block header.
#[deref]
#[deref_mut]
pub header: SealedHeader,
pub header: SealedHeader<H>,
/// Block body.
pub body: BlockBody,
pub body: B,
}

impl<H, B> Default for SealedBlock<H, B>
where
H: Default,
B: Default,
{
fn default() -> Self {
Self { header: SealedHeader::<H>::new(H::default(), B256::ZERO), body: Default::default() }
}
}
impl SealedBlock {
/// Create a new sealed block instance using the sealed header and block body.
#[inline]
Expand Down
5 changes: 3 additions & 2 deletions crates/stages/stages/src/stages/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use reth_db_api::{
};
use reth_etl::Collector;
use reth_network_p2p::headers::{downloader::HeaderDownloader, error::HeadersDownloaderError};
use reth_primitives::{SealedHeader, StaticFileSegment};
use reth_primitives::{Header, SealedHeader, StaticFileSegment};
use reth_provider::{
providers::{StaticFileProvider, StaticFileWriter},
BlockHashReader, DBProvider, HeaderProvider, HeaderSyncGap, HeaderSyncGapProvider,
Expand Down Expand Up @@ -121,7 +121,8 @@ where
info!(target: "sync::stages::headers", progress = %format!("{:.2}%", (index as f64 / total_headers as f64) * 100.0), "Writing headers");
}

let (sealed_header, _) = SealedHeader::from_compact(&header_buf, header_buf.len());
let (sealed_header, _) =
SealedHeader::<Header>::from_compact(&header_buf, header_buf.len());
let (header, header_hash) = sealed_header.split();
if header.number == 0 {
continue
Expand Down
Loading