Skip to content

Commit

Permalink
chore(trie): clean up state root methods (#10598)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Aug 28, 2024
1 parent 3be92d5 commit 6f086d1
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 153 deletions.
5 changes: 4 additions & 1 deletion 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/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use reth_provider::{
FullExecutionDataProvider, ProviderError, StateRootProvider,
};
use reth_revm::database::StateProviderDatabase;
use reth_trie::updates::TrieUpdates;
use reth_trie::{updates::TrieUpdates, HashedPostState};
use reth_trie_parallel::parallel_root::ParallelStateRoot;
use std::{
collections::BTreeMap,
Expand Down Expand Up @@ -232,7 +232,10 @@ impl AppendableChain {
.map(|(root, updates)| (root, Some(updates)))
.map_err(ProviderError::from)?
} else {
(provider.state_root(initial_execution_outcome.state())?, None)
let hashed_state =
HashedPostState::from_bundle_state(&initial_execution_outcome.state().state);
let state_root = provider.state_root(hashed_state)?;
(state_root, None)
};
if block.state_root != state_root {
return Err(ConsensusError::BodyStateRootDiff(
Expand Down
10 changes: 5 additions & 5 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,11 @@ mod tests {
}

impl StateRootProvider for MockStateProvider {
fn hashed_state_root(&self, _hashed_state: HashedPostState) -> ProviderResult<B256> {
fn state_root(&self, _hashed_state: HashedPostState) -> ProviderResult<B256> {
Ok(B256::random())
}

fn hashed_state_root_from_nodes(
fn state_root_from_nodes(
&self,
_nodes: TrieUpdates,
_post_state: HashedPostState,
Expand All @@ -899,14 +899,14 @@ mod tests {
Ok(B256::random())
}

fn hashed_state_root_with_updates(
fn state_root_with_updates(
&self,
_hashed_state: HashedPostState,
) -> ProviderResult<(B256, TrieUpdates)> {
Ok((B256::random(), TrieUpdates::default()))
}

fn hashed_state_root_from_nodes_with_updates(
fn state_root_from_nodes_with_updates(
&self,
_nodes: TrieUpdates,
_post_state: HashedPostState,
Expand All @@ -917,7 +917,7 @@ mod tests {
}

impl StorageRootProvider for MockStateProvider {
fn hashed_storage_root(
fn storage_root(
&self,
_address: Address,
_hashed_storage: HashedStorage,
Expand Down
32 changes: 10 additions & 22 deletions crates/chain-state/src/memory_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ impl AccountReader for MemoryOverlayStateProvider {
}

impl StateRootProvider for MemoryOverlayStateProvider {
fn hashed_state_root(&self, hashed_state: HashedPostState) -> ProviderResult<B256> {
fn state_root(&self, hashed_state: HashedPostState) -> ProviderResult<B256> {
let prefix_sets = hashed_state.construct_prefix_sets();
self.hashed_state_root_from_nodes(TrieUpdates::default(), hashed_state, prefix_sets)
self.state_root_from_nodes(TrieUpdates::default(), hashed_state, prefix_sets)
}

fn hashed_state_root_from_nodes(
fn state_root_from_nodes(
&self,
nodes: TrieUpdates,
state: HashedPostState,
Expand All @@ -116,22 +116,18 @@ impl StateRootProvider for MemoryOverlayStateProvider {
let MemoryOverlayTrieState { mut trie_nodes, mut hashed_state } = self.trie_state().clone();
trie_nodes.extend(nodes);
hashed_state.extend(state);
self.historical.hashed_state_root_from_nodes(trie_nodes, hashed_state, prefix_sets)
self.historical.state_root_from_nodes(trie_nodes, hashed_state, prefix_sets)
}

fn hashed_state_root_with_updates(
fn state_root_with_updates(
&self,
hashed_state: HashedPostState,
) -> ProviderResult<(B256, TrieUpdates)> {
let prefix_sets = hashed_state.construct_prefix_sets();
self.hashed_state_root_from_nodes_with_updates(
TrieUpdates::default(),
hashed_state,
prefix_sets,
)
self.state_root_from_nodes_with_updates(TrieUpdates::default(), hashed_state, prefix_sets)
}

fn hashed_state_root_from_nodes_with_updates(
fn state_root_from_nodes_with_updates(
&self,
nodes: TrieUpdates,
state: HashedPostState,
Expand All @@ -140,21 +136,13 @@ impl StateRootProvider for MemoryOverlayStateProvider {
let MemoryOverlayTrieState { mut trie_nodes, mut hashed_state } = self.trie_state().clone();
trie_nodes.extend(nodes);
hashed_state.extend(state);
self.historical.hashed_state_root_from_nodes_with_updates(
trie_nodes,
hashed_state,
prefix_sets,
)
self.historical.state_root_from_nodes_with_updates(trie_nodes, hashed_state, prefix_sets)
}
}

impl StorageRootProvider for MemoryOverlayStateProvider {
// TODO: Currently this does not reuse available in-memory trie nodes.
fn hashed_storage_root(
&self,
address: Address,
storage: HashedStorage,
) -> ProviderResult<B256> {
fn storage_root(&self, address: Address, storage: HashedStorage) -> ProviderResult<B256> {
let mut hashed_storage = self
.trie_state()
.hashed_state
Expand All @@ -163,7 +151,7 @@ impl StorageRootProvider for MemoryOverlayStateProvider {
.cloned()
.unwrap_or_default();
hashed_storage.extend(&storage);
self.historical.hashed_storage_root(address, hashed_storage)
self.historical.storage_root(address, hashed_storage)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/consensus/auto-seal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ reth-consensus.workspace = true
reth-rpc-types.workspace = true
reth-network-peers.workspace = true
reth-tokio-util.workspace = true
reth-trie.workspace = true

# async
futures-util.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/consensus/auto-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use reth_primitives::{
use reth_provider::{BlockReaderIdExt, StateProviderFactory, StateRootProvider};
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::TransactionPool;
use reth_trie::HashedPostState;
use std::{
collections::HashMap,
sync::Arc,
Expand Down Expand Up @@ -378,6 +379,7 @@ impl StorageInner {
executor.executor(&mut db).execute((&block, U256::ZERO).into())?;
let gas_used = block_execution_output.gas_used;
let execution_outcome = ExecutionOutcome::from((block_execution_output, block.number));
let hashed_state = HashedPostState::from_bundle_state(&execution_outcome.state().state);

// todo(onbjerg): we should not pass requests around as this is building a block, which
// means we need to extract the requests from the execution output and compute the requests
Expand All @@ -389,7 +391,7 @@ impl StorageInner {
trace!(target: "consensus::auto", ?execution_outcome, ?header, ?body, "executed block, calculating state root and completing header");

// now we need to update certain header fields with the results of the execution
header.state_root = db.state_root(execution_outcome.state())?;
header.state_root = db.state_root(hashed_state)?;
header.gas_used = gas_used;

let receipts = execution_outcome.receipts_by_block(header.number);
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ where

let root_time = Instant::now();
let (state_root, trie_output) =
state_provider.hashed_state_root_with_updates(hashed_state.clone())?;
state_provider.state_root_with_updates(hashed_state.clone())?;
if state_root != block.state_root {
return Err(ConsensusError::BodyStateRootDiff(
GotExpected { got: state_root, expected: block.state_root }.into(),
Expand Down
1 change: 1 addition & 0 deletions crates/engine/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ reth-revm.workspace = true
reth-provider.workspace = true
reth-ethereum-forks.workspace = true
revm-primitives.workspace = true
reth-trie.workspace = true

# async
tokio = { workspace = true, default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion crates/engine/util/src/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use reth_rpc_types::{
ExecutionPayload,
};
use reth_rpc_types_compat::engine::payload::block_to_payload;
use reth_trie::HashedPostState;
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EVMError, EnvWithHandlerCfg};
use std::{
collections::VecDeque,
Expand Down Expand Up @@ -367,6 +368,7 @@ where
reorg_target.number,
Default::default(),
);
let hashed_state = HashedPostState::from_bundle_state(&outcome.state().state);

let (blob_gas_used, excess_blob_gas) =
if chain_spec.is_cancun_active_at_timestamp(reorg_target.timestamp) {
Expand Down Expand Up @@ -406,7 +408,7 @@ where
gas_used: cumulative_gas_used,
blob_gas_used,
excess_blob_gas,
state_root: state_provider.state_root(outcome.state())?,
state_root: state_provider.state_root(hashed_state)?,
},
body: transactions,
ommers: reorg_target.ommers,
Expand Down
1 change: 1 addition & 0 deletions crates/ethereum/payload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ reth-basic-payload-builder.workspace = true
reth-evm.workspace = true
reth-evm-ethereum.workspace = true
reth-errors.workspace = true
reth-trie.workspace = true

# ethereum
revm.workspace = true
Expand Down
24 changes: 15 additions & 9 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use reth_primitives::{
use reth_provider::StateProviderFactory;
use reth_revm::database::StateProviderDatabase;
use reth_transaction_pool::{BestTransactionsAttributes, TransactionPool};
use reth_trie::HashedPostState;
use revm::{
db::states::bundle_state::BundleRetention,
primitives::{EVMError, EnvWithHandlerCfg, InvalidTransaction, ResultAndState},
Expand Down Expand Up @@ -170,14 +171,17 @@ where

// calculate the state root
let bundle_state = db.take_bundle();
let state_root = db.database.state_root(&bundle_state).map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
err
})?;
let state_root = db
.database
.state_root(HashedPostState::from_bundle_state(&bundle_state.state))
.map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
err
})?;

let mut excess_blob_gas = None;
let mut blob_gas_used = None;
Expand Down Expand Up @@ -497,7 +501,9 @@ where
// calculate the state root
let state_root = {
let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.state_root(execution_outcome.state())?
state_provider
.db
.state_root(HashedPostState::from_bundle_state(&execution_outcome.state().state))?
};

// create the block header
Expand Down
19 changes: 9 additions & 10 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ where

// calculate the state root
let bundle_state = db.take_bundle();
let state_root = db.database.state_root(&bundle_state).map_err(|err| {
let hashed_state = HashedPostState::from_bundle_state(&bundle_state.state);
let state_root = db.database.state_root(hashed_state).map_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
Expand Down Expand Up @@ -529,15 +530,13 @@ where
let hashed_state = HashedPostState::from_bundle_state(&execution_outcome.state().state);
let (state_root, trie_output) = {
let state_provider = db.database.0.inner.borrow_mut();
state_provider.db.hashed_state_root_with_updates(hashed_state.clone()).inspect_err(
|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
},
)?
state_provider.db.state_root_with_updates(hashed_state.clone()).inspect_err(|err| {
warn!(target: "payload_builder",
parent_hash=%parent_block.hash(),
%err,
"failed to calculate state root for empty payload"
);
})?
};

// create the block header
Expand Down
10 changes: 5 additions & 5 deletions crates/revm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ impl BlockHashReader for StateProviderTest {
}

impl StateRootProvider for StateProviderTest {
fn hashed_state_root(&self, _hashed_state: HashedPostState) -> ProviderResult<B256> {
fn state_root(&self, _hashed_state: HashedPostState) -> ProviderResult<B256> {
unimplemented!("state root computation is not supported")
}

fn hashed_state_root_from_nodes(
fn state_root_from_nodes(
&self,
_nodes: TrieUpdates,
_hashed_state: HashedPostState,
Expand All @@ -85,14 +85,14 @@ impl StateRootProvider for StateProviderTest {
unimplemented!("state root computation is not supported")
}

fn hashed_state_root_with_updates(
fn state_root_with_updates(
&self,
_hashed_state: HashedPostState,
) -> ProviderResult<(B256, TrieUpdates)> {
unimplemented!("state root computation is not supported")
}

fn hashed_state_root_from_nodes_with_updates(
fn state_root_from_nodes_with_updates(
&self,
_nodes: TrieUpdates,
_hashed_state: HashedPostState,
Expand All @@ -103,7 +103,7 @@ impl StateRootProvider for StateProviderTest {
}

impl StorageRootProvider for StateProviderTest {
fn hashed_storage_root(
fn storage_root(
&self,
_address: Address,
_hashed_storage: HashedStorage,
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc-eth-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ reth-execution-types.workspace = true
reth-rpc-eth-types.workspace = true
reth-rpc-server-types.workspace = true
reth-network-api.workspace = true
reth-trie.workspace = true

# ethereum
alloy-dyn-abi = { workspace = true, features = ["eip712"] }
Expand Down
Loading

0 comments on commit 6f086d1

Please sign in to comment.