Skip to content

Commit

Permalink
Define helper triat MaybeCompact
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Nov 19, 2024
1 parent 7c7baca commit 16b2caa
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 28 deletions.
3 changes: 2 additions & 1 deletion crates/primitives-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ serde = [
"revm-primitives/serde",
"roaring/serde",
"revm-primitives/serde",
]
]
reth-codec = []
13 changes: 9 additions & 4 deletions crates/primitives-traits/src/block/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
use alloc::fmt;

use alloy_consensus::Transaction;
use reth_codecs::Compact;

use crate::{FullSignedTx, InMemorySize, MaybeSerde};
use crate::{BlockHeader, FullBlockHeader, FullSignedTx, InMemorySize, MaybeSerde};

/// Helper trait that unifies all behaviour required by transaction to support full node operations.
pub trait FullBlockBody: BlockBody<Transaction: FullSignedTx> + Compact {}
pub trait FullBlockBody: BlockBody<Header: FullBlockHeader, Transaction: FullSignedTx> {}

impl<T> FullBlockBody for T where T: BlockBody<Transaction: FullSignedTx> + Compact {}
impl<T> FullBlockBody for T where T: BlockBody<Header: FullBlockHeader, Transaction: FullSignedTx> {}

/// Abstraction for block's body.
#[auto_impl::auto_impl(&, Arc)]
Expand All @@ -28,10 +27,16 @@ pub trait BlockBody:
+ InMemorySize
+ MaybeSerde
{
/// Uncle block header.
type Header: BlockHeader + 'static;

/// Ordered list of signed transactions as committed in block.
// todo: requires trait for signed transaction
type Transaction: Transaction;

/// Returns reference to transactions in block.
fn transactions(&self) -> &[Self::Transaction];

/// Returns slice of uncle blocks.
fn ommers(&self) -> &[Self::Header];
}
7 changes: 3 additions & 4 deletions crates/primitives-traits/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
use core::fmt;

use alloy_primitives::Sealable;
use reth_codecs::Compact;

use crate::{InMemorySize, MaybeSerde};
use crate::{InMemorySize, MaybeCompact, MaybeSerde};

/// Helper trait that unifies all behaviour required by block header to support full node
/// operations.
pub trait FullBlockHeader: BlockHeader + Compact {}
pub trait FullBlockHeader: BlockHeader + MaybeCompact {}

impl<T> FullBlockHeader for T where T: BlockHeader + Compact {}
impl<T> FullBlockHeader for T where T: BlockHeader + MaybeCompact {}

/// Abstraction of a block header.
pub trait BlockHeader:
Expand Down
8 changes: 3 additions & 5 deletions crates/primitives-traits/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ pub mod header;

use alloc::fmt;

use reth_codecs::Compact;

use crate::{BlockHeader, FullBlockHeader, InMemorySize, MaybeSerde};
use crate::{BlockHeader, FullBlockBody, FullBlockHeader, InMemorySize, MaybeSerde};

/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: Compact> {}
pub trait FullBlock: Block<Header: FullBlockHeader, Body: FullBlockBody> {}

impl<T> FullBlock for T where T: Block<Header: FullBlockHeader> {}
impl<T> FullBlock for T where T: Block<Header: FullBlockHeader, Body: FullBlockBody> {}

/// Abstraction of block data type.
// todo: make sealable super-trait, depends on <https://github.com/paradigmxyz/reth/issues/11449>
Expand Down
14 changes: 14 additions & 0 deletions crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,17 @@ pub trait MaybeSerde {}
impl<T> MaybeSerde for T where T: serde::Serialize + for<'de> serde::Deserialize<'de> {}
#[cfg(not(feature = "serde"))]
impl<T> MaybeSerde for T {}

/// Helper trait that requires databse encoding implementation since `reth-codecs` feature is

Check failure on line 110 in crates/primitives-traits/src/lib.rs

View workflow job for this annotation

GitHub Actions / codespell

