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

User receipt Serdebincodecompat for chain type serde #14669

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
47 changes: 43 additions & 4 deletions crates/evm/execution-types/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash};
use core::{fmt, ops::RangeInclusive};
use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError};
use reth_primitives_traits::{
serde_bincode_compat::SerdeBincodeCompat,
transaction::signed::SignedTransactionIntoRecoveredExt, Block, BlockBody, NodePrimitives,
RecoveredBlock, SealedHeader, SignedTransaction,
};
Expand Down Expand Up @@ -526,7 +527,7 @@ pub enum ChainSplit<N: NodePrimitives = reth_ethereum_primitives::EthPrimitives>
/// Bincode-compatible [`Chain`] serde implementation.
#[cfg(feature = "serde-bincode-compat")]
pub(super) mod serde_bincode_compat {
use crate::ExecutionOutcome;
use crate::{ExecutionOutcome, ExecutionOutcomeBincode};
use alloc::borrow::Cow;
use alloy_primitives::BlockNumber;
use reth_ethereum_primitives::EthPrimitives;
Expand Down Expand Up @@ -554,15 +555,29 @@ pub(super) mod serde_bincode_compat {
/// chain: Chain,
/// }
/// ```
// #[derive(Debug, Serialize, Deserialize)]
// pub struct Chain<'a, N = EthPrimitives>
// where
// N: NodePrimitives<
// Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
// >,
// {
// blocks: RecoveredBlocks<'a, N::Block>,
// execution_outcome: Cow<'a, ExecutionOutcome<N::Receipt>>,
// trie_updates: Option<TrieUpdates<'a>>,
// }

#[derive(Debug, Serialize, Deserialize)]
pub struct Chain<'a, N = EthPrimitives>
where
N: NodePrimitives<
Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
Receipt: SerdeBincodeCompat,
>,
<N::Receipt as SerdeBincodeCompat>::BincodeRepr<'a>: Clone,
{
blocks: RecoveredBlocks<'a, N::Block>,
execution_outcome: Cow<'a, ExecutionOutcome<N::Receipt>>,
execution_outcome: Cow<'a, ExecutionOutcomeBincode<'a, N::Receipt>>, // Use the new type
trie_updates: Option<TrieUpdates<'a>>,
}

Expand Down Expand Up @@ -610,12 +625,14 @@ pub(super) mod serde_bincode_compat {
where
N: NodePrimitives<
Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
Receipt: SerdeBincodeCompat,
>,
<N::Receipt as SerdeBincodeCompat>::BincodeRepr<'a>: Clone,
{
fn from(value: &'a super::Chain<N>) -> Self {
Self {
blocks: RecoveredBlocks(Cow::Borrowed(&value.blocks)),
execution_outcome: Cow::Borrowed(&value.execution_outcome),
execution_outcome: Cow::Owned(value.execution_outcome.as_repr()),
trie_updates: value.trie_updates.as_ref().map(Into::into),
}
}
Expand All @@ -625,12 +642,30 @@ pub(super) mod serde_bincode_compat {
where
N: NodePrimitives<
Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
Receipt: SerdeBincodeCompat,
>,
for<'b> <N::Receipt as SerdeBincodeCompat>::BincodeRepr<'b>: Clone,
{
fn from(value: Chain<'a, N>) -> Self {
let converted_execution_outcome = {
let original = value.execution_outcome.into_owned();
let converted_receipts = original
.receipts
.into_iter()
.map(|vec| vec.into_iter().map(N::Receipt::from_repr).collect::<Vec<_>>())
.collect::<Vec<_>>();

ExecutionOutcome {
bundle: original.bundle.into_owned(),
receipts: converted_receipts,
requests: original.requests.into_owned(),
first_block: original.first_block,
}
};

Self {
blocks: value.blocks.0.into_owned(),
execution_outcome: value.execution_outcome.into_owned(),
execution_outcome: converted_execution_outcome,
trie_updates: value.trie_updates.map(Into::into),
}
}
Expand All @@ -640,7 +675,9 @@ pub(super) mod serde_bincode_compat {
where
N: NodePrimitives<
Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
Receipt: SerdeBincodeCompat,
>,
for<'a> <N::Receipt as SerdeBincodeCompat>::BincodeRepr<'a>: Clone,
{
fn serialize_as<S>(source: &super::Chain<N>, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -654,7 +691,9 @@ pub(super) mod serde_bincode_compat {
where
N: NodePrimitives<
Block: Block<Header: SerdeBincodeCompat, Body: SerdeBincodeCompat> + 'static,
Receipt: SerdeBincodeCompat,
>,
for<'a> <N::Receipt as SerdeBincodeCompat>::BincodeRepr<'a>: Clone,
{
fn deserialize_as<D>(deserializer: D) -> Result<super::Chain<N>, D::Error>
where
Expand Down
47 changes: 46 additions & 1 deletion crates/evm/execution-types/src/execution_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use crate::{BlockExecutionOutput, BlockExecutionResult};
use alloc::{vec, vec::Vec};
use alloy_eips::eip7685::Requests;
use alloy_primitives::{logs_bloom, map::HashMap, Address, BlockNumber, Bloom, Log, B256, U256};
use reth_primitives_traits::{Account, Bytecode, Receipt, StorageEntry};
use reth_primitives_traits::{
serde_bincode_compat::SerdeBincodeCompat, Account, Bytecode, Receipt, StorageEntry,
};
use reth_trie_common::{HashedPostState, KeyHasher};
use revm::state::AccountInfo;
use revm_database::{states::BundleState, BundleAccount};
use std::borrow::Cow;

/// Represents a changed account
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -49,6 +52,48 @@ pub struct ExecutionOutcome<T = reth_ethereum_primitives::Receipt> {
pub requests: Vec<Requests>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExecutionOutcomeBincode<'a, T: SerdeBincodeCompat + std::fmt::Debug> {
pub bundle: Cow<'a, BundleState>,
pub receipts: Vec<Vec<T::BincodeRepr<'a>>>,
pub first_block: BlockNumber,
pub requests: Cow<'a, Vec<Requests>>,
}

impl<T: SerdeBincodeCompat + std::fmt::Debug> SerdeBincodeCompat for ExecutionOutcome<T> {
type BincodeRepr<'a>
= ExecutionOutcomeBincode<'a, T>
where
Self: 'a;

fn as_repr(&self) -> Self::BincodeRepr<'_> {
ExecutionOutcomeBincode {
bundle: Cow::Borrowed(&self.bundle),
receipts: self
.receipts
.iter()
.map(|vec| vec.iter().map(|receipt| T::as_repr(receipt)).collect())
.collect(),
first_block: self.first_block,
requests: Cow::Borrowed(&self.requests),
}
}

fn from_repr(repr: Self::BincodeRepr<'_>) -> Self {
ExecutionOutcome {
bundle: repr.bundle.into_owned(),
receipts: repr
.receipts
.into_iter()
.map(|vec| vec.into_iter().map(|receipt| T::from_repr(receipt)).collect())
.collect(),
first_block: repr.first_block,
requests: repr.requests.into_owned(),
}
}
}

impl<T> Default for ExecutionOutcome<T> {
fn default() -> Self {
Self {
Expand Down
Loading