• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari / 24709704177

21 Apr 2026 07:25AM UTC coverage: 60.325% (-0.7%) from 61.03%
24709704177

push

github

web-flow
fix: better sync (#7774)

Description
---
Makes sure we keep the sync peer initial state, and listens for a
termination signal for a closed connection.

24 of 36 new or added lines in 4 files covered. (66.67%)

896 existing lines in 29 files now uncovered.

69979 of 116003 relevant lines covered (60.33%)

224152.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.16
/base_layer/core/src/validation/mocks.rs
1
// Copyright 2019. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22

23
use std::sync::{
24
    Arc,
25
    atomic::{AtomicBool, Ordering},
26
};
27

28
use tari_common_types::{
29
    chain_metadata::ChainMetadata,
30
    types::{CompressedCommitment, FixedHash},
31
};
32
use tari_node_components::blocks::{Block, BlockHeader, ChainBlock};
33
use tari_transaction_components::{tari_proof_of_work::Difficulty, transaction_components::Transaction};
34
use tari_utilities::epoch_time::EpochTime;
35

36
use super::{
37
    BlockBodyValidator,
38
    HeaderChainLinkedValidator,
39
    InternalConsistencyValidator,
40
    TransactionValidator,
41
    traits::CandidateBlockValidator,
42
};
43
use crate::{
44
    chain_storage::BlockchainBackend,
45
    proof_of_work::{AchievedTargetDifficulty, randomx_factory::RandomXFactory},
46
    test_helpers::create_consensus_rules,
47
    validation::{DifficultyCalculator, FinalHorizonStateValidation, error::ValidationError},
48
};
49

50
#[derive(Clone)]
51
pub struct MockValidator {
52
    is_valid: Arc<AtomicBool>,
53
}
54

55
pub struct SharedFlag(Arc<AtomicBool>);
56

57
impl SharedFlag {
58
    pub fn set(&self, v: bool) {
×
59
        self.0.store(v, Ordering::SeqCst);
×
60
    }
×
61
}
62

63
impl MockValidator {
64
    pub fn new(is_valid: bool) -> Self {
504✔
65
        Self {
504✔
66
            is_valid: Arc::new(AtomicBool::new(is_valid)),
504✔
67
        }
504✔
68
    }
504✔
69

70
    pub fn shared_flag(&self) -> SharedFlag {
×
71
        SharedFlag(self.is_valid.clone())
×
72
    }
×
73
}
74

75
impl<B: BlockchainBackend> BlockBodyValidator<B> for MockValidator {
76
    fn validate_body(&self, _: &B, block: &Block) -> Result<Block, ValidationError> {
5✔
77
        if self.is_valid.load(Ordering::SeqCst) {
5✔
78
            Ok(block.clone())
5✔
79
        } else {
80
            Err(ValidationError::ConsensusError(
×
81
                "This mock validator always returns an error".to_string(),
×
82
            ))
×
83
        }
84
    }
5✔
85
}
86

87
impl<B: BlockchainBackend> CandidateBlockValidator<B> for MockValidator {
88
    fn validate_body_with_metadata(&self, _: &B, _: &ChainBlock, _: &ChainMetadata) -> Result<(), ValidationError> {
237✔
89
        if self.is_valid.load(Ordering::SeqCst) {
237✔
90
            Ok(())
237✔
91
        } else {
UNCOV
92
            Err(ValidationError::ConsensusError(
×
UNCOV
93
                "This mock validator always returns an error".to_string(),
×
UNCOV
94
            ))
×
95
        }
96
    }
237✔
97

98
    fn validate_body_at_height(&self, _: &B, _: &ChainBlock) -> Result<(), ValidationError> {
×
99
        if self.is_valid.load(Ordering::SeqCst) {
×
100
            Ok(())
×
101
        } else {
102
            Err(ValidationError::ConsensusError(
×
103
                "This mock validator always returns an error".to_string(),
×
104
            ))
×
105
        }
106
    }
×
107
}
108

109
// #[async_trait]
110
impl InternalConsistencyValidator for MockValidator {
111
    fn validate_internal_consistency(&self, _item: &Block) -> Result<(), ValidationError> {
×
112
        if self.is_valid.load(Ordering::SeqCst) {
×
113
            Ok(())
×
114
        } else {
115
            Err(ValidationError::ConsensusError(
×
116
                "This mock validator always returns an error".to_string(),
×
117
            ))
×
118
        }
119
    }
×
120
}
121

122
impl<B: BlockchainBackend> HeaderChainLinkedValidator<B> for MockValidator {
123
    fn validate(
285✔
124
        &self,
285✔
125
        db: &B,
285✔
126
        header: &BlockHeader,
285✔
127
        _: &BlockHeader,
285✔
128
        _: &[EpochTime],
285✔
129
        _: Option<Difficulty>,
285✔
130
        _: FixedHash,
285✔
131
    ) -> Result<AchievedTargetDifficulty, ValidationError> {
285✔
132
        if self.is_valid.load(Ordering::SeqCst) {
285✔
133
            // this assumes consensus rules are the same as the test rules which is a little brittle
134
            let difficulty_calculator = DifficultyCalculator::new(create_consensus_rules(), RandomXFactory::default());
283✔
135
            let achieved_target_diff = difficulty_calculator.check_achieved_and_target_difficulty(db, header)?;
283✔
136
            Ok(achieved_target_diff)
283✔
137
        } else {
138
            Err(ValidationError::ConsensusError(
2✔
139
                "This mock validator always returns an error".to_string(),
2✔
140
            ))
2✔
141
        }
142
    }
285✔
143
}
144

145
impl TransactionValidator for MockValidator {
146
    fn validate(&self, _transaction: &Transaction) -> Result<(), ValidationError> {
30✔
147
        if self.is_valid.load(Ordering::SeqCst) {
30✔
148
            Ok(())
30✔
149
        } else {
150
            Err(ValidationError::ConsensusError(
×
151
                "This mock validator always returns an error".to_string(),
×
152
            ))
×
153
        }
154
    }
30✔
155
}
156

157
impl<B: BlockchainBackend> FinalHorizonStateValidation<B> for MockValidator {
158
    fn validate(
×
159
        &self,
×
160
        _backend: &B,
×
161
        _height: u64,
×
162
        _total_utxo_sum: &CompressedCommitment,
×
163
        _total_kernel_sum: &CompressedCommitment,
×
164
        _total_burned_sum: &CompressedCommitment,
×
165
    ) -> Result<(), ValidationError> {
×
166
        if self.is_valid.load(Ordering::SeqCst) {
×
167
            Ok(())
×
168
        } else {
169
            Err(ValidationError::ConsensusError(
×
170
                "This mock validator always returns an error".to_string(),
×
171
            ))
×
172
        }
173
    }
×
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc