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 default_with_version() -> DataContractConfig {
80 Self::default().into()
81 }
82}
83
84impl DataContractConfigV1 {
85 #[inline(always)]
104 pub(super) fn get_contract_configuration_properties_v1(
105 contract: &BTreeMap<String, Value>,
106 ) -> Result<DataContractConfigV1, ProtocolError> {
107 let keeps_history = contract
108 .get_optional_bool(config::property::KEEPS_HISTORY)?
109 .unwrap_or(DEFAULT_CONTRACT_KEEPS_HISTORY);
110 let can_be_deleted = contract
111 .get_optional_bool(config::property::CAN_BE_DELETED)?
112 .unwrap_or(DEFAULT_CONTRACT_CAN_BE_DELETED);
113
114 let readonly = contract
115 .get_optional_bool(config::property::READONLY)?
116 .unwrap_or(!DEFAULT_CONTRACT_MUTABILITY);
117
118 let documents_keep_history_contract_default = contract
119 .get_optional_bool(config::property::DOCUMENTS_KEEP_HISTORY_CONTRACT_DEFAULT)?
120 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY);
121
122 let documents_mutable_contract_default = contract
123 .get_optional_bool(config::property::DOCUMENTS_MUTABLE_CONTRACT_DEFAULT)?
124 .unwrap_or(DEFAULT_CONTRACT_DOCUMENT_MUTABILITY);
125
126 let documents_can_be_deleted_contract_default = contract
127 .get_optional_bool(config::property::DOCUMENTS_CAN_BE_DELETED_CONTRACT_DEFAULT)?
128 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED);
129
130 let requires_identity_encryption_bounded_key = contract
131 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
132 .map(|int| int.try_into())
133 .transpose()?;
134
135 let requires_identity_decryption_bounded_key = contract
136 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_DECRYPTION_BOUNDED_KEY)?
137 .map(|int| int.try_into())
138 .transpose()?;
139
140 let sized_integer_types = contract
141 .get_optional_bool(config::property::SIZED_INTEGER_TYPES)?
142 .unwrap_or(DEFAULT_SIZED_INTEGER_TYPES);
143
144 Ok(DataContractConfigV1 {
145 can_be_deleted,
146 readonly,
147 keeps_history,
148 documents_keep_history_contract_default,
149 documents_mutable_contract_default,
150 documents_can_be_deleted_contract_default,
151 requires_identity_encryption_bounded_key,
152 requires_identity_decryption_bounded_key,
153 sized_integer_types,
154 })
155 }
156}