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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Copyright (c) 2022 MASSA LABS <info@massa.net>

//! This file provides structures representing changes to the final state

use massa_async_pool::{
    AsyncPoolChanges, AsyncPoolChangesDeserializer, AsyncPoolChangesSerializer,
};
use massa_executed_ops::{
    ExecutedDenunciationsChanges, ExecutedDenunciationsChangesDeserializer,
    ExecutedDenunciationsChangesSerializer, ExecutedOpsChanges, ExecutedOpsChangesDeserializer,
    ExecutedOpsChangesSerializer,
};
use massa_hash::{HashDeserializer, HashSerializer};
use massa_ledger_exports::{
    LedgerChanges, LedgerChangesDeserializer, LedgerChangesSerializer, SetOrKeep,
    SetOrKeepDeserializer, SetOrKeepSerializer,
};
use massa_pos_exports::{PoSChanges, PoSChangesDeserializer, PoSChangesSerializer};
use massa_serialization::{Deserializer, SerializeError, Serializer};
use nom::{
    error::{context, ContextError, ParseError},
    sequence::tuple,
    IResult, Parser,
};
use serde::{Deserialize, Serialize};

/// represents changes that can be applied to the execution state
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct StateChanges {
    /// ledger changes
    pub ledger_changes: LedgerChanges,
    /// asynchronous pool changes
    pub async_pool_changes: AsyncPoolChanges,
    /// roll state changes
    pub pos_changes: PoSChanges,
    /// executed operations changes
    pub executed_ops_changes: ExecutedOpsChanges,
    /// executed denunciations changes
    pub executed_denunciations_changes: ExecutedDenunciationsChanges,
    /// execution trail hash change
    pub execution_trail_hash_change: SetOrKeep<massa_hash::Hash>,
}

/// Basic `StateChanges` serializer.
pub struct StateChangesSerializer {
    ledger_changes_serializer: LedgerChangesSerializer,
    async_pool_changes_serializer: AsyncPoolChangesSerializer,
    pos_changes_serializer: PoSChangesSerializer,
    ops_changes_serializer: ExecutedOpsChangesSerializer,
    de_changes_serializer: ExecutedDenunciationsChangesSerializer,
    execution_trail_hash_change_serializer: SetOrKeepSerializer<massa_hash::Hash, HashSerializer>,
}

impl Default for StateChangesSerializer {
    fn default() -> Self {
        Self::new()
    }
}

impl StateChangesSerializer {
    /// Creates a `StateChangesSerializer`
    pub fn new() -> Self {
        Self {
            ledger_changes_serializer: LedgerChangesSerializer::new(),
            async_pool_changes_serializer: AsyncPoolChangesSerializer::new(),
            pos_changes_serializer: PoSChangesSerializer::new(),
            ops_changes_serializer: ExecutedOpsChangesSerializer::new(),
            de_changes_serializer: ExecutedDenunciationsChangesSerializer::new(),
            execution_trail_hash_change_serializer: SetOrKeepSerializer::new(HashSerializer::new()),
        }
    }
}

impl Serializer<StateChanges> for StateChangesSerializer {
    fn serialize(&self, value: &StateChanges, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        self.ledger_changes_serializer
            .serialize(&value.ledger_changes, buffer)?;
        self.async_pool_changes_serializer
            .serialize(&value.async_pool_changes, buffer)?;
        self.pos_changes_serializer
            .serialize(&value.pos_changes, buffer)?;
        self.ops_changes_serializer
            .serialize(&value.executed_ops_changes, buffer)?;
        self.de_changes_serializer
            .serialize(&value.executed_denunciations_changes, buffer)?;
        self.execution_trail_hash_change_serializer
            .serialize(&value.execution_trail_hash_change, buffer)?;
        Ok(())
    }
}

/// Basic `StateChanges` deserializer
pub struct StateChangesDeserializer {
    ledger_changes_deserializer: LedgerChangesDeserializer,
    async_pool_changes_deserializer: AsyncPoolChangesDeserializer,
    pos_changes_deserializer: PoSChangesDeserializer,
    ops_changes_deserializer: ExecutedOpsChangesDeserializer,
    de_changes_deserializer: ExecutedDenunciationsChangesDeserializer,
    execution_trail_hash_change_deserializer:
        SetOrKeepDeserializer<massa_hash::Hash, HashDeserializer>,
}

