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
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

pub type ChildrenMerkleMap =
    BTreeMap<ActualSlabIndex, VanillaChildrenTable<MerkleHash>>;

pub struct State {
    manager: Arc<StateManager>,
    snapshot_db: SnapshotDb,
    snapshot_epoch_id: EpochId,
    snapshot_merkle_root: MerkleHash,
    maybe_intermediate_trie: Option<Arc<DeltaMpt>>,
    intermediate_trie_root: Option<NodeRefDeltaMpt>,
    intermediate_trie_root_merkle: MerkleHash,
    /// A None value indicate the special case when snapshot_db is actually the
    /// snapshot_db from the intermediate_epoch_id.
    maybe_intermediate_trie_key_padding: Option<DeltaMptKeyPadding>,
    delta_trie: Arc<DeltaMpt>,
    delta_trie_root: Option<NodeRefDeltaMpt>,
    delta_trie_key_padding: DeltaMptKeyPadding,
    intermediate_epoch_id: EpochId,
    delta_trie_height: Option<u32>,
    height: Option<u64>,
    owned_node_set: Option<OwnedNodeSet>,
    dirty: bool,

    /// Children merkle hashes. Only used for committing and computing
    /// merkle root. It will be cleared after being committed.
    children_merkle_map: ChildrenMerkleMap,

    // FIXME: this is a hack to get pivot chain from parent snapshot to a
    // FIXME: snapshot. it should be done in consensus.
    parent_epoch_id: EpochId,
    recover_mpt_during_construct_pivot_state: bool,
}

impl State {
    pub fn new(
        manager: Arc<StateManager>, state_trees: StateTrees,
        construct_pivot_state: bool,
    ) -> Self {
        Self {
            manager,
            snapshot_db: state_trees.snapshot_db,
            snapshot_epoch_id: state_trees.snapshot_epoch_id,
            snapshot_merkle_root: state_trees.snapshot_merkle_root,
            maybe_intermediate_trie: state_trees.maybe_intermediate_trie,
            intermediate_trie_root: state_trees.intermediate_trie_root,
            intermediate_trie_root_merkle: state_trees
                .intermediate_trie_root_merkle,
            maybe_intermediate_trie_key_padding: state_trees
                .maybe_intermediate_trie_key_padding,
            delta_trie: state_trees.delta_trie,
            delta_trie_root: state_trees.delta_trie_root,
            delta_trie_key_padding: state_trees.delta_trie_key_padding,
            intermediate_epoch_id: state_trees.intermediate_epoch_id,
            delta_trie_height: state_trees.maybe_delta_trie_height,
            height: state_trees.maybe_height,
            owned_node_set: Some(Default::default()),
            dirty: false,
            children_merkle_map: ChildrenMerkleMap::new(),
            parent_epoch_id: state_trees.parent_epoch_id,
            recover_mpt_during_construct_pivot_state: construct_pivot_state,
        }
    }

    fn state_root(&self, merkle_root: MerkleHash) -> StateRootWithAuxInfo {
        let state_root = StateRoot {
            snapshot_root: self.snapshot_merkle_root,
            intermediate_delta_root: self.intermediate_trie_root_merkle,
            delta_root: merkle_root,
        };
        let state_root_hash = state_root.compute_state_root_hash();
        StateRootWithAuxInfo {
            state_root,
            aux_info: StateRootAuxInfo {
                snapshot_epoch_id: self.snapshot_epoch_id.clone(),
                intermediate_epoch_id: self.intermediate_epoch_id.clone(),
                maybe_intermediate_mpt_key_padding: self
                    .maybe_intermediate_trie_key_padding
                    .clone(),
                delta_mpt_key_padding: self.delta_trie_key_padding.clone(),
                state_root_hash,
            },
        }
    }

    fn check_freshly_synced_snapshot(&self, op: &'static str) -> Result<()> {
        // Can't offer proof if we are operating on a synced snapshot, which is
        // missing intermediate mpt.
        if self.maybe_intermediate_trie_key_padding.is_some()
            || self.intermediate_epoch_id == NULL_EPOCH
        {
            Ok(())
        } else {
            Err(Error::UnsupportedByFreshlySyncedSnapshot(op).into())
        }
    }

