reth_consensus/
test_utils.rs

1use 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/// Consensus engine implementation for testing
7#[derive(Debug)]
8pub struct TestConsensus {
9    /// Flag whether the header validation should purposefully fail
10    fail_validation: AtomicBool,
11    /// Separate flag for setting whether `validate_body_against_header` should fail. It is needed
12    /// for testing networking logic for which the body failing this check is getting completely
13    /// rejected while more high-level failures are handled by the sync logic.
14    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    /// Get the failed validation flag.
28    pub fn fail_validation(&self) -> bool {
29        self.fail_validation.load(Ordering::SeqCst)
30    }
31
32    /// Update the validation flag.
33    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    /// Returns the body validation flag.
39    pub fn fail_body_against_header(&self) -> bool {
40        self.fail_body_against_header.load(Ordering::SeqCst)
41    }
42
43    /// Update the body validation flag.
44    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}