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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
// Copyright (c) 2022 MASSA LABS <info@massa.net>

//! This file provides structures representing changes to ledger entries

use crate::ledger_entry::{LedgerEntry, LedgerEntryDeserializer, LedgerEntrySerializer};
use massa_models::address::{Address, AddressDeserializer, AddressSerializer};
use massa_models::amount::{Amount, AmountDeserializer, AmountSerializer};
use massa_models::bytecode::{Bytecode, BytecodeDeserializer, BytecodeSerializer};
use massa_models::prehash::PreHashMap;
use massa_models::serialization::{VecU8Deserializer, VecU8Serializer};
use massa_models::types::{
    Applicable, SetOrDelete, SetOrDeleteDeserializer, SetOrDeleteSerializer, SetOrKeep,
    SetOrKeepDeserializer, SetOrKeepSerializer, SetUpdateOrDelete, SetUpdateOrDeleteDeserializer,
    SetUpdateOrDeleteSerializer,
};
use massa_serialization::{
    Deserializer, SerializeError, Serializer, U64VarIntDeserializer, U64VarIntSerializer,
};
use nom::error::{context, ContextError, ParseError};
use nom::multi::length_count;
use nom::sequence::tuple;
use nom::{IResult, Parser};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::collections::{hash_map, BTreeMap};
use std::ops::Bound::Included;

/// represents an update to one or more fields of a `LedgerEntry`
#[serde_as]
#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct LedgerEntryUpdate {
    /// change the balance
    pub balance: SetOrKeep<Amount>,
    /// change the executable bytecode
    pub bytecode: SetOrKeep<Bytecode>,
    /// change datastore entries
    #[serde_as(as = "Vec<(_, _)>")]
    pub datastore: BTreeMap<Vec<u8>, SetOrDelete<Vec<u8>>>,
}

/// Serializer for `datastore` field of `LedgerEntryUpdate`
pub struct DatastoreUpdateSerializer {
    u64_serializer: U64VarIntSerializer,
    vec_u8_serializer: VecU8Serializer,
    value_serializer: SetOrDeleteSerializer<Vec<u8>, VecU8Serializer>,
}

impl DatastoreUpdateSerializer {
    /// Creates a new `DatastoreUpdateSerializer`
    pub fn new() -> Self {
        Self {
            u64_serializer: U64VarIntSerializer::new(),
            vec_u8_serializer: VecU8Serializer::new(),
            value_serializer: SetOrDeleteSerializer::new(VecU8Serializer::new()),
        }
    }
}

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

impl Serializer<BTreeMap<Vec<u8>, SetOrDelete<Vec<u8>>>> for DatastoreUpdateSerializer {
    /// ## Example
    /// ```rust
    /// use std::collections::BTreeMap;
    /// use massa_ledger_exports::{DatastoreUpdateSerializer};
    /// use massa_serialization::Serializer;
    /// use massa_models::types::SetOrDelete;
    ///
    /// let serializer = DatastoreUpdateSerializer::new();
    /// let mut buffer = Vec::new();
    /// let mut datastore = BTreeMap::new();
    /// datastore.insert(vec![1, 2, 3], SetOrDelete::Set(vec![4, 5, 6]));
    /// datastore.insert(vec![3, 4, 5], SetOrDelete::Delete);
    /// serializer.serialize(&datastore, &mut buffer).unwrap();
    /// ```
    fn serialize(
        &self,
        value: &BTreeMap<Vec<u8>, SetOrDelete<Vec<u8>>>,
        buffer: &mut Vec<u8>,
    ) -> Result<(), SerializeError> {
        let entry_count: u64 = value.len().try_into().map_err(|err| {
            SerializeError::GeneralError(format!(
                "too many entries in ConsensusLedgerSubset: {}",
                err
            ))
        })?;
        self.u64_serializer.serialize(&entry_count, buffer)?;
        for (key, value) in value.iter() {
            self.vec_u8_serializer.serialize(key, buffer)?;
            self.value_serializer.serialize(value, buffer)?;
        }
        Ok(())
    }
}

/// Serializer for `datastore` field of `LedgerEntryUpdate`
pub struct DatastoreUpdateDeserializer {
    length_deserializer: U64VarIntDeserializer,
    key_deserializer: VecU8Deserializer,
    value_deserializer: SetOrDeleteDeserializer<Vec<u8>, VecU8Deserializer>,
}

impl DatastoreUpdateDeserializer {
    /// Creates a new `DatastoreUpdateDeserializer`
    pub fn new(
        max_datastore_key_length: u8,
        max_datastore_value_length: u64,
        max_datastore_entry_count: u64,
    ) -> Self {
        Self {
            length_deserializer: U64VarIntDeserializer::new(
                Included(u64::MIN),
                Included(max_datastore_entry_count),
            ),
            key_deserializer: VecU8Deserializer::new(
                Included(u64::MIN),
                Included(max_datastore_key_length as u64),
            ),
            value_deserializer: SetOrDeleteDeserializer::new(VecU8Deserializer::new(
                Included(u64::MIN),
                Included(max_datastore_value_length),
            )),
        }
    }
}

