Skip to content

Commit

Permalink
Perf: nondet sigs + genesis serde (#124)
Browse files Browse the repository at this point in the history
* bump version to 1.2 and update patch commit

* update patch commit, broken docker build

* switch risc0-build to use unstable (will in future fix build issue)

* Update to risc0 1.2.1-rc.0

* Update to risc0 1.2.1-rc.0

* Update lock files, cache block

* remove bogus block

* nondet sigs + genesis serde + refactors

* nits, formatting

* Added backoff/retry for HTTP Provider cache collect (#125)

* fix bonsai support

* update reth patch

* use rkyv for serde

* cleanup commented code

* fix tests

---------

Co-authored-by: Austin Abell <[email protected]>
Co-authored-by: Frank Laub <[email protected]>
Co-authored-by: Parker Thompson <[email protected]>
  • Loading branch information
4 people authored Dec 12, 2024
1 parent 4c6a74f commit 4466ac7
Show file tree
Hide file tree
Showing 25 changed files with 1,051 additions and 167 deletions.
238 changes: 208 additions & 30 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ alloy = { version = "0.4.2", features = ["full"] }
alloy-chains = "0.1.38"
alloy-consensus = "0.4.2"
alloy-genesis = "0.4.2"
alloy-primitives = { version = "0.8.8", default-features = false }
alloy-primitives = { version = "0.8.8", default-features = false, features = ["k256"] }
alloy-rlp = "0.3.8"
op-alloy-consensus = "0.4.0"
op-alloy-network = "0.4.0"
Expand Down Expand Up @@ -96,6 +96,7 @@ log = "0.4.22"
flate2 = "1.0.34"
once_cell = "1.20.2"
pot = "3.0.1"
rkyv = "0.8.9"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["alloc"] }
serde_with = "3.11.0"
Expand Down
1 change: 0 additions & 1 deletion bin/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ workspace = true
[dependencies.zeth-guests]
workspace = true


[dependencies]
anyhow.workspace = true
reth-chainspec.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/core-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ where
// Instantiate execution engine using database
let mut executor = EthExecutorProvider::ethereum(chain_spec.clone())
.batch_executor(db.take().expect("Missing database."));

// Verify the transaction signatures and compute senders
let mut senders = Vec::with_capacity(block.body.transactions.len());
for (i, tx) in block.body.transactions().enumerate() {
Expand Down
1 change: 0 additions & 1 deletion crates/core-optimism/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ where
// Instantiate execution engine using database
let mut executor = OpExecutorProvider::optimism(chain_spec.clone())
.batch_executor(db.take().expect("Missing database"));

// Verify the transaction signatures and compute senders
let mut vk_it = signers.iter();
let mut senders = Vec::with_capacity(block.body.transactions.len());
Expand Down
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ alloy-primitives.workspace = true
alloy-rlp.workspace = true
k256.workspace = true
pot.workspace = true
rkyv.workspace = true
serde.workspace = true
thiserror.workspace = true
tiny-keccak.workspace = true
Expand All @@ -18,4 +19,3 @@ reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-revm.workspace = true
reth-storage-errors.workspace = true

4 changes: 2 additions & 2 deletions crates/core/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use std::sync::Arc;

pub trait CoreDriver: Default {
type ChainSpec: 'static;
type Block: Serialize + DeserializeOwned + 'static;
type Header: Serialize + DeserializeOwned + 'static;
type Block: Clone + Serialize + DeserializeOwned + 'static;
type Header: Clone + Serialize + DeserializeOwned + 'static;
type Receipt: Serialize + DeserializeOwned + 'static;
type Transaction: Serialize + DeserializeOwned + 'static;

Expand Down
83 changes: 77 additions & 6 deletions crates/core/src/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,41 @@ pub const EMPTY_ROOT: B256 =
/// optimizing storage. However, operations targeting a truncated part will fail and
/// return an error. Another distinction of this implementation is that branches cannot
/// store values, aligning with the construction of MPTs in Ethereum.
#[derive(Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
Ord,
PartialOrd,
Serialize,
Deserialize,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[rkyv(bytecheck(
bounds(
__C: rkyv::validation::ArchiveContext,
__C::Error: rkyv::rancor::Source,
)
))]
#[rkyv(serialize_bounds(
__S: rkyv::ser::Writer + rkyv::ser::Allocator,
__S::Error: rkyv::rancor::Source,
))]
#[rkyv(deserialize_bounds(
__D::Error: rkyv::rancor::Source
))]
pub struct MptNode {
/// The type and data of the node.
data: MptNodeData,
#[rkyv(omit_bounds)]
pub data: MptNodeData,
/// Cache for a previously computed reference of this node. This is skipped during
/// serialization.
#[serde(skip)]
#[rkyv(with = rkyv::with::Skip)]
cached_reference: RefCell<Option<MptNodeReference>>,
}

