Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Use correct length for Pallet and Storage prefix when calculating `Ke…
Browse files Browse the repository at this point in the history
…yLenOf` (#11994)

* Use correct length for Pallet and Storage prefix when calculating `KeyLenOf`

* Add tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
bkchr and ggwpez authored Aug 11, 2022
1 parent e351248 commit dc45cc2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
19 changes: 15 additions & 4 deletions frame/support/src/storage/types/double_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
KeyLenOf, StorageAppend, StorageDecodeLength, StoragePrefixedMap, StorageTryAppend,
},
traits::{Get, GetDefault, StorageInfo, StorageInstance},
StorageHasher, Twox128,
};
use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen};
use sp_arithmetic::traits::SaturatedConversion;
Expand Down Expand Up @@ -91,10 +92,10 @@ impl<Prefix, Hasher1, Key1, Hasher2, Key2, Value, QueryKind, OnEmpty, MaxValues>
Key2: MaxEncodedLen,
{
fn get() -> u32 {
let z = Hasher1::max_len::<Key1>() +
Hasher2::max_len::<Key2>() +
Prefix::pallet_prefix().len() +
Prefix::STORAGE_PREFIX.len();
// The `max_len` of both key hashes plus the pallet prefix and storage prefix (which both
// are hashed with `Twox128`).
let z =
Hasher1::max_len::<Key1>() + Hasher2::max_len::<Key2>() + Twox128::max_len::<()>() * 2;
z as u32
}
}
Expand Down Expand Up @@ -755,6 +756,16 @@ mod test {
}
}

#[test]
fn keylenof_works() {
// Works with Blake2_128Concat and Twox64Concat.
type A = StorageDoubleMap<Prefix, Blake2_128Concat, u64, Twox64Concat, u32, u32>;
let size = 16 * 2 // Two Twox128
+ 16 + 8 // Blake2_128Concat = hash + key
+ 8 + 4; // Twox64Concat = hash + key
assert_eq!(KeyLenOf::<A>::get(), size);
}

#[test]
fn test() {
type A =
Expand Down
27 changes: 25 additions & 2 deletions frame/support/src/storage/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
KeyLenOf, StorageAppend, StorageDecodeLength, StoragePrefixedMap, StorageTryAppend,
},
traits::{Get, GetDefault, StorageInfo, StorageInstance},
StorageHasher, Twox128,
};
use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen};
use sp_arithmetic::traits::SaturatedConversion;
Expand Down Expand Up @@ -61,8 +62,9 @@ where
Key: FullCodec + MaxEncodedLen,
{
fn get() -> u32 {
let z =
Hasher::max_len::<Key>() + Prefix::pallet_prefix().len() + Prefix::STORAGE_PREFIX.len();
// The `max_len` of the key hash plus the pallet prefix and storage prefix (which both are
// hashed with `Twox128`).
let z = Hasher::max_len::<Key>() + Twox128::max_len::<()>() * 2;
z as u32
}
}
Expand Down Expand Up @@ -501,6 +503,27 @@ mod test {
}
}

#[test]
fn keylenof_works() {
// Works with Blake2_128Concat.
type A = StorageMap<Prefix, Blake2_128Concat, u32, u32>;
let size = 16 * 2 // Two Twox128
+ 16 + 4; // Blake2_128Concat = hash + key
assert_eq!(KeyLenOf::<A>::get(), size);

// Works with Blake2_256.
type B = StorageMap<Prefix, Blake2_256, u32, u32>;
let size = 16 * 2 // Two Twox128
+ 32; // Blake2_256
assert_eq!(KeyLenOf::<B>::get(), size);

// Works with Twox64Concat.
type C = StorageMap<Prefix, Twox64Concat, u32, u32>;
let size = 16 * 2 // Two Twox128
+ 8 + 4; // Twox64Concat = hash + key
assert_eq!(KeyLenOf::<C>::get(), size);
}

#[test]
fn test() {
type A = StorageMap<Prefix, Blake2_128Concat, u16, u32, OptionQuery>;
Expand Down

0 comments on commit dc45cc2

Please sign in to comment.