impl Deserializer<BTreeMap<Vec<u8>, SetOrDelete<Vec<u8>>>> for DatastoreUpdateDeserializer {
    /// ## Example
    /// ```rust
    /// use std::collections::BTreeMap;
    /// use massa_ledger_exports::{DatastoreUpdateDeserializer, DatastoreUpdateSerializer};
    /// use massa_serialization::{Serializer, Deserializer, DeserializeError};
    /// use massa_models::types::SetOrDelete;
    ///
    /// let serializer = DatastoreUpdateSerializer::new();
    /// let deserializer = DatastoreUpdateDeserializer::new(255, 255, 255);
    /// let mut buffer = Vec::new();
    /// let mut datastore = BTreeMap::new();
    /// datastore.insert(vec![1, 2, 3], SetOrDelete::Set(vec![4, 5, 6]));
    /// datastore.insert(vec![3, 4, 5], SetOrDelete::Delete);
    /// serializer.serialize(&datastore, &mut buffer).unwrap();
    /// let (rest, deserialized) = deserializer.deserialize::<DeserializeError>(&buffer).unwrap();
    /// assert_eq!(rest.len(), 0);
    /// assert_eq!(deserialized, datastore);
    /// ```
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], BTreeMap<Vec<u8>, SetOrDelete<Vec<u8>>>, E> {
        context(
            "Failed Datastore deserialization",
            length_count(
                context("Failed length deserialization", |input| {
                    self.length_deserializer.deserialize(input)
                }),
                |input| {
                    tuple((
                        context("Failed key deserialization", |input| {
                            self.key_deserializer.deserialize(input)
                        }),
                        context("Failed value deserialization", |input| {
                            self.value_deserializer.deserialize(input)
                        }),
                    ))(input)
                },
            ),
        )
        .map(|elems| elems.into_iter().collect())
        .parse(buffer)
    }
}

/// Serializer for `LedgerEntryUpdate`
pub struct LedgerEntryUpdateSerializer {
    balance_serializer: SetOrKeepSerializer<Amount, AmountSerializer>,
    bytecode_serializer: SetOrKeepSerializer<Bytecode, BytecodeSerializer>,
    datastore_serializer: DatastoreUpdateSerializer,
}

impl LedgerEntryUpdateSerializer {
    /// Creates a new `LedgerEntryUpdateSerializer`
    pub fn new() -> Self {
        Self {
            balance_serializer: SetOrKeepSerializer::new(AmountSerializer::new()),
            bytecode_serializer: SetOrKeepSerializer::new(BytecodeSerializer::new()),
            datastore_serializer: DatastoreUpdateSerializer::new(),
        }
    }
}

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

impl Serializer<LedgerEntryUpdate> for LedgerEntryUpdateSerializer {
    /// ## Example
    /// ```
    /// use massa_serialization::Serializer;
    /// use massa_models::{prehash::PreHashMap, address::Address, amount::Amount, bytecode::Bytecode};
    /// use std::str::FromStr;
    /// use std::collections::BTreeMap;
    /// use massa_models::types::{SetOrDelete, SetOrKeep};
    /// use massa_ledger_exports::{LedgerEntryUpdate, LedgerEntryUpdateSerializer};
    ///
    /// let key = "hello world".as_bytes().to_vec();
    /// let mut datastore = BTreeMap::default();
    /// datastore.insert(key, SetOrDelete::Set(vec![1, 2, 3]));
    /// let amount = Amount::from_str("1").unwrap();
    /// let bytecode = Bytecode(vec![1, 2, 3]);
    /// let ledger_entry = LedgerEntryUpdate {
    ///    balance: SetOrKeep::Keep,
    ///    bytecode: SetOrKeep::Set(bytecode.clone()),
    ///    datastore,
    /// };
    /// let mut serialized = Vec::new();
    /// let serializer = LedgerEntryUpdateSerializer::new();
    /// serializer.serialize(&ledger_entry, &mut serialized).unwrap();
    /// ```
    fn serialize(
        &self,
        value: &LedgerEntryUpdate,
        buffer: &mut Vec<u8>,
    ) -> Result<(), SerializeError> {
        self.balance_serializer.serialize(&value.balance, buffer)?;
        self.bytecode_serializer
            .serialize(&value.bytecode, buffer)?;
        self.datastore_serializer
            .serialize(&value.datastore, buffer)?;
        Ok(())
    }
}

