pchain_runtime/execution/
state.rs

1/*
2    Copyright © 2023, ParallelChain Lab
3    Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
4*/
5
6//! Defines a struct as Execution State which is being updated during execution.
7//!
8//! This state is not as same as the concept of state in World State. Execution encapsulates the changing information
9//! during execution life-cycle. It is the state of execution model, but not referring to blockchain storage.
10
11use std::ops::{Deref, DerefMut};
12
13use pchain_world_state::{
14    keys::AppKey,
15    network::{constants::NETWORK_ADDRESS, network_account::NetworkAccountStorage},
16    storage::WorldStateStorage,
17};
18
19use crate::{transition::TransitionContext, types::BaseTx, BlockchainParams};
20
21/// ExecutionState is a collection of all useful information required to transit an state through Phases.
22/// Methods defined in ExecutionState do not directly update data to world state, but associate with the
23/// [crate::read_write_set::ReadWriteSet] in [TransitionContext] which serves as a data cache in between runtime and world state.
24pub(crate) struct ExecutionState<S>
25where
26    S: WorldStateStorage + Send + Sync + Clone + 'static,
27{
28    /*** Transaction ***/
29    /// Base Transaction as a transition input
30    pub tx: BaseTx,
31    /// size of serialized Transaction
32    pub tx_size: usize,
33    /// length of commands in the transaction
34    pub commands_len: usize,
35
36    /*** Blockchain ***/
37    /// Blockchain data as a transition input
38    pub bd: BlockchainParams,
39
40    /*** World State ***/
41    /// Transition Context which also contains world state as input
42    pub ctx: TransitionContext<S>,
43}
44
45impl<S> Deref for ExecutionState<S>
46where
47    S: WorldStateStorage + Send + Sync + Clone,
48{
49    type Target = TransitionContext<S>;
50
51    fn deref(&self) -> &Self::Target {
52        &self.ctx
53    }
54}
55
56impl<S> DerefMut for ExecutionState<S>
57where
58    S: WorldStateStorage + Send + Sync + Clone,
59{
60    fn deref_mut(&mut self) -> &mut Self::Target {
61        &mut self.ctx
62    }
63}
64
65/// ExecutionState implements NetworkAccountStorage with Read Write operations that:
66/// - Gas is charged in every Get/Contains/Set
67/// - Account Storage State (for app data) is opened in every Set to contract storage
68impl<S> NetworkAccountStorage for ExecutionState<S>
69where
70    S: WorldStateStorage + Send + Sync + Clone,
71{
72    fn get(&self, key: &[u8]) -> Option<Vec<u8>> {
73        self.app_data(NETWORK_ADDRESS, AppKey::new(key.to_vec())).0
74    }
75
76    fn contains(&self, key: &[u8]) -> bool {
77        self.contains_app_data(NETWORK_ADDRESS, AppKey::new(key.to_vec()))
78    }
79
80    fn set(&mut self, key: &[u8], value: Vec<u8>) {
81        self.set_app_data(NETWORK_ADDRESS, AppKey::new(key.to_vec()), value);
82    }
83
84    fn delete(&mut self, key: &[u8]) {
85        self.set_app_data(NETWORK_ADDRESS, AppKey::new(key.to_vec()), Vec::new());
86    }
87}