Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use platform_value::Identifier;
use platform_version::version::PlatformVersion;

mod v0;
mod v1;

impl DataContractConfig {
pub fn validate_update(
Expand All @@ -20,9 +21,10 @@ impl DataContractConfig {
.validate_config_update
{
0 => Ok(self.validate_update_v0(new_config, contract_id)),
1 => Ok(self.validate_update_v1(new_config, contract_id)),
version => Err(ProtocolError::UnknownVersionMismatch {
method: "validate_update".to_string(),
known_versions: vec![0],
known_versions: vec![0, 1],
received: version,
}),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use crate::consensus::state::data_contract::data_contract_config_update_error::DataContractConfigUpdateError;
use crate::data_contract::config::v1::DataContractConfigGettersV1;
use crate::data_contract::config::DataContractConfig;
use crate::validation::SimpleConsensusValidationResult;
use platform_value::Identifier;

impl DataContractConfig {
#[inline(always)]
pub(super) fn validate_update_v1(
&self,
new_config: &DataContractConfig,
contract_id: Identifier,
) -> SimpleConsensusValidationResult {
// Run all v0 checks first
let v0_result = self.validate_update_v0(new_config, contract_id);
if !v0_result.is_valid() {
return v0_result;
}

// Validate: sized_integer_types cannot change from true to false.
// V1→V0 (true→false) is DANGEROUS: documents serialized with sized types (version byte 1/2)
// would break when deserialized with I64 types.
// V0→V1 (false→true) is SAFE: version byte 0 docs use from_bytes_v0 which forces I64
// regardless of current config.
if self.sized_integer_types() && !new_config.sized_integer_types() {
return SimpleConsensusValidationResult::new_with_error(
DataContractConfigUpdateError::new(
contract_id,
"contract can not disable sized integer types once enabled, as this would break deserialization of existing documents",
)
.into(),
);
}
Comment on lines +46 to +54

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure you can go V0 to V1?, Also I would include in V1 a verification that the DataContractConfig is V1, makes no sense to register or update to V0 anymore.

@shumkov shumkov Feb 12, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can upgrade from v0 to v1. Agree, we shouldn't allow v0.


SimpleConsensusValidationResult::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::data_contract::config::v0::DataContractConfigV0;
use crate::data_contract::config::v1::DataContractConfigV1;

#[test]
fn test_v1_to_v0_rejected() {
let contract_id = Identifier::new([1u8; 32]);
let config_v1 = DataContractConfig::V1(DataContractConfigV1::default());
let config_v0 = DataContractConfig::V0(DataContractConfigV0::default());

// ConfigV1 has sized_integer_types=true, ConfigV0 has sized_integer_types=false
assert!(config_v1.sized_integer_types());
assert!(!config_v0.sized_integer_types());

let result = config_v1.validate_update_v1(&config_v0, contract_id);
assert!(
!result.is_valid(),
"V1→V0 config change should be rejected because it disables sized integer types. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v1_sized_true_to_v1_sized_false_rejected() {
let contract_id = Identifier::new([1u8; 32]);
let config_v1_true = DataContractConfig::V1(DataContractConfigV1::default());
let mut v1_false = DataContractConfigV1::default();
v1_false.sized_integer_types = false;
let config_v1_false = DataContractConfig::V1(v1_false);

assert!(config_v1_true.sized_integer_types());
assert!(!config_v1_false.sized_integer_types());

let result = config_v1_true.validate_update_v1(&config_v1_false, contract_id);
assert!(
!result.is_valid(),
"V1(sized=true)→V1(sized=false) should be rejected. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v0_to_v1_allowed() {
let contract_id = Identifier::new([1u8; 32]);
let config_v0 = DataContractConfig::V0(DataContractConfigV0::default());
let config_v1 = DataContractConfig::V1(DataContractConfigV1::default());

// V0→V1 (false→true) is safe because version byte 0 docs use from_bytes_v0
let result = config_v0.validate_update_v1(&config_v1, contract_id);
assert!(
result.is_valid(),
"V0→V1 config change should be allowed (safe direction). Errors: {:?}",
result.errors
);
}

#[test]
fn test_v0_to_v0_allowed() {
let contract_id = Identifier::new([1u8; 32]);
let config_v0 = DataContractConfig::V0(DataContractConfigV0::default());
let config_v0_2 = DataContractConfig::V0(DataContractConfigV0::default());

let result = config_v0.validate_update_v1(&config_v0_2, contract_id);
assert!(
result.is_valid(),
"V0→V0 (no change) should be allowed. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v1_to_v1_same_allowed() {
let contract_id = Identifier::new([1u8; 32]);
let config_v1 = DataContractConfig::V1(DataContractConfigV1::default());
let config_v1_2 = DataContractConfig::V1(DataContractConfigV1::default());

let result = config_v1.validate_update_v1(&config_v1_2, contract_id);
assert!(
result.is_valid(),
"V1→V1 (same config) should be allowed. Errors: {:?}",
result.errors
);
}

#[test]
fn test_all_v0_checks_still_work() {
let contract_id = Identifier::new([1u8; 32]);
let config_v1 = DataContractConfig::V1(DataContractConfigV1::default());

// Changing keeps_history should be rejected
let mut modified = DataContractConfigV1::default();
modified.keeps_history = !modified.keeps_history;
let config_modified = DataContractConfig::V1(modified);

let result = config_v1.validate_update_v1(&config_modified, contract_id);
assert!(
!result.is_valid(),
"Changing keeps_history should be rejected by validate_update_v1"
);

// Changing readonly (to true) should be rejected
let mut modified2 = DataContractConfigV1::default();
modified2.readonly = true;
let config_readonly = DataContractConfig::V1(modified2);

let result2 = config_v1.validate_update_v1(&config_readonly, contract_id);
assert!(
!result2.is_valid(),
"Changing readonly to true should be rejected by validate_update_v1"
);

// Changing can_be_deleted should be rejected
let mut modified3 = DataContractConfigV1::default();
modified3.can_be_deleted = !modified3.can_be_deleted;
let config_can_be_deleted = DataContractConfig::V1(modified3);

let result3 = config_v1.validate_update_v1(&config_can_be_deleted, contract_id);
assert!(
!result3.is_valid(),
"Changing can_be_deleted should be rejected by validate_update_v1"
);

// Changing documents_mutable_contract_default should be rejected
let mut modified4 = DataContractConfigV1::default();
modified4.documents_mutable_contract_default =
!modified4.documents_mutable_contract_default;
let config_docs_mutable = DataContractConfig::V1(modified4);

let result4 = config_v1.validate_update_v1(&config_docs_mutable, contract_id);
assert!(
!result4.is_valid(),
"Changing documents_mutable_contract_default should be rejected by validate_update_v1"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use versioned_feature_core::FeatureVersion;

pub mod v1;
pub mod v2;
pub mod v3;

#[derive(Clone, Debug, Default)]
pub struct DPPValidationVersions {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::version::dpp_versions::dpp_validation_versions::{
DPPValidationVersions, DataContractValidationVersions, DocumentTypeValidationVersions,
JsonSchemaValidatorVersions, VotingValidationVersions,
};

pub const DPP_VALIDATION_VERSIONS_V3: DPPValidationVersions = DPPValidationVersions {
json_schema_validator: JsonSchemaValidatorVersions {
new: 0,
validate: 0,
compile: 0,
compile_and_validate: 0,
},
data_contract: DataContractValidationVersions {
validate: 0,
// prevent sized_integer_types config downgrade on contract update
validate_config_update: 1,
Comment thread
shumkov marked this conversation as resolved.
validate_token_config_update: 0,
validate_index_definitions: 0,
validate_index_naming_duplicates: 0,
validate_not_defined_properties: 0,
validate_property_definition: 0,
validate_token_config_groups_exist: 0,
validate_localizations: 0,
},
document_type: DocumentTypeValidationVersions {
validate_update: 0,
contested_index_limit: 1,
unique_index_limit: 10,
},
voting: VotingValidationVersions {
allow_other_contenders_time_mainnet_ms: 604_800_000, // 1 week in ms
allow_other_contenders_time_testing_ms: 2_700_000, //45 minutes
votes_allowed_per_masternode: 5,
},
};
7 changes: 4 additions & 3 deletions packages/rs-platform-version/src/version/v12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::version::dpp_versions::dpp_state_transition_method_versions::v1::STAT
use crate::version::dpp_versions::dpp_state_transition_serialization_versions::v2::STATE_TRANSITION_SERIALIZATION_VERSIONS_V2;
use crate::version::dpp_versions::dpp_state_transition_versions::v3::STATE_TRANSITION_VERSIONS_V3;
use crate::version::dpp_versions::dpp_token_versions::v1::TOKEN_VERSIONS_V1;
use crate::version::dpp_versions::dpp_validation_versions::v2::DPP_VALIDATION_VERSIONS_V2;
use crate::version::dpp_versions::dpp_validation_versions::v3::DPP_VALIDATION_VERSIONS_V3;
use crate::version::dpp_versions::dpp_voting_versions::v2::VOTING_VERSION_V2;
use crate::version::dpp_versions::DPPVersion;
use crate::version::drive_abci_versions::drive_abci_checkpoint_parameters::v1::DRIVE_ABCI_CHECKPOINT_PARAMETERS_V1;
Expand All @@ -30,7 +30,7 @@ use crate::version::ProtocolVersion;

pub const PROTOCOL_VERSION_12: ProtocolVersion = 12;

/// This version was for Platform release 3.1.0
/// This version is for Platform release 3.1.0
pub const PLATFORM_V12: PlatformVersion = PlatformVersion {
protocol_version: PROTOCOL_VERSION_12,
drive: DRIVE_VERSION_V6,
Expand All @@ -44,7 +44,8 @@ pub const PLATFORM_V12: PlatformVersion = PlatformVersion {
},
dpp: DPPVersion {
costs: DPP_COSTS_VERSIONS_V1,
validation: DPP_VALIDATION_VERSIONS_V2,
// prevent sized_integer_types config downgrade on contract update
validation: DPP_VALIDATION_VERSIONS_V3,
Comment thread
shumkov marked this conversation as resolved.
state_transition_serialization_versions: STATE_TRANSITION_SERIALIZATION_VERSIONS_V2,
state_transition_conversion_versions: STATE_TRANSITION_CONVERSION_VERSIONS_V2,
state_transition_method_versions: STATE_TRANSITION_METHOD_VERSIONS_V1,
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm-dpp/src/data_contract/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl DataContractWasm {
pub fn set_config(&mut self, config: JsValue) -> Result<(), JsValue> {
let value = config.with_serde_to_platform_value()?;

let platform_version = &PlatformVersion::first();
let platform_version = PlatformVersion::latest();

let data_contract_config =
DataContractConfig::from_value(value, platform_version).with_js_error()?;
Expand Down
Loading