/// Deserializer for `LedgerEntryUpdate`
pub struct LedgerEntryUpdateDeserializer {
    amount_deserializer: SetOrKeepDeserializer<Amount, AmountDeserializer>,
    bytecode_deserializer: SetOrKeepDeserializer<Bytecode, BytecodeDeserializer>,
    datastore_deserializer: DatastoreUpdateDeserializer,
}

impl LedgerEntryUpdateDeserializer {
    /// Creates a new `LedgerEntryUpdateDeserializer`
    pub fn new(
        max_datastore_key_length: u8,
        max_datastore_value_length: u64,
        max_datastore_entry_count: u64,
    ) -> Self {
        Self {
            amount_deserializer: SetOrKeepDeserializer::new(AmountDeserializer::new(
                Included(Amount::MIN),
                Included(Amount::MAX),
            )),
            bytecode_deserializer: SetOrKeepDeserializer::new(BytecodeDeserializer::new(
                max_datastore_value_length,
            )),
            datastore_deserializer: DatastoreUpdateDeserializer::new(
                max_datastore_key_length,
                max_datastore_value_length,
                max_datastore_entry_count,
            ),
        }
    }
}

impl Deserializer<LedgerEntryUpdate> for LedgerEntryUpdateDeserializer {
    /// ## Example
    /// ```
    /// use massa_serialization::{Deserializer, Serializer, DeserializeError};
    /// use massa_models::{prehash::PreHashMap, address::Address, amount::Amount, bytecode::Bytecode};
    /// use std::str::FromStr;
    /// use massa_models::types::{SetOrDelete, SetOrKeep};
    /// use std::collections::BTreeMap;
    /// use massa_ledger_exports::{LedgerEntryUpdate, LedgerEntryUpdateSerializer, LedgerEntryUpdateDeserializer};
    ///
    /// let key = "hello world".as_bytes().to_vec();
    /// let mut datastore = BTreeMap::default();
    /// datastore.insert(key, SetOrDelete::Set(vec![1, 2, 3]));
    /// let amount = Amount::from_str("1").unwrap();
    /// let bytecode = Bytecode(vec![1, 2, 3]);
    /// let ledger_entry = LedgerEntryUpdate {
    ///    balance: SetOrKeep::Keep,
    ///    bytecode: SetOrKeep::Set(bytecode.clone()),
    ///    datastore,
    /// };
    /// let mut serialized = Vec::new();
    /// let serializer = LedgerEntryUpdateSerializer::new();
    /// let deserializer = LedgerEntryUpdateDeserializer::new(255, 10000, 10000);
    /// serializer.serialize(&ledger_entry, &mut serialized).unwrap();
    /// let (rest, ledger_entry_deser) = deserializer.deserialize::<DeserializeError>(&serialized).unwrap();
    /// assert!(rest.is_empty());
    /// assert_eq!(ledger_entry, ledger_entry_deser);
    /// ```
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], LedgerEntryUpdate, E> {
        context(
            "Failed LedgerEntryUpdate deserialization",
            tuple((
                context("Failed balance deserialization", |input| {
                    self.amount_deserializer.deserialize(input)
                }),
                context("Failed bytecode deserialization", |input| {
                    self.bytecode_deserializer.deserialize(input)
                }),
                context("Failed datastore deserialization", |input| {
                    self.datastore_deserializer.deserialize(input)
                }),
            )),
        )
        .map(|(balance, bytecode, datastore)| LedgerEntryUpdate {
            balance,
            bytecode,
            datastore,
        })
        .parse(buffer)
    }
}

impl Applicable<LedgerEntryUpdate> for LedgerEntryUpdate {
    /// extends the `LedgerEntryUpdate` with another one
    fn apply(&mut self, update: LedgerEntryUpdate) {
        self.balance.apply(update.balance);
        self.bytecode.apply(update.bytecode);
        self.datastore.extend(update.datastore);
    }
}

/// represents a list of changes to multiple ledger entries
#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct LedgerChanges(
    pub PreHashMap<Address, SetUpdateOrDelete<LedgerEntry, LedgerEntryUpdate>>,
);

/// `LedgerChanges` serializer
pub struct LedgerChangesSerializer {
    u64_serializer: U64VarIntSerializer,
    address_serializer: AddressSerializer,
    entry_serializer: SetUpdateOrDeleteSerializer<
        LedgerEntry,
        LedgerEntryUpdate,
        LedgerEntrySerializer,
        LedgerEntryUpdateSerializer,
    >,
}

impl LedgerChangesSerializer {
    /// Creates a new `LedgerChangesSerializer`
    pub fn new() -> Self {
        Self {
            u64_serializer: U64VarIntSerializer::new(),
            address_serializer: AddressSerializer::new(),
            entry_serializer: SetUpdateOrDeleteSerializer::new(
                LedgerEntrySerializer::new(),
                LedgerEntryUpdateSerializer::new(),
            ),
        }
    }
}

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

