1use crate::stages::MERKLE_STAGE_DEFAULT_INCREMENTAL_THRESHOLD;
2use alloy_consensus::BlockHeader;
3use alloy_primitives::BlockNumber;
4use num_traits::Zero;
5use reth_config::config::ExecutionConfig;
6use reth_consensus::{ConsensusError, FullConsensus};
7use reth_db::{static_file::HeaderMask, tables};
8use reth_evm::{execute::Executor, metrics::ExecutorMetrics, ConfigureEvm};
9use reth_execution_types::Chain;
10use reth_exex::{ExExManagerHandle, ExExNotification, ExExNotificationSource};
11use reth_primitives_traits::{format_gas_throughput, BlockBody, NodePrimitives};
12use reth_provider::{
13 providers::{StaticFileProvider, StaticFileWriter},
14 BlockHashReader, BlockReader, DBProvider, EitherWriter, ExecutionOutcome, HeaderProvider,
15 LatestStateProviderRef, OriginalValuesKnown, ProviderError, StateWriter,
16 StaticFileProviderFactory, StatsReader, StorageSettingsCache, TransactionVariant,
17};
18use reth_revm::database::StateProviderDatabase;
19use reth_stages_api::{
20 BlockErrorKind, CheckpointBlockRange, EntitiesCheckpoint, ExecInput, ExecOutput,
21 ExecutionCheckpoint, ExecutionStageThresholds, Stage, StageCheckpoint, StageError, StageId,
22 UnwindInput, UnwindOutput,
23};
24use reth_static_file_types::StaticFileSegment;
25use std::{
26 cmp::Ordering,
27 ops::RangeInclusive,
28 sync::Arc,
29 task::{ready, Context, Poll},
30 time::{Duration, Instant},
31};
32use tracing::*;
33
34use super::missing_static_data_error;
35
36#[derive(Debug)]
65pub struct ExecutionStage<E>
66where
67 E: ConfigureEvm,
68{
69 evm_config: E,
71 consensus: Arc<dyn FullConsensus<E::Primitives, Error = ConsensusError>>,
73 thresholds: ExecutionStageThresholds,
75 external_clean_threshold: u64,
80 post_execute_commit_input: Option<Chain<E::Primitives>>,
84 post_unwind_commit_input: Option<Chain<E::Primitives>>,
88 exex_manager_handle: ExExManagerHandle<E::Primitives>,
90 metrics: ExecutorMetrics,
92}
93
94impl<E> ExecutionStage<E>
95where
96 E: ConfigureEvm,
97{
98 pub fn new(
100 evm_config: E,
101 consensus: Arc<dyn FullConsensus<E::Primitives, Error = ConsensusError>>,
102 thresholds: ExecutionStageThresholds,
103 external_clean_threshold: u64,
104 exex_manager_handle: ExExManagerHandle<E::Primitives>,
105 ) -> Self {
106 Self {
107 external_clean_threshold,
108 evm_config,
109 consensus,
110 thresholds,
111 post_execute_commit_input: None,
112 post_unwind_commit_input: None,
113 exex_manager_handle,
114 metrics: ExecutorMetrics::default(),
115 }
116 }
117
118 pub fn new_with_executor(
122 evm_config: E,
123 consensus: Arc<dyn FullConsensus<E::Primitives, Error = ConsensusError>>,
124 ) -> Self {
125 Self::new(
126 evm_config,
127 consensus,
128 ExecutionStageThresholds::default(),
129 MERKLE_STAGE_DEFAULT_INCREMENTAL_THRESHOLD,
130 ExExManagerHandle::empty(),
131 )
132 }
133
134 pub fn from_config(
136 evm_config: E,
137 consensus: Arc<dyn FullConsensus<E::Primitives, Error = ConsensusError>>,
138 config: ExecutionConfig,
139 external_clean_threshold: u64,
140 ) -> Self {
141 Self::new(
142 evm_config,
143 consensus,
144 config.into(),
145 external_clean_threshold,
146 ExExManagerHandle::empty(),
147 )
148 }
149
150 fn can_prune_changesets(
162 &self,
163 provider: impl StatsReader,
164 start_block: u64,
165 max_block: u64,
166 ) -> Result<bool, StageError> {
167 Ok(max_block - start_block > self.external_clean_threshold ||
170 provider.count_entries::<tables::AccountsTrie>()?.is_zero())
171 }
172
173 fn ensure_consistency<Provider>(
182 &self,
183 provider: &Provider,
184 checkpoint: u64,
185 unwind_to: Option<u64>,
186 ) -> Result<(), StageError>
187 where
188 Provider: StaticFileProviderFactory
189 + DBProvider
190 + BlockReader
191 + HeaderProvider
192 + StorageSettingsCache,
193 {
194 if EitherWriter::receipts_destination(provider).is_database() {
197 return Ok(())
198 }
199
200 let next_receipt_num =
202 provider.block_body_indices(checkpoint)?.map(|b| b.next_tx_num()).unwrap_or(0);
203
204 let static_file_provider = provider.static_file_provider();
205
206 let next_static_file_receipt_num = static_file_provider
208 .get_highest_static_file_tx(StaticFileSegment::Receipts)
209 .map(|num| num + 1)
210 .unwrap_or(0);
211
212 let static_file_block_num = static_file_provider
214 .get_highest_static_file_block(StaticFileSegment::Receipts)
215 .unwrap_or(0);
216
217 match static_file_block_num.cmp(&checkpoint) {
220 Ordering::Greater | Ordering::Equal => {
223 let mut static_file_producer =
224 static_file_provider.latest_writer(StaticFileSegment::Receipts)?;
225 static_file_producer.prune_receipts(
226 next_static_file_receipt_num.saturating_sub(next_receipt_num),
227 checkpoint,
228 )?;
229 static_file_producer.commit()?;
232 }
233 Ordering::Less => {
234 if let Some(unwind_to) = unwind_to &&
237 unwind_to <= static_file_block_num
238 {
239 return Ok(())
240 }
241
242 return Err(missing_static_data_error(
245 next_static_file_receipt_num.saturating_sub(1),
246 &static_file_provider,
247 provider,
248 StaticFileSegment::Receipts,
249 )?)
250 }
251 }
252
253 Ok(())
254 }
255}
256
257impl<E, Provider> Stage<Provider> for ExecutionStage<E>
258where
259 E: ConfigureEvm,
260 Provider: DBProvider
261 + BlockReader<
262 Block = <E::Primitives as NodePrimitives>::Block,
263 Header = <E::Primitives as NodePrimitives>::BlockHeader,
264 > + StaticFileProviderFactory<
265 Primitives: NodePrimitives<BlockHeader: reth_db_api::table::Value>,
266 > + StatsReader
267 + BlockHashReader
268 + StateWriter<Receipt = <E::Primitives as NodePrimitives>::Receipt>
269 + StorageSettingsCache,
270{
271 fn id(&self) -> StageId {
273 StageId::Execution
274 }
275
276 fn poll_execute_ready(
277 &mut self,
278 cx: &mut Context<'_>,
279 _: ExecInput,
280 ) -> Poll<Result<(), StageError>> {
281 ready!(self.exex_manager_handle.poll_ready(cx));
282
283 Poll::Ready(Ok(()))
284 }
285
286 fn execute(&mut self, provider: &Provider, input: ExecInput) -> Result<ExecOutput, StageError> {
288 if input.target_reached() {
289 return Ok(ExecOutput::done(input.checkpoint()))
290 }
291
292 let start_block = input.next_block();
293 let max_block = input.target();
294 let static_file_provider = provider.static_file_provider();
295
296 self.ensure_consistency(provider, input.checkpoint().block_number, None)?;
297
298 let db = StateProviderDatabase(LatestStateProviderRef::new(provider));
299 let mut executor = self.evm_config.batch_executor(db);
300
301 let mut stage_progress = start_block;
303 let mut stage_checkpoint = execution_checkpoint(
304 &static_file_provider,
305 start_block,
306 max_block,
307 input.checkpoint(),
308 )?;
309
310 let mut fetch_block_duration = Duration::default();
311 let mut execution_duration = Duration::default();
312
313 let mut last_block = start_block;
314 let mut last_execution_duration = Duration::default();
315 let mut last_cumulative_gas = 0;
316 let mut last_log_instant = Instant::now();
317 let log_duration = Duration::from_secs(10);
318
319 debug!(target: "sync::stages::execution", start = start_block, end = max_block, "Executing range");
320
321 let mut cumulative_gas = 0;
323 let batch_start = Instant::now();
324
325 let mut blocks = Vec::new();
326 let mut results = Vec::new();
327 for block_number in start_block..=max_block {
328 let fetch_block_start = Instant::now();
330
331 let block = provider
333 .recovered_block(block_number.into(), TransactionVariant::NoHash)?
334 .ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?;
335
336 fetch_block_duration += fetch_block_start.elapsed();
337
338 cumulative_gas += block.header().gas_used();
339
340 trace!(target: "sync::stages::execution", number = block_number, txs = block.body().transactions().len(), "Executing block");
342
343 let execute_start = Instant::now();
345
346 let result = self.metrics.metered_one(&block, |input| {
347 executor.execute_one(input).map_err(|error| StageError::Block {
348 block: Box::new(block.block_with_parent()),
349 error: BlockErrorKind::Execution(error),
350 })
351 })?;
352
353 if let Err(err) = self.consensus.validate_block_post_execution(&block, &result) {
354 return Err(StageError::Block {
355 block: Box::new(block.block_with_parent()),
356 error: BlockErrorKind::Validation(err),
357 })
358 }
359 results.push(result);
360
361 execution_duration += execute_start.elapsed();
362
363 if last_log_instant.elapsed() >= log_duration {
365 info!(
366 target: "sync::stages::execution",
367 start = last_block,
368 end = block_number,
369 throughput = format_gas_throughput(cumulative_gas - last_cumulative_gas, execution_duration - last_execution_duration),
370 "Executed block range"
371 );
372
373 last_block = block_number + 1;
374 last_execution_duration = execution_duration;
375 last_cumulative_gas = cumulative_gas;
376 last_log_instant = Instant::now();
377 }
378
379 stage_progress = block_number;
380 stage_checkpoint.progress.processed += block.header().gas_used();
381
382 if self.exex_manager_handle.has_exexs() {
384 blocks.push(block);
385 }
386
387 if self.thresholds.is_end_of_batch(
389 block_number - start_block,
390 executor.size_hint() as u64,
391 cumulative_gas,
392 batch_start.elapsed(),
393 ) {
394 break
395 }
396 }
397
398 let time = Instant::now();
400 let mut state = ExecutionOutcome::from_blocks(
401 start_block,
402 executor.into_state().take_bundle(),
403 results,
404 );
405 let write_preparation_duration = time.elapsed();
406
407 debug!(
409 target: "sync::stages::execution",
410 start = start_block,
411 end = stage_progress,
412 throughput = format_gas_throughput(cumulative_gas, execution_duration),
413 "Finished executing block range"
414 );
415
416 if !blocks.is_empty() {
421 let previous_input =
422 self.post_execute_commit_input.replace(Chain::new(blocks, state.clone(), None));
423
424 if previous_input.is_some() {
425 return Err(StageError::PostExecuteCommit(
428 "Previous post execute commit input wasn't processed",
429 ))
430 }
431 }
432
433 let time = Instant::now();
434
435 if self.can_prune_changesets(provider, start_block, max_block)? {
436 let prune_modes = provider.prune_modes_ref();
437
438 for block_number in start_block..=max_block {
440 let Some(reverts) =
441 state.bundle.reverts.get_mut((block_number - start_block) as usize)
442 else {
443 break
444 };
445
446 if prune_modes
449 .account_history
450 .is_some_and(|m| m.should_prune(block_number, max_block)) &&
451 prune_modes
452 .storage_history
453 .is_some_and(|m| m.should_prune(block_number, max_block))
454 {
455 reverts.clear();
456 }
457 }
458 }
459
460 provider.write_state(&state, OriginalValuesKnown::Yes)?;
462
463 let db_write_duration = time.elapsed();
464 debug!(
465 target: "sync::stages::execution",
466 block_fetch = ?fetch_block_duration,
467 execution = ?execution_duration,
468 write_preparation = ?write_preparation_duration,
469 write = ?db_write_duration,
470 "Execution time"
471 );
472
473 let done = stage_progress == max_block;
474 Ok(ExecOutput {
475 checkpoint: StageCheckpoint::new(stage_progress)
476 .with_execution_stage_checkpoint(stage_checkpoint),
477 done,
478 })
479 }
480
481 fn post_execute_commit(&mut self) -> Result<(), StageError> {
482 let Some(chain) = self.post_execute_commit_input.take() else { return Ok(()) };
483
484 let _ = self.exex_manager_handle.send(
487 ExExNotificationSource::Pipeline,
488 ExExNotification::ChainCommitted { new: Arc::new(chain) },
489 );
490
491 Ok(())
492 }
493
494 fn unwind(
496 &mut self,
497 provider: &Provider,
498 input: UnwindInput,
499 ) -> Result<UnwindOutput, StageError> {
500 let (range, unwind_to, _) =
501 input.unwind_block_range_with_threshold(self.thresholds.max_blocks.unwrap_or(u64::MAX));
502 if range.is_empty() {
503 return Ok(UnwindOutput {
504 checkpoint: input.checkpoint.with_block_number(input.unwind_to),
505 })
506 }
507
508 self.ensure_consistency(provider, input.checkpoint.block_number, Some(unwind_to))?;
509
510 let bundle_state_with_receipts = provider.take_state_above(unwind_to)?;
514
515 if self.exex_manager_handle.has_exexs() {
517 let blocks = provider.recovered_block_range(range.clone())?;
519 let previous_input = self.post_unwind_commit_input.replace(Chain::new(
520 blocks,
521 bundle_state_with_receipts,
522 None,
523 ));
524
525 debug_assert!(
526 previous_input.is_none(),
527 "Previous post unwind commit input wasn't processed"
528 );
529 if let Some(previous_input) = previous_input {
530 tracing::debug!(target: "sync::stages::execution", ?previous_input, "Previous post unwind commit input wasn't processed");
531 }
532 }
533
534 let mut stage_checkpoint = input.checkpoint.execution_stage_checkpoint();
536 if let Some(stage_checkpoint) = stage_checkpoint.as_mut() {
537 for block_number in range {
538 stage_checkpoint.progress.processed -= provider
539 .header_by_number(block_number)?
540 .ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?
541 .gas_used();
542 }
543 }
544 let checkpoint = if let Some(stage_checkpoint) = stage_checkpoint {
545 StageCheckpoint::new(unwind_to).with_execution_stage_checkpoint(stage_checkpoint)
546 } else {
547 StageCheckpoint::new(unwind_to)
548 };
549
550 Ok(UnwindOutput { checkpoint })
551 }
552
553 fn post_unwind_commit(&mut self) -> Result<(), StageError> {
554 let Some(chain) = self.post_unwind_commit_input.take() else { return Ok(()) };
555
556 let _ = self.exex_manager_handle.send(
559 ExExNotificationSource::Pipeline,
560 ExExNotification::ChainReverted { old: Arc::new(chain) },
561 );
562
563 Ok(())
564 }
565}
566
567fn execution_checkpoint<N>(
568 provider: &StaticFileProvider<N>,
569 start_block: BlockNumber,
570 max_block: BlockNumber,
571 checkpoint: StageCheckpoint,
572) -> Result<ExecutionCheckpoint, ProviderError>
573where
574 N: NodePrimitives<BlockHeader: reth_db_api::table::Value>,
575{
576 Ok(match checkpoint.execution_stage_checkpoint() {
577 Some(stage_checkpoint @ ExecutionCheckpoint { block_range, .. })
580 if block_range == CheckpointBlockRange::from(start_block..=max_block) =>
581 {
582 stage_checkpoint
583 }
584 Some(ExecutionCheckpoint {
587 block_range: CheckpointBlockRange { to, .. },
588 progress: EntitiesCheckpoint { processed, total },
589 }) if to == start_block - 1 => ExecutionCheckpoint {
590 block_range: CheckpointBlockRange { from: start_block, to: max_block },
591 progress: EntitiesCheckpoint {
592 processed,
593 total: total + calculate_gas_used_from_headers(provider, start_block..=max_block)?,
594 },
595 },
596 Some(ExecutionCheckpoint { block_range: CheckpointBlockRange { to, .. }, progress })
599 if to == max_block =>
600 {
601 ExecutionCheckpoint {
602 block_range: CheckpointBlockRange { from: start_block, to: max_block },
603 progress,
604 }
605 }
606 Some(ExecutionCheckpoint { progress: EntitiesCheckpoint { processed, .. }, .. }) => {
609 let after_checkpoint_block_number =
610 calculate_gas_used_from_headers(provider, checkpoint.block_number + 1..=max_block)?;
611
612 ExecutionCheckpoint {
613 block_range: CheckpointBlockRange { from: start_block, to: max_block },
614 progress: EntitiesCheckpoint {
615 processed,
616 total: processed + after_checkpoint_block_number,
617 },
618 }
619 }
620 _ => {
623 let processed = calculate_gas_used_from_headers(provider, 0..=start_block - 1)?;
624
625 ExecutionCheckpoint {
626 block_range: CheckpointBlockRange { from: start_block, to: max_block },
627 progress: EntitiesCheckpoint {
628 processed,
629 total: processed +
630 calculate_gas_used_from_headers(provider, start_block..=max_block)?,
631 },
632 }
633 }
634 })
635}
636
637pub fn calculate_gas_used_from_headers<N>(
639 provider: &StaticFileProvider<N>,
640 range: RangeInclusive<BlockNumber>,
641) -> Result<u64, ProviderError>
642where
643 N: NodePrimitives<BlockHeader: reth_db_api::table::Value>,
644{
645 debug!(target: "sync::stages::execution", ?range, "Calculating gas used from headers");
646
647 let mut gas_total = 0;
648
649 let start = Instant::now();
650
651 for entry in provider.fetch_range_iter(
652 StaticFileSegment::Headers,
653 *range.start()..*range.end() + 1,
654 |cursor, number| cursor.get_one::<HeaderMask<N::BlockHeader>>(number.into()),
655 )? {
656 if let Some(entry) = entry? {
657 gas_total += entry.gas_used();
658 }
659 }
660
661 let duration = start.elapsed();
662 debug!(target: "sync::stages::execution", ?range, ?duration, "Finished calculating gas used from headers");
663
664 Ok(gas_total)
665}
666
667#[cfg(test)]
668mod tests {
669 use super::*;
670 use crate::{stages::MERKLE_STAGE_DEFAULT_REBUILD_THRESHOLD, test_utils::TestStageDB};
671 use alloy_primitives::{address, hex_literal::hex, keccak256, Address, B256, U256};
672 use alloy_rlp::Decodable;
673 use assert_matches::assert_matches;
674 use reth_chainspec::ChainSpecBuilder;
675 use reth_db_api::{
676 models::{metadata::StorageSettings, AccountBeforeTx},
677 transaction::{DbTx, DbTxMut},
678 };
679 use reth_ethereum_consensus::EthBeaconConsensus;
680 use reth_ethereum_primitives::Block;
681 use reth_evm_ethereum::EthEvmConfig;
682 use reth_primitives_traits::{Account, Bytecode, SealedBlock, StorageEntry};
683 use reth_provider::{
684 test_utils::create_test_provider_factory, AccountReader, BlockWriter,
685 DatabaseProviderFactory, ReceiptProvider, StaticFileProviderFactory,
686 };
687 use reth_prune::PruneModes;
688 use reth_prune_types::{PruneMode, ReceiptsLogPruneConfig};
689 use reth_stages_api::StageUnitCheckpoint;
690 use reth_testing_utils::generators;
691 use std::collections::BTreeMap;
692
693 fn stage() -> ExecutionStage<EthEvmConfig> {
694 let evm_config =
695 EthEvmConfig::new(Arc::new(ChainSpecBuilder::mainnet().berlin_activated().build()));
696 let consensus = Arc::new(EthBeaconConsensus::new(Arc::new(
697 ChainSpecBuilder::mainnet().berlin_activated().build(),
698 )));
699 ExecutionStage::new(
700 evm_config,
701 consensus,
702 ExecutionStageThresholds {
703 max_blocks: Some(100),
704 max_changes: None,
705 max_cumulative_gas: None,
706 max_duration: None,
707 },
708 MERKLE_STAGE_DEFAULT_REBUILD_THRESHOLD,
709 ExExManagerHandle::empty(),
710 )
711 }
712
713 #[test]
714 fn execution_checkpoint_matches() {
715 let factory = create_test_provider_factory();
716
717 let previous_stage_checkpoint = ExecutionCheckpoint {
718 block_range: CheckpointBlockRange { from: 0, to: 0 },
719 progress: EntitiesCheckpoint { processed: 1, total: 2 },
720 };
721 let previous_checkpoint = StageCheckpoint {
722 block_number: 0,
723 stage_checkpoint: Some(StageUnitCheckpoint::Execution(previous_stage_checkpoint)),
724 };
725
726 let stage_checkpoint = execution_checkpoint(
727 &factory.static_file_provider(),
728 previous_stage_checkpoint.block_range.from,
729 previous_stage_checkpoint.block_range.to,
730 previous_checkpoint,
731 );
732
733 assert!(
734 matches!(stage_checkpoint, Ok(checkpoint) if checkpoint == previous_stage_checkpoint)
735 );
736 }
737
738 #[test]
739 fn execution_checkpoint_precedes() {
740 let factory = create_test_provider_factory();
741 let provider = factory.provider_rw().unwrap();
742
743 let mut genesis_rlp = hex!("f901faf901f5a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa045571b40ae66ca7480791bbb2887286e4e4c4b1b298b191c889d6959023a32eda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
744 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
745 let mut block_rlp = hex!("f90262f901f9a075c371ba45999d87f4542326910a11af515897aebce5265d3f6acd1f1161f82fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa098f2dcd87c8ae4083e7017a05456c14eea4b1db2032126e27b3b1563d57d7cc0a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba03f4e5c2ec5b2170b711d97ee755c160457bb58d8daa338e835ec02ae6860bbabb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8798203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0").as_slice();
746 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
747 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
748 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
749 provider
750 .static_file_provider()
751 .latest_writer(StaticFileSegment::Headers)
752 .unwrap()
753 .commit()
754 .unwrap();
755 provider.commit().unwrap();
756
757 let previous_stage_checkpoint = ExecutionCheckpoint {
758 block_range: CheckpointBlockRange { from: 0, to: 0 },
759 progress: EntitiesCheckpoint { processed: 1, total: 1 },
760 };
761 let previous_checkpoint = StageCheckpoint {
762 block_number: 1,
763 stage_checkpoint: Some(StageUnitCheckpoint::Execution(previous_stage_checkpoint)),
764 };
765
766 let stage_checkpoint =
767 execution_checkpoint(&factory.static_file_provider(), 1, 1, previous_checkpoint);
768
769 assert_matches!(stage_checkpoint, Ok(ExecutionCheckpoint {
770 block_range: CheckpointBlockRange { from: 1, to: 1 },
771 progress: EntitiesCheckpoint {
772 processed,
773 total
774 }
775 }) if processed == previous_stage_checkpoint.progress.processed &&
776 total == previous_stage_checkpoint.progress.total + block.gas_used);
777 }
778
779 #[test]
780 fn execution_checkpoint_recalculate_full_previous_some() {
781 let factory = create_test_provider_factory();
782 let provider = factory.provider_rw().unwrap();
783
784 let mut genesis_rlp = hex!("f901faf901f5a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa045571b40ae66ca7480791bbb2887286e4e4c4b1b298b191c889d6959023a32eda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
785 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
786 let mut block_rlp = hex!("f90262f901f9a075c371ba45999d87f4542326910a11af515897aebce5265d3f6acd1f1161f82fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa098f2dcd87c8ae4083e7017a05456c14eea4b1db2032126e27b3b1563d57d7cc0a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba03f4e5c2ec5b2170b711d97ee755c160457bb58d8daa338e835ec02ae6860bbabb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8798203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0").as_slice();
787 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
788 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
789 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
790 provider
791 .static_file_provider()
792 .latest_writer(StaticFileSegment::Headers)
793 .unwrap()
794 .commit()
795 .unwrap();
796 provider.commit().unwrap();
797
798 let previous_stage_checkpoint = ExecutionCheckpoint {
799 block_range: CheckpointBlockRange { from: 0, to: 0 },
800 progress: EntitiesCheckpoint { processed: 1, total: 1 },
801 };
802 let previous_checkpoint = StageCheckpoint {
803 block_number: 1,
804 stage_checkpoint: Some(StageUnitCheckpoint::Execution(previous_stage_checkpoint)),
805 };
806
807 let stage_checkpoint =
808 execution_checkpoint(&factory.static_file_provider(), 1, 1, previous_checkpoint);
809
810 assert_matches!(stage_checkpoint, Ok(ExecutionCheckpoint {
811 block_range: CheckpointBlockRange { from: 1, to: 1 },
812 progress: EntitiesCheckpoint {
813 processed,
814 total
815 }
816 }) if processed == previous_stage_checkpoint.progress.processed &&
817 total == previous_stage_checkpoint.progress.total + block.gas_used());
818 }
819
820 #[test]
821 fn execution_checkpoint_recalculate_full_previous_none() {
822 let factory = create_test_provider_factory();
823 let provider = factory.provider_rw().unwrap();
824
825 let mut genesis_rlp = hex!("f901faf901f5a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa045571b40ae66ca7480791bbb2887286e4e4c4b1b298b191c889d6959023a32eda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
826 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
827 let mut block_rlp = hex!("f90262f901f9a075c371ba45999d87f4542326910a11af515897aebce5265d3f6acd1f1161f82fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa098f2dcd87c8ae4083e7017a05456c14eea4b1db2032126e27b3b1563d57d7cc0a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba03f4e5c2ec5b2170b711d97ee755c160457bb58d8daa338e835ec02ae6860bbabb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8798203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0").as_slice();
828 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
829 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
830 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
831 provider
832 .static_file_provider()
833 .latest_writer(StaticFileSegment::Headers)
834 .unwrap()
835 .commit()
836 .unwrap();
837 provider.commit().unwrap();
838
839 let previous_checkpoint = StageCheckpoint { block_number: 1, stage_checkpoint: None };
840
841 let stage_checkpoint =
842 execution_checkpoint(&factory.static_file_provider(), 1, 1, previous_checkpoint);
843
844 assert_matches!(stage_checkpoint, Ok(ExecutionCheckpoint {
845 block_range: CheckpointBlockRange { from: 1, to: 1 },
846 progress: EntitiesCheckpoint {
847 processed: 0,
848 total
849 }
850 }) if total == block.gas_used);
851 }
852
853 #[tokio::test]
854 async fn sanity_execution_of_block() {
855 let factory = create_test_provider_factory();
856 let provider = factory.provider_rw().unwrap();
857 let input = ExecInput { target: Some(1), checkpoint: None };
858 let mut genesis_rlp = hex!("f901faf901f5a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa045571b40ae66ca7480791bbb2887286e4e4c4b1b298b191c889d6959023a32eda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
859 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
860 let mut block_rlp = hex!("f90262f901f9a075c371ba45999d87f4542326910a11af515897aebce5265d3f6acd1f1161f82fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa098f2dcd87c8ae4083e7017a05456c14eea4b1db2032126e27b3b1563d57d7cc0a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba03f4e5c2ec5b2170b711d97ee755c160457bb58d8daa338e835ec02ae6860bbabb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8798203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0").as_slice();
861 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
862 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
863 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
864 provider
865 .static_file_provider()
866 .latest_writer(StaticFileSegment::Headers)
867 .unwrap()
868 .commit()
869 .unwrap();
870 {
871 let static_file_provider = provider.static_file_provider();
872 let mut receipts_writer =
873 static_file_provider.latest_writer(StaticFileSegment::Receipts).unwrap();
874 receipts_writer.increment_block(0).unwrap();
875 receipts_writer.commit().unwrap();
876 }
877 provider.commit().unwrap();
878
879 let provider = factory.provider_rw().unwrap();
881
882 let db_tx = provider.tx_ref();
883 let acc1 = address!("0x1000000000000000000000000000000000000000");
884 let acc2 = address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
885 let code = hex!("5a465a905090036002900360015500");
886 let balance = U256::from(0x3635c9adc5dea00000u128);
887 let code_hash = keccak256(code);
888 db_tx
889 .put::<tables::PlainAccountState>(
890 acc1,
891 Account { nonce: 0, balance: U256::ZERO, bytecode_hash: Some(code_hash) },
892 )
893 .unwrap();
894 db_tx
895 .put::<tables::PlainAccountState>(
896 acc2,
897 Account { nonce: 0, balance, bytecode_hash: None },
898 )
899 .unwrap();
900 db_tx.put::<tables::Bytecodes>(code_hash, Bytecode::new_raw(code.to_vec().into())).unwrap();
901 provider.commit().unwrap();
902
903 let modes = [None, Some(PruneModes::default())];
908 let random_filter = ReceiptsLogPruneConfig(BTreeMap::from([(
909 Address::random(),
910 PruneMode::Distance(100000),
911 )]));
912
913 for mut mode in modes {
915 let mut provider = factory.database_provider_rw().unwrap();
916
917 if let Some(mode) = &mut mode {
918 mode.receipts_log_filter = random_filter.clone();
920 }
921
922 let mut execution_stage = stage();
923 provider.set_prune_modes(mode.clone().unwrap_or_default());
924
925 let output = execution_stage.execute(&provider, input).unwrap();
926 provider.commit().unwrap();
927
928 assert_matches!(output, ExecOutput {
929 checkpoint: StageCheckpoint {
930 block_number: 1,
931 stage_checkpoint: Some(StageUnitCheckpoint::Execution(ExecutionCheckpoint {
932 block_range: CheckpointBlockRange {
933 from: 1,
934 to: 1,
935 },
936 progress: EntitiesCheckpoint {
937 processed,
938 total
939 }
940 }))
941 },
942 done: true
943 } if processed == total && total == block.gas_used);
944
945 let provider = factory.provider().unwrap();
946
947 let account1 = address!("0x1000000000000000000000000000000000000000");
949 let account1_info =
950 Account { balance: U256::ZERO, nonce: 0x00, bytecode_hash: Some(code_hash) };
951 let account2 = address!("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba");
952 let account2_info = Account {
953 balance: U256::from(0x1bc16d674ece94bau128),
954 nonce: 0x00,
955 bytecode_hash: None,
956 };
957 let account3 = address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
958 let account3_info = Account {
959 balance: U256::from(0x3635c9adc5de996b46u128),
960 nonce: 0x01,
961 bytecode_hash: None,
962 };
963
964 assert!(
966 matches!(provider.basic_account(&account1), Ok(Some(acc)) if acc == account1_info)
967 );
968 assert!(
969 matches!(provider.basic_account(&account2), Ok(Some(acc)) if acc == account2_info)
970 );
971 assert!(
972 matches!(provider.basic_account(&account3), Ok(Some(acc)) if acc == account3_info)
973 );
974 assert!(matches!(
977 provider.tx_ref().get::<tables::PlainStorageState>(account1),
978 Ok(Some(entry)) if entry.key == B256::with_last_byte(1) && entry.value == U256::from(2)
979 ));
980
981 let mut provider = factory.database_provider_rw().unwrap();
982 let mut stage = stage();
983 provider.set_prune_modes(mode.unwrap_or_default());
984
985 let _result = stage
986 .unwind(
987 &provider,
988 UnwindInput { checkpoint: output.checkpoint, unwind_to: 0, bad_block: None },
989 )
990 .unwrap();
991 provider.commit().unwrap();
992 }
993 }
994
995 #[tokio::test]
996 async fn sanity_execute_unwind() {
997 let factory = create_test_provider_factory();
998 let provider = factory.provider_rw().unwrap();
999 let input = ExecInput { target: Some(1), checkpoint: None };
1000 let mut genesis_rlp = hex!("f901faf901f5a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa045571b40ae66ca7480791bbb2887286e4e4c4b1b298b191c889d6959023a32eda056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000808502540be400808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
1001 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
1002 let mut block_rlp = hex!("f90262f901f9a075c371ba45999d87f4542326910a11af515897aebce5265d3f6acd1f1161f82fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa098f2dcd87c8ae4083e7017a05456c14eea4b1db2032126e27b3b1563d57d7cc0a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba03f4e5c2ec5b2170b711d97ee755c160457bb58d8daa338e835ec02ae6860bbabb901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8798203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0").as_slice();
1003 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
1004 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
1005 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
1006 provider
1007 .static_file_provider()
1008 .latest_writer(StaticFileSegment::Headers)
1009 .unwrap()
1010 .commit()
1011 .unwrap();
1012 {
1013 let static_file_provider = provider.static_file_provider();
1014 let mut receipts_writer =
1015 static_file_provider.latest_writer(StaticFileSegment::Receipts).unwrap();
1016 receipts_writer.increment_block(0).unwrap();
1017 receipts_writer.commit().unwrap();
1018 }
1019 provider.commit().unwrap();
1020
1021 let code = hex!("5a465a905090036002900360015500");
1023 let balance = U256::from(0x3635c9adc5dea00000u128);
1024 let code_hash = keccak256(code);
1025 let provider = factory.provider_rw().unwrap();
1027
1028 let db_tx = provider.tx_ref();
1029 let acc1 = address!("0x1000000000000000000000000000000000000000");
1030 let acc1_info = Account { nonce: 0, balance: U256::ZERO, bytecode_hash: Some(code_hash) };
1031 let acc2 = address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
1032 let acc2_info = Account { nonce: 0, balance, bytecode_hash: None };
1033
1034 db_tx.put::<tables::PlainAccountState>(acc1, acc1_info).unwrap();
1035 db_tx.put::<tables::PlainAccountState>(acc2, acc2_info).unwrap();
1036 db_tx.put::<tables::Bytecodes>(code_hash, Bytecode::new_raw(code.to_vec().into())).unwrap();
1037 provider.commit().unwrap();
1038
1039 let mut provider = factory.database_provider_rw().unwrap();
1041
1042 let modes = [None, Some(PruneModes::default())];
1045 let random_filter = ReceiptsLogPruneConfig(BTreeMap::from([(
1046 Address::random(),
1047 PruneMode::Before(100000),
1048 )]));
1049
1050 for mut mode in modes {
1052 if let Some(mode) = &mut mode {
1053 mode.receipts_log_filter = random_filter.clone();
1055 }
1056
1057 let mut execution_stage = stage();
1059 provider.set_prune_modes(mode.clone().unwrap_or_default());
1060
1061 let result = execution_stage.execute(&provider, input).unwrap();
1062 provider.commit().unwrap();
1063
1064 provider = factory.database_provider_rw().unwrap();
1066 let mut stage = stage();
1067 provider.set_prune_modes(mode.clone().unwrap_or_default());
1068
1069 let result = stage
1070 .unwind(
1071 &provider,
1072 UnwindInput { checkpoint: result.checkpoint, unwind_to: 0, bad_block: None },
1073 )
1074 .unwrap();
1075
1076 provider.static_file_provider().commit().unwrap();
1077
1078 assert_matches!(result, UnwindOutput {
1079 checkpoint: StageCheckpoint {
1080 block_number: 0,
1081 stage_checkpoint: Some(StageUnitCheckpoint::Execution(ExecutionCheckpoint {
1082 block_range: CheckpointBlockRange {
1083 from: 1,
1084 to: 1,
1085 },
1086 progress: EntitiesCheckpoint {
1087 processed: 0,
1088 total
1089 }
1090 }))
1091 }
1092 } if total == block.gas_used);
1093
1094 assert!(matches!(provider.basic_account(&acc1), Ok(Some(acc)) if acc == acc1_info));
1096 assert!(matches!(provider.basic_account(&acc2), Ok(Some(acc)) if acc == acc2_info));
1097
1098 let miner_acc = address!("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba");
1099 assert!(matches!(provider.basic_account(&miner_acc), Ok(None)));
1100
1101 assert!(matches!(provider.receipt(0), Ok(None)));
1102 }
1103 }
1104
1105 #[tokio::test]
1106 async fn test_selfdestruct() {
1107 let test_db = TestStageDB::default();
1108 let provider = test_db.factory.database_provider_rw().unwrap();
1109 let input = ExecInput { target: Some(1), checkpoint: None };
1110 let mut genesis_rlp = hex!("f901f8f901f3a00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa0c9ceb8372c88cb461724d8d3d87e8b933f6fc5f679d4841800e662f4428ffd0da056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000080830f4240808000a00000000000000000000000000000000000000000000000000000000000000000880000000000000000c0c0").as_slice();
1111 let genesis = SealedBlock::<Block>::decode(&mut genesis_rlp).unwrap();
1112 let mut block_rlp = hex!("f9025ff901f7a0c86e8cc0310ae7c531c758678ddbfd16fc51c8cef8cec650b032de9869e8b94fa01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa050554882fbbda2c2fd93fdc466db9946ea262a67f7a76cc169e714f105ab583da00967f09ef1dfed20c0eacfaa94d5cd4002eda3242ac47eae68972d07b106d192a0e3c8b47fbfc94667ef4cceb17e5cc21e3b1eebd442cebb27f07562b33836290db90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001830f42408238108203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f862f860800a83061a8094095e7baea6a6c7c4c2dfeb977efac326af552d8780801ba072ed817487b84ba367d15d2f039b5fc5f087d0a8882fbdf73e8cb49357e1ce30a0403d800545b8fc544f92ce8124e2255f8c3c6af93f28243a120585d4c4c6a2a3c0").as_slice();
1113 let block = SealedBlock::<Block>::decode(&mut block_rlp).unwrap();
1114 provider.insert_block(genesis.try_recover().unwrap()).unwrap();
1115 provider.insert_block(block.clone().try_recover().unwrap()).unwrap();
1116 provider
1117 .static_file_provider()
1118 .latest_writer(StaticFileSegment::Headers)
1119 .unwrap()
1120 .commit()
1121 .unwrap();
1122 {
1123 let static_file_provider = provider.static_file_provider();
1124 let mut receipts_writer =
1125 static_file_provider.latest_writer(StaticFileSegment::Receipts).unwrap();
1126 receipts_writer.increment_block(0).unwrap();
1127 receipts_writer.commit().unwrap();
1128 }
1129 provider.commit().unwrap();
1130
1131 let caller_address = address!("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
1133 let destroyed_address = address!("0x095e7baea6a6c7c4c2dfeb977efac326af552d87");
1134 let beneficiary_address = address!("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba");
1135
1136 let code = hex!("73095e7baea6a6c7c4c2dfeb977efac326af552d8731ff00");
1137 let balance = U256::from(0x0de0b6b3a7640000u64);
1138 let code_hash = keccak256(code);
1139
1140 let caller_info = Account { nonce: 0, balance, bytecode_hash: None };
1142 let destroyed_info =
1143 Account { nonce: 0, balance: U256::ZERO, bytecode_hash: Some(code_hash) };
1144
1145 let provider = test_db.factory.provider_rw().unwrap();
1147 provider.tx_ref().put::<tables::PlainAccountState>(caller_address, caller_info).unwrap();
1148 provider
1149 .tx_ref()
1150 .put::<tables::PlainAccountState>(destroyed_address, destroyed_info)
1151 .unwrap();
1152 provider
1153 .tx_ref()
1154 .put::<tables::Bytecodes>(code_hash, Bytecode::new_raw(code.to_vec().into()))
1155 .unwrap();
1156 provider
1158 .tx_ref()
1159 .put::<tables::PlainStorageState>(
1160 destroyed_address,
1161 StorageEntry { key: B256::ZERO, value: U256::ZERO },
1162 )
1163 .unwrap();
1164 provider
1165 .tx_ref()
1166 .put::<tables::PlainStorageState>(
1167 destroyed_address,
1168 StorageEntry { key: B256::with_last_byte(1), value: U256::from(1u64) },
1169 )
1170 .unwrap();
1171
1172 provider.commit().unwrap();
1173
1174 let provider = test_db.factory.database_provider_rw().unwrap();
1176 let mut execution_stage = stage();
1177 let _ = execution_stage.execute(&provider, input).unwrap();
1178 provider.commit().unwrap();
1179
1180 let provider = test_db.factory.database_provider_rw().unwrap();
1182 assert!(matches!(provider.basic_account(&destroyed_address), Ok(None)));
1183
1184 assert!(matches!(
1185 provider.tx_ref().get::<tables::PlainStorageState>(destroyed_address),
1186 Ok(None)
1187 ));
1188 drop(provider);
1190 let plain_accounts = test_db.table::<tables::PlainAccountState>().unwrap();
1191 let plain_storage = test_db.table::<tables::PlainStorageState>().unwrap();
1192
1193 assert_eq!(
1194 plain_accounts,
1195 vec![
1196 (
1197 beneficiary_address,
1198 Account {
1199 nonce: 0,
1200 balance: U256::from(0x1bc16d674eca30a0u64),
1201 bytecode_hash: None
1202 }
1203 ),
1204 (
1205 caller_address,
1206 Account {
1207 nonce: 1,
1208 balance: U256::from(0xde0b6b3a761cf60u64),
1209 bytecode_hash: None
1210 }
1211 )
1212 ]
1213 );
1214 assert!(plain_storage.is_empty());
1215
1216 let account_changesets = test_db.table::<tables::AccountChangeSets>().unwrap();
1217 let storage_changesets = test_db.table::<tables::StorageChangeSets>().unwrap();
1218
1219 assert_eq!(
1220 account_changesets,
1221 vec![
1222 (
1223 block.number,
1224 AccountBeforeTx { address: destroyed_address, info: Some(destroyed_info) },
1225 ),
1226 (block.number, AccountBeforeTx { address: beneficiary_address, info: None }),
1227 (
1228 block.number,
1229 AccountBeforeTx { address: caller_address, info: Some(caller_info) }
1230 ),
1231 ]
1232 );
1233
1234 assert_eq!(
1235 storage_changesets,
1236 vec![
1237 (
1238 (block.number, destroyed_address).into(),
1239 StorageEntry { key: B256::ZERO, value: U256::ZERO }
1240 ),
1241 (
1242 (block.number, destroyed_address).into(),
1243 StorageEntry { key: B256::with_last_byte(1), value: U256::from(1u64) }
1244 )
1245 ]
1246 );
1247 }
1248
1249 #[test]
1250 fn test_ensure_consistency_with_skipped_receipts() {
1251 let factory = create_test_provider_factory();
1256 factory.set_storage_settings_cache(
1257 StorageSettings::legacy().with_receipts_in_static_files(true),
1258 );
1259
1260 let provider_rw = factory.database_provider_rw().unwrap();
1262 let mut rng = generators::rng();
1263 let genesis = generators::random_block(&mut rng, 0, Default::default());
1264 provider_rw.insert_block(genesis.try_recover().unwrap()).expect("failed to insert genesis");
1265 let block = generators::random_block(
1266 &mut rng,
1267 1,
1268 generators::BlockParams { tx_count: Some(2), ..Default::default() },
1269 );
1270 provider_rw.insert_block(block.try_recover().unwrap()).expect("failed to insert block");
1271
1272 let static_file_provider = provider_rw.static_file_provider();
1273 static_file_provider.latest_writer(StaticFileSegment::Headers).unwrap().commit().unwrap();
1274
1275 {
1278 let mut receipts_writer =
1279 static_file_provider.latest_writer(StaticFileSegment::Receipts).unwrap();
1280 receipts_writer.increment_block(0).unwrap();
1281 receipts_writer.increment_block(1).unwrap();
1282 receipts_writer.commit().unwrap();
1283 } provider_rw.commit().expect("failed to commit");
1286
1287 assert_eq!(
1289 factory
1290 .static_file_provider()
1291 .get_highest_static_file_block(StaticFileSegment::Receipts),
1292 Some(1)
1293 );
1294 assert_eq!(
1295 factory.static_file_provider().get_highest_static_file_tx(StaticFileSegment::Receipts),
1296 None
1297 );
1298
1299 let stage = stage();
1301
1302 let provider = factory.provider().unwrap();
1306 stage
1307 .ensure_consistency(&provider, 1, None)
1308 .expect("ensure_consistency should succeed when receipts are intentionally skipped");
1309 }
1310}