-
Notifications
You must be signed in to change notification settings - Fork 57
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 7 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
175 changes: 175 additions & 0 deletions
175
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,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(), | ||
| ); | ||
| } | ||
|
|
||
| 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" | ||
| ); | ||
| } | ||
| } | ||
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
35 changes: 35 additions & 0 deletions
35
packages/rs-platform-version/src/version/dpp_versions/dpp_validation_versions/v3.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,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, | ||
|
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, | ||
| }, | ||
| }; | ||
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
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
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.