    fn get_from_delta<WithProof: StaticBool>(
        &self, mpt: &DeltaMpt, maybe_root_node: Option<NodeRefDeltaMpt>,
        access_key: &[u8],
    ) -> Result<(MptValue<Box<[u8]>>, Option<TrieProof>)> {
        // Get won't create any new nodes so it's fine to pass an empty
        // owned_node_set.
        let mut empty_owned_node_set: Option<OwnedNodeSet> =
            Some(Default::default());

        match maybe_root_node {
            None => Ok((MptValue::None, None)),
            Some(root_node) => {
                let maybe_value = SubTrieVisitor::new(
                    mpt,
                    root_node.clone(),
                    &mut empty_owned_node_set,
                )?
                .get(access_key)?;

                let maybe_proof = match WithProof::value() {
                    false => None,
                    true => Some(
                        SubTrieVisitor::new(
                            mpt,
                            root_node,
                            &mut empty_owned_node_set,
                        )?
                        .get_proof(access_key)?,
                    ),
                };

                Ok((maybe_value, maybe_proof))
            }
        }
    }

    pub fn get_from_snapshot<WithProof: StaticBool>(
        &self, access_key: &[u8],
    ) -> Result<(Option<Box<[u8]>>, Option<TrieProof>)> {
        let value = self.snapshot_db.get(access_key)?;
        Ok((
            value,
            if WithProof::value() {
                let mut mpt = self.snapshot_db.open_snapshot_mpt_shared()?;
                let mut cursor = MptCursor::<
                    &mut dyn SnapshotMptTraitRead,
                    BasicPathNode<&mut dyn SnapshotMptTraitRead>,
                >::new(&mut mpt);
                cursor.load_root()?;
                cursor.open_path_for_key::<access_mode::Read>(access_key)?;
                let proof = cursor.to_proof();
                cursor.finish()?;

                Some(proof)
            } else {
                None
            },
        ))
    }

    fn get_from_all_tries<WithProof: StaticBool>(
        &self, access_key: StorageKeyWithSpace,
    ) -> Result<(Option<Box<[u8]>>, StateProof)> {
        let mut proof = StateProof::default();

        let (maybe_value, maybe_delta_proof) = self
            .get_from_delta::<WithProof>(
                &self.delta_trie,
                self.delta_trie_root.clone(),
                &access_key
                    .to_delta_mpt_key_bytes(&self.delta_trie_key_padding),
            )?;
        proof.with_delta(maybe_delta_proof);

        match maybe_value {
            MptValue::Some(value) => {
                return Ok((Some(value), proof));
            }
            MptValue::TombStone => {
                return Ok((None, proof));
            }
            _ => {}
        }

        // FIXME This is for the case of read-only access of the first snapshot
        // state where intermediate_mpt is some.
        if self.maybe_intermediate_trie_key_padding.is_some() {
            if let Some(intermediate_trie) =
                self.maybe_intermediate_trie.as_ref()
            {
                let (maybe_value, maybe_proof) = self
                    .get_from_delta::<WithProof>(
                        intermediate_trie,
                        self.intermediate_trie_root.clone(),
                        &access_key.to_delta_mpt_key_bytes(
                            &self
                                .maybe_intermediate_trie_key_padding
                                .as_ref()
                                .unwrap(),
                        ),
                    )?;

                proof.with_intermediate(maybe_proof);

                match maybe_value {
                    MptValue::Some(value) => {
                        return Ok((Some(value), proof));
                    }
                    MptValue::TombStone => {
                        return Ok((None, proof));
                    }
                    _ => {}
                }
            }
        }

        let (maybe_value, maybe_proof) =
            self.get_from_snapshot::<WithProof>(&access_key.to_key_bytes())?;
        proof.with_snapshot(maybe_proof);

        Ok((maybe_value, proof))
    }
}

impl Drop for State {
    fn drop(&mut self) {
        if self.dirty {
            panic!("State is dirty however is not committed before free.");
        }
    }
}