impl Serializer<LedgerChanges> for LedgerChangesSerializer {
    /// ## Example
    /// ```
    /// use massa_serialization::Serializer;
    /// use massa_ledger_exports::{LedgerEntry, LedgerChanges, LedgerChangesSerializer};
    /// use std::str::FromStr;
    /// use massa_models::types::{SetUpdateOrDelete};
    /// use std::collections::BTreeMap;
    /// use massa_models::{amount::Amount, address::Address, bytecode::Bytecode};
    ///
    /// let key = "hello world".as_bytes().to_vec();
    /// let mut datastore = BTreeMap::new();
    /// datastore.insert(key, vec![1, 2, 3]);
    /// let balance = Amount::from_str("1").unwrap();
    /// let bytecode = Bytecode(vec![1, 2, 3]);
    /// let ledger_entry = LedgerEntry {
    ///    balance,
    ///    bytecode,
    ///    datastore,
    /// };
    /// let mut serialized = Vec::new();
    /// let mut changes = LedgerChanges::default();
    /// changes.0.insert(
    ///    Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
    ///    SetUpdateOrDelete::Set(ledger_entry),
    /// );
    /// LedgerChangesSerializer::new().serialize(&changes, &mut serialized).unwrap();
    /// ```
    fn serialize(&self, value: &LedgerChanges, buffer: &mut Vec<u8>) -> Result<(), SerializeError> {
        let entry_count: u64 = value.0.len().try_into().map_err(|err| {
            SerializeError::GeneralError(format!("too many entries in LedgerChanges: {}", err))
        })?;
        self.u64_serializer.serialize(&entry_count, buffer)?;
        for (address, data) in value.0.iter() {
            self.address_serializer.serialize(address, buffer)?;
            self.entry_serializer.serialize(data, buffer)?;
        }
        Ok(())
    }
}

/// `LedgerChanges` deserializer
pub struct LedgerChangesDeserializer {
    length_deserializer: U64VarIntDeserializer,
    address_deserializer: AddressDeserializer,
    entry_deserializer: SetUpdateOrDeleteDeserializer<
        LedgerEntry,
        LedgerEntryUpdate,
        LedgerEntryDeserializer,
        LedgerEntryUpdateDeserializer,
    >,
}

impl LedgerChangesDeserializer {
    /// Creates a new `LedgerChangesDeserializer`
    pub fn new(
        max_ledger_changes_count: u64,
        max_datastore_key_length: u8,
        max_datastore_value_length: u64,
        max_datastore_entry_count: u64,
    ) -> Self {
        Self {
            length_deserializer: U64VarIntDeserializer::new(
                Included(u64::MIN),
                Included(max_ledger_changes_count),
            ),
            address_deserializer: AddressDeserializer::new(),
            entry_deserializer: SetUpdateOrDeleteDeserializer::new(
                LedgerEntryDeserializer::new(
                    max_datastore_entry_count,
                    max_datastore_key_length,
                    max_datastore_value_length,
                ),
                LedgerEntryUpdateDeserializer::new(
                    max_datastore_key_length,
                    max_datastore_value_length,
                    max_datastore_entry_count,
                ),
            ),
        }
    }
}

impl Deserializer<LedgerChanges> for LedgerChangesDeserializer {
    /// ## Example
    /// ```
    /// use massa_serialization::{Deserializer, Serializer, DeserializeError};
    /// use massa_ledger_exports::{LedgerEntry,  LedgerChanges, LedgerChangesSerializer, LedgerChangesDeserializer};
    /// use std::str::FromStr;
    /// use std::collections::BTreeMap;
    /// use massa_models::types::{SetUpdateOrDelete};
    /// use massa_models::{amount::Amount, address::Address, bytecode::Bytecode};
    ///
    /// let key = "hello world".as_bytes().to_vec();
    /// let mut datastore = BTreeMap::new();
    /// datastore.insert(key, vec![1, 2, 3]);
    /// let balance = Amount::from_str("1").unwrap();
    /// let bytecode = Bytecode(vec![1, 2, 3]);
    /// let ledger_entry = LedgerEntry {
    ///    balance,
    ///    bytecode,
    ///    datastore,
    /// };
    /// let mut serialized = Vec::new();
    /// let mut changes = LedgerChanges::default();
    /// changes.0.insert(
    ///    Address::from_str("AU12dG5xP1RDEB5ocdHkymNVvvSJmUL9BgHwCksDowqmGWxfpm93x").unwrap(),
    ///    SetUpdateOrDelete::Set(ledger_entry),
    /// );
    /// LedgerChangesSerializer::new().serialize(&changes, &mut serialized).unwrap();
    /// let (rest, changes_deser) = LedgerChangesDeserializer::new(255, 255, 10000, 10000).deserialize::<DeserializeError>(&serialized).unwrap();
    /// assert!(rest.is_empty());
    /// assert_eq!(changes, changes_deser);
    /// ```
    fn deserialize<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(
        &self,
        buffer: &'a [u8],
    ) -> IResult<&'a [u8], LedgerChanges, E> {
        context(
            "Failed LedgerChanges deserialization",
            length_count(
                context("Failed length deserialization", |input| {
                    self.length_deserializer.deserialize(input)
                }),
                tuple((
                    context("Failed address deserialization", |input| {
                        self.address_deserializer.deserialize(input)
                    }),
                    context("Failed entry deserialization", |input| {
                        self.entry_deserializer.deserialize(input)
                    }),
                )),
            ),
        )
        .map(|res| LedgerChanges(res.into_iter().collect()))
        .parse(buffer)
    }
}

