massa_deferred_calls/
registry_changes.rs

1use massa_models::{amount::Amount, deferred_calls::DeferredCallId, slot::Slot};
2use serde::{Deserialize, Serialize};
3use serde_with::serde_as;
4use std::collections::BTreeMap;
5
6use crate::{
7    slot_changes::DeferredRegistrySlotChanges, DeferredCall, DeferredRegistryCallChange,
8    DeferredRegistryGasChange,
9};
10
11#[serde_as]
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct DeferredCallRegistryChanges {
14    #[serde_as(as = "Vec<(_, _)>")]
15    pub slots_change: BTreeMap<Slot, DeferredRegistrySlotChanges>,
16
17    pub effective_total_gas: DeferredRegistryGasChange<u128>,
18    // stats : (success, failed, cancel)
19    pub exec_stats: (u64, u64, u64),
20}
21
22impl Default for DeferredCallRegistryChanges {
23    fn default() -> Self {
24        Self {
25            slots_change: Default::default(),
26            effective_total_gas: DeferredRegistryGasChange::Keep,
27            exec_stats: (0, 0, 0),
28        }
29    }
30}
31
32impl DeferredCallRegistryChanges {
33    pub fn delete_call(&mut self, target_slot: Slot, id: &DeferredCallId) {
34        self.slots_change
35            .entry(target_slot)
36            .or_default()
37            .delete_call(id)
38    }
39
40    pub fn set_call(&mut self, id: DeferredCallId, call: DeferredCall) {
41        self.slots_change
42            .entry(call.target_slot)
43            .or_default()
44            .set_call(id, call);
45    }
46
47    /// Returns the raw change entry for `(target_slot, id)` so that callers
48    /// can distinguish `Set` (present), `Delete` (tombstoned) and `None`
49    pub fn get_call_change(
50        &self,
51        target_slot: &Slot,
52        id: &DeferredCallId,
53    ) -> Option<&DeferredRegistryCallChange> {
54        self.slots_change
55            .get(target_slot)
56            .and_then(|slot_changes| slot_changes.get_call_change(id))
57    }
58
59    pub fn get_effective_slot_gas(&self, target_slot: &Slot) -> Option<u64> {
60        self.slots_change
61            .get(target_slot)
62            .and_then(|slot_changes| slot_changes.get_effective_slot_gas())
63    }
64
65    pub fn set_effective_slot_gas(&mut self, target_slot: Slot, gas: u64) {
66        self.slots_change
67            .entry(target_slot)
68            .or_default()
69            .set_effective_slot_gas(gas);
70    }
71
72    pub fn set_slot_base_fee(&mut self, target_slot: Slot, base_fee: Amount) {
73        self.slots_change
74            .entry(target_slot)
75            .or_default()
76            .set_base_fee(base_fee);
77    }
78
79    pub fn get_slot_base_fee(&self, target_slot: &Slot) -> Option<Amount> {
80        self.slots_change
81            .get(target_slot)
82            .and_then(|slot_changes| slot_changes.get_base_fee())
83    }
84
85    pub fn set_effective_total_gas(&mut self, gas: u128) {
86        self.effective_total_gas = DeferredRegistryGasChange::Set(gas);
87    }
88
89    pub fn get_effective_total_gas(&self) -> Option<u128> {
90        match self.effective_total_gas {
91            DeferredRegistryGasChange::Set(v) => Some(v),
92            DeferredRegistryGasChange::Keep => None,
93        }
94    }
95}