Expand Down Expand Up @@ -88,21 +116,64 @@ impl From<B256> for MptNode {
/// Each node in the trie can be of one of several types, each with its own specific data
/// structure. This enum provides a clear and type-safe way to represent the data
/// associated with each node type.
#[derive(Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Serialize, Deserialize)]
#[derive(
Clone,
Debug,
Default,
PartialEq,
Eq,
Ord,
PartialOrd,
Serialize,
Deserialize,
rkyv::Archive,
rkyv::Serialize,
rkyv::Deserialize,
)]
#[rkyv(bytecheck(
bounds(
__C: rkyv::validation::ArchiveContext,
)
))]
#[rkyv(serialize_bounds(
__S: rkyv::ser::Writer + rkyv::ser::Allocator,
__S::Error: rkyv::rancor::Source,
))]
#[rkyv(deserialize_bounds(
__D::Error: rkyv::rancor::Source
))]
pub enum MptNodeData {
/// Represents an empty trie node.
#[default]
Null,
/// A node that can have up to 16 children. Each child is an optional boxed [MptNode].
Branch([Option<Box<MptNode>>; 16]),
Branch(
// #[rkyv(with = rkyv::with::Map<rkyv::with::Map<Box<ArchivedMptNode>>>)]
#[rkyv(omit_bounds)] [Option<Box<MptNode>>; 16],
),
/// A leaf node that contains a key and a value, both represented as byte vectors.
Leaf(Vec<u8>, Vec<u8>),
/// A node that has exactly one child and is used to represent a shared prefix of
/// several keys.
Extension(Vec<u8>, Box<MptNode>),
Extension(
Vec<u8>,
// #[rkyv(with = Box<ArchivedMptNode>)]
#[rkyv(omit_bounds)] Box<MptNode>,
),
/// Represents a sub-trie by its hash, allowing for efficient storage of large
/// sub-tries without storing their entire content.
Digest(B256),
Digest(#[rkyv(with = B256Def)] B256),
}

#[derive(Clone, Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
#[rkyv(remote = B256)]
#[rkyv(archived = ArchivedB256)]
pub struct B256Def(pub [u8; 32]);

impl From<B256Def> for B256 {
fn from(value: B256Def) -> Self {
B256::new(value.0)
}
}

/// Represents the ways in which one node can reference another node inside the sparse
Expand Down
16 changes: 11 additions & 5 deletions crates/core/src/stateless/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

use crate::driver::CoreDriver;
use crate::rescue::{Recoverable, Wrapper};
use crate::stateless::data::StatelessClientData;
use crate::stateless::data::{
RkyvStatelessClientData, StatelessClientChainData, StatelessClientData,
};
use crate::stateless::engine::StatelessClientEngine;
use crate::stateless::execute::ExecutionStrategy;
use crate::stateless::finalize::FinalizationStrategy;
use crate::stateless::initialize::InitializationStrategy;
use crate::stateless::validate::ValidationStrategy;
use std::io::Read;

pub trait StatelessClient<Driver, Database>
where
Expand All @@ -32,10 +33,15 @@ where
type Execution: ExecutionStrategy<Driver, Wrapper<Database>>;
type Finalization: FinalizationStrategy<Driver, Database>;

fn data_from_reader<I: Read>(
reader: I,
fn data_from_parts(
rkyv_slice: &[u8],
pot_slice: &[u8],
) -> anyhow::Result<StatelessClientData<Driver::Block, Driver::Header>> {
Ok(pot::from_reader(reader)?)
let rkyv_data =
rkyv::from_bytes::<RkyvStatelessClientData, rkyv::rancor::Error>(rkyv_slice)?;
let chain_data =
pot::from_slice::<StatelessClientChainData<Driver::Block, Driver::Header>>(pot_slice)?;
Ok(StatelessClientData::<Driver::Block, Driver::Header>::from_parts(rkyv_data, chain_data))
}

fn data_from_slice(
Expand Down
Loading

0 comments on commit 4466ac7

Please sign in to comment.