impl Applicable<LedgerChanges> for LedgerChanges {
    /// extends the current `LedgerChanges` with another one
    fn apply(&mut self, changes: LedgerChanges) {
        for (addr, change) in changes.0 {
            match self.0.entry(addr) {
                hash_map::Entry::Occupied(mut occ) => {
                    // apply incoming change if a change on this entry already exists
                    occ.get_mut().apply(change);
                }
                hash_map::Entry::Vacant(vac) => {
                    // otherwise insert the incoming change
                    vac.insert(change);
                }
            }
        }
    }
}

impl LedgerChanges {
    /// Get an item from the `LedgerChanges`
    pub fn get(
        &self,
        addr: &Address,
    ) -> Option<&SetUpdateOrDelete<LedgerEntry, LedgerEntryUpdate>> {
        self.0.get(addr)
    }

    /// Retrieves all the bytcode updates contained in the current changes
    pub fn get_bytecode_updates(&self) -> Vec<Bytecode> {
        let mut v = Vec::new();
        for (_addr, change) in self.0.iter() {
            match change {
                SetUpdateOrDelete::Set(LedgerEntry { bytecode, .. }) => {
                    // When creating a new address all fields are filled and bytecode is empty
                    if !bytecode.0.is_empty() {
                        v.push(bytecode.clone())
                    }
                }
                SetUpdateOrDelete::Update(entry_update) => {
                    if let SetOrKeep::Set(bytecode) = entry_update.bytecode.clone() {
                        if !bytecode.0.is_empty() {
                            v.push(bytecode);
                        }
                    }
                }
                SetUpdateOrDelete::Delete => (),
            }
        }
        v
    }

    /// Create a new, empty address.
    /// Overwrites the address if it is already there.
    pub fn create_address(&mut self, address: &Address) {
        self.0
            .insert(*address, SetUpdateOrDelete::Set(LedgerEntry::default()));
    }

    /// Tries to return the balance of an entry
    /// or gets it from a function if the entry's status is unknown.
    ///
    /// This function is used as an optimization:
    /// if the value can be deduced unambiguously from the `LedgerChanges`,
    /// no need to dig further (for example in the `FinalLedger`).
    ///
    /// # Arguments
    /// * `addr`: address for which to get the value
    /// * `f`: fallback function with no arguments and returning `Option<Amount>`
    ///
    /// # Returns
    /// * Some(v) if a value is present, where v is a copy of the value
    /// * None if the value is absent
    /// * f() if the value is unknown
    pub fn get_balance_or_else<F: FnOnce() -> Option<Amount>>(
        &self,
        addr: &Address,
        f: F,
    ) -> Option<Amount> {
        // Get the changes for the provided address
        match self.0.get(addr) {
            // This entry is being replaced by a new one: get the balance from the new entry
            Some(SetUpdateOrDelete::Set(v)) => Some(v.balance),

            // This entry is being updated
            Some(SetUpdateOrDelete::Update(LedgerEntryUpdate { balance, .. })) => match balance {
                // The update sets a new balance: return it
                SetOrKeep::Set(v) => Some(*v),
                // The update keeps the old balance.
                // We therefore have no info on the absolute value of the balance.
                // We call the fallback function and return its output.
                SetOrKeep::Keep => f(),
            },

            // This entry is being deleted: return None.
            Some(SetUpdateOrDelete::Delete) => None,

            // This entry is not being changed.
            // We therefore have no info on the absolute value of the balance.
            // We call the fallback function and return its output.
            None => f(),
        }
    }

