feat: SerdeBincodeCompat trait (#12991)

This commit is contained in:
Arsenii Kulikov
2024-11-29 16:23:16 +04:00
committed by GitHub
parent a8e2b77df5
commit 1f1671ad8c
9 changed files with 143 additions and 50 deletions

View File

@ -525,7 +525,9 @@ pub(super) mod serde_bincode_compat {
use crate::ExecutionOutcome; use crate::ExecutionOutcome;
use alloc::borrow::Cow; use alloc::borrow::Cow;
use alloy_primitives::BlockNumber; use alloy_primitives::BlockNumber;
use reth_primitives::serde_bincode_compat::SealedBlockWithSenders; use reth_primitives::{
serde_bincode_compat::SealedBlockWithSenders, EthPrimitives, NodePrimitives,
};
use reth_trie_common::serde_bincode_compat::updates::TrieUpdates; use reth_trie_common::serde_bincode_compat::updates::TrieUpdates;
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs}; use serde_with::{DeserializeAs, SerializeAs};
@ -547,18 +549,24 @@ pub(super) mod serde_bincode_compat {
/// } /// }
/// ``` /// ```
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Chain<'a> { pub struct Chain<'a, N = EthPrimitives>
blocks: SealedBlocksWithSenders<'a>, where
execution_outcome: Cow<'a, ExecutionOutcome>, N: NodePrimitives,
{
blocks: SealedBlocksWithSenders<'a, N::Block>,
execution_outcome: Cow<'a, ExecutionOutcome<N::Receipt>>,
trie_updates: Option<TrieUpdates<'a>>, trie_updates: Option<TrieUpdates<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
struct SealedBlocksWithSenders<'a>( struct SealedBlocksWithSenders<'a, B: reth_primitives_traits::Block>(
Cow<'a, BTreeMap<BlockNumber, reth_primitives::SealedBlockWithSenders>>, Cow<'a, BTreeMap<BlockNumber, reth_primitives::SealedBlockWithSenders<B>>>,
); );
impl Serialize for SealedBlocksWithSenders<'_> { impl<B> Serialize for SealedBlocksWithSenders<'_, B>
where
B: reth_primitives_traits::Block,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -573,20 +581,26 @@ pub(super) mod serde_bincode_compat {
} }
} }
impl<'de> Deserialize<'de> for SealedBlocksWithSenders<'_> { impl<'de, B> Deserialize<'de> for SealedBlocksWithSenders<'_, B>
where
B: reth_primitives_traits::Block,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
Ok(Self(Cow::Owned( Ok(Self(Cow::Owned(
BTreeMap::<BlockNumber, SealedBlockWithSenders<'_>>::deserialize(deserializer) BTreeMap::<BlockNumber, SealedBlockWithSenders<'_, B>>::deserialize(deserializer)
.map(|blocks| blocks.into_iter().map(|(n, b)| (n, b.into())).collect())?, .map(|blocks| blocks.into_iter().map(|(n, b)| (n, b.into())).collect())?,
))) )))
} }
} }
impl<'a> From<&'a super::Chain> for Chain<'a> { impl<'a, N> From<&'a super::Chain<N>> for Chain<'a, N>
fn from(value: &'a super::Chain) -> Self { where
N: NodePrimitives,
{
fn from(value: &'a super::Chain<N>) -> Self {
Self { Self {
blocks: SealedBlocksWithSenders(Cow::Borrowed(&value.blocks)), blocks: SealedBlocksWithSenders(Cow::Borrowed(&value.blocks)),
execution_outcome: Cow::Borrowed(&value.execution_outcome), execution_outcome: Cow::Borrowed(&value.execution_outcome),
@ -595,8 +609,11 @@ pub(super) mod serde_bincode_compat {
} }
} }
impl<'a> From<Chain<'a>> for super::Chain { impl<'a, N> From<Chain<'a, N>> for super::Chain<N>
fn from(value: Chain<'a>) -> Self { where
N: NodePrimitives,
{
fn from(value: Chain<'a, N>) -> Self {
Self { Self {
blocks: value.blocks.0.into_owned(), blocks: value.blocks.0.into_owned(),
execution_outcome: value.execution_outcome.into_owned(), execution_outcome: value.execution_outcome.into_owned(),

View File

@ -15,6 +15,7 @@ workspace = true
# reth # reth
reth-chain-state.workspace = true reth-chain-state.workspace = true
reth-execution-types.workspace = true reth-execution-types.workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true reth-primitives-traits.workspace = true
# reth # reth

View File

@ -76,6 +76,7 @@ pub(super) mod serde_bincode_compat {
use std::sync::Arc; use std::sync::Arc;
use reth_execution_types::serde_bincode_compat::Chain; use reth_execution_types::serde_bincode_compat::Chain;
use reth_primitives::{EthPrimitives, NodePrimitives};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs}; use serde_with::{DeserializeAs, SerializeAs};
@ -96,14 +97,21 @@ pub(super) mod serde_bincode_compat {
/// ``` /// ```
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[allow(missing_docs)] #[allow(missing_docs)]
pub enum ExExNotification<'a> { #[serde(bound = "")]
ChainCommitted { new: Chain<'a> }, pub enum ExExNotification<'a, N = EthPrimitives>
ChainReorged { old: Chain<'a>, new: Chain<'a> }, where
ChainReverted { old: Chain<'a> }, N: NodePrimitives,
{
ChainCommitted { new: Chain<'a, N> },
ChainReorged { old: Chain<'a, N>, new: Chain<'a, N> },
ChainReverted { old: Chain<'a, N> },
} }
impl<'a> From<&'a super::ExExNotification> for ExExNotification<'a> { impl<'a, N> From<&'a super::ExExNotification<N>> for ExExNotification<'a, N>
fn from(value: &'a super::ExExNotification) -> Self { where
N: NodePrimitives,
{
fn from(value: &'a super::ExExNotification<N>) -> Self {
match value { match value {
super::ExExNotification::ChainCommitted { new } => { super::ExExNotification::ChainCommitted { new } => {
ExExNotification::ChainCommitted { new: Chain::from(new.as_ref()) } ExExNotification::ChainCommitted { new: Chain::from(new.as_ref()) }
@ -121,8 +129,11 @@ pub(super) mod serde_bincode_compat {
} }
} }
impl<'a> From<ExExNotification<'a>> for super::ExExNotification { impl<'a, N> From<ExExNotification<'a, N>> for super::ExExNotification<N>
fn from(value: ExExNotification<'a>) -> Self { where
N: NodePrimitives,
{
fn from(value: ExExNotification<'a, N>) -> Self {
match value { match value {
ExExNotification::ChainCommitted { new } => { ExExNotification::ChainCommitted { new } => {
Self::ChainCommitted { new: Arc::new(new.into()) } Self::ChainCommitted { new: Arc::new(new.into()) }

View File

@ -4,7 +4,10 @@ use alloc::{fmt, vec::Vec};
use alloy_eips::eip4895::Withdrawals; use alloy_eips::eip4895::Withdrawals;
use crate::{FullSignedTx, InMemorySize, MaybeArbitrary, MaybeSerde, SignedTransaction}; use crate::{
FullSignedTx, InMemorySize, MaybeArbitrary, MaybeSerde, MaybeSerdeBincodeCompat,
SignedTransaction,
};
/// Helper trait that unifies all behaviour required by transaction to support full node operations. /// Helper trait that unifies all behaviour required by transaction to support full node operations.
pub trait FullBlockBody: BlockBody<Transaction: FullSignedTx> {} pub trait FullBlockBody: BlockBody<Transaction: FullSignedTx> {}
@ -26,6 +29,7 @@ pub trait BlockBody:
+ InMemorySize + InMemorySize
+ MaybeSerde + MaybeSerde
+ MaybeArbitrary + MaybeArbitrary
+ MaybeSerdeBincodeCompat
{ {
/// Ordered list of signed transactions as committed in block. /// Ordered list of signed transactions as committed in block.
type Transaction: SignedTransaction; type Transaction: SignedTransaction;

View File

@ -4,7 +4,7 @@ use core::fmt;
use alloy_primitives::Sealable; use alloy_primitives::Sealable;
use crate::{InMemorySize, MaybeArbitrary, MaybeCompact, MaybeSerde}; use crate::{InMemorySize, MaybeArbitrary, MaybeCompact, MaybeSerde, MaybeSerdeBincodeCompat};
/// Helper trait that unifies all behaviour required by block header to support full node /// Helper trait that unifies all behaviour required by block header to support full node
/// operations. /// operations.
@ -29,6 +29,7 @@ pub trait BlockHeader:
+ InMemorySize + InMemorySize
+ MaybeSerde + MaybeSerde
+ MaybeArbitrary + MaybeArbitrary
+ MaybeSerdeBincodeCompat
{ {
} }
@ -48,5 +49,6 @@ impl<T> BlockHeader for T where
+ InMemorySize + InMemorySize
+ MaybeSerde + MaybeSerde
+ MaybeArbitrary + MaybeArbitrary
+ MaybeSerdeBincodeCompat
{ {
} }

View File

@ -173,11 +173,12 @@ where
/// Bincode-compatible [`SealedHeader`] serde implementation. /// Bincode-compatible [`SealedHeader`] serde implementation.
#[cfg(feature = "serde-bincode-compat")] #[cfg(feature = "serde-bincode-compat")]
pub(super) mod serde_bincode_compat { pub(super) mod serde_bincode_compat {
use alloy_consensus::serde_bincode_compat::Header;
use alloy_primitives::BlockHash; use alloy_primitives::BlockHash;
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs}; use serde_with::{DeserializeAs, SerializeAs};
use crate::serde_bincode_compat::SerdeBincodeCompat;
/// Bincode-compatible [`super::SealedHeader`] serde implementation. /// Bincode-compatible [`super::SealedHeader`] serde implementation.
/// ///
/// Intended to use with the [`serde_with::serde_as`] macro in the following way: /// Intended to use with the [`serde_with::serde_as`] macro in the following way:
@ -193,20 +194,21 @@ pub(super) mod serde_bincode_compat {
/// header: SealedHeader, /// header: SealedHeader,
/// } /// }
/// ``` /// ```
#[derive(Debug, Serialize, Deserialize)] #[derive(derive_more::Debug, Serialize, Deserialize)]
pub struct SealedHeader<'a> { #[debug(bound(H::BincodeRepr<'a>: core::fmt::Debug))]
pub struct SealedHeader<'a, H: SerdeBincodeCompat = super::Header> {
hash: BlockHash, hash: BlockHash,
header: Header<'a>, header: H::BincodeRepr<'a>,
} }
impl<'a> From<&'a super::SealedHeader> for SealedHeader<'a> { impl<'a, H: SerdeBincodeCompat> From<&'a super::SealedHeader<H>> for SealedHeader<'a, H> {
fn from(value: &'a super::SealedHeader) -> Self { fn from(value: &'a super::SealedHeader<H>) -> Self {
Self { hash: value.hash, header: Header::from(&value.header) } Self { hash: value.hash, header: (&value.header).into() }
} }
} }
impl<'a> From<SealedHeader<'a>> for super::SealedHeader { impl<'a, H: SerdeBincodeCompat> From<SealedHeader<'a, H>> for super::SealedHeader<H> {
fn from(value: SealedHeader<'a>) -> Self { fn from(value: SealedHeader<'a, H>) -> Self {
Self { hash: value.hash, header: value.header.into() } Self { hash: value.hash, header: value.header.into() }
} }
} }
@ -229,6 +231,9 @@ pub(super) mod serde_bincode_compat {
} }
} }
impl<H: SerdeBincodeCompat> SerdeBincodeCompat for super::SealedHeader<H> {
type BincodeRepr<'a> = SealedHeader<'a, H>;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::super::{serde_bincode_compat, SealedHeader}; use super::super::{serde_bincode_compat, SealedHeader};

View File

@ -68,9 +68,7 @@ pub use header::{BlockWithParent, Header, HeaderError, SealedHeader};
/// ///
/// Read more: <https://github.com/bincode-org/bincode/issues/326> /// Read more: <https://github.com/bincode-org/bincode/issues/326>
#[cfg(feature = "serde-bincode-compat")] #[cfg(feature = "serde-bincode-compat")]
pub mod serde_bincode_compat { pub mod serde_bincode_compat;
pub use super::header::{serde_bincode_compat as header, serde_bincode_compat::*};
}
/// Heuristic size trait /// Heuristic size trait
pub mod size; pub mod size;
@ -118,3 +116,16 @@ pub trait MaybeCompact {}
impl<T> MaybeCompact for T where T: reth_codecs::Compact {} impl<T> MaybeCompact for T where T: reth_codecs::Compact {}
#[cfg(not(feature = "reth-codec"))] #[cfg(not(feature = "reth-codec"))]
impl<T> MaybeCompact for T {} impl<T> MaybeCompact for T {}
/// Helper trait that requires serde bincode compatibility implementation.
#[cfg(feature = "serde-bincode-compat")]
pub trait MaybeSerdeBincodeCompat: crate::serde_bincode_compat::SerdeBincodeCompat {}
/// Noop. Helper trait that would require serde bincode compatibility implementation if
/// `serde-bincode-compat` feature were enabled.
#[cfg(not(feature = "serde-bincode-compat"))]
pub trait MaybeSerdeBincodeCompat {}
#[cfg(feature = "serde-bincode-compat")]
impl<T> MaybeSerdeBincodeCompat for T where T: crate::serde_bincode_compat::SerdeBincodeCompat {}
#[cfg(not(feature = "serde-bincode-compat"))]
impl<T> MaybeSerdeBincodeCompat for T {}

View File

@ -0,0 +1,14 @@
use core::fmt::Debug;
pub use super::header::{serde_bincode_compat as header, serde_bincode_compat::*};
use serde::{de::DeserializeOwned, Serialize};
/// Trait for types that can be serialized and deserialized using bincode.
pub trait SerdeBincodeCompat: Sized + 'static {
/// Serde representation of the type for bincode serialization.
type BincodeRepr<'a>: Debug + Serialize + DeserializeOwned + From<&'a Self> + Into<Self>;
}
impl SerdeBincodeCompat for alloy_consensus::Header {
type BincodeRepr<'a> = alloy_consensus::serde_bincode_compat::Header<'a>;
}

View File

@ -744,7 +744,7 @@ pub(super) mod serde_bincode_compat {
use alloy_consensus::serde_bincode_compat::Header; use alloy_consensus::serde_bincode_compat::Header;
use alloy_eips::eip4895::Withdrawals; use alloy_eips::eip4895::Withdrawals;
use alloy_primitives::Address; use alloy_primitives::Address;
use reth_primitives_traits::serde_bincode_compat::SealedHeader; use reth_primitives_traits::serde_bincode_compat::{SealedHeader, SerdeBincodeCompat};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{DeserializeAs, SerializeAs}; use serde_with::{DeserializeAs, SerializeAs};
@ -810,6 +810,10 @@ pub(super) mod serde_bincode_compat {
} }
} }
impl SerdeBincodeCompat for super::BlockBody {
type BincodeRepr<'a> = BlockBody<'a>;
}
/// Bincode-compatible [`super::SealedBlock`] serde implementation. /// Bincode-compatible [`super::SealedBlock`] serde implementation.
/// ///
/// Intended to use with the [`serde_with::serde_as`] macro in the following way: /// Intended to use with the [`serde_with::serde_as`] macro in the following way:
@ -826,19 +830,34 @@ pub(super) mod serde_bincode_compat {
/// } /// }
/// ``` /// ```
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct SealedBlock<'a> { pub struct SealedBlock<'a, H = super::Header, B = super::BlockBody>
header: SealedHeader<'a>, where
body: BlockBody<'a>, H: SerdeBincodeCompat,
B: SerdeBincodeCompat,
{
header: SealedHeader<'a, H>,
body: B::BincodeRepr<'a>,
} }
impl<'a> From<&'a super::SealedBlock> for SealedBlock<'a> { impl<'a, H, B> From<&'a super::SealedBlock<H, B>> for SealedBlock<'a, H, B>
fn from(value: &'a super::SealedBlock) -> Self { where
Self { header: SealedHeader::from(&value.header), body: BlockBody::from(&value.body) } H: SerdeBincodeCompat,
B: SerdeBincodeCompat,
{
fn from(value: &'a super::SealedBlock<H, B>) -> Self {
Self {
header: SealedHeader::from(&value.header),
body: B::BincodeRepr::from(&value.body),
}
} }
} }
impl<'a> From<SealedBlock<'a>> for super::SealedBlock { impl<'a, H, B> From<SealedBlock<'a, H, B>> for super::SealedBlock<H, B>
fn from(value: SealedBlock<'a>) -> Self { where
H: SerdeBincodeCompat,
B: SerdeBincodeCompat,
{
fn from(value: SealedBlock<'a, H, B>) -> Self {
Self { header: value.header.into(), body: value.body.into() } Self { header: value.header.into(), body: value.body.into() }
} }
} }
@ -877,19 +896,28 @@ pub(super) mod serde_bincode_compat {
/// } /// }
/// ``` /// ```
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct SealedBlockWithSenders<'a> { pub struct SealedBlockWithSenders<'a, B = super::Block>
block: SealedBlock<'a>, where
B: reth_primitives_traits::Block,
{
block: SealedBlock<'a, B::Header, B::Body>,
senders: Cow<'a, Vec<Address>>, senders: Cow<'a, Vec<Address>>,
} }
impl<'a> From<&'a super::SealedBlockWithSenders> for SealedBlockWithSenders<'a> { impl<'a, B> From<&'a super::SealedBlockWithSenders<B>> for SealedBlockWithSenders<'a, B>
fn from(value: &'a super::SealedBlockWithSenders) -> Self { where
B: reth_primitives_traits::Block,
{
fn from(value: &'a super::SealedBlockWithSenders<B>) -> Self {
Self { block: SealedBlock::from(&value.block), senders: Cow::Borrowed(&value.senders) } Self { block: SealedBlock::from(&value.block), senders: Cow::Borrowed(&value.senders) }
} }
} }
impl<'a> From<SealedBlockWithSenders<'a>> for super::SealedBlockWithSenders { impl<'a, B> From<SealedBlockWithSenders<'a, B>> for super::SealedBlockWithSenders<B>
fn from(value: SealedBlockWithSenders<'a>) -> Self { where
B: reth_primitives_traits::Block,
{
fn from(value: SealedBlockWithSenders<'a, B>) -> Self {
Self { block: value.block.into(), senders: value.senders.into_owned() } Self { block: value.block.into(), senders: value.senders.into_owned() }
} }
} }