// Copyright 2017-2019 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 .
//! Basic implementation for Externalities.
use std::collections::HashMap;
use std::iter::FromIterator;
use crate::backend::{Backend, InMemory};
use hash_db::Hasher;
use trie::TrieConfiguration;
use trie::trie_types::Layout;
use primitives::offchain;
use primitives::storage::well_known_keys::is_child_storage_key;
use super::{ChildStorageKey, Externalities};
use log::warn;
/// Simple HashMap-based Externalities impl.
#[derive(Debug)]
pub struct BasicExternalities {
top: HashMap, Vec>,
children: HashMap, HashMap, Vec>>,
}
impl BasicExternalities {
/// Create a new instance of `BasicExternalities`
pub fn new(top: HashMap, Vec>) -> Self {
Self::new_with_children(top, Default::default())
}
/// Create a new instance of `BasicExternalities` with children
pub fn new_with_children(
top: HashMap, Vec>,
children: HashMap, HashMap, Vec>>,
) -> Self {
BasicExternalities {
top,
children,
}
}
/// Insert key/value
pub fn insert(&mut self, k: Vec, v: Vec) -> Option> {
self.top.insert(k, v)
}
/// Consume self and returns inner storages
pub fn into_storages(self) -> (
HashMap, Vec>,
HashMap, HashMap, Vec>>,
) {
(self.top, self.children)
}
}
impl PartialEq for BasicExternalities {
fn eq(&self, other: &BasicExternalities) -> bool {
self.top.eq(&other.top) && self.children.eq(&other.children)
}
}
impl FromIterator<(Vec, Vec)> for BasicExternalities {
fn from_iter, Vec)>>(iter: I) -> Self {
let mut t = Self::default();
t.top.extend(iter);
t
}
}
impl Default for BasicExternalities {
fn default() -> Self { Self::new(Default::default()) }
}
impl From, Vec>> for BasicExternalities {
fn from(hashmap: HashMap, Vec>) -> Self {
BasicExternalities {
top: hashmap,
children: Default::default(),
}
}
}
impl Externalities for BasicExternalities where H::Out: Ord {
fn storage(&self, key: &[u8]) -> Option> {
self.top.get(key).cloned()
}
fn original_storage(&self, key: &[u8]) -> Option> {
Externalities::::storage(self, key)
}
fn child_storage(&self, storage_key: ChildStorageKey, key: &[u8]) -> Option> {
self.children.get(storage_key.as_ref()).and_then(|child| child.get(key)).cloned()
}
fn place_storage(&mut self, key: Vec, maybe_value: Option>) {
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to set child storage key via main storage");
return;
}
match maybe_value {
Some(value) => { self.top.insert(key, value); }
None => { self.top.remove(&key); }
}
}
fn place_child_storage(
&mut self,
storage_key: ChildStorageKey,
key: Vec,
value: Option>
) {
let child_map = self.children.entry(storage_key.into_owned()).or_default();
if let Some(value) = value {
child_map.insert(key, value);
} else {
child_map.remove(&key);
}
}
fn kill_child_storage(&mut self, storage_key: ChildStorageKey) {
self.children.remove(storage_key.as_ref());
}
fn clear_prefix(&mut self, prefix: &[u8]) {
if is_child_storage_key(prefix) {
warn!(
target: "trie",
"Refuse to clear prefix that is part of child storage key via main storage"
);
return;
}
self.top.retain(|key, _| !key.starts_with(prefix));
}
fn chain_id(&self) -> u64 { 42 }
fn storage_root(&mut self) -> H::Out {
Layout::::trie_root(self.top.clone())
}
fn child_storage_root(&mut self, storage_key: ChildStorageKey) -> Vec {
if let Some(child) = self.children.get(storage_key.as_ref()) {
let delta = child.clone().into_iter().map(|(k, v)| (k, Some(v)));
InMemory::::default().child_storage_root(storage_key.as_ref(), delta).0
} else {
vec![]
}
}
fn storage_changes_root(&mut self, _parent: H::Out) -> Result