chore(sdk): Define MaybeSerde (#12577)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Emilia Hane
2024-11-18 14:30:44 +01:00
committed by GitHub
parent cee11dfb7c
commit ff22c8eef8
28 changed files with 145 additions and 109 deletions

View File

@ -12,15 +12,16 @@ description = "Common types in reth."
workspace = true
[dependencies]
# reth
reth-codecs.workspace = true
alloy-consensus = { workspace = true, features = ["serde"] }
# ethereum
alloy-consensus.workspace = true
alloy-eips.workspace = true
alloy-genesis.workspace = true
alloy-primitives.workspace = true
alloy-rlp.workspace = true
revm-primitives = { workspace = true, features = ["serde"] }
revm-primitives.workspace = true
# misc
byteorder = "1"
@ -76,7 +77,19 @@ arbitrary = [
"reth-codecs/arbitrary"
]
serde-bincode-compat = [
"serde",
"serde_with",
"alloy-consensus/serde-bincode-compat",
"alloy-eips/serde-bincode-compat"
]
serde = [
"alloy-consensus/serde",
"alloy-eips/serde",
"alloy-primitives/serde",
"bytes/serde",
"rand/serde",
"reth-codecs/serde",
"revm-primitives/serde",
"roaring/serde",
"revm-primitives/serde",
]

View File

@ -6,7 +6,6 @@ use bytes::Buf;
use derive_more::Deref;
use reth_codecs::{add_arbitrary_tests, Compact};
use revm_primitives::{AccountInfo, Bytecode as RevmBytecode, BytecodeDecodeError, JumpTable};
use serde::{Deserialize, Serialize};
/// Identifier for [`LegacyRaw`](RevmBytecode::LegacyRaw).
const LEGACY_RAW_BYTECODE_ID: u8 = 0;
@ -24,7 +23,8 @@ const EOF_BYTECODE_ID: u8 = 3;
const EIP7702_BYTECODE_ID: u8 = 4;
/// An Ethereum account.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize, Compact)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Compact)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub struct Account {
@ -60,7 +60,8 @@ impl Account {
/// Bytecode for an account.
///
/// A wrapper around [`revm::primitives::Bytecode`][RevmBytecode] with encoding/decoding support.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Deref)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default, PartialEq, Eq, Deref)]
pub struct Bytecode(pub RevmBytecode);
impl Bytecode {

View File

@ -1,6 +1,6 @@
//! Block body abstraction.
use crate::InMemorySize;
use crate::{InMemorySize, MaybeSerde};
use alloc::fmt;
use alloy_consensus::Transaction;
@ -15,11 +15,10 @@ pub trait BlockBody:
+ fmt::Debug
+ PartialEq
+ Eq
+ serde::Serialize
+ for<'de> serde::Deserialize<'de>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ InMemorySize
+ MaybeSerde
{
/// Ordered list of signed transactions as committed in block.
// todo: requires trait for signed transaction

View File

@ -1,10 +1,12 @@
//! Block header data primitive.
use crate::InMemorySize;
use alloy_primitives::Sealable;
use core::fmt;
use alloy_primitives::Sealable;
use reth_codecs::Compact;
use crate::{InMemorySize, MaybeSerde};
/// Helper trait that unifies all behaviour required by block header to support full node
/// operations.
pub trait FullBlockHeader: BlockHeader + Compact {}
@ -26,6 +28,7 @@ pub trait BlockHeader:
+ alloy_consensus::BlockHeader
+ Sealable
+ InMemorySize
+ MaybeSerde
{
}
@ -38,12 +41,11 @@ impl<T> BlockHeader for T where
+ fmt::Debug
+ PartialEq
+ Eq
+ serde::Serialize
+ for<'de> serde::Deserialize<'de>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ alloy_consensus::BlockHeader
+ Sealable
+ InMemorySize
+ MaybeSerde
{
}

View File

@ -7,7 +7,7 @@ use alloc::fmt;
use reth_codecs::Compact;
use crate::{BlockHeader, FullBlockHeader, InMemorySize};
use crate::{BlockHeader, FullBlockHeader, InMemorySize, MaybeSerde};
/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: Compact> + Compact {}
@ -20,17 +20,7 @@ impl<T> FullBlock for T where T: Block<Header: FullBlockHeader> + Compact {}
// senders
#[auto_impl::auto_impl(&, Arc)]
pub trait Block:
Send
+ Sync
+ Unpin
+ Clone
+ Default
+ fmt::Debug
+ PartialEq
+ Eq
+ serde::Serialize
+ for<'a> serde::Deserialize<'a>
+ InMemorySize
Send + Sync + Unpin + Clone + Default + fmt::Debug + PartialEq + Eq + InMemorySize + MaybeSerde
{
/// Header part of the block.
type Header: BlockHeader + 'static;

View File

@ -92,3 +92,16 @@ pub trait MaybeArbitrary {}
impl<T> MaybeArbitrary for T where T: for<'a> arbitrary::Arbitrary<'a> {}
#[cfg(not(any(feature = "test-utils", feature = "arbitrary")))]
impl<T> MaybeArbitrary for T {}
/// Helper trait that requires de-/serialize implementation since `serde` feature is enabled.
#[cfg(feature = "serde")]
pub trait MaybeSerde: serde::Serialize + for<'de> serde::Deserialize<'de> {}
/// Noop. Helper trait that would require de-/serialize implementation if `serde` feature were
/// enabled.
#[cfg(not(feature = "serde"))]
pub trait MaybeSerde {}
#[cfg(feature = "serde")]
impl<T> MaybeSerde for T where T: serde::Serialize + for<'de> serde::Deserialize<'de> {}
#[cfg(not(feature = "serde"))]
impl<T> MaybeSerde for T {}

View File

@ -1,17 +1,17 @@
use core::fmt;
use crate::{BlockBody, FullBlock, FullReceipt, FullSignedTx, FullTxType};
use crate::{BlockBody, FullBlock, FullReceipt, FullSignedTx, FullTxType, MaybeSerde};
/// Configures all the primitive types of the node.
pub trait NodePrimitives: Send + Sync + Unpin + Clone + Default + fmt::Debug + 'static {
/// Block primitive.
type Block: Send + Sync + Unpin + Clone + Default + fmt::Debug + 'static;
type Block: Send + Sync + Unpin + Clone + Default + fmt::Debug + MaybeSerde + 'static;
/// Signed version of the transaction type.
type SignedTx: Send + Sync + Unpin + Clone + Default + fmt::Debug + 'static;
type SignedTx: Send + Sync + Unpin + Clone + Default + fmt::Debug + MaybeSerde + 'static;
/// Transaction envelope type ID.
type TxType: Send + Sync + Unpin + Clone + Default + fmt::Debug + 'static;
/// A receipt.
type Receipt: Send + Sync + Unpin + Clone + Default + fmt::Debug + 'static;
type Receipt: Send + Sync + Unpin + Clone + Default + fmt::Debug + MaybeSerde + 'static;
}
impl NodePrimitives for () {

View File

@ -1,13 +1,11 @@
//! Receipt abstraction
use crate::{InMemorySize, MaybeSerde};
use alloc::vec::Vec;
use alloy_consensus::TxReceipt;
use alloy_primitives::B256;
use core::fmt;
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};
use crate::InMemorySize;
/// Helper trait that unifies all behaviour required by receipt to support full node operations.
pub trait FullReceipt: Receipt + Compact {}
@ -26,9 +24,8 @@ pub trait Receipt:
+ TxReceipt
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ Serialize
+ MaybeSerde
+ InMemorySize
+ for<'de> Deserialize<'de>
{
/// Returns transaction type.
fn tx_type(&self) -> u8;

View File

@ -6,9 +6,8 @@ use core::{fmt, hash::Hash};
use alloy_primitives::B256;
use reth_codecs::Compact;
use serde::{Deserialize, Serialize};
use crate::{FullTxType, InMemorySize, MaybeArbitrary, TxType};
use crate::{FullTxType, InMemorySize, MaybeArbitrary, MaybeSerde, TxType};
/// Helper trait that unifies all behaviour required by transaction to support full node operations.
pub trait FullTransaction: Transaction<Type: FullTxType> + Compact {}
@ -26,10 +25,9 @@ pub trait Transaction:
+ Eq
+ PartialEq
+ Hash
+ Serialize
+ for<'de> Deserialize<'de>
+ TransactionExt
+ InMemorySize
+ MaybeSerde
+ MaybeArbitrary
{
}
@ -44,10 +42,9 @@ impl<T> Transaction for T where
+ Eq
+ PartialEq
+ Hash
+ Serialize
+ for<'de> Deserialize<'de>
+ TransactionExt
+ InMemorySize
+ MaybeSerde
+ MaybeArbitrary
{
}

View File

@ -8,7 +8,7 @@ use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256};
use reth_codecs::Compact;
use revm_primitives::TxEnv;
use crate::{FullTransaction, InMemorySize, MaybeArbitrary, Transaction};
use crate::{FullTransaction, InMemorySize, MaybeArbitrary, MaybeSerde, Transaction};
/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullSignedTx: SignedTransaction<Transaction: FullTransaction> + Compact {}
@ -27,13 +27,12 @@ pub trait SignedTransaction:
+ PartialEq
+ Eq
+ Hash
+ serde::Serialize
+ for<'a> serde::Deserialize<'a>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ Encodable2718
+ Decodable2718
+ alloy_consensus::Transaction
+ MaybeSerde
+ MaybeArbitrary
+ InMemorySize
{