cfx_storage/
state.rs

1// Copyright 2019 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5/// A block defines a list of transactions that it sees and the sequence of
6/// the transactions (ledger). At the view of a block, after all
7/// transactions being executed, the data associated with all addresses is
8/// a State after the epoch defined by the block.
9///
10/// A writable state is copy-on-write reference to the base state in the
11/// state manager. State is supposed to be owned by single user.
12pub use super::impls::state::State;
13use cfx_types::AddressWithSpace;
14
15pub type WithProof = primitives::static_bool::Yes;
16pub type NoProof = primitives::static_bool::No;
17
18// The trait is created to separate the implementation to another file, and the
19// concrete struct is put into inner mod, because the implementation is
20// anticipated to be too complex to present in the same file of the API.
21pub trait StateTrait: Sync + Send {
22    // Actions.
23    fn get(&self, access_key: StorageKeyWithSpace)
24        -> Result<Option<Box<[u8]>>>;
25    fn set(
26        &mut self, access_key: StorageKeyWithSpace, value: Box<[u8]>,
27    ) -> Result<()>;
28    fn delete(&mut self, access_key: StorageKeyWithSpace) -> Result<()>;
29    fn delete_test_only(
30        &mut self, access_key: StorageKeyWithSpace,
31    ) -> Result<Option<Box<[u8]>>>;
32    // Delete everything prefixed by access_key and return deleted key value
33    // pairs.
34    fn delete_all(
35        &mut self, access_key_prefix: StorageKeyWithSpace,
36    ) -> Result<Option<Vec<MptKeyValue>>>;
37    // TODO: Remove this mut.
38    fn read_all(
39        &mut self, access_key_prefix: StorageKeyWithSpace,
40    ) -> Result<Option<Vec<MptKeyValue>>>;
41
42    fn read_all_with_callback(
43        &mut self, _access_key_prefix: StorageKeyWithSpace,
44        _callback: &mut dyn FnMut(MptKeyValue), _only_account_key: bool,
45    ) -> Result<()> {
46        Err(Error::Msg("Not implemented".into()))
47    }
48
49    // Finalize
50    /// It's costly to compute state root however it's only necessary to compute
51    /// state root once before committing.
52    fn compute_state_root(&mut self) -> Result<StateRootWithAuxInfo>;
53    fn get_state_root(&self) -> Result<StateRootWithAuxInfo>;
54    fn commit(&mut self, epoch: EpochId) -> Result<StateRootWithAuxInfo>;
55}
56
57pub trait StateTraitExt {
58    fn get_with_proof(
59        &self, access_key: StorageKeyWithSpace,
60    ) -> Result<(Option<Box<[u8]>>, StateProof)>;
61
62    /// Compute the merkle of the node under `access_key` in all tries.
63    /// Node merkle is computed on the value and children hashes, ignoring the
64    /// compressed path.
65    fn get_node_merkle_all_versions<WithProof: StaticBool>(
66        &self, access_key: StorageKeyWithSpace,
67    ) -> Result<(NodeMerkleTriplet, NodeMerkleProof)>;
68}
69
70// We skip the accessed_entries for getting original value.
71pub trait StateDbGetOriginalMethods {
72    fn get_original_raw_with_proof(
73        &self, key: StorageKeyWithSpace,
74    ) -> Result<(Option<Box<[u8]>>, StateProof)>;
75
76    fn get_original_storage_root(
77        &self, address: &AddressWithSpace,
78    ) -> Result<StorageRoot>;
79
80    fn get_original_storage_root_with_proof(
81        &self, address: &AddressWithSpace,
82    ) -> Result<(StorageRoot, StorageRootProof)>;
83}
84
85use super::{
86    impls::{
87        errors::*, node_merkle_proof::NodeMerkleProof, state_proof::StateProof,
88    },
89    MptKeyValue, StateRootWithAuxInfo,
90};
91use crate::StorageRootProof;
92use primitives::{
93    EpochId, NodeMerkleTriplet, StaticBool, StorageKeyWithSpace, StorageRoot,
94};