Implement ResultQuery (#11257)

* Implement ResultQuery

* Fix test expectations

* Add more tests

* Fix test expectations

* Clean up some names

* Silence warnings

* Specify error type when supplying error type to ResultQuery

* cargo fmt

* Add support for type parameters in parameter_types macro

* Reduce deeply indented code

* Fixes

* Update test expectation

* Rewrite and document formula for calculating max storage size

* More docs

* cargo fmt

* formatting

Co-authored-by: parity-processbot <>
This commit is contained in:
Keith Yeung
2022-08-25 01:47:08 +08:00
committed by GitHub
parent 6aba2fdc99
commit edc8f7b409
16 changed files with 770 additions and 77 deletions
+129 -10
View File
@@ -229,6 +229,7 @@ pub mod pallet {
pub enum Error<T> {
/// doc comment put into metadata
InsufficientProposersBalance,
NonExistentStorageValue,
Code(u8),
#[codec(skip)]
Skipped(u128),
@@ -282,6 +283,10 @@ pub mod pallet {
pub type Map2<T> =
StorageMap<Hasher = Twox64Concat, Key = u16, Value = u32, MaxValues = ConstU32<3>>;
#[pallet::storage]
pub type Map3<T> =
StorageMap<_, Blake2_128Concat, u32, u64, ResultQuery<Error<T>::NonExistentStorageValue>>;
#[pallet::storage]
pub type DoubleMap<T> = StorageDoubleMap<_, Blake2_128Concat, u8, Twox64Concat, u16, u32>;
@@ -295,6 +300,17 @@ pub mod pallet {
MaxValues = ConstU32<5>,
>;
#[pallet::storage]
pub type DoubleMap3<T> = StorageDoubleMap<
_,
Blake2_128Concat,
u32,
Twox64Concat,
u64,
u128,
ResultQuery<Error<T>::NonExistentStorageValue>,
>;
#[pallet::storage]
#[pallet::getter(fn nmap)]
pub type NMap<T> = StorageNMap<_, storage::Key<Blake2_128Concat, u8>, u32>;
@@ -307,6 +323,15 @@ pub mod pallet {
MaxValues = ConstU32<11>,
>;
#[pallet::storage]
#[pallet::getter(fn nmap3)]
pub type NMap3<T> = StorageNMap<
_,
(NMapKey<Blake2_128Concat, u8>, NMapKey<Twox64Concat, u16>),
u128,
ResultQuery<Error<T>::NonExistentStorageValue>,
>;
#[pallet::storage]
#[pallet::getter(fn conditional_value)]
#[cfg(feature = "conditional-storage")]
@@ -934,6 +959,16 @@ fn storage_expand() {
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
assert_eq!(&k[..32], &<pallet::Map2<Runtime>>::final_prefix());
pallet::Map3::<Runtime>::insert(1, 2);
let mut k = [twox_128(b"Example"), twox_128(b"Map3")].concat();
k.extend(1u32.using_encoded(blake2_128_concat));
assert_eq!(unhashed::get::<u64>(&k), Some(2u64));
assert_eq!(&k[..32], &<pallet::Map3<Runtime>>::final_prefix());
assert_eq!(
pallet::Map3::<Runtime>::get(2),
Err(pallet::Error::<Runtime>::NonExistentStorageValue),
);
pallet::DoubleMap::<Runtime>::insert(&1, &2, &3);
let mut k = [twox_128(b"Example"), twox_128(b"DoubleMap")].concat();
k.extend(1u8.using_encoded(blake2_128_concat));
@@ -948,6 +983,17 @@ fn storage_expand() {
assert_eq!(unhashed::get::<u64>(&k), Some(3u64));
assert_eq!(&k[..32], &<pallet::DoubleMap2<Runtime>>::final_prefix());
pallet::DoubleMap3::<Runtime>::insert(&1, &2, &3);
let mut k = [twox_128(b"Example"), twox_128(b"DoubleMap3")].concat();
k.extend(1u32.using_encoded(blake2_128_concat));
k.extend(2u64.using_encoded(twox_64_concat));
assert_eq!(unhashed::get::<u128>(&k), Some(3u128));
assert_eq!(&k[..32], &<pallet::DoubleMap3<Runtime>>::final_prefix());
assert_eq!(
pallet::DoubleMap3::<Runtime>::get(2, 3),
Err(pallet::Error::<Runtime>::NonExistentStorageValue),
);
pallet::NMap::<Runtime>::insert((&1,), &3);
let mut k = [twox_128(b"Example"), twox_128(b"NMap")].concat();
k.extend(1u8.using_encoded(blake2_128_concat));
@@ -961,6 +1007,17 @@ fn storage_expand() {
assert_eq!(unhashed::get::<u64>(&k), Some(3u64));
assert_eq!(&k[..32], &<pallet::NMap2<Runtime>>::final_prefix());
pallet::NMap3::<Runtime>::insert((&1, &2), &3);
let mut k = [twox_128(b"Example"), twox_128(b"NMap3")].concat();
k.extend(1u8.using_encoded(blake2_128_concat));
k.extend(2u16.using_encoded(twox_64_concat));
assert_eq!(unhashed::get::<u128>(&k), Some(3u128));
assert_eq!(&k[..32], &<pallet::NMap3<Runtime>>::final_prefix());
assert_eq!(
pallet::NMap3::<Runtime>::get((2, 3)),
Err(pallet::Error::<Runtime>::NonExistentStorageValue),
);
#[cfg(feature = "conditional-storage")]
{
pallet::ConditionalValue::<Runtime>::put(1);
@@ -1171,6 +1228,17 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
StorageEntryMetadata {
name: "Map3",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
key: meta_type::<u32>(),
value: meta_type::<u64>(),
hashers: vec![StorageHasher::Blake2_128Concat],
},
default: vec![1, 1],
docs: vec![],
},
StorageEntryMetadata {
name: "DoubleMap",
modifier: StorageEntryModifier::Optional,
@@ -1199,6 +1267,20 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
StorageEntryMetadata {
name: "DoubleMap3",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
value: meta_type::<u128>(),
key: meta_type::<(u32, u64)>(),
hashers: vec![
StorageHasher::Blake2_128Concat,
StorageHasher::Twox64Concat,
],
},
default: vec![1, 1],
docs: vec![],
},
StorageEntryMetadata {
name: "NMap",
modifier: StorageEntryModifier::Optional,
@@ -1224,6 +1306,20 @@ fn metadata() {
default: vec![0],
docs: vec![],
},
StorageEntryMetadata {
name: "NMap3",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
key: meta_type::<(u8, u16)>(),
hashers: vec![
StorageHasher::Blake2_128Concat,
StorageHasher::Twox64Concat,
],
value: meta_type::<u128>(),
},
default: vec![1, 1],
docs: vec![],
},
#[cfg(feature = "conditional-storage")]
StorageEntryMetadata {
name: "ConditionalValue",
@@ -1436,6 +1532,8 @@ fn test_storage_info() {
traits::{StorageInfo, StorageInfoTrait},
};
// Storage max size is calculated by adding up all the hasher size, the key type size and the
// value type size
assert_eq!(
Example::storage_info(),
vec![
@@ -1465,42 +1563,63 @@ fn test_storage_info() {
storage_name: b"Map".to_vec(),
prefix: prefix(b"Example", b"Map").to_vec(),
max_values: None,
max_size: Some(3 + 16),
max_size: Some(16 + 1 + 2),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"Map2".to_vec(),
prefix: prefix(b"Example", b"Map2").to_vec(),
max_values: Some(3),
max_size: Some(6 + 8),
max_size: Some(8 + 2 + 4),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"Map3".to_vec(),
prefix: prefix(b"Example", b"Map3").to_vec(),
max_values: None,
max_size: Some(16 + 4 + 8),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"DoubleMap".to_vec(),
prefix: prefix(b"Example", b"DoubleMap").to_vec(),
max_values: None,
max_size: Some(7 + 16 + 8),
max_size: Some(16 + 1 + 8 + 2 + 4),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"DoubleMap2".to_vec(),
prefix: prefix(b"Example", b"DoubleMap2").to_vec(),
max_values: Some(5),
max_size: Some(14 + 8 + 16),
max_size: Some(8 + 2 + 16 + 4 + 8),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"DoubleMap3".to_vec(),
prefix: prefix(b"Example", b"DoubleMap3").to_vec(),
max_values: None,
max_size: Some(16 + 4 + 8 + 8 + 16),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"NMap".to_vec(),
prefix: prefix(b"Example", b"NMap").to_vec(),
max_values: None,
max_size: Some(5 + 16),
max_size: Some(16 + 1 + 4),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"NMap2".to_vec(),
prefix: prefix(b"Example", b"NMap2").to_vec(),
max_values: Some(11),
max_size: Some(14 + 8 + 16),
max_size: Some(8 + 2 + 16 + 4 + 8),
},
StorageInfo {
pallet_name: b"Example".to_vec(),
storage_name: b"NMap3".to_vec(),
prefix: prefix(b"Example", b"NMap3").to_vec(),
max_values: None,
max_size: Some(16 + 1 + 8 + 2 + 16),
},
#[cfg(feature = "conditional-storage")]
{
@@ -1519,7 +1638,7 @@ fn test_storage_info() {
storage_name: b"ConditionalMap".to_vec(),
prefix: prefix(b"Example", b"ConditionalMap").to_vec(),
max_values: Some(12),
max_size: Some(6 + 8),
max_size: Some(8 + 2 + 4),
}
},
#[cfg(feature = "conditional-storage")]
@@ -1529,7 +1648,7 @@ fn test_storage_info() {
storage_name: b"ConditionalDoubleMap".to_vec(),
prefix: prefix(b"Example", b"ConditionalDoubleMap").to_vec(),
max_values: None,
max_size: Some(7 + 16 + 8),
max_size: Some(16 + 1 + 8 + 2 + 4),
}
},
#[cfg(feature = "conditional-storage")]
@@ -1539,7 +1658,7 @@ fn test_storage_info() {
storage_name: b"ConditionalNMap".to_vec(),
prefix: prefix(b"Example", b"ConditionalNMap").to_vec(),
max_values: None,
max_size: Some(7 + 16 + 8),
max_size: Some(16 + 1 + 8 + 2 + 4),
}
},
StorageInfo {
@@ -1547,7 +1666,7 @@ fn test_storage_info() {
storage_name: b"RenamedCountedMap".to_vec(),
prefix: prefix(b"Example", b"RenamedCountedMap").to_vec(),
max_values: None,
max_size: Some(1 + 4 + 8),
max_size: Some(8 + 1 + 4),
},
StorageInfo {
pallet_name: b"Example".to_vec(),