Allow to specify some max number of values for storages in pallet macro. (#8735)

* implement max_values + storages info

* some formatting + doc

* rename StoragesInfo -> PalletStorageInfo

* merge both StorageInfoTrait and PalletStorageInfo

I think it is more future proof. In the future some storage could make
use of multiple prefix. Like one to store how much value has been
inserted, etc...

* Update frame/support/procedural/src/storage/parse.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Update frame/support/procedural/src/storage/storage_struct.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Fix max_size using hasher information

hasher now expose `max_len` which allows to computes their maximum len.
For hasher without concatenation, it is the size of the hash part,
for hasher with concatenation, it is the size of the hash part + max
encoded len of the key.

* fix tests

* fix ui tests

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2021-05-17 15:44:24 +02:00
committed by GitHub
parent 59f34ab8bc
commit 9bf62ef65d
26 changed files with 1161 additions and 149 deletions
+38
View File
@@ -20,6 +20,7 @@
use codec::Codec;
use sp_std::prelude::Vec;
use sp_io::hashing::{blake2_128, blake2_256, twox_64, twox_128, twox_256};
use crate::traits::MaxEncodedLen;
// This trait must be kept coherent with frame-support-procedural HasherKind usage
pub trait Hashable: Sized {
@@ -59,6 +60,9 @@ pub trait StorageHasher: 'static {
const METADATA: frame_metadata::StorageHasher;
type Output: AsRef<[u8]>;
fn hash(x: &[u8]) -> Self::Output;
/// The max length of the final hash, for the given key type.
fn max_len<K: MaxEncodedLen>() -> usize;
}
/// Hasher to use to hash keys to insert to storage.
@@ -79,6 +83,9 @@ impl StorageHasher for Identity {
fn hash(x: &[u8]) -> Vec<u8> {
x.to_vec()
}
fn max_len<K: MaxEncodedLen>() -> usize {
K::max_encoded_len()
}
}
impl ReversibleStorageHasher for Identity {
fn reverse(x: &[u8]) -> &[u8] {
@@ -98,6 +105,9 @@ impl StorageHasher for Twox64Concat {
.cloned()
.collect::<Vec<_>>()
}
fn max_len<K: MaxEncodedLen>() -> usize {
K::max_encoded_len().saturating_add(8)
}
}
impl ReversibleStorageHasher for Twox64Concat {
fn reverse(x: &[u8]) -> &[u8] {
@@ -121,6 +131,9 @@ impl StorageHasher for Blake2_128Concat {
.cloned()
.collect::<Vec<_>>()
}
fn max_len<K: MaxEncodedLen>() -> usize {
K::max_encoded_len().saturating_add(16)
}
}
impl ReversibleStorageHasher for Blake2_128Concat {
fn reverse(x: &[u8]) -> &[u8] {
@@ -140,6 +153,9 @@ impl StorageHasher for Blake2_128 {
fn hash(x: &[u8]) -> [u8; 16] {
blake2_128(x)
}
fn max_len<K: MaxEncodedLen>() -> usize {
16
}
}
/// Hash storage keys with blake2 256
@@ -150,6 +166,9 @@ impl StorageHasher for Blake2_256 {
fn hash(x: &[u8]) -> [u8; 32] {
blake2_256(x)
}
fn max_len<K: MaxEncodedLen>() -> usize {
32
}
}
/// Hash storage keys with twox 128
@@ -160,6 +179,9 @@ impl StorageHasher for Twox128 {
fn hash(x: &[u8]) -> [u8; 16] {
twox_128(x)
}
fn max_len<K: MaxEncodedLen>() -> usize {
16
}
}
/// Hash storage keys with twox 256
@@ -170,6 +192,9 @@ impl StorageHasher for Twox256 {
fn hash(x: &[u8]) -> [u8; 32] {
twox_256(x)
}
fn max_len<K: MaxEncodedLen>() -> usize {
32
}
}
#[cfg(test)]
@@ -187,4 +212,17 @@ mod tests {
let r = Blake2_128Concat::hash(b"foo");
assert_eq!(r.split_at(16), (&blake2_128(b"foo")[..], &b"foo"[..]))
}
#[test]
fn max_lengths() {
use codec::Encode;
let encoded_0u32 = &0u32.encode()[..];
assert_eq!(Twox64Concat::hash(encoded_0u32).len(), Twox64Concat::max_len::<u32>());
assert_eq!(Twox128::hash(encoded_0u32).len(), Twox128::max_len::<u32>());
assert_eq!(Twox256::hash(encoded_0u32).len(), Twox256::max_len::<u32>());
assert_eq!(Blake2_128::hash(encoded_0u32).len(), Blake2_128::max_len::<u32>());
assert_eq!(Blake2_128Concat::hash(encoded_0u32).len(), Blake2_128Concat::max_len::<u32>());
assert_eq!(Blake2_256::hash(encoded_0u32).len(), Blake2_256::max_len::<u32>());
assert_eq!(Identity::hash(encoded_0u32).len(), Identity::max_len::<u32>());
}
}