casper_storage/data_access_layer/
protocol_upgrade.rs

1use casper_types::{execution::Effects, Digest, ProtocolUpgradeConfig};
2
3use crate::system::protocol_upgrade::ProtocolUpgradeError;
4
5/// Request to upgrade the protocol.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ProtocolUpgradeRequest {
8    config: ProtocolUpgradeConfig,
9}
10
11impl ProtocolUpgradeRequest {
12    /// Creates a new instance of ProtocolUpgradeRequest.
13    pub fn new(config: ProtocolUpgradeConfig) -> Self {
14        ProtocolUpgradeRequest { config }
15    }
16
17    /// Get the protocol upgrade config.
18    pub fn config(&self) -> &ProtocolUpgradeConfig {
19        &self.config
20    }
21
22    /// Get the pre_state_hash to apply protocol upgrade to.
23    pub fn pre_state_hash(&self) -> Digest {
24        self.config.pre_state_hash()
25    }
26}
27
28/// Response to attempt to upgrade the protocol.
29#[derive(Debug, Clone)]
30pub enum ProtocolUpgradeResult {
31    /// Global state root not found.
32    RootNotFound,
33    /// Protocol upgraded successfully.
34    Success {
35        /// State hash after protocol upgrade is committed to the global state.
36        post_state_hash: Digest,
37        /// Effects of protocol upgrade.
38        effects: Effects,
39    },
40    /// Failed to upgrade protocol.
41    Failure(ProtocolUpgradeError),
42}
43
44impl ProtocolUpgradeResult {
45    /// Is success.
46    pub fn is_success(&self) -> bool {
47        matches!(self, ProtocolUpgradeResult::Success { .. })
48    }
49
50    /// Is an error
51    pub fn is_err(&self) -> bool {
52        match self {
53            ProtocolUpgradeResult::RootNotFound | ProtocolUpgradeResult::Failure(_) => true,
54            ProtocolUpgradeResult::Success { .. } => false,
55        }
56    }
57}
58
59impl From<ProtocolUpgradeError> for ProtocolUpgradeResult {
60    fn from(err: ProtocolUpgradeError) -> Self {
61        ProtocolUpgradeResult::Failure(err)
62    }
63}