Repository navigation
fix(sdk): prevent sized_integer_types config downgrade that breaks document #3071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1320f34
fix(sdk): prevent sized_integer_types config downgrade that breaks do…
shumkov 75ead7c
fix(dpp): resolve dotted keys in flattened_properties during integer …
shumkov 2c3b8a1
fix(test): preserve sizedIntegerTypes in data contract update test
shumkov e504bb1
fix(dpp): allow V0 config updates from SDKs that cannot express sized…
shumkov 5ed4ff3
fix(wasm-dpp): use PlatformVersion::latest() in setConfig to preserve…
shumkov bafb94d
revert(dpp): remove serialization fallback logic for sized integer ty…
shumkov 0e28255
docs: add comments to explain changes
shumkov 56b0232
feat: enforce minimum config version check
shumkov d2a4d66
chore: fix formatting and unused import in config validation
shumkov c45c05e
fix: include v1 in known_versions for VersionNotActive error
shumkov 66dce4f
chore: fix npm audit vulnerabilities (qs, tar, webpack)
shumkov 5426d4d
chore: reword
shumkov 43f626c
fix: set default config version in test fixture contract loader
shumkov 3f49766
docs: comment version
shumkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
packages/rs-dpp/src/data_contract/config/methods/validate_update/v1/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| ); | ||
| } | ||
|
|
||
| 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" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...validation/state_transition/state_transitions/data_contract_create/basic_structure/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub(crate) mod v0; | ||
| pub(crate) mod v1; |
52 changes: 52 additions & 0 deletions
52
...idation/state_transition/state_transitions/data_contract_create/basic_structure/v1/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.