From 0e01a237d028748982d8cc064ede34411ecf929c Mon Sep 17 00:00:00 2001 From: greg <82421016+greged93@users.noreply.github.com> Date: Wed, 15 Jan 2025 08:43:29 +0100 Subject: [PATCH] feat: skip state root validation during unwinding (#135) Signed-off-by: Gregory Edison --- crates/scroll/bin/scroll-reth/Cargo.toml | 3 ++- crates/stages/stages/src/stages/merkle.rs | 27 ++++++++++++------- crates/storage/provider/Cargo.toml | 1 + .../src/providers/database/provider.rs | 7 +++++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/crates/scroll/bin/scroll-reth/Cargo.toml b/crates/scroll/bin/scroll-reth/Cargo.toml index 82acad87a813..975a0a2432b2 100644 --- a/crates/scroll/bin/scroll-reth/Cargo.toml +++ b/crates/scroll/bin/scroll-reth/Cargo.toml @@ -31,7 +31,8 @@ scroll = [ ] skip-state-root-validation = [ "reth-node-builder/skip-state-root-validation", - "reth-scroll-node/skip-state-root-validation" + "reth-scroll-node/skip-state-root-validation", + "reth-provider/skip-state-root-validation" ] optimism = [ "reth-provider/optimism", diff --git a/crates/stages/stages/src/stages/merkle.rs b/crates/stages/stages/src/stages/merkle.rs index 4696a8e3ed69..f4c3c1307e93 100644 --- a/crates/stages/stages/src/stages/merkle.rs +++ b/crates/stages/stages/src/stages/merkle.rs @@ -1,22 +1,27 @@ use alloy_consensus::BlockHeader; -use alloy_primitives::{BlockNumber, B256}; use reth_codecs::Compact; -use reth_consensus::ConsensusError; use reth_db::tables; use reth_db_api::transaction::{DbTx, DbTxMut}; -use reth_primitives::{GotExpected, SealedHeader}; use reth_provider::{ DBProvider, HeaderProvider, LatestStateProviderRef, ProviderError, StageCheckpointReader, StageCheckpointWriter, StateCommitmentProvider, StateRootProviderExt, StatsReader, TrieWriter, }; use reth_stages_api::{ - BlockErrorKind, EntitiesCheckpoint, ExecInput, ExecOutput, MerkleCheckpoint, Stage, - StageCheckpoint, StageError, StageId, UnwindInput, UnwindOutput, + EntitiesCheckpoint, ExecInput, ExecOutput, MerkleCheckpoint, Stage, StageCheckpoint, + StageError, StageId, UnwindInput, UnwindOutput, }; use reth_trie::{IntermediateStateRootState, StateRootProgress, StoredSubNode}; use std::fmt::Debug; use tracing::*; +#[cfg(not(feature = "skip-state-root-validation"))] +use { + alloy_primitives::{BlockNumber, B256}, + reth_consensus::ConsensusError, + reth_primitives::{GotExpected, SealedHeader}, + reth_stages_api::BlockErrorKind, +}; + // TODO: automate the process outlined below so the user can just send in a debugging package /// The error message that we include in invalid state root errors to tell users what information /// they should include in a bug report, since true state root errors can be impossible to debug @@ -276,9 +281,7 @@ where self.save_execution_checkpoint(provider, None)?; #[cfg(feature = "skip-state-root-validation")] - { - debug!(target: "sync::stages::merkle::exec", ?trie_root, block_number = target_block.number()); - } + debug!(target: "sync::stages::merkle::exec", ?trie_root, block_number = target_block.number()); #[cfg(not(feature = "skip-state-root-validation"))] validate_state_root(trie_root, SealedHeader::seal(target_block), to_block)?; @@ -334,6 +337,9 @@ where .header_by_number(input.unwind_to)? .ok_or_else(|| ProviderError::HeaderNotFound(input.unwind_to.into()))?; + #[cfg(feature = "skip-state-root-validation")] + debug!(target: "sync::stages::merkle::unwind", ?block_root, block_number = target.number()); + #[cfg(not(feature = "skip-state-root-validation"))] validate_state_root(block_root, SealedHeader::seal(target), input.unwind_to)?; // Validation passed, apply unwind changes to the database. @@ -348,6 +354,7 @@ where /// Check that the computed state root matches the root in the expected header. #[inline] +#[cfg(not(feature = "skip-state-root-validation"))] fn validate_state_root( got: B256, expected: SealedHeader, @@ -373,10 +380,10 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, StorageKind, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; - use alloy_primitives::{keccak256, U256}; + use alloy_primitives::{keccak256, B256, U256}; use assert_matches::assert_matches; use reth_db_api::cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}; - use reth_primitives::{SealedBlock, StaticFileSegment, StorageEntry}; + use reth_primitives::{SealedBlock, SealedHeader, StaticFileSegment, StorageEntry}; use reth_provider::{providers::StaticFileWriter, StaticFileProviderFactory}; use reth_stages_api::StageUnitCheckpoint; use reth_testing_utils::generators::{ diff --git a/crates/storage/provider/Cargo.toml b/crates/storage/provider/Cargo.toml index 84051a68e28e..915722215601 100644 --- a/crates/storage/provider/Cargo.toml +++ b/crates/storage/provider/Cargo.toml @@ -145,3 +145,4 @@ scroll = [ "reth-execution-types/scroll", "reth-evm/scroll", ] +skip-state-root-validation = [] diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index c2a9cb85f9fd..5dc30b5c1451 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -321,8 +321,15 @@ impl DatabaseProvider