Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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, platform_version)),
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,203 @@
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;
use platform_version::version::FeatureVersion;
use platform_version::version::PlatformVersion;

impl DataContractConfig {
#[inline(always)]
pub(super) fn validate_update_v1(
&self,
new_config: &DataContractConfig,
contract_id: Identifier,
platform_version: &PlatformVersion,
) -> 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: new config version meets minimum version requirement.
// Since protocol version 12, V0 config is no longer accepted because it lacks
// sized_integer_types support.
let min_version = platform_version.dpp.contract_versions.config.min_version;
if (new_config.version() as FeatureVersion) < min_version {
return SimpleConsensusValidationResult::new_with_error(
DataContractConfigUpdateError::new(
contract_id,
format!(
"config version {} is not supported, minimum version is {}",
new_config.version(),
min_version
),
)
.into(),
);
}

// 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 platform_version = PlatformVersion::latest();
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, platform_version);
assert!(
!result.is_valid(),
"V1→V0 config change should be rejected. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v1_sized_true_to_v1_sized_false_rejected() {
let platform_version = PlatformVersion::latest();
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, platform_version);
assert!(
!result.is_valid(),
"V1(sized=true)→V1(sized=false) should be rejected. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v0_to_v1_allowed() {
let platform_version = PlatformVersion::latest();
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, platform_version);
assert!(
result.is_valid(),
"V0→V1 config change should be allowed (safe direction). Errors: {:?}",
result.errors
);
}

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

// On latest platform version (v12+), V0 config is below min_version=1
let result = config_v0.validate_update_v1(&config_v0_2, contract_id, platform_version);
assert!(
!result.is_valid(),
"V0→V0 should be rejected on latest platform version where min config version is 1. Errors: {:?}",
result.errors
);
}

#[test]
fn test_v1_to_v1_same_allowed() {
let platform_version = PlatformVersion::latest();
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, platform_version);
assert!(
result.is_valid(),
"V1→V1 (same config) should be allowed. Errors: {:?}",
result.errors
);
}

#[test]
fn test_all_v0_checks_still_work() {
let platform_version = PlatformVersion::latest();
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, platform_version);
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, platform_version);
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, platform_version);
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, platform_version);
assert!(
!result4.is_valid(),
"Changing documents_mutable_contract_default should be rejected by validate_update_v1"
);
}
}
28 changes: 14 additions & 14 deletions packages/rs-dpp/src/data_contract/methods/validate_update/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,11 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

new_data_contract.as_v0_mut().unwrap().owner_id = Identifier::random();
new_data_contract.set_owner_id(Identifier::random());

let result = old_data_contract
.validate_update(&new_data_contract, &BlockInfo::default(), platform_version)
Expand All @@ -392,7 +392,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let new_data_contract = old_data_contract.clone();

Expand All @@ -413,7 +413,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

Expand All @@ -437,7 +437,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

Expand All @@ -463,21 +463,21 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

new_data_contract.set_version(old_data_contract.version() + 1);

let DocumentTypeMutRef::V0(new_document_type) = new_data_contract
match new_data_contract
.document_types_mut()
.get_mut("niceDocument")
.unwrap()
.as_mut_ref()
else {
panic!("expected v0")
};
new_document_type.documents_mutable = false;
{
DocumentTypeMutRef::V0(dt) => dt.documents_mutable = false,
DocumentTypeMutRef::V1(dt) => dt.documents_mutable = false,
}

let result = old_data_contract
.validate_update_v0(&new_data_contract, &BlockInfo::default(), platform_version)
Expand All @@ -496,7 +496,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let mut old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

// Remove document that uses $defs, so we can safely remove it for testing
old_data_contract
Expand Down Expand Up @@ -527,7 +527,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

Expand Down Expand Up @@ -567,7 +567,7 @@ mod tests {
let platform_version = PlatformVersion::latest();

let old_data_contract =
get_data_contract_fixture(None, IdentityNonce::default(), 1).data_contract_owned();
get_data_contract_fixture(None, IdentityNonce::default(), platform_version.protocol_version).data_contract_owned();

let mut new_data_contract = old_data_contract.clone();

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod v0;
pub(crate) mod v1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::error::Error;
use dpp::consensus::state::data_contract::data_contract_config_update_error::DataContractConfigUpdateError;
use dpp::dashcore::Network;
use dpp::state_transition::data_contract_create_transition::accessors::DataContractCreateTransitionAccessorsV0;
use dpp::state_transition::data_contract_create_transition::DataContractCreateTransition;
use dpp::validation::SimpleConsensusValidationResult;
use dpp::version::{FeatureVersion, PlatformVersion};

use super::v0::DataContractCreateStateTransitionBasicStructureValidationV0;

pub(in crate::execution::validation::state_transition::state_transitions::data_contract_create) trait DataContractCreateStateTransitionBasicStructureValidationV1
{
fn validate_basic_structure_v1(
&self,
network_type: Network,
platform_version: &PlatformVersion,
) -> Result<SimpleConsensusValidationResult, Error>;
}

impl DataContractCreateStateTransitionBasicStructureValidationV1 for DataContractCreateTransition {
fn validate_basic_structure_v1(
&self,
network_type: Network,
platform_version: &PlatformVersion,
) -> Result<SimpleConsensusValidationResult, Error> {
// First run all v0 validations
let v0_result = self.validate_basic_structure_v0(network_type, platform_version)?;
if !v0_result.is_valid() {
return Ok(v0_result);
}

// Validate config version meets minimum requirement for the current protocol version.
// Since protocol version 12, V0 config is no longer accepted because it lacks
// sized_integer_types support.
let config_min_version = platform_version.dpp.contract_versions.config.min_version;
if (self.data_contract().config().version() as FeatureVersion) < config_min_version {
return Ok(SimpleConsensusValidationResult::new_with_error(
DataContractConfigUpdateError::new(
self.data_contract().id(),
format!(
"config version {} is not supported, minimum version is {}",
self.data_contract().config().version(),
config_min_version
),
)
.into(),
));
}

Ok(SimpleConsensusValidationResult::new())
}
}
Loading
Loading