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 15 commits
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
6 changes: 3 additions & 3 deletions Makefile
emhane marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ fmt:
cargo +nightly fmt

lint-reth:
cargo +nightly clippy \
cargo +nightly-2024-09-25-aarch64-apple-darwin clippy \
--workspace \
--bin "reth" \
--lib \
Expand All @@ -336,7 +336,7 @@ lint-reth:
-- -D warnings

lint-op-reth:
cargo +nightly clippy \
cargo +nightly-2024-09-25-aarch64-apple-darwin clippy \
--workspace \
--bin "op-reth" \
--lib \
Expand All @@ -347,7 +347,7 @@ lint-op-reth:
-- -D warnings

lint-other-targets:
cargo +nightly clippy \
cargo +nightly-2024-09-25-aarch64-apple-darwin clippy \
--workspace \
--lib \
--examples \
Expand Down
7 changes: 5 additions & 2 deletions crates/primitives-traits/src/header/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ impl SealedHeader {
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for SealedHeader {
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 = Header::arbitrary(u)?;
let header = H::arbitrary(u)?;

let sealed = header.seal_slow();
let (header, seal) = sealed.into_parts();
Expand Down
36 changes: 29 additions & 7 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,26 @@ impl BlockWithSenders {
/// Sealed Ethereum full block.
///
/// 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 Expand Up @@ -477,6 +485,20 @@ impl From<SealedBlock> for Block {
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl<'a, H, B> arbitrary::Arbitrary<'a> for SealedBlock<H, B>
where
H: for<'b> arbitrary::Arbitrary<'b> + Sealable,
B: for<'b> arbitrary::Arbitrary<'b>,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let header = SealedHeader::<H>::arbitrary(u)?;
let body = B::arbitrary(u)?;

Ok(Self { header, body })
}
}

/// Sealed block with senders recovered from transactions.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Deref, DerefMut)]
pub struct SealedBlockWithSenders {
Expand Down Expand Up @@ -544,7 +566,7 @@ impl SealedBlockWithSenders {
#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for SealedBlockWithSenders {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let block = SealedBlock::arbitrary(u)?;
let block: SealedBlock<Header, BlockBody> = SealedBlock::arbitrary(u)?;

let senders = block
.body
Expand Down Expand Up @@ -1133,7 +1155,7 @@ mod tests {

#[test]
fn test_default_seal() {
let block = SealedBlock::default();
let block = SealedBlock::<Header, BlockBody>::default();
let sealed = block.hash();
let block = block.unseal();
let block = block.seal_slow();
Expand Down
Loading