The storage runtime interface should not enforce a hash type (#4231)

* The storage runtime interface should not enforce a hash type

Currently the runtime interface enforces `H256` as hash type, but in the
future people could use whatever they want as hash type. The hash type
always needs to match between the runtime and the node, but that is
already required.

* Update primitives/externalities/src/lib.rs

Co-Authored-By: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Bastian Köcher
2019-11-28 01:00:29 +01:00
committed by Gavin Wood
parent 39388b826b
commit 3e26fceda4
29 changed files with 253 additions and 165 deletions
+18 -13
View File
@@ -26,9 +26,10 @@ use primitives::{
well_known_keys::is_child_storage_key, ChildStorageKey, StorageOverlay,
ChildrenStorageOverlay
},
traits::Externalities, Blake2Hasher, hash::H256,
traits::Externalities, Blake2Hasher,
};
use log::warn;
use codec::Encode;
/// Simple HashMap-based Externalities impl.
#[derive(Debug)]
@@ -118,15 +119,15 @@ impl Externalities for BasicExternalities {
self.top.get(key).cloned()
}
fn storage_hash(&self, key: &[u8]) -> Option<H256> {
self.storage(key).map(|v| Blake2Hasher::hash(&v))
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn original_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage(key)
}
fn original_storage_hash(&self, key: &[u8]) -> Option<H256> {
fn original_storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
self.storage_hash(key)
}
@@ -134,11 +135,15 @@ impl Externalities for BasicExternalities {
self.children.get(storage_key.as_ref()).and_then(|child| child.get(key)).cloned()
}
fn child_storage_hash(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<H256> {
self.child_storage(storage_key, key).map(|v| Blake2Hasher::hash(&v))
fn child_storage_hash(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<Vec<u8>> {
self.child_storage(storage_key, key).map(|v| Blake2Hasher::hash(&v).encode())
}
fn original_child_storage_hash(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option<H256> {
fn original_child_storage_hash(
&self,
storage_key: ChildStorageKey,
key: &[u8],
) -> Option<Vec<u8>> {
self.child_storage_hash(storage_key, key)
}
@@ -196,7 +201,7 @@ impl Externalities for BasicExternalities {
fn chain_id(&self) -> u64 { 42 }
fn storage_root(&mut self) -> H256 {
fn storage_root(&mut self) -> Vec<u8> {
let mut top = self.top.clone();
let keys: Vec<_> = self.children.keys().map(|k| k.to_vec()).collect();
// Single child trie implementation currently allows using the same child
@@ -215,7 +220,7 @@ impl Externalities for BasicExternalities {
}
}
Layout::<Blake2Hasher>::trie_root(self.top.clone())
Layout::<Blake2Hasher>::trie_root(self.top.clone()).as_ref().into()
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey) -> Vec<u8> {
@@ -225,10 +230,10 @@ impl Externalities for BasicExternalities {
InMemory::<Blake2Hasher>::default().child_storage_root(storage_key.as_ref(), delta).0
} else {
default_child_trie_root::<Layout<Blake2Hasher>>(storage_key.as_ref())
}
}.encode()
}
fn storage_changes_root(&mut self, _parent: H256) -> Result<Option<H256>, ()> {
fn storage_changes_root(&mut self, _parent: &[u8]) -> Result<Option<Vec<u8>>, ()> {
Ok(None)
}
}
@@ -243,7 +248,7 @@ impl externalities::ExtensionStore for BasicExternalities {
#[cfg(test)]
mod tests {
use super::*;
use primitives::{H256, map};
use primitives::map;
use primitives::storage::well_known_keys::CODE;
use hex_literal::hex;
@@ -255,7 +260,7 @@ mod tests {
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
const ROOT: [u8; 32] = hex!("39245109cef3758c2eed2ccba8d9b370a917850af3824bc8348d505df2c298fa");
assert_eq!(ext.storage_root(), H256::from(ROOT));
assert_eq!(&ext.storage_root()[..], &ROOT);
}
#[test]