impl StateTrait for State {
    fn get(
        &self, access_key: StorageKeyWithSpace,
    ) -> Result<Option<Box<[u8]>>> {
        self.ensure_temp_slab_for_db_load();

        self.get_from_all_tries::<NoProof>(access_key)
            .map(|(value, _)| value)
    }

    fn set(
        &mut self, access_key: StorageKeyWithSpace, value: Box<[u8]>,
    ) -> Result<()> {
        self.pre_modification();

        let root_node = self.get_or_create_delta_root_node()?;
        self.delta_trie_root = SubTrieVisitor::new(
            &self.delta_trie,
            root_node,
            &mut self.owned_node_set,
        )?
        .set(
            &access_key.to_delta_mpt_key_bytes(&self.delta_trie_key_padding),
            value,
        )?
        .into();

        Ok(())
    }

    fn delete(&mut self, access_key: StorageKeyWithSpace) -> Result<()> {
        self.set(access_key, MptValue::<Box<[u8]>>::TombStone.unwrap())?;
        Ok(())
    }

    fn delete_test_only(
        &mut self, access_key: StorageKeyWithSpace,
    ) -> Result<Option<Box<[u8]>>> {
        self.pre_modification();

        match self.get_delta_root_node() {
            None => Ok(None),
            Some(old_root_node) => {
                let (old_value, _, root_node) = SubTrieVisitor::new(
                    &self.delta_trie,
                    old_root_node,
                    &mut self.owned_node_set,
                )?
                .delete(
                    &access_key
                        .to_delta_mpt_key_bytes(&self.delta_trie_key_padding),
                )?;
                self.delta_trie_root =
                    root_node.map(|maybe_node| maybe_node.into());
                Ok(old_value)
            }
        }
    }

    /// Delete all key/value pairs with access_key_prefix as prefix. These
    /// key/value pairs exist in three places: Delta Trie, Intermediate Trie
    /// and Snapshot DB.
    ///
    /// For key/value pairs in Delta Trie, we can simply delete them. For
    /// key/value pairs in Intermediate Trie and Snapshot DB, we try to
    /// enumerate all key/value pairs and set tombstone in Delta Trie only when
    /// necessary.
    fn delete_all(
        &mut self, access_key_prefix: StorageKeyWithSpace,
    ) -> Result<Option<Vec<MptKeyValue>>> {
        self.delete_all_impl::<access_mode::Write>(access_key_prefix)
    }

    fn read_all(
        &mut self, access_key_prefix: StorageKeyWithSpace,
    ) -> Result<Option<Vec<MptKeyValue>>> {
        self.delete_all_impl::<access_mode::Read>(access_key_prefix)
    }

    fn compute_state_root(&mut self) -> Result<StateRootWithAuxInfo> {
        self.ensure_temp_slab_for_db_load();

        let merkle_root = self.compute_merkle_root()?;
        Ok(self.state_root(merkle_root))
    }

    fn get_state_root(&self) -> Result<StateRootWithAuxInfo> {
        self.ensure_temp_slab_for_db_load();

        Ok(self.state_root(self.state_root_check()?))
    }

    // TODO(yz): replace coarse lock with a queue.
    fn commit(&mut self, epoch_id: EpochId) -> Result<StateRootWithAuxInfo> {
        self.ensure_temp_slab_for_db_load();

        let merkle_root = self.state_root_check()?;

        // TODO(yz): Think about leaving these node dirty and only commit when
        // the dirty node is removed from cache.
        let commit_result = self.do_db_commit(epoch_id, &merkle_root);
        debug!(
            "commit state for epoch {:?}: merkle_root = {:?}, delta_trie_height={:?} \
            has_intermediate={}, height={:?}, snapshot_epoch_id={:?}, \
            intermediate_epoch_id={:?}, intermediate_mpt_id={:?}, delta_mpt_id={}.",
            epoch_id,
            merkle_root,
            self.delta_trie_height,
            self.maybe_intermediate_trie.is_some(),
            self.height,
            self.snapshot_epoch_id,
            self.intermediate_epoch_id,
            self.maybe_intermediate_trie.as_ref().map(|mpt| mpt.get_mpt_id()),
            self.delta_trie.get_mpt_id(),
        );
        if commit_result.is_err() {
            self.revert();
            debug!("State commitment failed.");

            commit_result?;
        }
        if self.delta_trie_height.unwrap()
            >= self
                .manager
                .get_storage_manager()
                .get_snapshot_epoch_count()
                / 3
            && self.maybe_intermediate_trie.is_some()
        {
            // TODO: use a better criteria and put it in consensus maybe.
            let snapshot_height = self.height.clone().unwrap()
                - self.delta_trie_height.unwrap() as u64;
            self.manager.check_make_snapshot(
                self.maybe_intermediate_trie.clone(),
                self.intermediate_trie_root.clone(),
                &self.intermediate_epoch_id,
                snapshot_height,
                self.recover_mpt_during_construct_pivot_state,
            )?;
        }

        Ok(self.state_root(merkle_root))
    }
}

