Make use of StorageHasher and HasherKind (#2836)

* Make use of StorageHasher and HasherKind.

* Use Storagehasher hasher function for key2 of double map.

* Refactor to reuse hasher from implementation.

* Bump up since there is srml changes.

* Update metadata version.
This commit is contained in:
kaichao
2019-06-12 03:34:10 +08:00
committed by Bastian Köcher
parent c1654ecd23
commit cae324598f
8 changed files with 33 additions and 22 deletions
@@ -636,8 +636,10 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
}
fn key_for(k1: &#k1ty, k2: &#k2ty) -> Vec<u8> {
use #scrate::storage::hashed::generator::StorageHasher;
let mut key = #as_double_map::prefix_for(k1);
key.extend(&#scrate::Hashable::#k2_hasher(k2));
#scrate::codec::Encode::using_encoded(k2, |e| key.extend(&#scrate::#k2_hasher::hash(e)));
key
}
@@ -214,7 +214,13 @@ enum HasherKind {
impl From<&SetHasher> for HasherKind {
fn from(set_hasher: &SetHasher) -> Self {
match set_hasher.inner.content {
(&set_hasher.inner.content).into()
}
}
impl From<&Hasher> for HasherKind {
fn from(hasher: &Hasher) -> Self {
match hasher {
Hasher::Blake2_256(_) => HasherKind::Blake2_256,
Hasher::Blake2_128(_) => HasherKind::Blake2_128,
Hasher::Twox256(_) => HasherKind::Twox256,
@@ -223,6 +229,7 @@ impl From<&SetHasher> for HasherKind {
}
}
}
impl HasherKind {
fn into_storage_hasher_struct(&self) -> TokenStream2 {
match self {
@@ -606,7 +606,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_storage_hasher_struct(), key1_type, key2_type, key2_hasher)
i.double_map(hasher.into_storage_hasher_struct(), key1_type, key2_type, key2_hasher.into_storage_hasher_struct())
},
};
impls.extend(implementation)
@@ -758,14 +758,14 @@ fn store_functions_to_metadata (
let hasher = hasher.into_metadata();
let k1ty = clean_type_string(&quote!(#key1_type).to_string());
let k2ty = clean_type_string(&quote!(#key2_type).to_string());
let k2_hasher = clean_type_string(&key2_hasher.to_string());
let k2_hasher = key2_hasher.into_metadata();
quote!{
#scrate::metadata::StorageFunctionType::DoubleMap {
hasher: #scrate::metadata::#hasher,
key1: #scrate::metadata::DecodeDifferent::Encode(#k1ty),
key2: #scrate::metadata::DecodeDifferent::Encode(#k2ty),
value: #scrate::metadata::DecodeDifferent::Encode(#styp),
key2_hasher: #scrate::metadata::DecodeDifferent::Encode(#k2_hasher),
key2_hasher: #scrate::metadata::#k2_hasher,
}
}
},
@@ -870,7 +870,7 @@ enum DeclStorageTypeInfosKind<'a> {
hasher: HasherKind,
key1_type: &'a syn::Type,
key2_type: &'a syn::Type,
key2_hasher: TokenStream2,
key2_hasher: HasherKind,
}
}
@@ -900,7 +900,7 @@ fn get_type_infos(storage_type: &DeclStorageType) -> DeclStorageTypeInfos {
hasher: map.hasher.inner.as_ref().map(|h| h.into()).unwrap_or(HasherKind::Blake2_256),
key1_type: &map.key1,
key2_type: &map.key2.content,
key2_hasher: { let h = &map.key2_hasher; quote! { #h } },
key2_hasher: (&map.key2_hasher).into(),
}),
};