    /// Tries to return the executable bytecode of an entry
    /// or gets it from a function if the entry's status is unknown.
    ///
    /// This function is used as an optimization:
    /// if the value can be deduced unambiguously from the `LedgerChanges`,
    /// no need to dig further (for example in the `FinalLedger`).
    ///
    /// # Arguments
    /// * `addr`: address for which to get the value
    /// * `f`: fallback function with no arguments and returning `Option<Vec<u8>>`
    ///
    /// # Returns
    /// * Some(v) if a value is present, where v is a copy of the value
    /// * None if the value is absent
    /// * f() if the value is unknown
    pub fn get_bytecode_or_else<F: FnOnce() -> Option<Bytecode>>(
        &self,
        addr: &Address,
        f: F,
    ) -> Option<Bytecode> {
        // Get the changes to the provided address
        match self.0.get(addr) {
            // This entry is being replaced by a new one: get the bytecode from the new entry
            Some(SetUpdateOrDelete::Set(v)) => Some(v.bytecode.clone()),

            // This entry is being updated
            Some(SetUpdateOrDelete::Update(LedgerEntryUpdate { bytecode, .. })) => match bytecode {
                // The update sets a new bytecode: return it
                SetOrKeep::Set(v) => Some(v.clone()),

                // The update keeps the old bytecode.
                // We therefore have no info on the absolute value of the bytecode.
                // We call the fallback function and return its output.
                SetOrKeep::Keep => f(),
            },

            // This entry is being deleted: return None.
            Some(SetUpdateOrDelete::Delete) => None,

            // This entry is not being changed.
            // We therefore have no info on the absolute contents of the bytecode.
            // We call the fallback function and return its output.
            None => f(),
        }
    }

    /// Tries to return whether an entry exists
    /// or gets the information from a function if the entry's status is unknown.
    ///
    /// This function is used as an optimization:
    /// if the result can be deduced unambiguously from the `LedgerChanges`,
    /// no need to dig further (for example in the `FinalLedger`).
    ///
    /// # Arguments
    /// * `addr`: address to search for
    /// * `f`: fallback function with no arguments and returning a boolean
    ///
    /// # Returns
    /// * true if the entry exists
    /// * false if the value is absent
    /// * f() if the value's existence is unknown
    pub fn entry_exists_or_else<F: FnOnce() -> bool>(&self, addr: &Address, f: F) -> bool {
        // Get the changes for the provided address
        match self.0.get(addr) {
            // The entry is being replaced by a new one: it exists
            Some(SetUpdateOrDelete::Set(_)) => true,

            // The entry is being updated:
            // assume it exists because it will be created on update if it doesn't
            Some(SetUpdateOrDelete::Update(_)) => true,

            // The entry is being deleted: it doesn't exist anymore
            Some(SetUpdateOrDelete::Delete) => false,

            // This entry is not being changed.
            // We therefore have no info on its existence.
            // We call the fallback function and return its output.
            None => f(),
        }
    }

    /// Set the balance of an address.
    /// If the address doesn't exist, its ledger entry is created.
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `balance`: balance to set for the provided address
    pub fn set_balance(&mut self, addr: Address, balance: Amount) {
        // Get the changes for the entry associated to the provided address
        match self.0.entry(addr) {
            // That entry is being changed
            hash_map::Entry::Occupied(mut occ) => {
                match occ.get_mut() {
                    // The entry is being replaced by a new one
                    SetUpdateOrDelete::Set(v) => {
                        // update the balance of the replacement entry
                        v.balance = balance;
                    }

                    // The entry is being updated
                    SetUpdateOrDelete::Update(u) => {
                        // Make sure the update sets the balance of the entry to its new value
                        u.balance = SetOrKeep::Set(balance);
                    }

                    // The entry is being deleted
                    d @ SetUpdateOrDelete::Delete => {
                        // Replace that deletion with a replacement by a new default entry
                        // for which the balance was properly set
                        *d = SetUpdateOrDelete::Set(LedgerEntry {
                            balance,
                            ..Default::default()
                        });
                    }
                }
            }

            // This entry is not being changed
            hash_map::Entry::Vacant(vac) => {
                // Induce an Update to the entry that sets the balance to its new value
                vac.insert(SetUpdateOrDelete::Update(LedgerEntryUpdate {
                    balance: SetOrKeep::Set(balance),
                    ..Default::default()
                }));
            }
        }
    }

