massa_final_state/
state_changes.rs

1//! Copyright (c) 2022 MASSA LABS <info@massa.net>
2
3//! This file provides structures representing changes to the final state
4
5use massa_async_pool::AsyncPoolChanges;
6use massa_deferred_calls::registry_changes::DeferredCallRegistryChanges;
7use massa_executed_ops::{ExecutedDenunciationsChanges, ExecutedOpsChanges};
8use massa_ledger_exports::LedgerChanges;
9use massa_models::types::SetOrKeep;
10use massa_pos_exports::PoSChanges;
11use serde::{Deserialize, Serialize};
12
13/// represents changes that can be applied to the execution state
14#[derive(Default, Debug, Clone, Deserialize, Serialize)]
15pub struct StateChanges {
16    /// ledger changes
17    pub ledger_changes: LedgerChanges,
18    /// asynchronous pool changes
19    pub async_pool_changes: AsyncPoolChanges,
20    /// deferred call changes
21    pub deferred_call_changes: DeferredCallRegistryChanges,
22    /// roll state changes
23    pub pos_changes: PoSChanges,
24    /// executed operations changes
25    pub executed_ops_changes: ExecutedOpsChanges,
26    /// executed denunciations changes
27    pub executed_denunciations_changes: ExecutedDenunciationsChanges,
28    /// execution trail hash change
29    pub execution_trail_hash_change: SetOrKeep<massa_hash::Hash>,
30}
31
32#[cfg(test)]
33mod test {
34    use std::collections::BTreeMap;
35    use std::str::FromStr;
36
37    use massa_ledger_exports::LedgerEntryUpdate;
38    use massa_models::{
39        address::Address, amount::Amount, async_msg::AsyncMessage, bytecode::Bytecode, slot::Slot,
40        types::SetUpdateOrDelete,
41    };
42
43    use super::*;
44
45    impl PartialEq<StateChanges> for StateChanges {
46        fn eq(&self, other: &StateChanges) -> bool {
47            self.ledger_changes == other.ledger_changes &&
48                self.async_pool_changes == other.async_pool_changes &&
49                // pos_changes
50                self.pos_changes.seed_bits == other.pos_changes.seed_bits &&
51                self.pos_changes.roll_changes == other.pos_changes.roll_changes &&
52                self.pos_changes.production_stats == other.pos_changes.production_stats &&
53                self.pos_changes.deferred_credits.credits == other.pos_changes.deferred_credits.credits &&
54                self.executed_ops_changes == other.executed_ops_changes &&
55                self.executed_denunciations_changes == other.executed_denunciations_changes &&
56                self.execution_trail_hash_change == other.execution_trail_hash_change
57        }
58    }
59
60    #[test]
61    fn test_state_changes_serde() {
62        let mut state_changes = StateChanges::default();
63        let message = AsyncMessage::new(
64            Slot::new(1, 0),
65            0,
66            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
67            Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
68            String::from("test"),
69            10000000,
70            Amount::from_str("1").unwrap(),
71            Amount::from_str("1").unwrap(),
72            Slot::new(2, 0),
73            Slot::new(3, 0),
74            vec![1, 2, 3, 4],
75            None,
76            None,
77        );
78        let mut async_pool_changes = AsyncPoolChanges::default();
79        async_pool_changes
80            .0
81            .insert(message.compute_id(), SetUpdateOrDelete::Set(message));
82        state_changes.async_pool_changes = async_pool_changes;
83
84        let amount = Amount::from_str("1").unwrap();
85        let bytecode = Bytecode(vec![1, 2, 3]);
86        let mut datastore = BTreeMap::new();
87        datastore.insert(
88            b"hello".to_vec(),
89            massa_models::types::SetOrDelete::Set(b"world".to_vec()),
90        );
91        let ledger_entry = LedgerEntryUpdate {
92            balance: SetOrKeep::Set(amount),
93            bytecode: SetOrKeep::Set(bytecode),
94            datastore,
95        };
96        let mut ledger_changes = LedgerChanges::default();
97        ledger_changes.0.insert(
98            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
99            SetUpdateOrDelete::Update(ledger_entry),
100        );
101        state_changes.ledger_changes = ledger_changes;
102        let serialized = serde_json::to_string(&state_changes).unwrap();
103        let state_changes_deser = serde_json::from_str(&serialized).unwrap();
104        assert_eq!(state_changes, state_changes_deser);
105    }
106}