From a90697ca2926dd698f5f26f62837b9d966be2ad8 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Mon, 27 Jun 2022 16:02:16 +0100 Subject: [PATCH] Don't hash constant values; only their types (#587) * Don't hash constant values; only their types * Fix a test * cargo fmt --- metadata/src/lib.rs | 7 ++----- testing/integration-tests/src/metadata/validation.rs | 12 +++++------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/metadata/src/lib.rs b/metadata/src/lib.rs index 4fc57f4c36..fe2cee9224 100644 --- a/metadata/src/lib.rs +++ b/metadata/src/lib.rs @@ -302,10 +302,8 @@ pub fn get_constant_hash( .find(|c| c.name == constant_name) .ok_or(NotFound::Item)?; - let mut bytes = get_type_hash(&metadata.types, constant.ty.id(), &mut HashSet::new()); - bytes = xor(bytes, hash(constant.name.as_bytes())); - bytes = xor(bytes, hash(&constant.value)); - + // We only need to check that the type of the constant asked for matches. + let bytes = get_type_hash(&metadata.types, constant.ty.id(), &mut HashSet::new()); Ok(bytes) } @@ -363,7 +361,6 @@ pub fn get_pallet_hash( } for constant in pallet.constants.iter() { bytes = xor(bytes, hash(constant.name.as_bytes())); - bytes = xor(bytes, hash(&constant.value)); bytes = xor( bytes, get_type_hash(registry, constant.ty.id(), &mut visited_ids), diff --git a/testing/integration-tests/src/metadata/validation.rs b/testing/integration-tests/src/metadata/validation.rs index 47a2eb034c..3a0a903a35 100644 --- a/testing/integration-tests/src/metadata/validation.rs +++ b/testing/integration-tests/src/metadata/validation.rs @@ -94,7 +94,7 @@ async fn full_metadata_check() { } #[tokio::test] -async fn constants_check() { +async fn constant_values_are_not_validated() { let cxt = test_context().await; let api = &cxt.api; @@ -117,16 +117,14 @@ async fn constants_check() { .iter_mut() .find(|constant| constant.name == "ExistentialDeposit") .expect("ExistentialDeposit constant must be present"); + + // Modifying a constant value should not lead to an error: existential.value = vec![0u8; 32]; let new_api = metadata_to_api(metadata, &cxt).await; - assert!(new_api.validate_metadata().is_err()); - assert!(new_api - .constants() - .balances() - .existential_deposit() - .is_err()); + assert!(new_api.validate_metadata().is_ok()); + assert!(new_api.constants().balances().existential_deposit().is_ok()); // Other constant validation should not be impacted. assert!(new_api.constants().balances().max_locks().is_ok());