casper_storage/data_access_layer/
system_entity_registry.rs

1use crate::tracking_copy::TrackingCopyError;
2use casper_types::{
3    system::{AUCTION, HANDLE_PAYMENT, MINT},
4    Digest, Key, ProtocolVersion, SystemHashRegistry,
5};
6
7/// Used to specify is the requestor wants the registry itself or a named entry within it.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum SystemEntityRegistrySelector {
10    /// Requests all system entity entries.
11    All,
12    /// Requests system entity by name.
13    ByName(String),
14}
15
16impl SystemEntityRegistrySelector {
17    /// Create instance asking for the entire registry.
18    pub fn all() -> Self {
19        SystemEntityRegistrySelector::All
20    }
21
22    /// Create instance asking for mint.
23    pub fn mint() -> Self {
24        SystemEntityRegistrySelector::ByName(MINT.to_string())
25    }
26
27    /// Create instance asking for auction.
28    pub fn auction() -> Self {
29        SystemEntityRegistrySelector::ByName(AUCTION.to_string())
30    }
31
32    /// Create instance asking for handle payment.
33    pub fn handle_payment() -> Self {
34        SystemEntityRegistrySelector::ByName(HANDLE_PAYMENT.to_string())
35    }
36
37    /// Name of selected entity, if any.
38    pub fn name(&self) -> Option<String> {
39        match self {
40            SystemEntityRegistrySelector::All => None,
41            SystemEntityRegistrySelector::ByName(name) => Some(name.clone()),
42        }
43    }
44}
45
46/// Represents a request to obtain the system entity registry or an entry within it.
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct SystemEntityRegistryRequest {
49    /// State root hash.
50    state_hash: Digest,
51    /// Protocol version.
52    protocol_version: ProtocolVersion,
53    /// Selector.
54    selector: SystemEntityRegistrySelector,
55    enable_addressable_entity: bool,
56}
57
58impl SystemEntityRegistryRequest {
59    /// Create new request.
60    pub fn new(
61        state_hash: Digest,
62        protocol_version: ProtocolVersion,
63        selector: SystemEntityRegistrySelector,
64        enable_addressable_entity: bool,
65    ) -> Self {
66        SystemEntityRegistryRequest {
67            state_hash,
68            protocol_version,
69            selector,
70            enable_addressable_entity,
71        }
72    }
73
74    /// Returns the state hash.
75    pub fn state_hash(&self) -> Digest {
76        self.state_hash
77    }
78
79    /// Returns the current selector.
80    pub fn selector(&self) -> &SystemEntityRegistrySelector {
81        &self.selector
82    }
83
84    /// Protocol version.
85    pub fn protocol_version(&self) -> ProtocolVersion {
86        self.protocol_version
87    }
88
89    /// Enable the addressable entity and migrate accounts/contracts to entities.
90    pub fn enable_addressable_entity(&self) -> bool {
91        self.enable_addressable_entity
92    }
93}
94
95/// The payload of a successful request.
96#[derive(Debug, Clone, PartialEq, Eq)]
97pub enum SystemEntityRegistryPayload {
98    /// All registry entries.
99    All(SystemHashRegistry),
100    /// Specific system entity registry entry.
101    EntityKey(Key),
102}
103
104/// The result of a system entity registry request.
105#[derive(Debug)]
106pub enum SystemEntityRegistryResult {
107    /// Invalid state root hash.
108    RootNotFound,
109    /// The system contract registry was not found. This is a valid outcome
110    /// on older networks, which did not have the system contract registry prior
111    /// to protocol version 1.4
112    SystemEntityRegistryNotFound,
113    /// The named entity was not found in the registry.
114    NamedEntityNotFound(String),
115    /// Successful request.
116    Success {
117        /// What was asked for.
118        selected: SystemEntityRegistrySelector,
119        /// The payload asked for.
120        payload: SystemEntityRegistryPayload,
121    },
122    /// Failed to get requested data.
123    Failure(TrackingCopyError),
124}
125
126impl SystemEntityRegistryResult {
127    /// Is success.
128    pub fn is_success(&self) -> bool {
129        matches!(self, SystemEntityRegistryResult::Success { .. })
130    }
131
132    /// As registry payload.
133    pub fn as_registry_payload(&self) -> Result<SystemEntityRegistryPayload, String> {
134        match self {
135            SystemEntityRegistryResult::RootNotFound => Err("Root not found".to_string()),
136            SystemEntityRegistryResult::SystemEntityRegistryNotFound => {
137                Err("System entity registry not found".to_string())
138            }
139            SystemEntityRegistryResult::NamedEntityNotFound(name) => {
140                Err(format!("Named entity not found: {:?}", name))
141            }
142            SystemEntityRegistryResult::Failure(tce) => Err(format!("{:?}", tce)),
143            SystemEntityRegistryResult::Success { payload, .. } => Ok(payload.clone()),
144        }
145    }
146}