Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions packages/rs-dpp/src/data_contract/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ impl DataContractConfig {
pub fn default_for_version(
platform_version: &PlatformVersion,
) -> Result<DataContractConfig, ProtocolError> {
match platform_version.dpp.contract_versions.config {
match platform_version
.dpp
.contract_versions
.config
.default_current_version
{
0 => Ok(DataContractConfigV0::default().into()),
1 => Ok(DataContractConfigV1::default().into()),
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -41,11 +46,36 @@ impl DataContractConfig {
}
}

/// Adjusts the current `DataContractConfig` to be valid for the provided platform version.
///
/// This replaces the internal version with the `default_current_version` defined in the platform version's
/// feature bounds for contract config.
pub fn config_valid_for_platform_version(
self,
platform_version: &PlatformVersion,
) -> DataContractConfig {
match self {
DataContractConfig::V0(v0) => DataContractConfig::V0(v0),
DataContractConfig::V1(v1) => {
if platform_version.dpp.contract_versions.config.max_version == 0 {
DataContractConfig::V0(v1.into())
} else {
self
}
}
}
}

pub fn from_value(
value: Value,
platform_version: &PlatformVersion,
) -> Result<DataContractConfig, ProtocolError> {
match platform_version.dpp.contract_versions.config {
match platform_version
.dpp
.contract_versions
.config
.default_current_version
{
0 => {
let config: DataContractConfigV0 = platform_value::from_value(value)?;
Ok(config.into())
Expand Down Expand Up @@ -85,7 +115,12 @@ impl DataContractConfig {
contract: &BTreeMap<String, Value>,
platform_version: &PlatformVersion,
) -> Result<DataContractConfig, ProtocolError> {
match platform_version.dpp.contract_versions.config {
match platform_version
.dpp
.contract_versions
.config
.default_current_version
{
0 => Ok(
DataContractConfigV0::get_contract_configuration_properties_v0(contract)?.into(),
),
Expand Down
19 changes: 19 additions & 0 deletions packages/rs-dpp/src/data_contract/config/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::data_contract::config;
use crate::data_contract::config::v1::DataContractConfigV1;
use crate::data_contract::config::{
DataContractConfig, DEFAULT_CONTRACT_CAN_BE_DELETED, DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY, DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
Expand Down Expand Up @@ -192,3 +193,21 @@ impl DataContractConfigV0 {
})
}
}

impl From<DataContractConfigV1> for DataContractConfigV0 {
fn from(value: DataContractConfigV1) -> Self {
DataContractConfigV0 {
can_be_deleted: value.can_be_deleted,
readonly: value.readonly,
keeps_history: value.keeps_history,
documents_keep_history_contract_default: value.documents_keep_history_contract_default,
documents_mutable_contract_default: value.documents_mutable_contract_default,
documents_can_be_deleted_contract_default: value
.documents_can_be_deleted_contract_default,
requires_identity_encryption_bounded_key: value
.requires_identity_encryption_bounded_key,
requires_identity_decryption_bounded_key: value
.requires_identity_decryption_bounded_key,
}
}
}
34 changes: 21 additions & 13 deletions packages/rs-dpp/src/data_contract/serialized_version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::ProtocolError;
use bincode::{Decode, Encode};
use derive_more::From;
use platform_value::{Identifier, Value};
use platform_version::TryFromPlatformVersioned;
use platform_version::{IntoPlatformVersioned, TryFromPlatformVersioned};
use platform_versioning::PlatformVersioned;
#[cfg(feature = "data-contract-serde-conversion")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -160,11 +160,13 @@ impl TryFromPlatformVersioned<DataContractV0> for DataContractInSerializationFor
.default_current_version
{
0 => {
let v0_format: DataContractInSerializationFormatV0 = DataContract::V0(value).into();
let v0_format: DataContractInSerializationFormatV0 =
DataContract::V0(value).into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 = DataContract::V0(value).into();
let v1_format: DataContractInSerializationFormatV1 =
DataContract::V0(value).into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -191,12 +193,12 @@ impl TryFromPlatformVersioned<&DataContractV0> for DataContractInSerializationFo
{
0 => {
let v0_format: DataContractInSerializationFormatV0 =
DataContract::V0(value.to_owned()).into();
DataContract::V0(value.to_owned()).into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 =
DataContract::V0(value.to_owned()).into();
DataContract::V0(value.to_owned()).into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -222,11 +224,13 @@ impl TryFromPlatformVersioned<DataContractV1> for DataContractInSerializationFor
.default_current_version
{
0 => {
let v0_format: DataContractInSerializationFormatV0 = DataContract::V1(value).into();
let v0_format: DataContractInSerializationFormatV0 =
DataContract::V1(value).into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 = DataContract::V1(value).into();
let v1_format: DataContractInSerializationFormatV1 =
DataContract::V1(value).into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -253,12 +257,12 @@ impl TryFromPlatformVersioned<&DataContractV1> for DataContractInSerializationFo
{
0 => {
let v0_format: DataContractInSerializationFormatV0 =
DataContract::V1(value.to_owned()).into();
DataContract::V1(value.to_owned()).into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 =
DataContract::V1(value.to_owned()).into();
DataContract::V1(value.to_owned()).into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -284,11 +288,13 @@ impl TryFromPlatformVersioned<&DataContract> for DataContractInSerializationForm
.default_current_version
{
0 => {
let v0_format: DataContractInSerializationFormatV0 = value.clone().into();
let v0_format: DataContractInSerializationFormatV0 =
value.clone().into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 = value.clone().into();
let v1_format: DataContractInSerializationFormatV1 =
value.clone().into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand All @@ -314,11 +320,13 @@ impl TryFromPlatformVersioned<DataContract> for DataContractInSerializationForma
.default_current_version
{
0 => {
let v0_format: DataContractInSerializationFormatV0 = value.into();
let v0_format: DataContractInSerializationFormatV0 =
value.into_platform_versioned(platform_version);
Ok(v0_format.into())
}
1 => {
let v1_format: DataContractInSerializationFormatV1 = value.into();
let v1_format: DataContractInSerializationFormatV1 =
value.into_platform_versioned(platform_version);
Ok(v1_format.into())
}
version => Err(ProtocolError::UnknownVersionMismatch {
Expand Down
59 changes: 59 additions & 0 deletions packages/rs-dpp/src/data_contract/serialized_version/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::data_contract::v1::DataContractV1;
use crate::data_contract::{DataContract, DefinitionName, DocumentName};
use bincode::{Decode, Encode};
use platform_value::{Identifier, Value};
use platform_version::version::PlatformVersion;
use platform_version::FromPlatformVersioned;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -85,3 +87,60 @@ impl From<DataContract> for DataContractInSerializationFormatV0 {
}
}
}

impl FromPlatformVersioned<DataContract> for DataContractInSerializationFormatV0 {
fn from_platform_versioned(value: DataContract, platform_version: &PlatformVersion) -> Self {
match value {
DataContract::V0(v0) => {
let DataContractV0 {
id,
config,
version,
owner_id,
schema_defs,
document_types,
..
} = v0;

let config = config.config_valid_for_platform_version(platform_version);

DataContractInSerializationFormatV0 {
id,
config,
version,
owner_id,
document_schemas: document_types
.into_iter()
.map(|(key, document_type)| (key, document_type.schema_owned()))
.collect(),
schema_defs,
}
}
DataContract::V1(v1) => {
let DataContractV1 {
id,
config,
version,
owner_id,
schema_defs,
document_types,
..
} = v1;

let config = config.config_valid_for_platform_version(platform_version);

DataContractInSerializationFormatV0 {
id,
config,
version,
owner_id,
document_schemas: document_types
.into_iter()
.map(|(key, document_type)| (key, document_type.schema_owned()))
.collect(),
schema_defs,
}
}
}
}
}
88 changes: 88 additions & 0 deletions packages/rs-dpp/src/data_contract/serialized_version/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use crate::identity::TimestampMillis;
use crate::prelude::BlockHeight;
use bincode::{Decode, Encode};
use platform_value::{Identifier, Value};
use platform_version::version::PlatformVersion;
use platform_version::FromPlatformVersioned;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -100,6 +102,92 @@ where
.collect()
}

impl FromPlatformVersioned<DataContract> for DataContractInSerializationFormatV1 {
fn from_platform_versioned(value: DataContract, platform_version: &PlatformVersion) -> Self {
match value {
DataContract::V0(v0) => {
let DataContractV0 {
id,
config,
version,
owner_id,
schema_defs,
document_types,
..
} = v0;

let config = config.config_valid_for_platform_version(platform_version);

DataContractInSerializationFormatV1 {
id,
config,
version,
owner_id,
schema_defs,
document_schemas: document_types
.into_iter()
.map(|(key, document_type)| (key, document_type.schema_owned()))
.collect(),
created_at: None,
updated_at: None,
created_at_block_height: None,
updated_at_block_height: None,
created_at_epoch: None,
updated_at_epoch: None,
groups: Default::default(),
tokens: Default::default(),
keywords: Default::default(),
description: None,
}
}
DataContract::V1(v1) => {
let DataContractV1 {
id,
config,
version,
owner_id,
schema_defs,
document_types,
created_at,
updated_at,
created_at_block_height,
updated_at_block_height,
created_at_epoch,
updated_at_epoch,
groups,
tokens,
keywords,
description,
} = v1;

let config = config.config_valid_for_platform_version(platform_version);

DataContractInSerializationFormatV1 {
id,
config,
version,
owner_id,
schema_defs,
document_schemas: document_types
.into_iter()
.map(|(key, document_type)| (key, document_type.schema_owned()))
.collect(),
created_at,
updated_at,
created_at_block_height,
updated_at_block_height,
created_at_epoch,
updated_at_epoch,
groups,
tokens,
keywords,
description,
}
}
}
}
}

impl From<DataContract> for DataContractInSerializationFormatV1 {
fn from(value: DataContract) -> Self {
match value {
Expand Down
Loading