casper_storage/data_access_layer/
system_entity_registry.rs1use crate::tracking_copy::TrackingCopyError;
2use casper_types::{
3 system::{AUCTION, HANDLE_PAYMENT, MINT},
4 Digest, Key, ProtocolVersion, SystemHashRegistry,
5};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum SystemEntityRegistrySelector {
10 All,
12 ByName(String),
14}
15
16impl SystemEntityRegistrySelector {
17 pub fn all() -> Self {
19 SystemEntityRegistrySelector::All
20 }
21
22 pub fn mint() -> Self {
24 SystemEntityRegistrySelector::ByName(MINT.to_string())
25 }
26
27 pub fn auction() -> Self {
29 SystemEntityRegistrySelector::ByName(AUCTION.to_string())
30 }
31
32 pub fn handle_payment() -> Self {
34 SystemEntityRegistrySelector::ByName(HANDLE_PAYMENT.to_string())
35 }
36
37 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#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct SystemEntityRegistryRequest {
49 state_hash: Digest,
51 protocol_version: ProtocolVersion,
53 selector: SystemEntityRegistrySelector,
55 enable_addressable_entity: bool,
56}
57
58impl SystemEntityRegistryRequest {
59 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 pub fn state_hash(&self) -> Digest {
76 self.state_hash
77 }
78
79 pub fn selector(&self) -> &SystemEntityRegistrySelector {
81 &self.selector
82 }
83
84 pub fn protocol_version(&self) -> ProtocolVersion {
86 self.protocol_version
87 }
88
89 pub fn enable_addressable_entity(&self) -> bool {
91 self.enable_addressable_entity
92 }
93}
94
95#[derive(Debug, Clone, PartialEq, Eq)]
97pub enum SystemEntityRegistryPayload {
98 All(SystemHashRegistry),
100 EntityKey(Key),
102}
103
104#[derive(Debug)]
106pub enum SystemEntityRegistryResult {
107 RootNotFound,
109 SystemEntityRegistryNotFound,
113 NamedEntityNotFound(String),
115 Success {
117 selected: SystemEntityRegistrySelector,
119 payload: SystemEntityRegistryPayload,
121 },
122 Failure(TrackingCopyError),
124}
125
126impl SystemEntityRegistryResult {
127 pub fn is_success(&self) -> bool {
129 matches!(self, SystemEntityRegistryResult::Success { .. })
130 }
131
132 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}