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
+24 -6
View File
@@ -50,7 +50,7 @@ use primitives::{
};
#[cfg(feature = "std")]
use trie::{TrieConfiguration, trie_types::Layout};
use ::trie::{TrieConfiguration, trie_types::Layout};
use runtime_interface::{runtime_interface, Pointer};
@@ -186,28 +186,45 @@ pub trait Storage {
}
/// "Commit" all existing operations and compute the resulting storage root.
fn root(&mut self) -> H256 {
///
/// The hashing algorithm is defined by the `Block`.
///
/// Returns the SCALE encoded hash.
fn root(&mut self) -> Vec<u8> {
self.storage_root()
}
/// "Commit" all existing operations and compute the resulting child storage root.
///
/// The hashing algorithm is defined by the `Block`.
///
/// Returns the SCALE encoded hash.
fn child_root(&mut self, child_storage_key: &[u8]) -> Vec<u8> {
let storage_key = child_storage_key_or_panic(child_storage_key);
self.child_storage_root(storage_key)
}
/// "Commit" all existing operations and get the resulting storage change root.
fn changes_root(&mut self, parent_hash: [u8; 32]) -> Option<H256> {
self.storage_changes_root(parent_hash.into()).ok().and_then(|h| h)
/// `parent_hash` is a SCALE encoded hash.
///
/// The hashing algorithm is defined by the `Block`.
///
/// Returns an `Option` that holds the SCALE encoded hash.
fn changes_root(&mut self, parent_hash: &[u8]) -> Option<Vec<u8>> {
self.storage_changes_root(parent_hash).ok().and_then(|h| h)
}
}
/// Interface that provides trie related functionality.
#[runtime_interface]
pub trait Trie {
/// A trie root formed from the iterated items.
fn blake2_256_trie_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
fn blake2_256_root(input: Vec<(Vec<u8>, Vec<u8>)>) -> H256 {
Layout::<primitives::Blake2Hasher>::trie_root(input)
}
/// A trie root formed from the enumerated items.
fn blake2_256_ordered_trie_root(input: Vec<Vec<u8>>) -> H256 {
fn blake2_256_ordered_root(input: Vec<Vec<u8>>) -> H256 {
Layout::<primitives::Blake2Hasher>::ordered_trie_root(input)
}
}
@@ -779,6 +796,7 @@ pub type SubstrateHostFunctions = (
allocator::HostFunctions,
logging::HostFunctions,
sandbox::HostFunctions,
crate::trie::HostFunctions,
);
#[cfg(test)]