casper_storage/data_access_layer/
block_global.rs

1use crate::tracking_copy::TrackingCopyError;
2use casper_types::{execution::Effects, BlockTime, Digest, ProtocolVersion};
3use std::fmt::{Display, Formatter};
4use thiserror::Error;
5
6/// Block global kind.
7#[derive(Debug, Copy, Clone, PartialEq, Eq)]
8pub enum BlockGlobalKind {
9    /// Block time.
10    BlockTime(BlockTime),
11    /// Message count.
12    MessageCount(u64),
13    /// Protocol version.
14    ProtocolVersion(ProtocolVersion),
15    /// Addressable entity flag.
16    AddressableEntity(bool),
17}
18
19impl Default for BlockGlobalKind {
20    fn default() -> Self {
21        BlockGlobalKind::BlockTime(BlockTime::default())
22    }
23}
24
25/// Block global request.
26#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
27pub struct BlockGlobalRequest {
28    state_hash: Digest,
29    protocol_version: ProtocolVersion,
30    block_global_kind: BlockGlobalKind,
31}
32
33impl BlockGlobalRequest {
34    /// Returns block time setting request.
35    pub fn block_time(
36        state_hash: Digest,
37        protocol_version: ProtocolVersion,
38        block_time: BlockTime,
39    ) -> Self {
40        let block_global_kind = BlockGlobalKind::BlockTime(block_time);
41        BlockGlobalRequest {
42            state_hash,
43            protocol_version,
44            block_global_kind,
45        }
46    }
47
48    /// Returns protocol version setting request.
49    pub fn set_protocol_version(state_hash: Digest, protocol_version: ProtocolVersion) -> Self {
50        let block_global_kind = BlockGlobalKind::ProtocolVersion(protocol_version);
51        BlockGlobalRequest {
52            state_hash,
53            protocol_version,
54            block_global_kind,
55        }
56    }
57
58    /// Returns addressable entity flag setting request.
59    pub fn set_addressable_entity(
60        state_hash: Digest,
61        protocol_version: ProtocolVersion,
62        addressable_entity: bool,
63    ) -> Self {
64        let block_global_kind = BlockGlobalKind::AddressableEntity(addressable_entity);
65        BlockGlobalRequest {
66            state_hash,
67            protocol_version,
68            block_global_kind,
69        }
70    }
71
72    /// Returns state hash.
73    pub fn state_hash(&self) -> Digest {
74        self.state_hash
75    }
76
77    /// Returns protocol version.
78    pub fn protocol_version(&self) -> ProtocolVersion {
79        self.protocol_version
80    }
81
82    /// Returns block global kind.
83    pub fn block_global_kind(&self) -> BlockGlobalKind {
84        self.block_global_kind
85    }
86}
87
88/// Block global result.
89#[derive(Error, Debug, Clone)]
90pub enum BlockGlobalResult {
91    /// Returned if a passed state root hash is not found.
92    RootNotFound,
93    /// Failed to store block global data.
94    Failure(TrackingCopyError),
95    /// Successfully stored block global data.
96    Success {
97        /// State hash after data committed to the global state.
98        post_state_hash: Digest,
99        /// The effects of putting the data to global state.
100        effects: Box<Effects>,
101    },
102}
103
104impl Display for BlockGlobalResult {
105    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
106        match self {
107            BlockGlobalResult::RootNotFound => f.write_str("root not found"),
108            BlockGlobalResult::Failure(tce) => {
109                write!(f, "failed {}", tce)
110            }
111            BlockGlobalResult::Success { .. } => f.write_str("success"),
112        }
113    }
114}