dpp/data_contract/config/v1/
mod.rs1use crate::data_contract::config;
2use crate::data_contract::config::v0::{DataContractConfigGettersV0, DataContractConfigSettersV0};
3use crate::data_contract::config::{
4 DataContractConfig, DEFAULT_CONTRACT_CAN_BE_DELETED, DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
5 DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY, DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
6 DEFAULT_CONTRACT_KEEPS_HISTORY, DEFAULT_CONTRACT_MUTABILITY, DEFAULT_SIZED_INTEGER_TYPES,
7};
8use crate::data_contract::storage_requirements::keys_for_document_type::StorageKeyRequirements;
9#[cfg(feature = "json-conversion")]
10use crate::serialization::json_safe_fields;
11use crate::ProtocolError;
12use bincode::{Decode, Encode};
13use platform_value::btreemap_extensions::BTreeValueMapHelper;
14use platform_value::Value;
15use serde::{Deserialize, Serialize};
16use std::collections::BTreeMap;
17
18#[cfg_attr(feature = "json-conversion", json_safe_fields)]
19#[derive(Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq)]
20#[serde(rename_all = "camelCase", default)]
21pub struct DataContractConfigV1 {
22 pub can_be_deleted: bool,
26 pub readonly: bool,
29 pub keeps_history: bool,
31 pub documents_keep_history_contract_default: bool,
34 pub documents_mutable_contract_default: bool,
38 pub documents_can_be_deleted_contract_default: bool,
42 pub requires_identity_encryption_bounded_key: Option<StorageKeyRequirements>,
44 pub requires_identity_decryption_bounded_key: Option<StorageKeyRequirements>,
46 pub sized_integer_types: bool,
48}
49
50pub trait DataContractConfigGettersV1: DataContractConfigGettersV0 {
52 fn sized_integer_types(&self) -> bool;
54}
55
56pub trait DataContractConfigSettersV1: DataContractConfigSettersV0 {
58 fn set_sized_integer_types_enabled(&mut self, enable: bool);
60}
61
62impl Default for DataContractConfigV1 {
63 fn default() -> Self {
64 DataContractConfigV1 {
65 can_be_deleted: DEFAULT_CONTRACT_CAN_BE_DELETED,
66 readonly: !DEFAULT_CONTRACT_MUTABILITY,
67 keeps_history: DEFAULT_CONTRACT_KEEPS_HISTORY,
68 documents_keep_history_contract_default: DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY,
69 documents_mutable_contract_default: DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
70 documents_can_be_deleted_contract_default: DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
71 requires_identity_encryption_bounded_key: None,
72 requires_identity_decryption_bounded_key: None,
73 sized_integer_types: true,
74 }
75 }
76}
77
78impl DataContractConfigV1 {
79 pub fn from_value(value: Value) -> Result<Self, ProtocolError> {
80 platform_value::from_value(value).map_err(ProtocolError::ValueError)
81 }
82
83 pub fn default_with_version() -> DataContractConfig {
84 Self::default().into()
85 }
86}
87
88impl DataContractConfigV1 {
89 #[inline(always)]
108 pub(super) fn get_contract_configuration_properties_v1(
109 contract: &BTreeMap<String, Value>,
110 ) -> Result<DataContractConfigV1, ProtocolError> {
111 let keeps_history = contract
112 .get_optional_bool(config::property::KEEPS_HISTORY)?
113 .unwrap_or(DEFAULT_CONTRACT_KEEPS_HISTORY);
114 let can_be_deleted = contract
115 .get_optional_bool(config::property::CAN_BE_DELETED)?
116 .unwrap_or(DEFAULT_CONTRACT_CAN_BE_DELETED);
117
118 let readonly = contract
119 .get_optional_bool(config::property::READONLY)?
120 .unwrap_or(!DEFAULT_CONTRACT_MUTABILITY);
121
122 let documents_keep_history_contract_default = contract
123 .get_optional_bool(config::property::DOCUMENTS_KEEP_HISTORY_CONTRACT_DEFAULT)?
124 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY);
125
126 let documents_mutable_contract_default = contract
127 .get_optional_bool(config::property::DOCUMENTS_MUTABLE_CONTRACT_DEFAULT)?
128 .unwrap_or(DEFAULT_CONTRACT_DOCUMENT_MUTABILITY);
129
130 let documents_can_be_deleted_contract_default = contract
131 .get_optional_bool(config::property::DOCUMENTS_CAN_BE_DELETED_CONTRACT_DEFAULT)?
132 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED);
133
134 let requires_identity_encryption_bounded_key = contract
135 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
136 .map(|int| int.try_into())
137 .transpose()?;
138
139 let requires_identity_decryption_bounded_key = contract
140 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_DECRYPTION_BOUNDED_KEY)?
141 .map(|int| int.try_into())
142 .transpose()?;
143
144 let sized_integer_types = contract
145 .get_optional_bool(config::property::SIZED_INTEGER_TYPES)?
146 .unwrap_or(DEFAULT_SIZED_INTEGER_TYPES);
147
148 Ok(DataContractConfigV1 {
149 can_be_deleted,
150 readonly,
151 keeps_history,
152 documents_keep_history_contract_default,
153 documents_mutable_contract_default,
154 documents_can_be_deleted_contract_default,
155 requires_identity_encryption_bounded_key,
156 requires_identity_decryption_bounded_key,
157 sized_integer_types,
158 })
159 }
160}