impl StateTraitExt for State {
    fn get_with_proof(
        &self, access_key: StorageKeyWithSpace,
    ) -> Result<(Option<Box<[u8]>>, StateProof)> {
        self.ensure_temp_slab_for_db_load();

        self.check_freshly_synced_snapshot("proof")?;
        self.get_from_all_tries::<WithProof>(access_key)
    }

    fn get_node_merkle_all_versions<WithProof: StaticBool>(
        &self, access_key: StorageKeyWithSpace,
    ) -> Result<(NodeMerkleTriplet, NodeMerkleProof)> {
        self.check_freshly_synced_snapshot("proof")?;
        let mut proof = NodeMerkleProof::default();

        // ----------- get from delta -----------
        let delta = match self.delta_trie_root {
            Some(ref root_node) => {
                let key = access_key
                    .to_delta_mpt_key_bytes(&self.delta_trie_key_padding);

                let mut owned_node_set = Some(Default::default());

                let mut visitor = SubTrieVisitor::new(
                    &self.delta_trie,
                    root_node.clone(),
                    // won't create any new nodes
                    &mut owned_node_set,
                )?;

                let delta = visitor.get_merkle_hash_wo_compressed_path(&key)?;

                let maybe_proof = match WithProof::value() {
                    false => None,
                    true => Some(
                        SubTrieVisitor::new(
                            &self.delta_trie,
                            root_node.clone(),
                            // won't create any new nodes
                            &mut Some(Default::default()),
                        )?
                        .get_proof(&key)?,
                    ),
                };

                proof.with_delta(maybe_proof);

                // for tombstones, we ignore the node merkle
                if visitor.get(&key)?.is_tombstone() {
                    MptValue::TombStone
                } else {
                    MptValue::from(delta)
                }
            }
            None => MptValue::None,
        };

        // ----------- get from intermediate -----------
        let intermediate = match (
            &self.intermediate_trie_root,
            &self.maybe_intermediate_trie,
            &self.maybe_intermediate_trie_key_padding,
        ) {
            (
                Some(ref root_node),
                Some(ref intermediate_trie),
                Some(ref intermediate_trie_key_padding),
            ) => {
                let key = access_key
                    .to_delta_mpt_key_bytes(&intermediate_trie_key_padding);

                let mut owned_node_set = Some(Default::default());

                let mut visitor = SubTrieVisitor::new(
                    &intermediate_trie,
                    root_node.clone(),
                    // won't create any new nodes
                    &mut owned_node_set,
                )?;

                let intermediate =
                    visitor.get_merkle_hash_wo_compressed_path(&key)?;

                let maybe_proof = match WithProof::value() {
                    false => None,
                    true => Some(
                        SubTrieVisitor::new(
                            &intermediate_trie,
                            root_node.clone(),
                            // won't create any new nodes
                            &mut Some(Default::default()),
                        )?
                        .get_proof(&key)?,
                    ),
                };

                proof.with_intermediate(maybe_proof);

                // for tombstones, we ignore the node merkle
                if visitor.get(&key)?.is_tombstone() {
                    MptValue::TombStone
                } else {
                    MptValue::from(intermediate)
                }
            }
            _ => MptValue::None,
        };

        // ----------- get from snapshot -----------
        let key = access_key.to_key_bytes();

        let mut mpt = self.snapshot_db.open_snapshot_mpt_shared()?;
        let mut cursor = MptCursor::<
            &mut dyn SnapshotMptTraitRead,
            BasicPathNode<&mut dyn SnapshotMptTraitRead>,
        >::new(&mut mpt);
        cursor.load_root()?;
        let snapshot =
            match cursor.open_path_for_key::<access_mode::Read>(&key)? {
                CursorOpenPathTerminal::Arrived => Some(
                    cursor
                        .current_node_mut()
                        .trie_node
                        .get_merkle_hash_wo_compressed_path(),
                ),
                _ => None,
            };
        let maybe_proof = match WithProof::value() {
            false => None,
            true => Some(cursor.to_proof()),
        };
        cursor.finish()?;
        proof.with_snapshot(maybe_proof);

        let triplet = NodeMerkleTriplet {
            delta,
            intermediate,
            snapshot,
        };

        Ok((triplet, proof))
    }
}

