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

tari-project / tari / 15423730966

03 Jun 2025 05:20PM UTC coverage: 72.329% (-0.4%) from 72.763%
15423730966

push

github

SWvheerden
update readme

81584 of 112795 relevant lines covered (72.33%)

252196.45 hits per line

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

93.75
/base_layer/core/src/proof_of_work/target_difficulty_window.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::cmp;
24

25
use tari_utilities::epoch_time::EpochTime;
26

27
use crate::proof_of_work::{difficulty::DifficultyAdjustment, lwma_diff::LinearWeightedMovingAverage, Difficulty};
28

29
/// A window of target difficulties
30
#[derive(Debug, Clone)]
31
pub struct TargetDifficultyWindow {
32
    lwma: LinearWeightedMovingAverage,
33
}
34

35
impl TargetDifficultyWindow {
36
    /// Initialize a new `TargetDifficultyWindow`
37
    pub(crate) fn new(block_window: usize, target_time: u64) -> Result<Self, String> {
280✔
38
        Ok(Self {
280✔
39
            lwma: LinearWeightedMovingAverage::new(block_window, target_time)?,
280✔
40
        })
41
    }
280✔
42

43
    /// Appends a target difficulty. If the number of stored difficulties exceeds the block window, the stored
44
    /// difficulty at the front is removed keeping the size of the stored difficulties equal to the block window.
45
    #[inline]
46
    pub fn add_back(&mut self, time: EpochTime, difficulty: Difficulty) {
26✔
47
        self.lwma.add_back(time, difficulty);
26✔
48
    }
26✔
49

50
    /// Prepends a target difficulty. If the number of stored difficulties exceeds the block window, the stored
51
    /// difficulty at the back is removed keeping the size of the stored difficulties equal to the block window.
52
    #[inline]
53
    pub fn add_front(&mut self, time: EpochTime, difficulty: Difficulty) {
937✔
54
        self.lwma.add_front(time, difficulty);
937✔
55
    }
937✔
56

57
    /// Returns true of the TargetDifficulty has `block_window` data points, otherwise false
58
    #[inline]
59
    pub fn is_full(&self) -> bool {
730✔
60
        self.lwma.is_full()
730✔
61
    }
730✔
62

63
    /// Returns the number of target difficulties in the window
64
    pub fn len(&self) -> usize {
1✔
65
        self.lwma.num_samples()
1✔
66
    }
1✔
67

68
    /// Returns true if the window is empty
69
    #[inline]
70
    pub fn is_empty(&self) -> bool {
1✔
71
        self.lwma.num_samples() == 0
1✔
72
    }
1✔
73

74
    /// Calculates the target difficulty for the current set of target difficulties.
75
    pub fn calculate(&self, min: Difficulty, max: Difficulty) -> Difficulty {
285✔
76
        let difficulty = self.lwma.get_difficulty().unwrap_or(min);
285✔
77
        cmp::max(min, cmp::min(max, difficulty))
285✔
78
    }
285✔
79

80
    pub fn update_target_time(&mut self, target_time: u64) -> Result<(), String> {
×
81
        self.lwma.update_target_time(target_time)
×
82
    }
×
83
}
84

85
#[cfg(test)]
86
mod test {
87
    use super::*;
88

89
    #[test]
90
    fn it_calculates_the_target_difficulty() {
1✔
91
        let mut target_difficulties = TargetDifficultyWindow::new(5, 60).unwrap();
1✔
92
        let mut time = Difficulty::from_u64(60).unwrap().as_u64().into();
1✔
93
        target_difficulties.add_back(time, Difficulty::from_u64(100).unwrap());
1✔
94
        time = time
1✔
95
            .checked_add(EpochTime::from(Difficulty::from_u64(60).unwrap().as_u64()))
1✔
96
            .unwrap();
1✔
97
        target_difficulties.add_back(time, Difficulty::from_u64(100).unwrap());
1✔
98
        time = time
1✔
99
            .checked_add(EpochTime::from(Difficulty::from_u64(60).unwrap().as_u64()))
1✔
100
            .unwrap();
1✔
101
        target_difficulties.add_back(time, Difficulty::from_u64(100).unwrap());
1✔
102
        time = time
1✔
103
            .checked_add(EpochTime::from(Difficulty::from_u64(60).unwrap().as_u64()))
1✔
104
            .unwrap();
1✔
105
        target_difficulties.add_back(time, Difficulty::from_u64(100).unwrap());
1✔
106

1✔
107
        assert_eq!(
1✔
108
            target_difficulties.calculate(Difficulty::from_u64(1).unwrap(), Difficulty::from_u64(400).unwrap()),
1✔
109
            Difficulty::from_u64(100).unwrap()
1✔
110
        );
1✔
111
    }
1✔
112
}
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