databse ==> database
/// enabled.
#[cfg(feature = "reth-codec")]
pub trait MaybeCompact: reth_codecs::Compact {}
/// Noop. Helper trait that would require database encoding implementation if `reth-codecs` feature
/// were enabled.
#[cfg(not(feature = "reth-codec"))]
pub trait MaybeCompact {}

#[cfg(feature = "reth-codec")]
impl<T> MaybeCompact for T where T: reth_codecs::Compact {}
#[cfg(not(feature = "reth-codec"))]
impl<T> MaybeCompact for T {}
11 changes: 6 additions & 5 deletions crates/primitives-traits/src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
//! Receipt abstraction

use crate::{InMemorySize, MaybeSerde};
use core::fmt;

use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::B256;
use core::fmt;
use reth_codecs::Compact;

use crate::{InMemorySize, MaybeCompact, MaybeSerde};

/// Helper trait that unifies all behaviour required by receipt to support full node operations.
pub trait FullReceipt: Receipt + Compact {}
pub trait FullReceipt: Receipt + MaybeCompact {}

impl<T> FullReceipt for T where T: ReceiptExt + Compact {}
impl<T> FullReceipt for T where T: ReceiptExt + MaybeCompact {}

/// Abstraction of a receipt.
#[auto_impl::auto_impl(&, Arc)]
Expand Down
7 changes: 3 additions & 4 deletions crates/primitives-traits/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ pub mod signed;
use core::{fmt, hash::Hash};

use alloy_primitives::B256;
use reth_codecs::Compact;

use crate::{FullTxType, InMemorySize, MaybeArbitrary, MaybeSerde, TxType};
use crate::{FullTxType, InMemorySize, MaybeArbitrary, MaybeCompact, MaybeSerde, TxType};

/// Helper trait that unifies all behaviour required by transaction to support full node operations.
pub trait FullTransaction: Transaction<Type: FullTxType> + Compact {}
pub trait FullTransaction: Transaction<Type: FullTxType> + MaybeCompact {}

impl<T> FullTransaction for T where T: Transaction<Type: FullTxType> + Compact {}
impl<T> FullTransaction for T where T: Transaction<Type: FullTxType> + MaybeCompact {}

/// Abstraction of a transaction.
pub trait Transaction:
Expand Down
7 changes: 3 additions & 4 deletions crates/primitives-traits/src/tx_type.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use core::fmt;

use alloy_primitives::{U64, U8};
use reth_codecs::Compact;

use crate::InMemorySize;
use crate::{InMemorySize, MaybeCompact};

/// Helper trait that unifies all behaviour required by transaction type ID to support full node
/// operations.
pub trait FullTxType: TxType + Compact {}
pub trait FullTxType: TxType + MaybeCompact {}

impl<T> FullTxType for T where T: TxType + Compact {}
impl<T> FullTxType for T where T: TxType + MaybeCompact {}

/// Trait representing the behavior of a transaction type.
pub trait TxType:
Expand Down
7 changes: 6 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ std = [
"serde/std",
"alloy-trie/std"
]
reth-codec = ["dep:reth-codecs", "dep:zstd", "dep:modular-bitfield", "std"]
reth-codec = [
"dep:reth-codecs",
"dep:zstd",
"dep:modular-bitfield", "std",
"reth-primitives-traits/reth-codec",
]
asm-keccak = ["alloy-primitives/asm-keccak", "revm-primitives/asm-keccak"]
arbitrary = [
"dep:arbitrary",
Expand Down
5 changes: 5 additions & 0 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,16 @@ impl InMemorySize for BlockBody {
}

impl reth_primitives_traits::BlockBody for BlockBody {
type Header = Header;
type Transaction = TransactionSigned;

fn transactions(&self) -> &[Self::Transaction] {
&self.transactions
}

fn ommers(&self) -> &[Self::Header] {
&self.ommers
}
}

impl From<Block> for BlockBody {
Expand Down

0 comments on commit 16b2caa

Please sign in to comment.