impl StateDbGetOriginalMethods for State {
    fn get_original_raw_with_proof(
        &self, key: StorageKeyWithSpace,
    ) -> Result<(Option<Box<[u8]>>, StateProof)> {
        let r = Ok(self.get_with_proof(key)?);
        trace!("get_original_raw_with_proof key={:?}, value={:?}", key, r);
        r
    }

    fn get_original_storage_root(
        &self, address: &AddressWithSpace,
    ) -> Result<StorageRoot> {
        let key = StorageKey::new_storage_root_key(&address.address)
            .with_space(address.space);

        let (root, _) = self.get_node_merkle_all_versions::<NoProof>(key)?;

        Ok(root)
    }

    fn get_original_storage_root_with_proof(
        &self, address: &AddressWithSpace,
    ) -> Result<(StorageRoot, StorageRootProof)> {
        let key = StorageKey::new_storage_root_key(&address.address)
            .with_space(address.space);

        self.get_node_merkle_all_versions::<WithProof>(key)
            .map_err(Into::into)
    }
}

impl State {
    fn ensure_temp_slab_for_db_load(&self) {
        self.delta_trie.get_node_memory_manager().enlarge().ok();
    }

    fn pre_modification(&mut self) {
        if !self.dirty {
            self.dirty = true
        }
        self.delta_trie.get_node_memory_manager().enlarge().ok();
    }

    fn get_delta_root_node(&self) -> Option<NodeRefDeltaMpt> {
        self.delta_trie_root.clone()
    }

    fn get_or_create_delta_root_node(&mut self) -> Result<NodeRefDeltaMpt> {
        if self.delta_trie_root.is_none() {
            let allocator =
                self.delta_trie.get_node_memory_manager().get_allocator();
            let (root_cow, entry) = CowNodeRef::new_uninitialized_node(
                &allocator,
                self.owned_node_set.as_mut().unwrap(),
                self.delta_trie.get_mpt_id(),
            )?;
            // Insert empty node.
            entry.insert(UnsafeCell::new(Default::default()));

            self.delta_trie_root =
                root_cow.into_child().map(|maybe_node| maybe_node.into());
        }

        // Safe because in either branch the result is Some.
        Ok(self.get_delta_root_node().unwrap())
    }

    fn compute_merkle_root(&mut self) -> Result<MerkleHash> {
        assert!(self.children_merkle_map.len() == 0);

        match &self.delta_trie_root {
            None => {
                // Don't commit empty state. Empty state shouldn't exists since
                // genesis block.
                Ok(MERKLE_NULL_NODE)
            }
            Some(root_node) => {
                let mut cow_root = CowNodeRef::new(
                    root_node.clone(),
                    self.owned_node_set.as_ref().unwrap(),
                    self.delta_trie.get_mpt_id(),
                );
                let allocator =
                    self.delta_trie.get_node_memory_manager().get_allocator();
                let arc_db = self.delta_trie.get_arc_db()?;
                let merkle = cow_root.get_or_compute_merkle(
                    &self.delta_trie,
                    self.owned_node_set.as_mut().unwrap(),
                    &allocator,
                    &mut *arc_db.to_owned_read()?,
                    &mut self.children_merkle_map,
                    0,
                )?;
                cow_root.into_child();

                Ok(merkle)
            }
        }
    }

