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:
thiolliere
2019-05-06 08:44:36 +02:00
committed by GitHub
parent 5a820db7f5
commit b0c21da94f
6 changed files with 89 additions and 16 deletions
+2 -2
View File
@@ -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)