diff --git a/Cargo.lock b/Cargo.lock index 07a56cde4e2..0eeef6e60c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6200,7 +6200,7 @@ dependencies = [ [[package]] name = "revm" version = "3.3.0" -source = "git+https://github.com/bluealloy/revm.git#3606d70d4aed9207b8299c3bf20d7f4cfc150bf2" +source = "git+https://github.com/bluealloy/revm.git#1053d0e50569a7e2f524297654b098fbfc5ba2c8" dependencies = [ "auto_impl", "once_cell", @@ -6212,7 +6212,7 @@ dependencies = [ [[package]] name = "revm-interpreter" version = "1.1.2" -source = "git+https://github.com/bluealloy/revm.git#3606d70d4aed9207b8299c3bf20d7f4cfc150bf2" +source = "git+https://github.com/bluealloy/revm.git#1053d0e50569a7e2f524297654b098fbfc5ba2c8" dependencies = [ "derive_more", "enumn", @@ -6223,7 +6223,7 @@ dependencies = [ [[package]] name = "revm-precompile" version = "2.0.3" -source = "git+https://github.com/bluealloy/revm.git#3606d70d4aed9207b8299c3bf20d7f4cfc150bf2" +source = "git+https://github.com/bluealloy/revm.git#1053d0e50569a7e2f524297654b098fbfc5ba2c8" dependencies = [ "k256", "num", @@ -6239,7 +6239,7 @@ dependencies = [ [[package]] name = "revm-primitives" version = "1.1.2" -source = "git+https://github.com/bluealloy/revm.git#3606d70d4aed9207b8299c3bf20d7f4cfc150bf2" +source = "git+https://github.com/bluealloy/revm.git#1053d0e50569a7e2f524297654b098fbfc5ba2c8" dependencies = [ "arbitrary", "auto_impl", diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index 95b59cd7bc3..f1593e29726 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -309,7 +309,7 @@ impl StorageInner { executor.post_execution_state_change(block, U256::ZERO)?; // merge transitions - executor.db().merge_transitions(); + executor.db().merge_transitions(true); // apply post block changes Ok((executor.take_output_state(), gas_used)) diff --git a/crates/consensus/beacon/src/engine/test_utils.rs b/crates/consensus/beacon/src/engine/test_utils.rs index 6b863ae555e..20f1a3b9dca 100644 --- a/crates/consensus/beacon/src/engine/test_utils.rs +++ b/crates/consensus/beacon/src/engine/test_utils.rs @@ -199,17 +199,31 @@ where } } - fn stats(&self) -> reth_provider::BlockExecutorStats { + fn take_output_state(&mut self) -> reth_provider::BundleState { match self { - EitherBlockExecutor::Left(a) => a.stats(), - EitherBlockExecutor::Right(b) => b.stats(), + EitherBlockExecutor::Left(a) => a.take_output_state(), + EitherBlockExecutor::Right(b) => b.take_output_state(), } } - fn take_output_state(&mut self) -> reth_provider::BundleState { + fn set_prune_modes(&mut self, prune_modes: PruneModes) { match self { - EitherBlockExecutor::Left(a) => a.take_output_state(), - EitherBlockExecutor::Right(b) => b.take_output_state(), + EitherBlockExecutor::Left(a) => a.set_prune_modes(prune_modes), + EitherBlockExecutor::Right(b) => b.set_prune_modes(prune_modes), + } + } + + fn set_tip(&mut self, tip: BlockNumber) { + match self { + EitherBlockExecutor::Left(a) => a.set_tip(tip), + EitherBlockExecutor::Right(b) => b.set_tip(tip), + } + } + + fn stats(&self) -> reth_provider::BlockExecutorStats { + match self { + EitherBlockExecutor::Left(a) => a.stats(), + EitherBlockExecutor::Right(b) => b.stats(), } } } diff --git a/crates/payload/basic/src/lib.rs b/crates/payload/basic/src/lib.rs index e7300ad045c..85a9455d4ee 100644 --- a/crates/payload/basic/src/lib.rs +++ b/crates/payload/basic/src/lib.rs @@ -752,7 +752,7 @@ where commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, attributes.withdrawals)?; // merge made transaction into bundle state. - db.merge_transitions(); + db.merge_transitions(false); let bundle = BundleState::new(db.take_bundle(), vec![receipts], block_number); let receipts_root = bundle.receipts_root_slow(block_number).expect("Number is in range"); @@ -829,7 +829,7 @@ where commit_withdrawals(&mut db, &chain_spec, attributes.timestamp, attributes.withdrawals)?; // merge transition, this would apply the withdrawal balance changes. - db.merge_transitions(); + db.merge_transitions(false); // calculate the state root let bundle_state = BundleState::new(db.take_bundle(), vec![], block_number); diff --git a/crates/revm/src/processor.rs b/crates/revm/src/processor.rs index 4e11b64aa73..dc4b23e3502 100644 --- a/crates/revm/src/processor.rs +++ b/crates/revm/src/processor.rs @@ -11,8 +11,8 @@ use reth_interfaces::{ Error, }; use reth_primitives::{ - Address, Block, BlockNumber, Bloom, ChainSpec, Hardfork, Header, Receipt, ReceiptWithBloom, - TransactionSigned, H256, U256, + Address, Block, BlockNumber, Bloom, ChainSpec, Hardfork, Header, PruneModes, Receipt, + ReceiptWithBloom, TransactionSigned, H256, U256, }; use reth_provider::{change::BundleState, BlockExecutor, BlockExecutorStats, StateProvider}; use revm::{ @@ -32,6 +32,10 @@ pub struct EVMProcessor<'a> { /// First block will be initialized to ZERO /// and be set to the block number of first block executed. first_block: BlockNumber, + /// The maximum known block . + tip: Option, + /// Pruning configuration. + prune_modes: PruneModes, /// Execution stats stats: BlockExecutorStats, } @@ -47,6 +51,8 @@ impl<'a> From> for EVMProcessor<'a> { stack: InspectorStack::new(InspectorStackConfig::default()), receipts: Vec::new(), first_block: 0, + tip: None, + prune_modes: PruneModes::none(), stats: BlockExecutorStats::default(), } } @@ -70,6 +76,8 @@ impl<'a> EVMProcessor<'a> { stack: InspectorStack::new(InspectorStackConfig::default()), receipts: Vec::new(), first_block: 0, + tip: None, + prune_modes: PruneModes::default(), stats: BlockExecutorStats::default(), } } @@ -294,7 +302,11 @@ impl<'a> BlockExecutor for EVMProcessor<'a> { self.stats.apply_post_execution_changes_duration += time.elapsed(); let time = Instant::now(); - self.db().merge_transitions(); + let with_reverts = self.tip.map_or(true, |tip| { + !self.prune_modes.should_prune_account_history(block.number, tip) && + !self.prune_modes.should_prune_storage_history(block.number, tip) + }); + self.db().merge_transitions(with_reverts); self.stats.merge_transitions_duration += time.elapsed(); if self.first_block == 0 { @@ -337,6 +349,14 @@ impl<'a> BlockExecutor for EVMProcessor<'a> { Ok(()) } + fn set_prune_modes(&mut self, prune_modes: PruneModes) { + self.prune_modes = prune_modes; + } + + fn set_tip(&mut self, tip: BlockNumber) { + self.tip = Some(tip); + } + fn take_output_state(&mut self) -> BundleState { let receipts = std::mem::take(&mut self.receipts); BundleState::new(self.evm.db().unwrap().take_bundle(), receipts, self.first_block) diff --git a/crates/rpc/rpc/src/eth/api/pending_block.rs b/crates/rpc/rpc/src/eth/api/pending_block.rs index 80171e5f013..fa77869d5bf 100644 --- a/crates/rpc/rpc/src/eth/api/pending_block.rs +++ b/crates/rpc/rpc/src/eth/api/pending_block.rs @@ -113,8 +113,8 @@ impl PendingBlockEnv { // append transaction to the list of executed transactions executed_txs.push(tx.into_signed()); } - // merge made transations into bundle state. - db.merge_transitions(); + // merge made transitions into bundle state. + db.merge_transitions(false); let bundle = BundleState::new(db.take_bundle(), vec![receipts], block_number); diff --git a/crates/stages/src/stages/execution.rs b/crates/stages/src/stages/execution.rs index d3f2a526f36..0670962487a 100644 --- a/crates/stages/src/stages/execution.rs +++ b/crates/stages/src/stages/execution.rs @@ -119,12 +119,13 @@ impl ExecutionStage { let start_block = input.next_block(); let max_block = input.target(); - // TODO(rakita) integrate - let _prune_modes = self.adjust_prune_modes(provider, start_block, max_block)?; + let prune_modes = self.adjust_prune_modes(provider, start_block, max_block)?; // Build executor let mut executor = self.executor_factory.with_sp(LatestStateProviderRef::new(provider.tx_ref())); + executor.set_prune_modes(prune_modes); + executor.set_tip(max_block); // Progress tracking let mut stage_progress = start_block; @@ -138,8 +139,6 @@ impl ExecutionStage { let mut cumulative_gas = 0; - // TODO(rakita) integrate prunning. - //state.add_prune_modes(prune_modes); for block_number in start_block..=max_block { let time = Instant::now(); let td = provider diff --git a/crates/storage/provider/src/change.rs b/crates/storage/provider/src/change.rs index 709210c27d4..f07bd6026a3 100644 --- a/crates/storage/provider/src/change.rs +++ b/crates/storage/provider/src/change.rs @@ -618,7 +618,7 @@ mod tests { }, )])); - state.merge_transitions(); + state.merge_transitions(true); let mut revm_bundle_state = state.take_bundle(); // Write plain state and reverts separately. @@ -681,7 +681,7 @@ mod tests { }, )])); - state.merge_transitions(); + state.merge_transitions(true); let mut revm_bundle_state = state.take_bundle(); // Write plain state and reverts separately. @@ -770,7 +770,7 @@ mod tests { ), ])); - state.merge_transitions(); + state.merge_transitions(true); BundleState::new(state.take_bundle(), Vec::new(), 1) .write_to_db(provider.tx_ref(), false) @@ -870,7 +870,7 @@ mod tests { }, )])); - state.merge_transitions(); + state.merge_transitions(true); BundleState::new(state.take_bundle(), Vec::new(), 2) .write_to_db(provider.tx_ref(), false) .expect("Could not write bundle state to DB"); @@ -937,7 +937,7 @@ mod tests { ]), }, )])); - init_state.merge_transitions(); + init_state.merge_transitions(true); BundleState::new(init_state.take_bundle(), Vec::new(), 0) .write_to_db(provider.tx_ref(), false) .expect("Could not write init bundle state to DB"); @@ -966,7 +966,7 @@ mod tests { )]), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #2: destroy account. state.commit(HashMap::from([( @@ -977,7 +977,7 @@ mod tests { storage: HashMap::default(), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #3: re-create account and change storage. state.commit(HashMap::from([( @@ -988,7 +988,7 @@ mod tests { storage: HashMap::default(), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #4: change storage. state.commit(HashMap::from([( @@ -1015,7 +1015,7 @@ mod tests { ]), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #5: Destroy account again. state.commit(HashMap::from([( @@ -1026,7 +1026,7 @@ mod tests { storage: HashMap::default(), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #6: Create, change, destroy and re-create in the same block. state.commit(HashMap::from([( @@ -1065,7 +1065,7 @@ mod tests { storage: HashMap::default(), }, )])); - state.merge_transitions(); + state.merge_transitions(true); // Block #7: Change storage. state.commit(HashMap::from([( @@ -1080,7 +1080,7 @@ mod tests { )]), }, )])); - state.merge_transitions(); + state.merge_transitions(true); let bundle = state.take_bundle(); @@ -1248,7 +1248,7 @@ mod tests { ]), }, )])); - init_state.merge_transitions(); + init_state.merge_transitions(true); BundleState::new(init_state.take_bundle(), Vec::new(), 0) .write_to_db(provider.tx_ref(), false) .expect("Could not write init bundle state to DB"); @@ -1294,7 +1294,7 @@ mod tests { )])); // Commit block #1 changes to the database. - state.merge_transitions(); + state.merge_transitions(true); BundleState::new(state.take_bundle(), Vec::new(), 1) .write_to_db(provider.tx_ref(), false) .expect("Could not write bundle state to DB"); diff --git a/crates/storage/provider/src/test_utils/executor.rs b/crates/storage/provider/src/test_utils/executor.rs index 9b6c6cd6112..a1806b6b7c2 100644 --- a/crates/storage/provider/src/test_utils/executor.rs +++ b/crates/storage/provider/src/test_utils/executor.rs @@ -3,7 +3,7 @@ use crate::{ }; use parking_lot::Mutex; use reth_interfaces::executor::BlockExecutionError; -use reth_primitives::{Address, Block, ChainSpec, U256}; +use reth_primitives::{Address, Block, BlockNumber, ChainSpec, U256}; use std::sync::Arc; /// Test executor with mocked result. pub struct TestExecutor(pub Option); @@ -33,6 +33,10 @@ impl BlockExecutor for TestExecutor { Ok(()) } + fn set_prune_modes(&mut self, _prune_modes: reth_primitives::PruneModes) {} + + fn set_tip(&mut self, _tip: BlockNumber) {} + fn take_output_state(&mut self) -> BundleState { self.0.clone().unwrap_or_default() } diff --git a/crates/storage/provider/src/traits/executor.rs b/crates/storage/provider/src/traits/executor.rs index bed463fea07..17d31cc35ae 100644 --- a/crates/storage/provider/src/traits/executor.rs +++ b/crates/storage/provider/src/traits/executor.rs @@ -4,7 +4,7 @@ use std::time::Duration; use crate::{change::BundleState, StateProvider}; use reth_interfaces::executor::BlockExecutionError; -use reth_primitives::{Address, Block, ChainSpec, U256}; +use reth_primitives::{Address, Block, BlockNumber, ChainSpec, PruneModes, U256}; use tracing::info; /// Executor factory that would create the EVM with particular state provider. @@ -77,9 +77,15 @@ pub trait BlockExecutor { senders: Option>, ) -> Result<(), BlockExecutionError>; - /// Internal statistics of execution. - fn stats(&self) -> BlockExecutorStats; + /// Set prune modes. + fn set_prune_modes(&mut self, prune_modes: PruneModes); + + /// Set tip - highest known block number. + fn set_tip(&mut self, tip: BlockNumber); /// Return bundle state. This is output of the execution. fn take_output_state(&mut self) -> BundleState; + + /// Internal statistics of execution. + fn stats(&self) -> BlockExecutorStats; }