    fn do_db_commit(
        &mut self, epoch_id: EpochId, merkle_root: &MerkleHash,
    ) -> Result<()> {
        let maybe_existing_merkle_root =
            self.delta_trie.get_merkle_root_by_epoch_id(&epoch_id)?;
        if maybe_existing_merkle_root.is_some() {
            // TODO This may happen for genesis when we restart
            info!(
                "Overwriting computed state for epoch {:?}, \
                 committed merkle root {:?}, new merkle root {:?}",
                epoch_id,
                maybe_existing_merkle_root.unwrap(),
                merkle_root
            );
            assert_eq!(
                maybe_existing_merkle_root,
                Some(*merkle_root),
                "Overwriting computed state with a different merkle root."
            );
            self.revert();
            return Ok(());
        }

        // Use coarse lock to prevent row number from interleaving,
        // which makes it cleaner to restart from db failure. It also
        // benefits performance because without a coarse lock all
        // threads may not be able to do anything else when they compete
        // with each other on slow db writing.
        let mut commit_transaction = self.delta_trie.start_commit()?;

        let maybe_root_node = self.delta_trie_root.clone();
        match maybe_root_node {
            None => {}
            Some(root_node) => {
                let start_row_number = commit_transaction.info.row_number.value;

                let mut cow_root = CowNodeRef::new(
                    root_node,
                    self.owned_node_set.as_ref().unwrap(),
                    self.delta_trie.get_mpt_id(),
                );

                if cow_root.is_owned() {
                    let allocator = self
                        .delta_trie
                        .get_node_memory_manager()
                        .get_allocator();
                    let trie_node_mut = unsafe {
                        self.delta_trie
                            .get_node_memory_manager()
                            .dirty_node_as_mut_unchecked(
                                &allocator,
                                &mut cow_root.node_ref,
                            )
                    };
                    let result = cow_root.commit_dirty_recursively(
                        &self.delta_trie,
                        self.owned_node_set.as_mut().unwrap(),
                        trie_node_mut,
                        &mut commit_transaction,
                        &mut *self
                            .delta_trie
                            .get_node_memory_manager()
                            .get_cache_manager()
                            .lock(),
                        &allocator,
                        &mut self.children_merkle_map,
                    );
                    self.children_merkle_map.clear();
                    self.delta_trie_root =
                        cow_root.into_child().map(|r| r.into());
                    result?;

                    // TODO: check the guarantee of underlying db on transaction
                    // TODO: failure. may have to commit last_row_number
                    // TODO: separately in worst case.
                    commit_transaction.transaction.put(
                        "last_row_number".as_bytes(),
                        commit_transaction
                            .info
                            .row_number
                            .to_string()
                            .as_bytes(),
                    )?;
                }

                let db_key = *{
                    match self.delta_trie_root.as_ref().unwrap() {
                        // Dirty state are committed.
                        NodeRefDeltaMpt::Dirty { index: _ } => unsafe {
                            unreachable_unchecked();
                        },
                        // Empty block's state root points to its base state.
                        NodeRefDeltaMpt::Committed { db_key } => db_key,
                    }
                };

                commit_transaction.transaction.put(
                    ["db_key_for_epoch_id_".as_bytes(), epoch_id.as_ref()]
                        .concat()
                        .as_slice(),
                    db_key.to_string().as_bytes(),
                )?;

                commit_transaction.transaction.put(
                    ["db_key_for_root_".as_bytes(), merkle_root.as_ref()]
                        .concat()
                        .as_slice(),
                    db_key.to_string().as_bytes(),
                )?;

                self.manager.number_committed_nodes.fetch_add(
                    (commit_transaction.info.row_number.value
                        - start_row_number) as usize,
                    Ordering::Relaxed,
                );
            }
        }
        trace!("root after commit: {:?}", self.delta_trie_root);

        commit_transaction.transaction.put(
            ["parent_epoch_id_".as_bytes(), epoch_id.as_ref()]
                .concat()
                .as_slice(),
            self.parent_epoch_id.as_ref().to_hex::<String>().as_bytes(),
        )?;

        {
            let arc_db = self.delta_trie.get_arc_db()?;
            commit_transaction
                .transaction
                .commit(arc_db.db_ref().as_any())?;
        }

        self.delta_trie.state_root_committed(
            epoch_id,
            merkle_root,
            self.parent_epoch_id,
            self.delta_trie_root.clone(),
        );

        self.dirty = false;

        Ok(())
    }

