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
//! Copyright (c) 2022 MASSA LABS <info@massa.net>

use massa_models::slot::Slot;
use massa_models::stats::ExecutionStats;
use massa_time::MassaTime;
use std::collections::VecDeque;

/// Execution statistics counter
pub struct ExecutionStatsCounter {
    /// duration of the time window
    time_window_duration: MassaTime,
    /// final blocks in the time window (count, instant)
    final_blocks: VecDeque<(usize, MassaTime)>,
    /// final operations executed in the time window (count, instant)
    final_executed_ops: VecDeque<(usize, MassaTime)>,
    /// final denunciations executed in the time window (count, instant)
    final_executed_denunciations: VecDeque<(usize, MassaTime)>,
}

impl ExecutionStatsCounter {
    /// create a new `ExecutionStatsCounter`
    pub fn new(time_window_duration: MassaTime) -> Self {
        ExecutionStatsCounter {
            time_window_duration,
            final_blocks: Default::default(),
            final_executed_ops: Default::default(),
            final_executed_denunciations: Default::default(),
        }
    }

    /// refresh the counters and delete old records
    fn refresh(&mut self, current_time: MassaTime) {
        let start_time = current_time.saturating_sub(self.time_window_duration);

        // prune final blocks
        while let Some((_, t)) = self.final_blocks.front() {
            if t < &start_time {
                self.final_blocks.pop_front();
            } else {
                break;
            }
        }

        // prune final executed ops
        while let Some((_, t)) = self.final_executed_ops.front() {
            if t < &start_time {
                self.final_executed_ops.pop_front();
            } else {
                break;
            }
        }
    }

    /// register final blocks
    pub fn register_final_blocks(&mut self, count: usize) {
        let current_time = MassaTime::now();
        self.final_blocks.push_back((count, current_time));
        self.refresh(current_time);
    }

    /// register final executed operations
    pub fn register_final_executed_operations(&mut self, count: usize) {
        let current_time = MassaTime::now();
        self.final_executed_ops.push_back((count, current_time));
        self.refresh(current_time);
    }

    /// register final executed denunciations
    pub fn register_final_executed_denunciations(&mut self, count: usize) {
        let current_time = MassaTime::now();
        self.final_executed_denunciations
            .push_back((count, current_time));
        self.refresh(current_time);
    }

    /// get statistics
    pub fn get_stats(&self, active_cursor: Slot, final_cursor: Slot) -> ExecutionStats {
        let current_time = MassaTime::now();
        let start_time = current_time.saturating_sub(self.time_window_duration);
        let map_func = |pair: &(usize, MassaTime)| -> usize {
            let (cnt, t) = pair;
            if t >= &start_time && t <= &current_time {
                *cnt
            } else {
                0
            }
        };
        ExecutionStats {
            final_block_count: self.final_blocks.iter().map(map_func).sum(),
            final_executed_operations_count: self.final_executed_ops.iter().map(map_func).sum(),
            time_window_start: start_time,
            time_window_end: current_time,
            active_cursor,
            final_cursor,
        }
    }
}