    /// Set the executable bytecode of an address.
    /// If the address doesn't exist, its ledger entry is created.
    ///
    /// # Parameters
    /// * `addr`: target address
    /// * `bytecode`: executable bytecode to assign to that address
    pub fn set_bytecode(&mut self, addr: Address, bytecode: Bytecode) {
        // Get the current changes being applied to the entry associated to that address
        match self.0.entry(addr) {
            // There are changes currently being applied to the entry
            hash_map::Entry::Occupied(mut occ) => {
                match occ.get_mut() {
                    // The entry is being replaced by a new one
                    SetUpdateOrDelete::Set(v) => {
                        // update the bytecode of the replacement entry
                        v.bytecode = bytecode;
                    }

                    // The entry is being updated
                    SetUpdateOrDelete::Update(u) => {
                        // Ensure that the update includes setting the bytecode to its new value
                        u.bytecode = SetOrKeep::Set(bytecode);
                    }

                    // The entry is being deleted
                    d @ SetUpdateOrDelete::Delete => {
                        // Replace that deletion with a replacement by a new default entry
                        // for which the bytecode was properly set
                        *d = SetUpdateOrDelete::Set(LedgerEntry {
                            bytecode,
                            ..Default::default()
                        });
                    }
                }
            }

            // This entry is not being changed
            hash_map::Entry::Vacant(vac) => {
                // Induce an Update to the entry that sets the bytecode to its new value
                vac.insert(SetUpdateOrDelete::Update(LedgerEntryUpdate {
                    bytecode: SetOrKeep::Set(bytecode),
                    ..Default::default()
                }));
            }
        }
    }

    /// Tries to return a datastore entry for a given address,
    /// or gets it from a function if the value's status is unknown.
    ///
    /// This function is used as an optimization:
    /// if the result can be deduced unambiguously from the `LedgerChanges`,
    /// no need to dig further (for example in the `FinalLedger`).
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `key`: datastore key
    /// * `f`: fallback function with no arguments and returning `Option<Vec<u8>>`
    ///
    /// # Returns
    /// * Some(v) if the value was found, where v is a copy of the value
    /// * None if the value is absent
    /// * f() if the value is unknown
    pub fn get_data_entry_or_else<F: FnOnce() -> Option<Vec<u8>>>(
        &self,
        addr: &Address,
        key: &[u8],
        f: F,
    ) -> Option<Vec<u8>> {
        // Get the current changes being applied to the ledger entry associated to that address
        match self.0.get(addr) {
            // This ledger entry is being replaced by a new one:
            // get the datastore entry from the new ledger entry
            Some(SetUpdateOrDelete::Set(v)) => v.datastore.get(key).cloned(),

            // This ledger entry is being updated
            Some(SetUpdateOrDelete::Update(LedgerEntryUpdate { datastore, .. })) => {
                // Get the update being applied to that datastore entry
                match datastore.get(key) {
                    // A new datastore value is being set: return a clone of it
                    Some(SetOrDelete::Set(v)) => Some(v.clone()),

                    // This datastore entry is being deleted: return None
                    Some(SetOrDelete::Delete) => None,

                    // There are no changes to this particular datastore entry.
                    // We therefore have no info on the absolute contents of the datastore entry.
                    // We call the fallback function and return its output.
                    None => f(),
                }
            }

            // This ledger entry is being deleted: return None
            Some(SetUpdateOrDelete::Delete) => None,

            // This ledger entry is not being changed.
            // We therefore have no info on the absolute contents of its datastore entry.
            // We call the fallback function and return its output.
            None => f(),
        }
    }

    /// Tries to return whether the ledger changes contain a write for the given address
    /// and optionally if a datastore key write also exists in the address's datastore.
    /// Notes:
    /// - A ledger entry could be written to without any changes on the values associated,
    ///   for example if the value was changed multiple times in the same slot.
    /// - This code assumes Delete cannot be shadowed by Set operations in the same slot, which may not be the case
    ///   when / if we allow full entry Delete given the current LedgerChanges::Delete handling. In that case, a rework may be necessary.
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `key`: optional datastore key
    ///
    /// # Returns
    /// * true if the address and, optionally the datastore key, exists in the ledger changes
    pub fn has_writes(&self, addr: &Address, key: Option<Vec<u8>>) -> bool {
        // Get the current changes being applied to the ledger entry associated to that address
        match self.0.get(addr) {
            // This ledger entry is being replaced by a new one:
            // check if the new ledger entry has a datastore entry for the provided key
            Some(SetUpdateOrDelete::Set(v)) => key.map_or(true, |k| v.datastore.contains_key(&k)),

            // This ledger entry is being updated
            Some(SetUpdateOrDelete::Update(LedgerEntryUpdate { datastore, .. })) => {
                // Check if the update being applied to that datastore entry
                key.map_or(true, |k| datastore.contains_key(&k))
            }

            // This ledger entry is being deleted: return true
            Some(SetUpdateOrDelete::Delete) => true,

            // This ledger entry is not being changed.
            None => false,
        }
    }

