mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 23:57:56 +00:00
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:
@@ -31,7 +31,7 @@ use sp_runtime::{DispatchError, ModuleError};
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use codec::MaxEncodedLen;
|
||||
use frame_support::{pallet_prelude::*, scale_info};
|
||||
use frame_support::{pallet_prelude::*, parameter_types, scale_info};
|
||||
use frame_system::pallet_prelude::*;
|
||||
use sp_std::any::TypeId;
|
||||
|
||||
@@ -104,9 +104,11 @@ pub mod pallet {
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
#[derive(PartialEq, Eq)]
|
||||
pub enum Error<T, I = ()> {
|
||||
/// doc comment put into metadata
|
||||
InsufficientProposersBalance,
|
||||
NonExistentStorageValue,
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
@@ -128,6 +130,20 @@ pub mod pallet {
|
||||
#[pallet::storage]
|
||||
pub type Map2<T, I = ()> = StorageMap<_, Twox64Concat, u16, u32>;
|
||||
|
||||
parameter_types! {
|
||||
pub const Map3Default<T, I>: Result<u64, Error<T, I>> = Ok(1337);
|
||||
}
|
||||
|
||||
#[pallet::storage]
|
||||
pub type Map3<T, I = ()> = StorageMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
u64,
|
||||
ResultQuery<Error<T, I>::NonExistentStorageValue>,
|
||||
Map3Default<T, I>,
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap<T, I = ()> =
|
||||
StorageDoubleMap<_, Blake2_128Concat, u8, Twox64Concat, u16, u32>;
|
||||
@@ -136,6 +152,17 @@ pub mod pallet {
|
||||
pub type DoubleMap2<T, I = ()> =
|
||||
StorageDoubleMap<_, Twox64Concat, u16, Blake2_128Concat, u32, u64>;
|
||||
|
||||
#[pallet::storage]
|
||||
pub type DoubleMap3<T, I = ()> = StorageDoubleMap<
|
||||
_,
|
||||
Blake2_128Concat,
|
||||
u32,
|
||||
Twox64Concat,
|
||||
u64,
|
||||
u128,
|
||||
ResultQuery<Error<T, I>::NonExistentStorageValue>,
|
||||
>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn nmap)]
|
||||
pub type NMap<T, I = ()> = StorageNMap<_, storage::Key<Blake2_128Concat, u8>, u32>;
|
||||
@@ -145,6 +172,15 @@ pub mod pallet {
|
||||
pub type NMap2<T, I = ()> =
|
||||
StorageNMap<_, (storage::Key<Twox64Concat, u16>, storage::Key<Blake2_128Concat, u32>), u64>;
|
||||
|
||||
#[pallet::storage]
|
||||
#[pallet::getter(fn nmap3)]
|
||||
pub type NMap3<T, I = ()> = StorageNMap<
|
||||
_,
|
||||
(NMapKey<Blake2_128Concat, u8>, NMapKey<Twox64Concat, u16>),
|
||||
u128,
|
||||
ResultQuery<Error<T, I>::NonExistentStorageValue>,
|
||||
>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(Default)]
|
||||
pub struct GenesisConfig {
|
||||
@@ -436,6 +472,13 @@ 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), Ok(1337));
|
||||
|
||||
<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));
|
||||
@@ -450,6 +493,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));
|
||||
@@ -462,6 +516,17 @@ fn storage_expand() {
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
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),
|
||||
);
|
||||
});
|
||||
|
||||
TestExternalities::default().execute_with(|| {
|
||||
@@ -481,6 +546,13 @@ fn storage_expand() {
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(2u32));
|
||||
assert_eq!(&k[..32], &<pallet::Map2<Runtime, pallet::Instance1>>::final_prefix());
|
||||
|
||||
<pallet::Map3<Runtime, pallet::Instance1>>::insert(1, 2);
|
||||
let mut k = [twox_128(b"Instance1Example"), 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, pallet::Instance1>>::final_prefix());
|
||||
assert_eq!(<pallet::Map3<Runtime, pallet::Instance1>>::get(2), Ok(1337));
|
||||
|
||||
<pallet::DoubleMap<Runtime, pallet::Instance1>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance1Example"), twox_128(b"DoubleMap")].concat();
|
||||
k.extend(1u8.using_encoded(blake2_128_concat));
|
||||
@@ -495,6 +567,17 @@ fn storage_expand() {
|
||||
assert_eq!(unhashed::get::<u64>(&k), Some(3u64));
|
||||
assert_eq!(&k[..32], &<pallet::DoubleMap2<Runtime, pallet::Instance1>>::final_prefix());
|
||||
|
||||
<pallet::DoubleMap3<Runtime, pallet::Instance1>>::insert(&1, &2, &3);
|
||||
let mut k = [twox_128(b"Instance1Example"), 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, pallet::Instance1>>::final_prefix());
|
||||
assert_eq!(
|
||||
<pallet::DoubleMap3<Runtime, pallet::Instance1>>::get(2, 3),
|
||||
Err(pallet::Error::<Runtime, pallet::Instance1>::NonExistentStorageValue),
|
||||
);
|
||||
|
||||
<pallet::NMap<Runtime, pallet::Instance1>>::insert((&1,), &3);
|
||||
let mut k = [twox_128(b"Instance1Example"), twox_128(b"NMap")].concat();
|
||||
k.extend(1u8.using_encoded(blake2_128_concat));
|
||||
@@ -507,6 +590,17 @@ fn storage_expand() {
|
||||
k.extend(2u32.using_encoded(blake2_128_concat));
|
||||
assert_eq!(unhashed::get::<u64>(&k), Some(3u64));
|
||||
assert_eq!(&k[..32], &<pallet::NMap2<Runtime, pallet::Instance1>>::final_prefix());
|
||||
|
||||
<pallet::NMap3<Runtime, pallet::Instance1>>::insert((&1, &2), &3);
|
||||
let mut k = [twox_128(b"Instance1Example"), 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, pallet::Instance1>>::final_prefix());
|
||||
assert_eq!(
|
||||
<pallet::NMap3<Runtime, pallet::Instance1>>::get((2, 3)),
|
||||
Err(pallet::Error::<Runtime, pallet::Instance1>::NonExistentStorageValue),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -688,6 +782,17 @@ fn metadata() {
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "Map3",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Map {
|
||||
key: scale_info::meta_type::<u32>(),
|
||||
value: scale_info::meta_type::<u64>(),
|
||||
hashers: vec![StorageHasher::Blake2_128Concat],
|
||||
},
|
||||
default: vec![0, 57, 5, 0, 0, 0, 0, 0, 0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "DoubleMap",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
@@ -710,6 +815,17 @@ fn metadata() {
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "DoubleMap3",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Map {
|
||||
value: scale_info::meta_type::<u128>(),
|
||||
key: scale_info::meta_type::<(u32, u64)>(),
|
||||
hashers: vec![StorageHasher::Blake2_128Concat, StorageHasher::Twox64Concat],
|
||||
},
|
||||
default: vec![1, 1],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "NMap",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
@@ -732,6 +848,17 @@ fn metadata() {
|
||||
default: vec![0],
|
||||
docs: vec![],
|
||||
},
|
||||
StorageEntryMetadata {
|
||||
name: "NMap3",
|
||||
modifier: StorageEntryModifier::Optional,
|
||||
ty: StorageEntryType::Map {
|
||||
key: scale_info::meta_type::<(u8, u16)>(),
|
||||
hashers: vec![StorageHasher::Blake2_128Concat, StorageHasher::Twox64Concat],
|
||||
value: scale_info::meta_type::<u128>(),
|
||||
},
|
||||
default: vec![1, 1],
|
||||
docs: vec![],
|
||||
},
|
||||
],
|
||||
}),
|
||||
calls: Some(scale_info::meta_type::<pallet::Call<Runtime>>().into()),
|
||||
|
||||
Reference in New Issue
Block a user