impl StateChangesDeserializer {
    /// Creates a `StateChangesDeserializer`
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        thread_count: u8,
        max_async_pool_changes: u64,
        max_function_length: u16,
        max_function_params_length: u64,
        max_ledger_changes_count: u64,
        max_datastore_key_length: u8,
        max_datastore_value_length: u64,
        max_datastore_entry_count: u64,
        max_rolls_length: u64,
        max_production_stats_length: u64,
        max_credits_length: u64,
        max_ops_changes_length: u64,
        endorsement_count: u32,
        max_de_changes_length: u64,
    ) -> Self {
        Self {
            ledger_changes_deserializer: LedgerChangesDeserializer::new(
                max_ledger_changes_count,
                max_datastore_key_length,
                max_datastore_value_length,
                max_datastore_entry_count,
            ),
            async_pool_changes_deserializer: AsyncPoolChangesDeserializer::new(
                thread_count,
                max_async_pool_changes,
                max_function_length,
                max_function_params_length,
                max_datastore_key_length as u32,
            ),
            pos_changes_deserializer: PoSChangesDeserializer::new(
                thread_count,
                max_rolls_length,
                max_production_stats_length,
                max_credits_length,
            ),
            ops_changes_deserializer: ExecutedOpsChangesDeserializer::new(
                thread_count,
                max_ops_changes_length,
            ),
            de_changes_deserializer: ExecutedDenunciationsChangesDeserializer::new(
                thread_count,
                endorsement_count,
                max_de_changes_length,
            ),
            execution_trail_hash_change_deserializer: SetOrKeepDeserializer::new(
                HashDeserializer::new(),
            ),
        }
    }
}

impl Deserializer<StateChanges> for StateChangesDeserializer {
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], StateChanges, E> {
        context(
            "Failed StateChanges deserialization",
            tuple((
                context("Failed ledger_changes deserialization", |input| {
                    self.ledger_changes_deserializer.deserialize(input)
                }),
                context("Failed async_pool_changes deserialization", |input| {
                    self.async_pool_changes_deserializer.deserialize(input)
                }),
                context("Failed roll_state_changes deserialization", |input| {
                    self.pos_changes_deserializer.deserialize(input)
                }),
                context("Failed executed_ops_changes deserialization", |input| {
                    self.ops_changes_deserializer.deserialize(input)
                }),
                context("Failed de_changes deserialization", |input| {
                    self.de_changes_deserializer.deserialize(input)
                }),
                context(
                    "Failed execution_trail_hash_change deserialization",
                    |input| {
                        self.execution_trail_hash_change_deserializer
                            .deserialize(input)
                    },
                ),
            )),
        )
        .map(
            |(
                ledger_changes,
                async_pool_changes,
                pos_changes,
                executed_ops_changes,
                executed_denunciations_changes,
                execution_trail_hash_change,
            )| StateChanges {
                ledger_changes,
                async_pool_changes,
                pos_changes,
                executed_ops_changes,
                executed_denunciations_changes,
                execution_trail_hash_change,
            },
        )
        .parse(buffer)
    }
}

impl StateChanges {
    /// extends the current `StateChanges` with another one
    pub fn apply(&mut self, changes: StateChanges) {
        use massa_ledger_exports::Applicable;
        self.ledger_changes.apply(changes.ledger_changes);
        self.async_pool_changes.apply(changes.async_pool_changes);
        self.pos_changes.extend(changes.pos_changes);
        self.executed_ops_changes
            .extend(changes.executed_ops_changes);
        // Note: no need to version the changes, as this function is not used in the codebase
        self.executed_denunciations_changes
            .extend(changes.executed_denunciations_changes);
        self.execution_trail_hash_change
            .apply(changes.execution_trail_hash_change);
    }
}

#[cfg(test)]
mod test {
    use std::collections::BTreeMap;
    use std::str::FromStr;

    use massa_async_pool::AsyncMessage;
    use massa_ledger_exports::{LedgerEntryUpdate, SetUpdateOrDelete};
    use massa_models::address::Address;
    use massa_models::amount::Amount;
    use massa_models::bytecode::Bytecode;
    use massa_models::slot::Slot;
    use massa_serialization::DeserializeError;

    use massa_models::config::{
        ENDORSEMENT_COUNT, MAX_BOOTSTRAP_ASYNC_POOL_CHANGES, MAX_DATASTORE_ENTRY_COUNT,
        MAX_DATASTORE_KEY_LENGTH, MAX_DATASTORE_VALUE_LENGTH, MAX_DEFERRED_CREDITS_LENGTH,
        MAX_DENUNCIATION_CHANGES_LENGTH, MAX_EXECUTED_OPS_CHANGES_LENGTH, MAX_FUNCTION_NAME_LENGTH,
        MAX_LEDGER_CHANGES_COUNT, MAX_PARAMETERS_SIZE, MAX_PRODUCTION_STATS_LENGTH,
        MAX_ROLLS_COUNT_LENGTH, THREAD_COUNT,
    };

    use super::*;

