Don't hash constant values; only their types (#587)

* Don't hash constant values; only their types

* Fix a test

* cargo fmt
This commit is contained in:
James Wilson
2022-06-27 16:02:16 +01:00
committed by GitHub
parent f97b8963f5
commit a90697ca29
2 changed files with 7 additions and 12 deletions
+2 -5
View File
@@ -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),
@@ -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());