mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 03:31:03 +00:00
2604474880
* Implement a non generic version of child delta trie * Use delta_trie_root in state_machine * Expand InMemory backend to support multi-storage * Create Consolidate trait * Fix all crate compile and remove unused OverlayedChanges::drain * Implement child storage root support and overlay changes * Add child storage reader * Add child storage writer * Implement child storage cleaning * Fix light backend compile * Add all required ext functions for wasm executor * Add ext def to io * Add all io functions * Fix nostd compile * Add simple test * Remove unnecessary vec copy in child_storage_root_transaction * Use values_mut/for_each to make it shorter * Use extend to shorter a for loop * Move record_all_keys to trie so it's easier to generic them later * space -> tab * Remove to_owned in debug format * Clean out all to_owned * Break debug_trace to multiple lines * Remove 0.. * UserError copy/paste typo * Replace Vec::from_raw_parts by slice::from_raw_parts * Use iter::empty() * Wrap some long lines * Wrap a missing line * Remove unnecessary map https://github.com/paritytech/substrate/pull/856#discussion_r226222663 * Call ext_free after from_raw_parts * Fix tests in other crates
164 lines
5.0 KiB
Rust
164 lines
5.0 KiB
Rust
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Test implementation for Externalities.
|
|
|
|
use std::collections::HashMap;
|
|
use std::iter::FromIterator;
|
|
use hash_db::Hasher;
|
|
use heapsize::HeapSizeOf;
|
|
use trie::trie_root;
|
|
use backend::InMemory;
|
|
use changes_trie::{compute_changes_trie_root, InMemoryStorage as ChangesTrieInMemoryStorage};
|
|
use primitives::storage::well_known_keys::CHANGES_TRIE_CONFIG;
|
|
use super::{Externalities, OverlayedChanges};
|
|
|
|
/// Simple HashMap-based Externalities impl.
|
|
pub struct TestExternalities<H: Hasher> where H::Out: HeapSizeOf {
|
|
inner: HashMap<Vec<u8>, Vec<u8>>,
|
|
changes_trie_storage: ChangesTrieInMemoryStorage<H>,
|
|
changes: OverlayedChanges,
|
|
}
|
|
|
|
impl<H: Hasher> TestExternalities<H> where H::Out: HeapSizeOf {
|
|
/// Create a new instance of `TestExternalities`
|
|
pub fn new(inner: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
|
let mut overlay = OverlayedChanges::default();
|
|
super::set_changes_trie_config(
|
|
&mut overlay,
|
|
inner.get(&CHANGES_TRIE_CONFIG.to_vec()).cloned(),
|
|
false,
|
|
).expect("changes trie configuration is correct in test env; qed");
|
|
|
|
TestExternalities {
|
|
inner,
|
|
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
|
|
changes: overlay,
|
|
}
|
|
}
|
|
|
|
/// Insert key/value
|
|
pub fn insert(&mut self, k: Vec<u8>, v: Vec<u8>) -> Option<Vec<u8>> {
|
|
self.inner.insert(k, v)
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> ::std::fmt::Debug for TestExternalities<H> where H::Out: HeapSizeOf {
|
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
|
write!(f, "{:?}", self.inner)
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> PartialEq for TestExternalities<H> where H::Out: HeapSizeOf {
|
|
fn eq(&self, other: &TestExternalities<H>) -> bool {
|
|
self.inner.eq(&other.inner)
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> FromIterator<(Vec<u8>, Vec<u8>)> for TestExternalities<H> where H::Out: HeapSizeOf {
|
|
fn from_iter<I: IntoIterator<Item=(Vec<u8>, Vec<u8>)>>(iter: I) -> Self {
|
|
let mut t = Self::new(Default::default());
|
|
for i in iter {
|
|
t.inner.insert(i.0, i.1);
|
|
}
|
|
t
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> Default for TestExternalities<H> where H::Out: HeapSizeOf {
|
|
fn default() -> Self { Self::new(Default::default()) }
|
|
}
|
|
|
|
impl<H: Hasher> From<TestExternalities<H>> for HashMap<Vec<u8>, Vec<u8>> where H::Out: HeapSizeOf {
|
|
fn from(tex: TestExternalities<H>) -> Self {
|
|
tex.inner.into()
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> From< HashMap<Vec<u8>, Vec<u8>> > for TestExternalities<H> where H::Out: HeapSizeOf {
|
|
fn from(hashmap: HashMap<Vec<u8>, Vec<u8>>) -> Self {
|
|
TestExternalities {
|
|
inner: hashmap,
|
|
changes_trie_storage: ChangesTrieInMemoryStorage::new(),
|
|
changes: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<H: Hasher> Externalities<H> for TestExternalities<H> where H::Out: Ord + HeapSizeOf {
|
|
fn storage(&self, key: &[u8]) -> Option<Vec<u8>> {
|
|
self.inner.get(key).map(|x| x.to_vec())
|
|
}
|
|
|
|
fn child_storage(&self, _storage_key: &[u8], _key: &[u8]) -> Option<Vec<u8>> {
|
|
None
|
|
}
|
|
|
|
fn place_storage(&mut self, key: Vec<u8>, maybe_value: Option<Vec<u8>>) {
|
|
self.changes.set_storage(key.clone(), maybe_value.clone());
|
|
match maybe_value {
|
|
Some(value) => { self.inner.insert(key, value); }
|
|
None => { self.inner.remove(&key); }
|
|
}
|
|
}
|
|
|
|
fn place_child_storage(&mut self, _storage_key: Vec<u8>, _key: Vec<u8>, _value: Option<Vec<u8>>) -> bool {
|
|
false
|
|
}
|
|
|
|
fn kill_child_storage(&mut self, _storage_key: &[u8]) { }
|
|
|
|
fn clear_prefix(&mut self, prefix: &[u8]) {
|
|
self.changes.clear_prefix(prefix);
|
|
self.inner.retain(|key, _| !key.starts_with(prefix));
|
|
}
|
|
|
|
fn chain_id(&self) -> u64 { 42 }
|
|
|
|
fn storage_root(&mut self) -> H::Out {
|
|
trie_root::<H, _, _, _>(self.inner.clone())
|
|
}
|
|
|
|
fn child_storage_root(&mut self, _storage_key: &[u8]) -> Option<Vec<u8>> {
|
|
None
|
|
}
|
|
|
|
fn storage_changes_root(&mut self, block: u64) -> Option<H::Out> {
|
|
compute_changes_trie_root::<_, _, H>(
|
|
&InMemory::default(),
|
|
Some(&self.changes_trie_storage),
|
|
&self.changes,
|
|
block,
|
|
).map(|(root, _)| root.clone())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use primitives::{Blake2Hasher, H256};
|
|
|
|
#[test]
|
|
fn commit_should_work() {
|
|
let mut ext = TestExternalities::<Blake2Hasher>::default();
|
|
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
|
|
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
|
|
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
|
|
const ROOT: [u8; 32] = hex!("0b41e488cccbd67d1f1089592c2c235f5c5399b053f7fe9152dd4b5f279914cd");
|
|
assert_eq!(ext.storage_root(), H256(ROOT));
|
|
}
|
|
}
|