    impl PartialEq<StateChanges> for StateChanges {
        fn eq(&self, other: &StateChanges) -> bool {
            self.ledger_changes == other.ledger_changes &&
                self.async_pool_changes == other.async_pool_changes &&
                // pos_changes
                self.pos_changes.seed_bits == other.pos_changes.seed_bits &&
                self.pos_changes.roll_changes == other.pos_changes.roll_changes &&
                self.pos_changes.production_stats == other.pos_changes.production_stats &&
                self.pos_changes.deferred_credits.credits == other.pos_changes.deferred_credits.credits &&
                self.executed_ops_changes == other.executed_ops_changes &&
                self.executed_denunciations_changes == other.executed_denunciations_changes &&
                self.execution_trail_hash_change == other.execution_trail_hash_change
        }
    }

    #[test]
    fn test_state_changes_ser_der() {
        let mut state_changes = StateChanges::default();
        let message = AsyncMessage::new(
            Slot::new(1, 0),
            0,
            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
            Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
            String::from("test"),
            10000000,
            Amount::from_str("1").unwrap(),
            Amount::from_str("1").unwrap(),
            Slot::new(2, 0),
            Slot::new(3, 0),
            vec![1, 2, 3, 4],
            None,
            None,
        );
        let mut async_pool_changes = AsyncPoolChanges::default();
        async_pool_changes
            .0
            .insert(message.compute_id(), SetUpdateOrDelete::Set(message));
        state_changes.async_pool_changes = async_pool_changes;

        let amount = Amount::from_str("1").unwrap();
        let bytecode = Bytecode(vec![1, 2, 3]);
        let ledger_entry = LedgerEntryUpdate {
            balance: SetOrKeep::Set(amount),
            bytecode: SetOrKeep::Set(bytecode),
            datastore: BTreeMap::default(),
        };
        let mut ledger_changes = LedgerChanges::default();
        ledger_changes.0.insert(
            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
            SetUpdateOrDelete::Update(ledger_entry),
        );
        state_changes.ledger_changes = ledger_changes;
        let mut serialized = Vec::new();
        StateChangesSerializer::new()
            .serialize(&state_changes, &mut serialized)
            .unwrap();

        let (rest, state_changes_deser) = StateChangesDeserializer::new(
            THREAD_COUNT,
            MAX_BOOTSTRAP_ASYNC_POOL_CHANGES,
            MAX_FUNCTION_NAME_LENGTH,
            MAX_PARAMETERS_SIZE as u64,
            MAX_LEDGER_CHANGES_COUNT,
            MAX_DATASTORE_KEY_LENGTH,
            MAX_DATASTORE_VALUE_LENGTH,
            MAX_DATASTORE_ENTRY_COUNT,
            MAX_ROLLS_COUNT_LENGTH,
            MAX_PRODUCTION_STATS_LENGTH,
            MAX_DEFERRED_CREDITS_LENGTH,
            MAX_EXECUTED_OPS_CHANGES_LENGTH,
            ENDORSEMENT_COUNT,
            MAX_DENUNCIATION_CHANGES_LENGTH,
        )
        .deserialize::<DeserializeError>(&serialized)
        .unwrap();
        assert!(rest.is_empty());
        assert_eq!(state_changes, state_changes_deser);
    }

    #[test]
    fn test_state_changes_serde() {
        let mut state_changes = StateChanges::default();
        let message = AsyncMessage::new(
            Slot::new(1, 0),
            0,
            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
            Address::from_str("AU12htxRWiEm8jDJpJptr6cwEhWNcCSFWstN1MLSa96DDkVM9Y42G").unwrap(),
            String::from("test"),
            10000000,
            Amount::from_str("1").unwrap(),
            Amount::from_str("1").unwrap(),
            Slot::new(2, 0),
            Slot::new(3, 0),
            vec![1, 2, 3, 4],
            None,
            None,
        );
        let mut async_pool_changes = AsyncPoolChanges::default();
        async_pool_changes
            .0
            .insert(message.compute_id(), SetUpdateOrDelete::Set(message));
        state_changes.async_pool_changes = async_pool_changes;

        let amount = Amount::from_str("1").unwrap();
        let bytecode = Bytecode(vec![1, 2, 3]);
        let mut datastore = BTreeMap::new();
        datastore.insert(
            b"hello".to_vec(),
            massa_ledger_exports::SetOrDelete::Set(b"world".to_vec()),
        );
        let ledger_entry = LedgerEntryUpdate {
            balance: SetOrKeep::Set(amount),
            bytecode: SetOrKeep::Set(bytecode),
            datastore,
        };
        let mut ledger_changes = LedgerChanges::default();
        ledger_changes.0.insert(
            Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
            SetUpdateOrDelete::Update(ledger_entry),
        );
        state_changes.ledger_changes = ledger_changes;
        let serialized = serde_json::to_string(&state_changes).unwrap();
        let state_changes_deser = serde_json::from_str(&serialized).unwrap();
        assert_eq!(state_changes, state_changes_deser);
    }
}