storage_interface/
state_view.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
8use anyhow::Result;
9use diem_state_view::{StateView, StateViewId};
10use diem_types::{access_path::AccessPath, term_state::PosState};
11
12/// `VerifiedStateView` is a snapshot of the global state for PoS execution.
13///
14/// In Conflux PoS, the VM (`PosVM`) reads state exclusively through
15/// `pos_state()` and never calls `get()`.
16pub struct VerifiedStateView {
17    /// For logging and debugging purpose, identifies what this view is for.
18    id: StateViewId,
19
20    pos_state: PosState,
21}
22
23impl VerifiedStateView {
24    pub fn new(id: StateViewId, pos_state: PosState) -> Self {
25        Self { id, pos_state }
26    }
27}
28
29impl StateView for VerifiedStateView {
30    fn id(&self) -> StateViewId { self.id }
31
32    fn get(&self, _access_path: &AccessPath) -> Result<Option<Vec<u8>>> {
33        // SAFETY INVARIANT: PosVM must not read account state via
34        // StateView::get(). It exclusively uses pos_state(). If this
35        // panic is hit, a code change has violated this invariant and
36        // the scratchpad/Jellyfish Merkle state tree removal is no
37        // longer safe.
38        panic!(
39            "PosVM must not read account state via StateView::get(). \
40             Use pos_state() instead. If you need account state reads, \
41             the scratchpad state tree must be re-introduced."
42        );
43    }
44
45    fn multi_get(
46        &self, _access_paths: &[AccessPath],
47    ) -> Result<Vec<Option<Vec<u8>>>> {
48        unimplemented!();
49    }
50
51    fn is_genesis(&self) -> bool { false }
52
53    fn pos_state(&self) -> &PosState { &self.pos_state }
54}