use crate::tracking_copy::TrackingCopyError;
use casper_types::{
system::{AUCTION, HANDLE_PAYMENT, MINT},
Digest, Key, ProtocolVersion, SystemHashRegistry,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SystemEntityRegistrySelector {
All,
ByName(String),
}
impl SystemEntityRegistrySelector {
pub fn all() -> Self {
SystemEntityRegistrySelector::All
}
pub fn mint() -> Self {
SystemEntityRegistrySelector::ByName(MINT.to_string())
}
pub fn auction() -> Self {
SystemEntityRegistrySelector::ByName(AUCTION.to_string())
}
pub fn handle_payment() -> Self {
SystemEntityRegistrySelector::ByName(HANDLE_PAYMENT.to_string())
}
pub fn name(&self) -> Option<String> {
match self {
SystemEntityRegistrySelector::All => None,
SystemEntityRegistrySelector::ByName(name) => Some(name.clone()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SystemEntityRegistryRequest {
state_hash: Digest,
protocol_version: ProtocolVersion,
selector: SystemEntityRegistrySelector,
enable_addressable_entity: bool,
}
impl SystemEntityRegistryRequest {
pub fn new(
state_hash: Digest,
protocol_version: ProtocolVersion,
selector: SystemEntityRegistrySelector,
enable_addressable_entity: bool,
) -> Self {
SystemEntityRegistryRequest {
state_hash,
protocol_version,
selector,
enable_addressable_entity,
}
}
pub fn state_hash(&self) -> Digest {
self.state_hash
}
pub fn selector(&self) -> &SystemEntityRegistrySelector {
&self.selector
}
pub fn protocol_version(&self) -> ProtocolVersion {
self.protocol_version
}
pub fn enable_addressable_entity(&self) -> bool {
self.enable_addressable_entity
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SystemEntityRegistryPayload {
All(SystemHashRegistry),
EntityKey(Key),
}
#[derive(Debug)]
pub enum SystemEntityRegistryResult {
RootNotFound,
SystemEntityRegistryNotFound,
NamedEntityNotFound(String),
Success {
selected: SystemEntityRegistrySelector,
payload: SystemEntityRegistryPayload,
},
Failure(TrackingCopyError),
}
impl SystemEntityRegistryResult {
pub fn is_success(&self) -> bool {
matches!(self, SystemEntityRegistryResult::Success { .. })
}
pub fn as_registry_payload(&self) -> Result<SystemEntityRegistryPayload, String> {
match self {
SystemEntityRegistryResult::RootNotFound => Err("Root not found".to_string()),
SystemEntityRegistryResult::SystemEntityRegistryNotFound => {
Err("System entity registry not found".to_string())
}
SystemEntityRegistryResult::NamedEntityNotFound(name) => {
Err(format!("Named entity not found: {:?}", name))
}
SystemEntityRegistryResult::Failure(tce) => Err(format!("{:?}", tce)),
SystemEntityRegistryResult::Success { payload, .. } => Ok(payload.clone()),
}
}
}