Skip to content

Commit d8951d4

Browse files
fix(platform)!: load data contracts in their respective versions (#2644)
1 parent 80f8be4 commit d8951d4

File tree

36 files changed

+485
-145
lines changed

36 files changed

+485
-145
lines changed

packages/rs-dpp/src/data_contract/config/mod.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ impl DataContractConfig {
3030
pub fn default_for_version(
3131
platform_version: &PlatformVersion,
3232
) -> Result<DataContractConfig, ProtocolError> {
33-
match platform_version.dpp.contract_versions.config {
33+
match platform_version
34+
.dpp
35+
.contract_versions
36+
.config
37+
.default_current_version
38+
{
3439
0 => Ok(DataContractConfigV0::default().into()),
3540
1 => Ok(DataContractConfigV1::default().into()),
3641
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -41,11 +46,36 @@ impl DataContractConfig {
4146
}
4247
}
4348

49+
/// Adjusts the current `DataContractConfig` to be valid for the provided platform version.
50+
///
51+
/// This replaces the internal version with the `default_current_version` defined in the platform version's
52+
/// feature bounds for contract config.
53+
pub fn config_valid_for_platform_version(
54+
self,
55+
platform_version: &PlatformVersion,
56+
) -> DataContractConfig {
57+
match self {
58+
DataContractConfig::V0(v0) => DataContractConfig::V0(v0),
59+
DataContractConfig::V1(v1) => {
60+
if platform_version.dpp.contract_versions.config.max_version == 0 {
61+
DataContractConfig::V0(v1.into())
62+
} else {
63+
self
64+
}
65+
}
66+
}
67+
}
68+
4469
pub fn from_value(
4570
value: Value,
4671
platform_version: &PlatformVersion,
4772
) -> Result<DataContractConfig, ProtocolError> {
48-
match platform_version.dpp.contract_versions.config {
73+
match platform_version
74+
.dpp
75+
.contract_versions
76+
.config
77+
.default_current_version
78+
{
4979
0 => {
5080
let config: DataContractConfigV0 = platform_value::from_value(value)?;
5181
Ok(config.into())
@@ -85,7 +115,12 @@ impl DataContractConfig {
85115
contract: &BTreeMap<String, Value>,
86116
platform_version: &PlatformVersion,
87117
) -> Result<DataContractConfig, ProtocolError> {
88-
match platform_version.dpp.contract_versions.config {
118+
match platform_version
119+
.dpp
120+
.contract_versions
121+
.config
122+
.default_current_version
123+
{
89124
0 => Ok(
90125
DataContractConfigV0::get_contract_configuration_properties_v0(contract)?.into(),
91126
),

packages/rs-dpp/src/data_contract/config/v0/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::data_contract::config;
2+
use crate::data_contract::config::v1::DataContractConfigV1;
23
use crate::data_contract::config::{
34
DataContractConfig, DEFAULT_CONTRACT_CAN_BE_DELETED, DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
45
DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY, DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
@@ -192,3 +193,21 @@ impl DataContractConfigV0 {
192193
})
193194
}
194195
}
196+
197+
impl From<DataContractConfigV1> for DataContractConfigV0 {
198+
fn from(value: DataContractConfigV1) -> Self {
199+
DataContractConfigV0 {
200+
can_be_deleted: value.can_be_deleted,
201+
readonly: value.readonly,
202+
keeps_history: value.keeps_history,
203+
documents_keep_history_contract_default: value.documents_keep_history_contract_default,
204+
documents_mutable_contract_default: value.documents_mutable_contract_default,
205+
documents_can_be_deleted_contract_default: value
206+
.documents_can_be_deleted_contract_default,
207+
requires_identity_encryption_bounded_key: value
208+
.requires_identity_encryption_bounded_key,
209+
requires_identity_decryption_bounded_key: value
210+
.requires_identity_decryption_bounded_key,
211+
}
212+
}
213+
}

packages/rs-dpp/src/data_contract/serialized_version/mod.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::ProtocolError;
1717
use bincode::{Decode, Encode};
1818
use derive_more::From;
1919
use platform_value::{Identifier, Value};
20-
use platform_version::TryFromPlatformVersioned;
20+
use platform_version::{IntoPlatformVersioned, TryFromPlatformVersioned};
2121
use platform_versioning::PlatformVersioned;
2222
#[cfg(feature = "data-contract-serde-conversion")]
2323
use serde::{Deserialize, Serialize};
@@ -160,11 +160,13 @@ impl TryFromPlatformVersioned<DataContractV0> for DataContractInSerializationFor
160160
.default_current_version
161161
{
162162
0 => {
163-
let v0_format: DataContractInSerializationFormatV0 = DataContract::V0(value).into();
163+
let v0_format: DataContractInSerializationFormatV0 =
164+
DataContract::V0(value).into_platform_versioned(platform_version);
164165
Ok(v0_format.into())
165166
}
166167
1 => {
167-
let v1_format: DataContractInSerializationFormatV1 = DataContract::V0(value).into();
168+
let v1_format: DataContractInSerializationFormatV1 =
169+
DataContract::V0(value).into_platform_versioned(platform_version);
168170
Ok(v1_format.into())
169171
}
170172
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -191,12 +193,12 @@ impl TryFromPlatformVersioned<&DataContractV0> for DataContractInSerializationFo
191193
{
192194
0 => {
193195
let v0_format: DataContractInSerializationFormatV0 =
194-
DataContract::V0(value.to_owned()).into();
196+
DataContract::V0(value.to_owned()).into_platform_versioned(platform_version);
195197
Ok(v0_format.into())
196198
}
197199
1 => {
198200
let v1_format: DataContractInSerializationFormatV1 =
199-
DataContract::V0(value.to_owned()).into();
201+
DataContract::V0(value.to_owned()).into_platform_versioned(platform_version);
200202
Ok(v1_format.into())
201203
}
202204
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -222,11 +224,13 @@ impl TryFromPlatformVersioned<DataContractV1> for DataContractInSerializationFor
222224
.default_current_version
223225
{
224226
0 => {
225-
let v0_format: DataContractInSerializationFormatV0 = DataContract::V1(value).into();
227+
let v0_format: DataContractInSerializationFormatV0 =
228+
DataContract::V1(value).into_platform_versioned(platform_version);
226229
Ok(v0_format.into())
227230
}
228231
1 => {
229-
let v1_format: DataContractInSerializationFormatV1 = DataContract::V1(value).into();
232+
let v1_format: DataContractInSerializationFormatV1 =
233+
DataContract::V1(value).into_platform_versioned(platform_version);
230234
Ok(v1_format.into())
231235
}
232236
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -253,12 +257,12 @@ impl TryFromPlatformVersioned<&DataContractV1> for DataContractInSerializationFo
253257
{
254258
0 => {
255259
let v0_format: DataContractInSerializationFormatV0 =
256-
DataContract::V1(value.to_owned()).into();
260+
DataContract::V1(value.to_owned()).into_platform_versioned(platform_version);
257261
Ok(v0_format.into())
258262
}
259263
1 => {
260264
let v1_format: DataContractInSerializationFormatV1 =
261-
DataContract::V1(value.to_owned()).into();
265+
DataContract::V1(value.to_owned()).into_platform_versioned(platform_version);
262266
Ok(v1_format.into())
263267
}
264268
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -284,11 +288,13 @@ impl TryFromPlatformVersioned<&DataContract> for DataContractInSerializationForm
284288
.default_current_version
285289
{
286290
0 => {
287-
let v0_format: DataContractInSerializationFormatV0 = value.clone().into();
291+
let v0_format: DataContractInSerializationFormatV0 =
292+
value.clone().into_platform_versioned(platform_version);
288293
Ok(v0_format.into())
289294
}
290295
1 => {
291-
let v1_format: DataContractInSerializationFormatV1 = value.clone().into();
296+
let v1_format: DataContractInSerializationFormatV1 =
297+
value.clone().into_platform_versioned(platform_version);
292298
Ok(v1_format.into())
293299
}
294300
version => Err(ProtocolError::UnknownVersionMismatch {
@@ -314,11 +320,13 @@ impl TryFromPlatformVersioned<DataContract> for DataContractInSerializationForma
314320
.default_current_version
315321
{
316322
0 => {
317-
let v0_format: DataContractInSerializationFormatV0 = value.into();
323+
let v0_format: DataContractInSerializationFormatV0 =
324+
value.into_platform_versioned(platform_version);
318325
Ok(v0_format.into())
319326
}
320327
1 => {
321-
let v1_format: DataContractInSerializationFormatV1 = value.into();
328+
let v1_format: DataContractInSerializationFormatV1 =
329+
value.into_platform_versioned(platform_version);
322330
Ok(v1_format.into())
323331
}
324332
version => Err(ProtocolError::UnknownVersionMismatch {

packages/rs-dpp/src/data_contract/serialized_version/v0/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::data_contract::v1::DataContractV1;
77
use crate::data_contract::{DataContract, DefinitionName, DocumentName};
88
use bincode::{Decode, Encode};
99
use platform_value::{Identifier, Value};
10+
use platform_version::version::PlatformVersion;
11+
use platform_version::FromPlatformVersioned;
1012
use serde::{Deserialize, Serialize};
1113
use std::collections::BTreeMap;
1214

@@ -85,3 +87,60 @@ impl From<DataContract> for DataContractInSerializationFormatV0 {
8587
}
8688
}
8789
}
90+
91+
impl FromPlatformVersioned<DataContract> for DataContractInSerializationFormatV0 {
92+
fn from_platform_versioned(value: DataContract, platform_version: &PlatformVersion) -> Self {
93+
match value {
94+
DataContract::V0(v0) => {
95+
let DataContractV0 {
96+
id,
97+
config,
98+
version,
99+
owner_id,
100+
schema_defs,
101+
document_types,
102+
..
103+
} = v0;
104+
105+
let config = config.config_valid_for_platform_version(platform_version);
106+
107+
DataContractInSerializationFormatV0 {
108+
id,
109+
config,
110+
version,
111+
owner_id,
112+
document_schemas: document_types
113+
.into_iter()
114+
.map(|(key, document_type)| (key, document_type.schema_owned()))
115+
.collect(),
116+
schema_defs,
117+
}
118+
}
119+
DataContract::V1(v1) => {
120+
let DataContractV1 {
121+
id,
122+
config,
123+
version,
124+
owner_id,
125+
schema_defs,
126+
document_types,
127+
..
128+
} = v1;
129+
130+
let config = config.config_valid_for_platform_version(platform_version);
131+
132+
DataContractInSerializationFormatV0 {
133+
id,
134+
config,
135+
version,
136+
owner_id,
137+
document_schemas: document_types
138+
.into_iter()
139+
.map(|(key, document_type)| (key, document_type.schema_owned()))
140+
.collect(),
141+
schema_defs,
142+
}
143+
}
144+
}
145+
}
146+
}

packages/rs-dpp/src/data_contract/serialized_version/v1/mod.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::identity::TimestampMillis;
1414
use crate::prelude::BlockHeight;
1515
use bincode::{Decode, Encode};
1616
use platform_value::{Identifier, Value};
17+
use platform_version::version::PlatformVersion;
18+
use platform_version::FromPlatformVersioned;
1719
use serde::{Deserialize, Serialize};
1820
use std::collections::BTreeMap;
1921

@@ -100,6 +102,92 @@ where
100102
.collect()
101103
}
102104

105+
impl FromPlatformVersioned<DataContract> for DataContractInSerializationFormatV1 {
106+
fn from_platform_versioned(value: DataContract, platform_version: &PlatformVersion) -> Self {
107+
match value {
108+
DataContract::V0(v0) => {
109+
let DataContractV0 {
110+
id,
111+
config,
112+
version,
113+
owner_id,
114+
schema_defs,
115+
document_types,
116+
..
117+
} = v0;
118+
119+
let config = config.config_valid_for_platform_version(platform_version);
120+
121+
DataContractInSerializationFormatV1 {
122+
id,
123+
config,
124+
version,
125+
owner_id,
126+
schema_defs,
127+
document_schemas: document_types
128+
.into_iter()
129+
.map(|(key, document_type)| (key, document_type.schema_owned()))
130+
.collect(),
131+
created_at: None,
132+
updated_at: None,
133+
created_at_block_height: None,
134+
updated_at_block_height: None,
135+
created_at_epoch: None,
136+
updated_at_epoch: None,
137+
groups: Default::default(),
138+
tokens: Default::default(),
139+
keywords: Default::default(),
140+
description: None,
141+
}
142+
}
143+
DataContract::V1(v1) => {
144+
let DataContractV1 {
145+
id,
146+
config,
147+
version,
148+
owner_id,
149+
schema_defs,
150+
document_types,
151+
created_at,
152+
updated_at,
153+
created_at_block_height,
154+
updated_at_block_height,
155+
created_at_epoch,
156+
updated_at_epoch,
157+
groups,
158+
tokens,
159+
keywords,
160+
description,
161+
} = v1;
162+
163+
let config = config.config_valid_for_platform_version(platform_version);
164+
165+
DataContractInSerializationFormatV1 {
166+
id,
167+
config,
168+
version,
169+
owner_id,
170+
schema_defs,
171+
document_schemas: document_types
172+
.into_iter()
173+
.map(|(key, document_type)| (key, document_type.schema_owned()))
174+
.collect(),
175+
created_at,
176+
updated_at,
177+
created_at_block_height,
178+
updated_at_block_height,
179+
created_at_epoch,
180+
updated_at_epoch,
181+
groups,
182+
tokens,
183+
keywords,
184+
description,
185+
}
186+
}
187+
}
188+
}
189+
}
190+
103191
impl From<DataContract> for DataContractInSerializationFormatV1 {
104192
fn from(value: DataContract) -> Self {
105193
match value {

0 commit comments

Comments
 (0)