    /// Tries to return whether a datastore entry exists for a given address,
    /// or gets it from a function if the datastore entry's status is unknown.
    ///
    /// This function is used as an optimization:
    /// if the result can be deduced unambiguously from the `LedgerChanges`,
    /// no need to dig further (for example in the `FinalLedger`).
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `key`: datastore key
    /// * `f`: fallback function with no arguments and returning a boolean
    ///
    /// # Returns
    /// * true if the ledger entry exists and the key is present in its datastore
    /// * false if the ledger entry is absent, or if the key is not in its datastore
    /// * f() if the existence of the ledger entry or datastore entry is unknown
    pub fn has_data_entry_or_else<F: FnOnce() -> bool>(
        &self,
        addr: &Address,
        key: &[u8],
        f: F,
    ) -> bool {
        // Get the current changes being applied to the ledger entry associated to that address
        match self.0.get(addr) {
            // This ledger entry is being replaced by a new one:
            // check if the replacement ledger entry has the key in its datastore
            Some(SetUpdateOrDelete::Set(v)) => v.datastore.contains_key(key),

            // This ledger entry is being updated
            Some(SetUpdateOrDelete::Update(LedgerEntryUpdate { datastore, .. })) => {
                // Get the update being applied to that datastore entry
                match datastore.get(key) {
                    // A new datastore value is being set: the datastore entry exists
                    Some(SetOrDelete::Set(_)) => true,

                    // The datastore entry is being deletes: it doesn't exist anymore
                    Some(SetOrDelete::Delete) => false,

                    // There are no changes to this particular datastore entry.
                    // We therefore have no info on its existence.
                    // We call the fallback function and return its output.
                    None => f(),
                }
            }

            // This ledger entry is being deleted: it has no datastore anymore
            Some(SetUpdateOrDelete::Delete) => false,

            // This ledger entry is not being changed.
            // We therefore have no info on its datastore.
            // We call the fallback function and return its output.
            None => f(),
        }
    }

    /// Set a datastore entry for a given address.
    /// If the address doesn't exist, its ledger entry is created.
    /// If the datastore entry exists, its value is replaced, otherwise it is created.
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `key`: datastore key
    /// * `data`: datastore value to set
    pub fn set_data_entry(&mut self, addr: Address, key: Vec<u8>, data: Vec<u8>) {
        // Get the changes being applied to the ledger entry associated to that address
        match self.0.entry(addr) {
            // There are changes currently being applied to the ledger entry
            hash_map::Entry::Occupied(mut occ) => {
                match occ.get_mut() {
                    // The ledger entry is being replaced by a new one
                    SetUpdateOrDelete::Set(v) => {
                        // Insert the value in the datastore of the replacement entry
                        // Any existing value is overwritten
                        v.datastore.insert(key, data);
                    }

                    // The ledger entry is being updated
                    SetUpdateOrDelete::Update(u) => {
                        // Ensure that the update includes setting the datastore entry
                        u.datastore.insert(key, SetOrDelete::Set(data));
                    }

                    // The ledger entry is being deleted
                    d @ SetUpdateOrDelete::Delete => {
                        // Replace that ledger entry deletion with a replacement by a new default ledger entry
                        // for which the datastore contains the (key, value) to insert.
                        *d = SetUpdateOrDelete::Set(LedgerEntry {
                            datastore: vec![(key, data)].into_iter().collect(),
                            ..Default::default()
                        });
                    }
                }
            }

            // This ledger entry is not being changed
            hash_map::Entry::Vacant(vac) => {
                // Induce an Update to the ledger entry that sets the datastore entry to the desired value
                vac.insert(SetUpdateOrDelete::Update(LedgerEntryUpdate {
                    datastore: vec![(key, SetOrDelete::Set(data))].into_iter().collect(),
                    ..Default::default()
                }));
            }
        }
    }

    /// Deletes a datastore entry for a given address.
    /// Does nothing if the entry is missing.
    ///
    /// # Arguments
    /// * `addr`: target address
    /// * `key`: datastore key
    pub fn delete_data_entry(&mut self, addr: Address, key: Vec<u8>) {
        // Get the changes being applied to the ledger entry associated to that address
        match self.0.entry(addr) {
            // There are changes currently being applied to the ledger entry
            hash_map::Entry::Occupied(mut occ) => {
                match occ.get_mut() {
                    // The ledger entry is being replaced by a new one
                    SetUpdateOrDelete::Set(v) => {
                        // Delete the entry in the datastore of the replacement entry
                        v.datastore.remove(&key);
                    }

                    // The ledger entry is being updated
                    SetUpdateOrDelete::Update(u) => {
                        // Ensure that the update includes deleting the datastore entry
                        u.datastore.insert(key, SetOrDelete::Delete);
                    }

                    // The ledger entry is being deleted
                    SetUpdateOrDelete::Delete => {
                        // Do nothing because the whole ledger entry is being deleted
                    }
                }
            }

            // This ledger entry is not being changed
            hash_map::Entry::Vacant(vac) => {
                // Induce an Update to the ledger entry that deletes the datastore entry
                vac.insert(SetUpdateOrDelete::Update(LedgerEntryUpdate {
                    datastore: vec![(key, SetOrDelete::Delete)].into_iter().collect(),
                    ..Default::default()
                }));
            }
        }
    }
}