casper_client/types/
contract_package.rs

1use std::fmt::{self, Display, Formatter};
2
3use itertools::Itertools;
4use serde::{Deserialize, Serialize};
5
6use casper_types::{ContractHash, URef};
7
8/// Contract version.
9#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
10#[serde(deny_unknown_fields)]
11pub struct ContractVersion {
12    protocol_version_major: u32,
13    contract_version: u32,
14    contract_hash: ContractHash,
15}
16
17impl ContractVersion {
18    /// Returns the major protocol version of the contract.
19    pub fn protocol_version_major(&self) -> u32 {
20        self.protocol_version_major
21    }
22
23    /// Returns the version of the contract.
24    pub fn contract_version(&self) -> u32 {
25        self.contract_version
26    }
27
28    /// Returns the hash used to identify the contract.
29    pub fn contract_hash(&self) -> &ContractHash {
30        &self.contract_hash
31    }
32}
33
34impl Display for ContractVersion {
35    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
36        write!(
37            formatter,
38            "contract-version {{ protocol-major {}, version {}, hash {} }}",
39            self.protocol_version_major, self.contract_version, self.contract_hash
40        )
41    }
42}
43
44/// Disabled contract version.
45#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
46#[serde(deny_unknown_fields)]
47pub struct DisabledVersion {
48    protocol_version_major: u32,
49    contract_version: u32,
50}
51
52impl DisabledVersion {
53    /// Returns the major protocol version of the disabled contract.
54    pub fn protocol_version_major(&self) -> u32 {
55        self.protocol_version_major
56    }
57
58    /// Returns the version of the disabled contract.
59    pub fn contract_version(&self) -> u32 {
60        self.contract_version
61    }
62}
63
64impl Display for DisabledVersion {
65    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
66        write!(
67            formatter,
68            "disabled contract-version {{ protocol-major {}, version {} }}",
69            self.protocol_version_major, self.contract_version
70        )
71    }
72}
73
74/// A set of `URef`s associated with a named user group.
75#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
76pub struct Group {
77    group: String,
78    keys: Vec<URef>,
79}
80
81impl Group {
82    /// Returns the name of the group.
83    pub fn group(&self) -> &str {
84        &self.group
85    }
86
87    /// Returns the URefs of the group.
88    pub fn keys(&self) -> impl Iterator<Item = &URef> {
89        self.keys.iter()
90    }
91}
92
93impl Display for Group {
94    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
95        write!(
96            formatter,
97            "group {{ {}: {{{}}} }}",
98            self.group,
99            self.keys().format(", ")
100        )
101    }
102}
103
104#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
105pub enum ContractPackageStatus {
106    Locked,
107    Unlocked,
108}
109
110/// Contract definition, metadata, and security container.
111#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
112#[serde(deny_unknown_fields)]
113pub struct ContractPackage {
114    access_key: URef,
115    versions: Vec<ContractVersion>,
116    disabled_versions: Vec<DisabledVersion>,
117    groups: Vec<Group>,
118    lock_status: ContractPackageStatus,
119}
120
121impl ContractPackage {
122    /// Returns the access key of the contract.
123    pub fn access_key(&self) -> &URef {
124        &self.access_key
125    }
126
127    /// Returns all versions of the contract.
128    pub fn versions(&self) -> impl Iterator<Item = &ContractVersion> {
129        self.versions.iter()
130    }
131
132    /// Returns the disabled versions of the contract.
133    pub fn disabled_versions(&self) -> impl Iterator<Item = &DisabledVersion> {
134        self.disabled_versions.iter()
135    }
136
137    /// Returns the user groups of the contract.
138    pub fn groups(&self) -> impl Iterator<Item = &Group> {
139        self.groups.iter()
140    }
141}
142
143impl Display for ContractPackage {
144    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
145        write!(
146            formatter,
147            "contract-package {{ {}, all versions {{{}}}, disabled versions {{{}}}, groups {{{}}} \
148            }}",
149            self.access_key,
150            self.versions().format(", "),
151            self.disabled_versions().format(", "),
152            self.groups().format(", "),
153        )
154    }
155}