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
use crate::{
cached_state_view::CachedStateView, no_proof_fetcher::NoProofFetcher, state_delta::StateDelta,
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, transaction::Version};
use std::ops::Deref;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct ExecutedTrees {
state: StateDelta,
transaction_accumulator: Arc<InMemoryAccumulator<TransactionAccumulatorHasher>>,
}
impl ExecutedTrees {
pub fn state(&self) -> &StateDelta {
&self.state
}
pub fn txn_accumulator(&self) -> &Arc<InMemoryAccumulator<TransactionAccumulatorHasher>> {
&self.transaction_accumulator
}
pub fn version(&self) -> Option<Version> {
self.num_transactions().checked_sub(1)
}
pub fn num_transactions(&self) -> u64 {
self.txn_accumulator().num_leaves() as u64
}
pub fn state_id(&self) -> HashValue {
self.txn_accumulator().root_hash()
}
pub fn new(
state: StateDelta,
transaction_accumulator: Arc<InMemoryAccumulator<TransactionAccumulatorHasher>>,
) -> Self {
assert_eq!(
state.current_version.map_or(0, |v| v + 1),
transaction_accumulator.num_leaves()
);
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 = StateDelta::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(
StateDelta::new_empty(),
Arc::new(InMemoryAccumulator::new_empty()),
)
}
pub fn is_same_view(&self, rhs: &Self) -> bool {
self.state().has_same_current_state(rhs.state())
&& self.transaction_accumulator.root_hash() == rhs.transaction_accumulator.root_hash()
}
pub fn verified_state_view<'a>(
&self,
id: StateViewId,
reader: &'a dyn DbReader,
) -> Result<CachedStateView<SyncProofFetcher<'a>>> {
CachedStateView::new(
id,
reader,
self.transaction_accumulator.num_leaves(),
self.state.current.clone(),
SyncProofFetcher::new(reader),
)
}
pub fn state_view(
&self,
id: StateViewId,
reader: &Arc<dyn DbReader>,
) -> Result<CachedStateView<NoProofFetcher>> {
CachedStateView::new(
id,
reader.deref(),
self.transaction_accumulator.num_leaves(),
self.state.current.clone(),
NoProofFetcher::new(reader.clone()),
)
}
}
impl Default for ExecutedTrees {
fn default() -> Self {
Self::new_empty()
}
}