    fn state_root_check(&self) -> Result<MerkleHash> {
        let maybe_merkle_root =
            self.delta_trie.get_merkle(self.delta_trie_root.clone())?;
        match maybe_merkle_root {
            // Empty state.
            None => Ok(MERKLE_NULL_NODE),
            Some(merkle_hash) => {
                // Non-empty state
                if merkle_hash.is_zero() {
                    Err(Error::StateCommitWithoutMerkleHash.into())
                } else {
                    Ok(merkle_hash)
                }
            }
        }
    }

    pub fn dump<DUMPER: KVInserter<MptKeyValue>>(
        &self, dumper: &mut DUMPER,
    ) -> Result<()> {
        let inserter = DeltaMptIterator {
            mpt: self.delta_trie.clone(),
            maybe_root_node: self.delta_trie_root.clone(),
        };

        inserter.iterate(dumper)
    }

    fn revert(&mut self) {
        self.dirty = false;

        // Free all modified nodes.
        let owned_node_set = self.owned_node_set.as_ref().unwrap();
        for owned_node in owned_node_set {
            self.delta_trie.get_node_memory_manager().free_owned_node(
                &mut owned_node.clone(),
                self.delta_trie.get_mpt_id(),
            );
        }
    }

    /// Delete all key/value pairs with access_key_prefix as prefix. These
    /// key/value pairs exist in three places: Delta Trie, Intermediate Trie
    /// and Snapshot DB.
    ///
    /// For key/value pairs in Delta Trie, we can simply delete them. For
    /// key/value pairs in Intermediate Trie and Snapshot DB, we try to
    /// enumerate all key/value pairs and set tombstone in Delta Trie only when
    /// necessary.
    ///
    /// When AM is Read, only calculate the key values to be deleted.
    fn delete_all_impl<AM: access_mode::AccessMode>(
        &mut self, access_key_prefix: StorageKeyWithSpace,
    ) -> Result<Option<Vec<MptKeyValue>>> {
        if AM::READ_ONLY {
            self.ensure_temp_slab_for_db_load();
        } else {
            self.pre_modification();
        }

        // Retrieve and delete key/value pairs from delta trie
        let delta_trie_kvs = match &self.delta_trie_root {
            None => None,
            Some(old_root_node) => {
                let delta_mpt_key_prefix = access_key_prefix
                    .to_delta_mpt_key_bytes(&self.delta_trie_key_padding);
                let deleted = if AM::READ_ONLY {
                    SubTrieVisitor::new(
                        &self.delta_trie,
                        old_root_node.clone(),
                        &mut self.owned_node_set,
                    )?
                    .traversal(&delta_mpt_key_prefix, &delta_mpt_key_prefix)?
                } else {
                    let (deleted, _, root_node) = SubTrieVisitor::new(
                        &self.delta_trie,
                        old_root_node.clone(),
                        &mut self.owned_node_set,
                    )?
                    .delete_all(&delta_mpt_key_prefix, &delta_mpt_key_prefix)?;
                    self.delta_trie_root =
                        root_node.map(|maybe_node| maybe_node.into());

                    deleted
                };
                deleted
            }
        };

        // Retrieve key/value pairs from intermediate trie
        let intermediate_trie_kvs = match &self.intermediate_trie_root {
            None => None,
            Some(root_node) => {
                if self.maybe_intermediate_trie_key_padding.is_some()
                    && self.maybe_intermediate_trie.is_some()
                {
                    let intermediate_trie_key_padding = self
                        .maybe_intermediate_trie_key_padding
                        .as_ref()
                        .unwrap();
                    let intermediate_mpt_key_prefix = access_key_prefix
                        .to_delta_mpt_key_bytes(intermediate_trie_key_padding);
                    let values = SubTrieVisitor::new(
                        self.maybe_intermediate_trie.as_ref().unwrap(),
                        root_node.clone(),
                        &mut self.owned_node_set,
                    )?
                    .traversal(
                        &intermediate_mpt_key_prefix,
                        &intermediate_mpt_key_prefix,
                    )?;

                    values
                } else {
                    None
                }
            }
        };

        // Retrieve key/value pairs from snapshot
        let mut kv_iterator = self.snapshot_db.snapshot_kv_iterator()?.take();
        let lower_bound_incl = access_key_prefix.to_key_bytes();
        let upper_bound_excl =
            to_key_prefix_iter_upper_bound(&lower_bound_incl);
        let mut kvs = kv_iterator
            .iter_range(
                lower_bound_incl.as_slice(),
                upper_bound_excl.as_ref().map(|v| &**v),
            )?
            .take();

        let mut snapshot_kvs = Vec::new();
        while let Some((key, value)) = kvs.next()? {
            snapshot_kvs.push((key, value));
        }

        let mut result = Vec::new();
        // This is used to keep track of the deleted keys.
        let mut deleted_keys = HashSet::new();
        if let Some(kvs) = delta_trie_kvs {
            for (k, v) in kvs {
                let storage_key = StorageKeyWithSpace::from_delta_mpt_key(&k);
                let k = storage_key.to_key_bytes();
                deleted_keys.insert(k.clone());
                if v.len() > 0 {
                    result.push((k, v));
                }
            }
        }

        if let Some(kvs) = intermediate_trie_kvs {
            for (k, v) in kvs {
                let storage_key = StorageKeyWithSpace::from_delta_mpt_key(&k);
                // Only delete non-empty keys.
                if v.len() > 0 && !AM::READ_ONLY {
                    self.delete(storage_key)?;
                }
                let k = storage_key.to_key_bytes();
                if !deleted_keys.contains(&k) {
                    deleted_keys.insert(k.clone());
                    if v.len() > 0 {
                        result.push((k, v));
                    }
                }
            }
        }

        // No need to check v.len() because there are no tombStone values in
        // snapshot.
        for (k, v) in snapshot_kvs {
            let storage_key =
                StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&k);
            if !AM::READ_ONLY {
                self.delete(storage_key)?;
            }
            if !deleted_keys.contains(&k) {
                result.push((k, v));
            }
        }

