1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use super::ConsensusState;
use massa_consensus_exports::error::ConsensusError;
use massa_models::stats::ConsensusStats;
use massa_time::MassaTime;
use std::cmp::max;

#[cfg(not(feature = "sandbox"))]
use tracing::warn;

#[cfg(not(feature = "sandbox"))]
use massa_consensus_exports::events::ConsensusEvent;

impl ConsensusState {
    /// Calculate and return stats about consensus
    pub fn get_stats(&self) -> Result<ConsensusStats, ConsensusError> {
        let timespan_end = max(self.launch_time, MassaTime::now());
        let timespan_start = max(
            timespan_end.saturating_sub(self.config.stats_timespan),
            self.launch_time,
        );
        let final_block_count = self
            .final_block_stats
            .iter()
            .filter(|(t, _, _)| *t >= timespan_start && *t < timespan_end)
            .count() as u64;
        let stale_block_count = self
            .stale_block_stats
            .iter()
            .filter(|t| **t >= timespan_start && **t < timespan_end)
            .count() as u64;
        let clique_count = self.get_clique_count() as u64;
        Ok(ConsensusStats {
            final_block_count,
            stale_block_count,
            clique_count,
            start_timespan: timespan_start,
            end_timespan: timespan_end,
        })
    }

    /// Must be called each tick to update stats. Will detect if a desynchronization happened
    pub fn stats_tick(&mut self) -> Result<(), ConsensusError> {
        #[cfg(not(feature = "sandbox"))]
        {
            self.check_desync()?;
        }
        // prune stats
        self.prune_stats()?;
        Ok(())
    }

    #[cfg(not(feature = "sandbox"))]
    /// Helper function for stats_tick. Checks if there are any final blocks is coming from protocol
    /// if none => we are probably desync
    /// Ignore if we are before the last_start_period
    fn check_desync(&mut self) -> Result<(), ConsensusError> {
        let now = MassaTime::now();
        if now
            > max(
                self.config
                    .genesis_timestamp
                    .checked_add(self.config.t0.checked_mul(self.config.last_start_period)?)?,
                self.launch_time,
            )
            .saturating_add(self.stats_desync_detection_timespan)
            && !self
                .final_block_stats
                .iter()
                .any(|(time, _, is_from_protocol)| {
                    time > &now.saturating_sub(self.stats_desync_detection_timespan)
                        && *is_from_protocol
                })
        {
            warn!("desynchronization detected because the recent final block history is empty or contains only blocks produced by this node");
            let _ = self
                .channels
                .controller_event_tx
                .send(ConsensusEvent::NeedSync);
        }

        Ok(())
    }

    /// Remove old stats from consensus storage
    fn prune_stats(&mut self) -> Result<(), ConsensusError> {
        let start_time = MassaTime::now().saturating_sub(self.stats_history_timespan);
        while let Some((t, _, _)) = self.final_block_stats.front() {
            if t < &start_time {
                self.final_block_stats.pop_front();
            } else {
                break;
            }
        }
        while let Some(t) = self.stale_block_stats.front() {
            if t < &start_time {
                self.stale_block_stats.pop_front();
            } else {
                break;
            }
        }
        while let Some((t, _)) = self.protocol_blocks.front() {
            if t < &start_time {
                self.protocol_blocks.pop_front();
            } else {
                break;
            }
        }
        Ok(())
    }
}