reth_consensus/
test_utils.rs1use crate::{Consensus, ConsensusError, FullConsensus, HeaderValidator};
2use core::sync::atomic::{AtomicBool, Ordering};
3use reth_execution_types::BlockExecutionResult;
4use reth_primitives_traits::{Block, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader};
5
6#[derive(Debug)]
8pub struct TestConsensus {
9 fail_validation: AtomicBool,
11 fail_body_against_header: AtomicBool,
15}
16
17impl Default for TestConsensus {
18 fn default() -> Self {
19 Self {
20 fail_validation: AtomicBool::new(false),
21 fail_body_against_header: AtomicBool::new(false),
22 }
23 }
24}
25
26impl TestConsensus {
27 pub fn fail_validation(&self) -> bool {
29 self.fail_validation.load(Ordering::SeqCst)
30 }
31
32 pub fn set_fail_validation(&self, val: bool) {
34 self.fail_validation.store(val, Ordering::SeqCst);
35 self.fail_body_against_header.store(val, Ordering::SeqCst);
36 }
37
38 pub fn fail_body_against_header(&self) -> bool {
40 self.fail_body_against_header.load(Ordering::SeqCst)
41 }
42
43 pub fn set_fail_body_against_header(&self, val: bool) {
45 self.fail_body_against_header.store(val, Ordering::SeqCst);
46 }
47}
48
49impl<N: NodePrimitives> FullConsensus<N> for TestConsensus {
50 fn validate_block_post_execution(
51 &self,
52 _block: &RecoveredBlock<N::Block>,
53 _result: &BlockExecutionResult<N::Receipt>,
54 ) -> Result<(), ConsensusError> {
55 if self.fail_validation() {
56 Err(ConsensusError::BaseFeeMissing)
57 } else {
58 Ok(())
59 }
60 }
61}
62
63impl<B: Block> Consensus<B> for TestConsensus {
64 type Error = ConsensusError;
65
66 fn validate_body_against_header(
67 &self,
68 _body: &B::Body,
69 _header: &SealedHeader<B::Header>,
70 ) -> Result<(), Self::Error> {
71 if self.fail_body_against_header() {
72 Err(ConsensusError::BaseFeeMissing)
73 } else {
74 Ok(())
75 }
76 }
77
78 fn validate_block_pre_execution(&self, _block: &SealedBlock<B>) -> Result<(), Self::Error> {
79 if self.fail_validation() {
80 Err(ConsensusError::BaseFeeMissing)
81 } else {
82 Ok(())
83 }
84 }
85}
86
87impl<H> HeaderValidator<H> for TestConsensus {
88 fn validate_header(&self, _header: &SealedHeader<H>) -> Result<(), ConsensusError> {
89 if self.fail_validation() {
90 Err(ConsensusError::BaseFeeMissing)
91 } else {
92 Ok(())
93 }
94 }
95
96 fn validate_header_against_parent(
97 &self,
98 _header: &SealedHeader<H>,
99 _parent: &SealedHeader<H>,
100 ) -> Result<(), ConsensusError> {
101 if self.fail_validation() {
102 Err(ConsensusError::BaseFeeMissing)
103 } else {
104 Ok(())
105 }
106 }
107}