        if result.is_empty() {
            Ok(None)
        } else {
            Ok(Some(result))
        }
    }
}

use crate::{
    impls::{
        delta_mpt::{node_memory_manager::ActualSlabIndex, *},
        errors::*,
        merkle_patricia_trie::{
            mpt_cursor::{BasicPathNode, CursorOpenPathTerminal, MptCursor},
            KVInserter, MptKeyValue, TrieProof, VanillaChildrenTable,
        },
        node_merkle_proof::NodeMerkleProof,
        state_manager::*,
        state_proof::StateProof,
    },
    state::*,
    storage_db::*,
    utils::{access_mode, to_key_prefix_iter_upper_bound},
    StorageRootProof,
};
use cfx_internal_common::{StateRootAuxInfo, StateRootWithAuxInfo};
use cfx_types::AddressWithSpace;
use fallible_iterator::FallibleIterator;
use primitives::{
    DeltaMptKeyPadding, EpochId, MerkleHash, MptValue, NodeMerkleTriplet,
    SkipInputCheck, StateRoot, StaticBool, StorageKey, StorageKeyWithSpace,
    StorageRoot, MERKLE_NULL_NODE, NULL_EPOCH,
};
use rustc_hex::ToHex;
use std::{
    cell::UnsafeCell,
    collections::{BTreeMap, HashSet},
    hint::unreachable_unchecked,
    sync::{atomic::Ordering, Arc},
};