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): Define helper trait MaybeCompact #12683

Merged
merged 15 commits into from
Nov 21, 2024
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",
]
]
emhane marked this conversation as resolved.
Show resolved Hide resolved
reth-codec = []
1 change: 0 additions & 1 deletion crates/primitives-traits/src/block/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub trait BlockBody:
+ MaybeSerde
{
/// Ordered list of signed transactions as committed in block.
// todo: requires trait for signed transaction
type Transaction: Transaction;

/// Returns reference to transactions in block.
Expand Down
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: 4 additions & 4 deletions crates/primitives-traits/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ pub mod header;

use alloc::fmt;

use alloy_rlp::{Decodable, Encodable};

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: FullBlockHeader, Body: FullBlockBody> + Encodable + Decodable
Block<Header: FullBlockHeader, Body: FullBlockBody> + alloy_rlp::Encodable + alloy_rlp::Decodable
{
}

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

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 @@ -103,3 +103,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 database encoding implementation since `reth-codec` feature is
/// enabled.
#[cfg(feature = "reth-codec")]
pub trait MaybeCompact: reth_codecs::Compact {}
/// Noop. Helper trait that would require database encoding implementation if `reth-codec` 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 @@ -7,14 +7,13 @@ pub mod tx_type;
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
11 changes: 6 additions & 5 deletions crates/primitives-traits/src/transaction/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ use core::hash::Hash;

use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256};
use reth_codecs::Compact;

use crate::{FillTxEnv, FullTransaction, InMemorySize, MaybeArbitrary, MaybeSerde, Transaction};
use crate::{
FillTxEnv, FullTransaction, InMemorySize, MaybeArbitrary, MaybeCompact, MaybeSerde, Transaction,
};

/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullSignedTx:
SignedTransaction<Transaction: FullTransaction> + FillTxEnv + Compact
SignedTransaction<Transaction: FullTransaction> + FillTxEnv + MaybeCompact
{
}

impl<T> FullSignedTx for T where
T: SignedTransaction<Transaction: FullTransaction> + FillTxEnv + Compact
T: SignedTransaction<Transaction: FullTransaction> + FillTxEnv + MaybeCompact
{
}

Expand All @@ -41,7 +42,7 @@ pub trait SignedTransaction:
+ MaybeArbitrary
+ InMemorySize
{
/// Transaction type that is signed.
/// Unsigned transaction type.
type Transaction: Transaction;

/// Returns reference to transaction hash.
Expand Down
7 changes: 3 additions & 4 deletions crates/primitives-traits/src/transaction/tx_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,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
Loading