Skip to content

Commit

Permalink
feat: scroll parallel state root (#100)
Browse files Browse the repository at this point in the history
* feat: add parallel trie for Scroll

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

* wip: `ParallelStateRoot` abstraction

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

* chore: remove abstraction

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

* fix: wasm check

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

* fix: answer comments

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

* test: add trie input test for `ParallelStateRoot`

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>

---------

Signed-off-by: Gregory Edison <gregory.edison1993@gmail.com>
  • Loading branch information
greged93 authored Dec 24, 2024
1 parent 4554b75 commit 944bfd0
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .github/assets/check_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ exclude_crates=(
reth-scroll-cli # tokio
reth-scroll-node # tokio
reth-scroll # tokio
reth-scroll-state-commitment # tokio
reth-scroll-chainspec # tokio
reth-scroll-evm # tokio
)

# Array to hold the results
Expand Down
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/blockchain-tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ alloy-primitives.workspace = true
alloy-eips.workspace = true

# scroll
reth-scroll-state-commitment = { workspace = true, optional = true }
reth-scroll-storage = { workspace = true, optional = true }

# common
Expand Down Expand Up @@ -82,7 +83,8 @@ test-utils = [
"reth-provider/test-utils",
"reth-trie-db/test-utils",
"reth-trie/test-utils",
"revm/test-utils"
"revm/test-utils",
"reth-scroll-state-commitment?/test-utils"
]
optimism = [
"reth-primitives/optimism",
Expand All @@ -103,5 +105,6 @@ scroll = [
"reth-testing-utils/scroll",
"reth-trie/scroll",
"revm/scroll",
"reth-scroll-storage/scroll"
"reth-scroll-storage/scroll",
"reth-scroll-state-commitment/scroll"
]
18 changes: 12 additions & 6 deletions crates/blockchain-tree/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use reth_provider::{
StateRootProvider, TryIntoHistoricalStateProvider,
};
use reth_trie::{updates::TrieUpdates, TrieInput};
use reth_trie_parallel::root::ParallelStateRoot;
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
Expand Down Expand Up @@ -228,13 +227,20 @@ impl AppendableChain {
let mut execution_outcome =
provider.block_execution_data_provider.execution_outcome().clone();
execution_outcome.extend(initial_execution_outcome.clone());
ParallelStateRoot::new(
#[cfg(feature = "scroll")]
let parallel_state_root = reth_scroll_state_commitment::ParallelStateRoot::new(
consistent_view,
TrieInput::from_state(provider.hashed_post_state(execution_outcome.state())),
)
.incremental_root_with_updates()
.map(|(root, updates)| (root, Some(updates)))
.map_err(ProviderError::from)?
);
#[cfg(not(feature = "scroll"))]
let parallel_state_root = reth_trie_parallel::root::ParallelStateRoot::new(
consistent_view,
TrieInput::from_state(provider.hashed_post_state(execution_outcome.state())),
);
parallel_state_root
.incremental_root_with_updates()
.map(|(root, updates)| (root, Some(updates)))
.map_err(ProviderError::from)?
} else {
let hashed_state = provider.hashed_post_state(initial_execution_outcome.state());
let state_root = provider.state_root_from_state(hashed_state)?;
Expand Down
1 change: 1 addition & 0 deletions crates/blockchain-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use reth_revm as _;
use reth_trie_parallel as _;

/// Re-export of the blockchain tree API.
pub use reth_blockchain_tree_api::*;
Expand Down
5 changes: 4 additions & 1 deletion crates/engine/tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ reth-trie-sparse.workspace = true
reth-trie.workspace = true

# scroll
reth-scroll-state-commitment = { workspace = true, optional = true }
reth-scroll-storage = { workspace = true, optional = true }

# alloy
Expand Down Expand Up @@ -125,8 +126,10 @@ test-utils = [
"reth-trie/test-utils",
"reth-prune-types?/test-utils",
"reth-trie-db/test-utils",
"reth-scroll-state-commitment?/test-utils"
]
scroll = [
"reth-scroll-primitives",
"reth-scroll-storage/scroll"
"reth-scroll-storage/scroll",
"reth-scroll-state-commitment/scroll"
]
10 changes: 8 additions & 2 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use reth_provider::{
};
use reth_stages_api::ControlFlow;
use reth_trie::{updates::TrieUpdates, HashedPostState, TrieInput};
use reth_trie_parallel::root::{ParallelStateRoot, ParallelStateRootError};
use reth_trie_parallel::root::ParallelStateRootError;
use revm_primitives::EvmState;
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -2374,7 +2374,13 @@ where
// Extend with block we are validating root for.
input.append_ref(hashed_state);

ParallelStateRoot::new(consistent_view, input).incremental_root_with_updates()
#[cfg(feature = "scroll")]
let parallel_state_root =
reth_scroll_state_commitment::ParallelStateRoot::new(consistent_view, input);
#[cfg(not(feature = "scroll"))]
let parallel_state_root =
reth_trie_parallel::root::ParallelStateRoot::new(consistent_view, input);
parallel_state_root.incremental_root_with_updates()
}

/// Handles an error that occurred while inserting a block.
Expand Down
27 changes: 20 additions & 7 deletions crates/scroll/state-commitment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ exclude.workspace = true
workspace = true

[dependencies]
reth-db.workspace = true
# reth
reth-db.workspace = true
reth-execution-errors.workspace = true
reth-primitives.workspace = true
reth-primitives-traits.workspace = true
reth-provider.workspace = true
reth-storage-api.workspace = true
reth-trie.workspace = true
reth-trie-db.workspace = true
reth-trie-parallel.workspace = true

# scroll
reth-scroll-execution.workspace = true
reth-scroll-primitives.workspace = true
reth-scroll-trie.workspace = true
reth-trie.workspace = true
reth-trie-db.workspace = true

# alloy
alloy-consensus.workspace = true
Expand All @@ -38,23 +44,26 @@ zktrie_rust = { git = "https://github.com/scroll-tech/zktrie.git", rev = "309160
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", rev = "309160464c1cd2b87a578ed6d9b6e98205ae4640", features = ["rs_zktrie"], optional = true }

# misc
tracing.workspace = true
itertools.workspace = true
rayon.workspace = true
tracing.workspace = true

[dev-dependencies]
reth-db-api.workspace = true
reth-primitives = { workspace = true, features = ["test-utils", "arbitrary"] }
reth-node-types.workspace = true
reth-scroll-state-commitment = { workspace = true, features = ["test-utils"] }
reth-trie = { workspace = true, features = ["test-utils" ] }
reth-trie = { workspace = true, features = ["test-utils"] }
reth-trie-common = { workspace = true, features = ["test-utils", "arbitrary"] }
reth-provider = { workspace = true, features = ["test-utils" ] }
reth-provider = { workspace = true, features = ["test-utils"] }
alloy-consensus.workspace = true
zktrie_rust = { git = "https://github.com/scroll-tech/zktrie.git", rev = "309160464c1cd2b87a578ed6d9b6e98205ae4640" }
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", rev = "309160464c1cd2b87a578ed6d9b6e98205ae4640", features = ["rs_zktrie"] }
proptest.workspace = true
proptest-arbitrary-interop.workspace = true

[features]
default = ["metrics"]
scroll = [
"reth-trie/scroll",
"reth-primitives-traits/scroll",
Expand All @@ -76,6 +85,10 @@ test-utils = [
"reth-trie-common/test-utils",
"reth-trie-db/test-utils"
]
metrics = ["reth-metrics", "dep:metrics"]
metrics = [
"reth-metrics",
"dep:metrics",
"reth-trie-parallel/metrics"
]


2 changes: 1 addition & 1 deletion crates/scroll/state-commitment/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory, State
pub struct BinaryMerklePatriciaTrie;

impl StateCommitment for BinaryMerklePatriciaTrie {
type KeyHasher = PoseidonKeyHasher;
type StateRoot<'a, TX: DbTx + 'a> =
StateRoot<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>;
type StorageRoot<'a, TX: DbTx + 'a> =
Expand All @@ -23,4 +22,5 @@ impl StateCommitment for BinaryMerklePatriciaTrie {
DatabaseTrieCursorFactory<'a, TX>,
DatabaseHashedCursorFactory<'a, TX>,
>;
type KeyHasher = PoseidonKeyHasher;
}
3 changes: 3 additions & 0 deletions crates/scroll/state-commitment/src/root/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use tracing::{debug, trace};
#[cfg(feature = "metrics")]
use reth_trie::metrics::{StateRootMetrics, TrieRootMetrics, TrieType};

mod parallel;
pub use parallel::ParallelStateRoot;

mod utils;
pub use utils::*;

Expand Down
Loading

0 comments on commit 944bfd0

Please sign in to comment.