dpp/data_contract/config/
mod.rs

1mod fields;
2mod methods;
3pub mod v0;
4pub mod v1;
5
6use crate::data_contract::config::v1::{
7    DataContractConfigGettersV1, DataContractConfigSettersV1, DataContractConfigV1,
8};
9use crate::data_contract::storage_requirements::keys_for_document_type::StorageKeyRequirements;
10#[cfg(feature = "json-conversion")]
11use crate::serialization::JsonConvertible;
12use crate::version::PlatformVersion;
13use crate::ProtocolError;
14use bincode::{Decode, Encode};
15use derive_more::From;
16pub use fields::*;
17use platform_value::Value;
18use serde::{Deserialize, Serialize};
19use std::collections::BTreeMap;
20use v0::{DataContractConfigGettersV0, DataContractConfigSettersV0, DataContractConfigV0};
21
22#[cfg_attr(feature = "json-conversion", derive(JsonConvertible))]
23#[derive(Serialize, Deserialize, Encode, Decode, Debug, Clone, Copy, PartialEq, Eq, From)]
24#[serde(tag = "$formatVersion")]
25pub enum DataContractConfig {
26    #[serde(rename = "0")]
27    V0(DataContractConfigV0),
28    #[serde(rename = "1")]
29    V1(DataContractConfigV1),
30}
31
32impl DataContractConfig {
33    pub fn version(&self) -> u16 {
34        match self {
35            DataContractConfig::V0(_) => 0,
36            DataContractConfig::V1(_) => 1,
37        }
38    }
39
40    pub fn default_for_version(
41        platform_version: &PlatformVersion,
42    ) -> Result<DataContractConfig, ProtocolError> {
43        match platform_version
44            .dpp
45            .contract_versions
46            .config
47            .default_current_version
48        {
49            0 => Ok(DataContractConfigV0::default().into()),
50            1 => Ok(DataContractConfigV1::default().into()),
51            version => Err(ProtocolError::UnknownVersionMismatch {
52                method: "DataContractConfig::default_for_version".to_string(),
53                known_versions: vec![0, 1],
54                received: version,
55            }),
56        }
57    }
58
59    /// Adjusts the current `DataContractConfig` to be valid for the provided platform version.
60    ///
61    /// This replaces the internal version with the `default_current_version` defined in the platform version's
62    /// feature bounds for contract config.
63    pub fn config_valid_for_platform_version(
64        self,
65        platform_version: &PlatformVersion,
66    ) -> DataContractConfig {
67        match self {
68            DataContractConfig::V0(v0) => DataContractConfig::V0(v0),
69            DataContractConfig::V1(v1) => {
70                if platform_version.dpp.contract_versions.config.max_version == 0 {
71                    DataContractConfig::V0(v1.into())
72                } else {
73                    self
74                }
75            }
76        }
77    }
78
79    pub fn from_value(
80        value: Value,
81        platform_version: &PlatformVersion,
82    ) -> Result<DataContractConfig, ProtocolError> {
83        match platform_version
84            .dpp
85            .contract_versions
86            .config
87            .default_current_version
88        {
89            0 => {
90                let config: DataContractConfigV0 = platform_value::from_value(value)?;
91                Ok(config.into())
92            }
93            1 => {
94                let config: DataContractConfigV1 = platform_value::from_value(value)?;
95                Ok(config.into())
96            }
97            version => Err(ProtocolError::UnknownVersionMismatch {
98                method: "DataContractConfig::from_value".to_string(),
99                known_versions: vec![0, 1],
100                received: version,
101            }),
102        }
103    }
104
105    // TODO: Remove, it's not using
106    /// Retrieve contract configuration properties.
107    ///
108    /// This method takes a BTreeMap representing a contract and retrieves
109    /// the configuration properties based on the values found in the map.
110    ///
111    /// The process of retrieving contract configuration properties is versioned,
112    /// and the version is determined by the platform version parameter.
113    /// If the version is not supported, an error is returned.
114    ///
115    /// # Parameters
116    ///
117    /// * `contract`: BTreeMap representing the contract.
118    /// * `platform_version`: The platform version being used.
119    ///
120    /// # Returns
121    ///
122    /// * `Result<ContractConfig, ProtocolError>`: On success, a ContractConfig.
123    ///   On failure, a ProtocolError.
124    pub(in crate::data_contract) fn get_contract_configuration_properties(
125        contract: &BTreeMap<String, Value>,
126        platform_version: &PlatformVersion,
127    ) -> Result<DataContractConfig, ProtocolError> {
128        match platform_version
129            .dpp
130            .contract_versions
131            .config
132            .default_current_version
133        {
134            0 => Ok(
135                DataContractConfigV0::get_contract_configuration_properties_v0(contract)?.into(),
136            ),
137            1 => Ok(
138                DataContractConfigV1::get_contract_configuration_properties_v1(contract)?.into(),
139            ),
140            version => Err(ProtocolError::UnknownVersionMismatch {
141                method: "DataContractConfig::get_contract_configuration_properties".to_string(),
142                known_versions: vec![0, 1],
143                received: version,
144            }),
145        }
146    }
147}
148
149impl DataContractConfigGettersV0 for DataContractConfig {
150    fn can_be_deleted(&self) -> bool {
151        match self {
152            DataContractConfig::V0(v0) => v0.can_be_deleted,
153            DataContractConfig::V1(v1) => v1.can_be_deleted,
154        }
155    }
156
157    fn readonly(&self) -> bool {
158        match self {
159            DataContractConfig::V0(v0) => v0.readonly,
160            DataContractConfig::V1(v1) => v1.readonly,
161        }
162    }
163
164    fn keeps_history(&self) -> bool {
165        match self {
166            DataContractConfig::V0(v0) => v0.keeps_history,
167            DataContractConfig::V1(v1) => v1.keeps_history,
168        }
169    }
170
171    fn documents_keep_history_contract_default(&self) -> bool {
172        match self {
173            DataContractConfig::V0(v0) => v0.documents_keep_history_contract_default,
174            DataContractConfig::V1(v1) => v1.documents_keep_history_contract_default,
175        }
176    }
177
178    fn documents_mutable_contract_default(&self) -> bool {
179        match self {
180            DataContractConfig::V0(v0) => v0.documents_mutable_contract_default,
181            DataContractConfig::V1(v1) => v1.documents_mutable_contract_default,
182        }
183    }
184
185    fn documents_can_be_deleted_contract_default(&self) -> bool {
186        match self {
187            DataContractConfig::V0(v0) => v0.documents_can_be_deleted_contract_default,
188            DataContractConfig::V1(v1) => v1.documents_can_be_deleted_contract_default,
189        }
190    }
191
192    /// Encryption key storage requirements
193    fn requires_identity_encryption_bounded_key(&self) -> Option<StorageKeyRequirements> {
194        match self {
195            DataContractConfig::V0(v0) => v0.requires_identity_encryption_bounded_key,
196            DataContractConfig::V1(v1) => v1.requires_identity_encryption_bounded_key,
197        }
198    }
199
200    /// Decryption key storage requirements
201    fn requires_identity_decryption_bounded_key(&self) -> Option<StorageKeyRequirements> {
202        match self {
203            DataContractConfig::V0(v0) => v0.requires_identity_decryption_bounded_key,
204            DataContractConfig::V1(v1) => v1.requires_identity_decryption_bounded_key,
205        }
206    }
207}
208
209impl DataContractConfigSettersV0 for DataContractConfig {
210    fn set_can_be_deleted(&mut self, value: bool) {
211        match self {
212            DataContractConfig::V0(v0) => v0.can_be_deleted = value,
213            DataContractConfig::V1(v1) => v1.can_be_deleted = value,
214        }
215    }
216
217    fn set_readonly(&mut self, value: bool) {
218        match self {
219            DataContractConfig::V0(v0) => v0.readonly = value,
220            DataContractConfig::V1(v1) => v1.readonly = value,
221        }
222    }
223
224    fn set_keeps_history(&mut self, value: bool) {
225        match self {
226            DataContractConfig::V0(v0) => v0.keeps_history = value,
227            DataContractConfig::V1(v1) => v1.keeps_history = value,
228        }
229    }
230
231    fn set_documents_keep_history_contract_default(&mut self, value: bool) {
232        match self {
233            DataContractConfig::V0(v0) => v0.documents_keep_history_contract_default = value,
234            DataContractConfig::V1(v1) => v1.documents_keep_history_contract_default = value,
235        }
236    }
237
238    fn set_documents_can_be_deleted_contract_default(&mut self, value: bool) {
239        match self {
240            DataContractConfig::V0(v0) => v0.documents_can_be_deleted_contract_default = value,
241            DataContractConfig::V1(v1) => v1.documents_can_be_deleted_contract_default = value,
242        }
243    }
244
245    fn set_documents_mutable_contract_default(&mut self, value: bool) {
246        match self {
247            DataContractConfig::V0(v0) => v0.documents_mutable_contract_default = value,
248            DataContractConfig::V1(v1) => v1.documents_mutable_contract_default = value,
249        }
250    }
251
252    fn set_requires_identity_encryption_bounded_key(
253        &mut self,
254        value: Option<StorageKeyRequirements>,
255    ) {
256        match self {
257            DataContractConfig::V0(v0) => v0.requires_identity_encryption_bounded_key = value,
258            DataContractConfig::V1(v1) => v1.requires_identity_encryption_bounded_key = value,
259        }
260    }
261
262    fn set_requires_identity_decryption_bounded_key(
263        &mut self,
264        value: Option<StorageKeyRequirements>,
265    ) {
266        match self {
267            DataContractConfig::V0(v0) => v0.requires_identity_decryption_bounded_key = value,
268            DataContractConfig::V1(v1) => v1.requires_identity_decryption_bounded_key = value,
269        }
270    }
271}
272
273impl DataContractConfigGettersV1 for DataContractConfig {
274    fn sized_integer_types(&self) -> bool {
275        match self {
276            DataContractConfig::V0(_) => false,
277            DataContractConfig::V1(v1) => v1.sized_integer_types,
278        }
279    }
280}
281
282impl DataContractConfigSettersV1 for DataContractConfig {
283    fn set_sized_integer_types_enabled(&mut self, enable: bool) {
284        match self {
285            DataContractConfig::V0(_) => {}
286            DataContractConfig::V1(v1) => v1.sized_integer_types = enable,
287        }
288    }
289}