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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use crate::{
    cached_state_view::CachedStateView, in_memory_state::InMemoryState,
    no_proof_fetcher::NoProofFetcher, sync_proof_fetcher::SyncProofFetcher, DbReader,
};
use anyhow::Result;
use aptos_crypto::{hash::TransactionAccumulatorHasher, HashValue};
use aptos_state_view::StateViewId;
use aptos_types::{
    proof::accumulator::InMemoryAccumulator, state_store::state_value::StateValue,
    transaction::Version,
};
use scratchpad::SparseMerkleTree;
use std::sync::Arc;

/// A wrapper of the in-memory state sparse merkle tree and the transaction accumulator that
/// represent a specific state collectively. Usually it is a state after executing a block.
#[derive(Clone, Debug)]
pub struct ExecutedTrees {
    /// The in-memory representation of state after execution.
    state: InMemoryState,

    /// The in-memory Merkle Accumulator representing a blockchain state consistent with the
    /// `state_tree`.
    transaction_accumulator: Arc<InMemoryAccumulator<TransactionAccumulatorHasher>>,
}

impl ExecutedTrees {
    pub fn state(&self) -> &InMemoryState {
        &self.state
    }

    pub fn state_tree(&self) -> SparseMerkleTree<StateValue> {
        self.state().current.clone()
    }

    pub fn txn_accumulator(&self) -> &Arc<InMemoryAccumulator<TransactionAccumulatorHasher>> {
        &self.transaction_accumulator
    }

    pub fn version(&self) -> Option<Version> {
        let num_elements = self.txn_accumulator().num_leaves() as u64;
        num_elements.checked_sub(1)
    }

    pub fn state_id(&self) -> HashValue {
        self.txn_accumulator().root_hash()
    }

    pub fn new(
        state: InMemoryState,
        transaction_accumulator: Arc<InMemoryAccumulator<TransactionAccumulatorHasher>>,
    ) -> Self {
        Self {
            state,
            transaction_accumulator,
        }
    }

    pub fn new_at_state_checkpoint(
        state_root_hash: HashValue,
        frozen_subtrees_in_accumulator: Vec<HashValue>,
        num_leaves_in_accumulator: u64,
    ) -> Self {
        let state = InMemoryState::new_at_checkpoint(
            state_root_hash,
            num_leaves_in_accumulator.checked_sub(1),
        );
        let transaction_accumulator = Arc::new(
            InMemoryAccumulator::new(frozen_subtrees_in_accumulator, num_leaves_in_accumulator)
                .expect("The startup info read from storage should be valid."),
        );

        Self::new(state, transaction_accumulator)
    }

    pub fn new_empty() -> Self {
        Self::new(
            InMemoryState::new_empty(),
            Arc::new(InMemoryAccumulator::new_empty()),
        )
    }

    pub fn is_same_view(&self, rhs: &Self) -> bool {
        self.transaction_accumulator.root_hash() == rhs.transaction_accumulator.root_hash()
    }

    pub fn verified_state_view(
        &self,
        id: StateViewId,
        reader: Arc<dyn DbReader>,
    ) -> Result<CachedStateView> {
        CachedStateView::new(
            id,
            reader.clone(),
            self.transaction_accumulator.num_leaves(),
            self.state.current.clone(),
            Arc::new(SyncProofFetcher::new(reader)),
        )
    }

    pub fn state_view(
        &self,
        id: StateViewId,
        reader: Arc<dyn DbReader>,
    ) -> Result<CachedStateView> {
        CachedStateView::new(
            id,
            reader.clone(),
            self.transaction_accumulator.num_leaves(),
            self.state.current.clone(),
            Arc::new(NoProofFetcher::new(reader)),
        )
    }
}

impl Default for ExecutedTrees {
    fn default() -> Self {
        Self::new_empty()
    }
}