massa_pos_exports/
pos_changes.rs

1use crate::{DeferredCredits, ProductionStats};
2use bitvec::prelude::*;
3use massa_models::{address::Address, prehash::PreHashMap};
4use serde::{Deserialize, Serialize};
5
6/// Recap of all PoS changes
7#[derive(Debug, Clone, Deserialize, Serialize, Default)]
8pub struct PoSChanges {
9    /// extra block seed bits added
10    pub seed_bits: BitVec<u8>,
11
12    /// new roll counts for addresses (can be 0 to remove the address from the registry)
13    pub roll_changes: PreHashMap<Address, u64>,
14
15    /// updated production statistics
16    pub production_stats: PreHashMap<Address, ProductionStats>,
17
18    /// set deferred credits indexed by target slot (can be set to 0 to cancel some, in case of slash)
19    /// ordered structure to ensure slot iteration order is deterministic
20    pub deferred_credits: DeferredCredits,
21}
22
23impl PoSChanges {
24    /// Check if changes are empty
25    pub fn is_empty(&self) -> bool {
26        self.seed_bits.is_empty()
27            && self.roll_changes.is_empty()
28            && self.production_stats.is_empty()
29            && self.deferred_credits.credits.is_empty()
30    }
31
32    /// Extends the current `PosChanges` with another one
33    pub fn extend(&mut self, other: PoSChanges) {
34        // extend seed bits
35        self.seed_bits.extend(other.seed_bits);
36
37        // extend roll changes
38        self.roll_changes.extend(other.roll_changes);
39
40        // extend production stats
41        for (other_addr, other_stats) in other.production_stats {
42            self.production_stats
43                .entry(other_addr)
44                .or_default()
45                .extend(&other_stats);
46        }
47
48        // extend deferred credits
49        self.deferred_credits.extend(other.deferred_credits);
50    }
51}