diem_state_view/lib.rs
1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8#![forbid(unsafe_code)]
9
10//! This crate defines [`trait StateView`](StateView).
11
12use anyhow::Result;
13use diem_crypto::HashValue;
14use diem_types::{
15 access_path::AccessPath, term_state::PosState, transaction::Version,
16};
17
18/// `StateView` is a trait that defines a read-only snapshot of the global
19/// state. It is passed to the VM for transaction execution, during which the VM
20/// is guaranteed to read anything at the given state.
21pub trait StateView: Sync {
22 /// For logging and debugging purpose, identifies what this view is for.
23 fn id(&self) -> StateViewId { StateViewId::Miscellaneous }
24
25 /// Gets the state for a single access path.
26 fn get(&self, access_path: &AccessPath) -> Result<Option<Vec<u8>>>;
27
28 /// Gets states for a list of access paths.
29 fn multi_get(
30 &self, access_paths: &[AccessPath],
31 ) -> Result<Vec<Option<Vec<u8>>>>;
32
33 /// VM needs this method to know whether the current state view is for
34 /// genesis state creation. Currently TransactionPayload::WriteSet is
35 /// only valid for genesis state creation.
36 fn is_genesis(&self) -> bool;
37
38 fn pos_state(&self) -> &PosState;
39}
40
41#[derive(Copy, Clone)]
42pub enum StateViewId {
43 /// State-sync applying a chunk of transactions.
44 ChunkExecution { first_version: Version },
45 /// LEC applying a block.
46 BlockExecution { block_id: HashValue },
47 /// VmValidator verifying incoming transaction.
48 TransactionValidation { base_version: Version },
49 /// For test, db-bootstrapper, etc. Usually not aimed to pass to VM.
50 Miscellaneous,
51}