mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
optimize double map first key hash (#2451)
* fix double map encoding it is now encoded as specified in the doc
This commit is contained in:
@@ -58,8 +58,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
|
||||
spec_name: create_runtime_str!("node"),
|
||||
impl_name: create_runtime_str!("substrate-node"),
|
||||
authoring_version: 10,
|
||||
spec_version: 70,
|
||||
impl_version: 70,
|
||||
spec_version: 71,
|
||||
impl_version: 71,
|
||||
apis: RUNTIME_API_VERSIONS,
|
||||
};
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ use proc_macro::TokenStream;
|
||||
///
|
||||
/// `hasher($hash)` is optional and its default is `blake2_256`.
|
||||
///
|
||||
/// /!\ Be careful with each key in the map that is inserted in the trie `$hash(module_name ++ storage_name ++ key)`.
|
||||
/// /!\ Be careful with each key in the map that is inserted in the trie `$hash(module_name ++ " " ++ storage_name ++ encoding(key))`.
|
||||
/// If the keys are not trusted (e.g. can be set by a user), a cryptographic `hasher` such as
|
||||
/// `blake2_256` must be used. Otherwise, other values in storage can be compromised.
|
||||
///
|
||||
@@ -71,7 +71,7 @@ use proc_macro::TokenStream;
|
||||
/// The final key is calculated as follows:
|
||||
///
|
||||
/// ```nocompile
|
||||
/// $hash(module_name ++ storage_name ++ first_key) ++ $hash2(second_key)
|
||||
/// $hash(module_name ++ " " ++ storage_name ++ encoding(first_key)) ++ $hash2(encoding(second_key))
|
||||
/// ```
|
||||
///
|
||||
/// If the first key is untrusted, a cryptographic `hasher` such as `blake2_256` must be used.
|
||||
|
||||
@@ -621,9 +621,11 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
|
||||
type Query = #value_type;
|
||||
|
||||
fn prefix_for(k1: &#k1ty) -> Vec<u8> {
|
||||
use #scrate::storage::hashed::generator::StorageHasher;
|
||||
|
||||
let mut key = #as_double_map::prefix().to_vec();
|
||||
#scrate::codec::Encode::encode_to(k1, &mut key);
|
||||
#scrate::Hashable::#hasher(&key).to_vec()
|
||||
#scrate::#hasher::hash(&key[..]).to_vec()
|
||||
}
|
||||
|
||||
fn prefix() -> &'static [u8] {
|
||||
|
||||
@@ -234,16 +234,6 @@ impl HasherKind {
|
||||
}
|
||||
}
|
||||
|
||||
fn into_hashable_fn(&self) -> TokenStream2 {
|
||||
match self {
|
||||
HasherKind::Blake2_256 => quote!( blake2_256 ),
|
||||
HasherKind::Blake2_128 => quote!( blake2_128 ),
|
||||
HasherKind::Twox256 => quote!( twox_256 ),
|
||||
HasherKind::Twox128 => quote!( twox_128 ),
|
||||
HasherKind::Twox64Concat => quote!( twox_64_concat),
|
||||
}
|
||||
}
|
||||
|
||||
fn into_metadata(&self) -> TokenStream2 {
|
||||
match self {
|
||||
HasherKind::Blake2_256 => quote!( StorageHasher::Blake2_256 ),
|
||||
|
||||
@@ -596,7 +596,7 @@ fn decl_storage_items(
|
||||
i.linked_map(hasher.into_storage_hasher_struct(), key_type)
|
||||
},
|
||||
DeclStorageTypeInfosKind::DoubleMap { key1_type, key2_type, key2_hasher, hasher } => {
|
||||
i.double_map(hasher.into_hashable_fn(), key1_type, key2_type, key2_hasher)
|
||||
i.double_map(hasher.into_storage_hasher_struct(), key1_type, key2_type, key2_hasher)
|
||||
},
|
||||
};
|
||||
impls.extend(implementation)
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
use runtime_io::{with_externalities, Blake2Hasher};
|
||||
use srml_support::{StorageValue, StorageMap, StorageDoubleMap};
|
||||
use srml_support::storage::unhashed;
|
||||
use srml_support::runtime_primitives::BuildStorage;
|
||||
use parity_codec::Encode;
|
||||
|
||||
pub trait Trait {
|
||||
type Origin;
|
||||
type BlockNumber;
|
||||
}
|
||||
|
||||
srml_support::decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
|
||||
}
|
||||
|
||||
srml_support::decl_storage!{
|
||||
trait Store for Module<T: Trait> as Module {
|
||||
pub Value config(value): u32;
|
||||
|
||||
pub Map: map u32 => u32;
|
||||
pub Map2: map hasher(twox_128) u32 => u32;
|
||||
|
||||
pub LinkedMap: linked_map u32 => u32;
|
||||
pub LinkedMap2: linked_map hasher(twox_128) u32 => u32;
|
||||
|
||||
pub DoubleMap: double_map u32, blake2_256(u32) => u32;
|
||||
pub DoubleMap2: double_map hasher(twox_128) u32, blake2_128(u32) => u32;
|
||||
}
|
||||
}
|
||||
|
||||
struct Test;
|
||||
impl Trait for Test {
|
||||
type BlockNumber = u32;
|
||||
type Origin = u32;
|
||||
}
|
||||
|
||||
fn new_test_ext() -> runtime_io::TestExternalities<Blake2Hasher> {
|
||||
GenesisConfig::<Test>::default().build_storage().unwrap().0.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_keys() {
|
||||
with_externalities(&mut new_test_ext(), || {
|
||||
<Value<Test>>::put(1);
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(b"Module Value")), Some(1u32));
|
||||
|
||||
<Map<Test>>::insert(1, 2);
|
||||
let mut k = b"Module Map".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<Map2<Test>>::insert(1, 2);
|
||||
let mut k = b"Module Map2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
|
||||
<LinkedMap<Test>>::insert(1, 2);
|
||||
let mut k = b"Module LinkedMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::blake2_256(&k)), Some(2u32));
|
||||
|
||||
<LinkedMap2<Test>>::insert(1, 2);
|
||||
let mut k = b"Module LinkedMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
assert_eq!(unhashed::get::<u32>(&runtime_io::twox_128(&k)), Some(2u32));
|
||||
|
||||
<DoubleMap<Test>>::insert(1, 2, 3);
|
||||
let mut k = b"Module DoubleMap".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::blake2_256(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_256(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
|
||||
<DoubleMap2<Test>>::insert(1, 2, 3);
|
||||
let mut k = b"Module DoubleMap2".to_vec();
|
||||
k.extend(1u32.encode());
|
||||
let mut k = runtime_io::twox_128(&k).to_vec();
|
||||
k.extend(&runtime_io::blake2_128(&2u32.encode()));
|
||||
assert_eq!(unhashed::get::<u32>(&k), Some(3u32));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user