Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Support multi-hash in multi-trie via PlainDB #1106

Merged
merged 15 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ is-it-maintained-open-issues = { repository = "paritytech/substrate" }
[profile.release]
# Substrate runtime requires unwinding.
panic = "unwind"

[patch.crates-io]
hash-db = { git = "https://github.com/paritytech/trie" }
hash256-std-hasher = { git = "https://github.com/paritytech/trie" }
keccak-hasher = { git = "https://github.com/paritytech/trie" }
memory-db = { git = "https://github.com/paritytech/trie" }
trie-bench = { git = "https://github.com/paritytech/trie" }
trie-db = { git = "https://github.com/paritytech/trie" }
trie-root = { git = "https://github.com/paritytech/trie" }
trie-standardmap = { git = "https://github.com/paritytech/trie" }
43 changes: 39 additions & 4 deletions core/executor/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions core/executor/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ lto = true

[workspace]
members = []

[patch.crates-io]
hash-db = { git = "https://github.com/paritytech/trie" }
hash256-std-hasher = { git = "https://github.com/paritytech/trie" }
keccak-hasher = { git = "https://github.com/paritytech/trie" }
memory-db = { git = "https://github.com/paritytech/trie" }
trie-bench = { git = "https://github.com/paritytech/trie" }
trie-db = { git = "https://github.com/paritytech/trie" }
trie-root = { git = "https://github.com/paritytech/trie" }
trie-standardmap = { git = "https://github.com/paritytech/trie" }
4 changes: 2 additions & 2 deletions core/state-machine/src/proving_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'a, S, H> ProvingBackendEssence<'a, S, H>

let map_e = |e| format!("Trie lookup error: {}", e);

read_trie_value_with(&eph, self.backend.root(), key, &mut *self.proof_recorder).map_err(map_e)
read_trie_value_with::<H, _, Ephemeral<S, H>>(&eph, self.backend.root(), key, &mut *self.proof_recorder).map_err(map_e)
}

pub fn child_storage(&mut self, storage_key: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, String> {
Expand All @@ -74,7 +74,7 @@ impl<'a, S, H> ProvingBackendEssence<'a, S, H>

let mut iter = move || -> Result<(), Box<TrieError<H::Out>>> {
let root = self.backend.root();
record_all_keys::<H>(&eph, root, &mut *self.proof_recorder)
record_all_keys::<H, _>(&eph, root, &mut *self.proof_recorder)
};

if let Err(e) = iter() {
Expand Down
4 changes: 2 additions & 2 deletions core/state-machine/src/trie_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<S: TrieBackendStorage<H>, H: Hasher> Backend<H> for TrieBackend<S, H> where
&mut write_overlay,
);

match delta_trie_root::<H, _, _, _>(&mut eph, root, delta) {
match delta_trie_root::<H, _, _, _, _>(&mut eph, root, delta) {
Ok(ret) => root = ret,
Err(e) => warn!(target: "trie", "Failed to write to trie: {}", e),
}
Expand Down Expand Up @@ -169,7 +169,7 @@ impl<S: TrieBackendStorage<H>, H: Hasher> Backend<H> for TrieBackend<S, H> where
&mut write_overlay,
);

match child_delta_trie_root::<H, _, _, _>(storage_key, &mut eph, root.clone(), delta) {
match child_delta_trie_root::<H, _, _, _, _>(storage_key, &mut eph, root.clone(), delta) {
Ok(ret) => root = ret,
Err(e) => warn!(target: "trie", "Failed to write to trie: {}", e),
}
Expand Down
97 changes: 77 additions & 20 deletions core/state-machine/src/trie_backend_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
//! Trie-based state machine backend essence used to read values
//! from storage.

use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;
use log::{debug, warn};
Expand Down Expand Up @@ -106,7 +105,7 @@ impl<S: TrieBackendStorage<H>, H: Hasher> TrieBackendEssence<S, H> where H::Out:
overlay: &mut read_overlay,
};

if let Err(e) = for_keys_in_child_trie::<H, _>(storage_key, &eph, &root, f) {
if let Err(e) = for_keys_in_child_trie::<H, _, Ephemeral<S, H>>(storage_key, &eph, &root, f) {
debug!(target: "trie", "Error while iterating child storage: {}", e);
}
}
Expand Down Expand Up @@ -149,6 +148,17 @@ pub(crate) struct Ephemeral<'a, S: 'a + TrieBackendStorage<H>, H: 'a + Hasher> {
overlay: &'a mut MemoryDB<H>,
}

impl<'a,
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher
> hash_db::AsPlainDB<H::Out, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn as_plain_db<'b>(&'b self) -> &'b (hash_db::PlainDB<H::Out, DBValue> + 'b) { self }
fn as_plain_db_mut<'b>(&'b mut self) -> &'b mut (hash_db::PlainDB<H::Out, DBValue> + 'b) { self }
}

impl<'a,
S: 'a + TrieBackendStorage<H>,
H: 'a + Hasher
Expand All @@ -172,50 +182,97 @@ impl<'a, S: TrieBackendStorage<H>, H: Hasher> Ephemeral<'a, S, H> {
impl<'a,
S: 'a + TrieBackendStorage<H>,
H: Hasher
> hash_db::HashDB<H, DBValue>
> hash_db::PlainDB<H::Out, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn keys(&self) -> HashMap<H::Out, i32> {
self.overlay.keys()
}

fn get(&self, key: &H::Out) -> Option<DBValue> {
match self.overlay.raw(key) {
Some((val, i)) => {
if i <= 0 {
if let Some(val) = hash_db::PlainDB::get(self.overlay, key) {
Some(val)
} else {
match self.storage.get(&key) {
Ok(x) => x,
Err(e) => {
warn!(target: "trie", "Failed to read from DB: {}", e);
None
} else {
Some(val.clone())
}
},
}
None => match self.storage.get(&key) {
}
}

fn contains(&self, key: &H::Out) -> bool {
hash_db::PlainDB::get(self, key).is_some()
}

fn emplace(&mut self, key: H::Out, value: DBValue) {
hash_db::PlainDB::emplace(self.overlay, key, value)
}

fn remove(&mut self, key: &H::Out) {
hash_db::PlainDB::remove(self.overlay, key)
}
}

impl<'a,
S: 'a + TrieBackendStorage<H>,
H: Hasher
> hash_db::PlainDBRef<H::Out, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn get(&self, key: &H::Out) -> Option<DBValue> { hash_db::PlainDB::get(self, key) }
fn contains(&self, key: &H::Out) -> bool { hash_db::PlainDB::contains(self, key) }
}

impl<'a,
S: 'a + TrieBackendStorage<H>,
H: Hasher
> hash_db::HashDB<H, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn get(&self, key: &H::Out) -> Option<DBValue> {
if let Some(val) = hash_db::HashDB::get(self.overlay, key) {
Some(val)
} else {
match self.storage.get(&key) {
Ok(x) => x,
Err(e) => {
warn!(target: "trie", "Failed to read from DB: {}", e);
None
},
},
}
}
}

fn contains(&self, key: &H::Out) -> bool {
self.get(key).is_some()
hash_db::HashDB::get(self, key).is_some()
}

fn insert(&mut self, value: &[u8]) -> H::Out {
self.overlay.insert(value)
hash_db::HashDB::insert(self.overlay, value)
}

fn emplace(&mut self, key: H::Out, value: DBValue) {
self.overlay.emplace(key, value)
hash_db::HashDB::emplace(self.overlay, key, value)
}

fn remove(&mut self, key: &H::Out) {
self.overlay.remove(key)
hash_db::HashDB::remove(self.overlay, key)
}
}

impl<'a,
S: 'a + TrieBackendStorage<H>,
H: Hasher
> hash_db::HashDBRef<H, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn get(&self, key: &H::Out) -> Option<DBValue> { hash_db::HashDB::get(self, key) }
fn contains(&self, key: &H::Out) -> bool { hash_db::HashDB::contains(self, key) }
}

/// Key-value pairs storage that is used by trie backend essence.
pub trait TrieBackendStorage<H: Hasher>: Send + Sync {
/// Get the value stored at key.
Expand All @@ -232,7 +289,7 @@ impl<H: Hasher> TrieBackendStorage<H> for Arc<Storage<H>> {
// This implementation is used by test storage trie clients.
impl<H: Hasher> TrieBackendStorage<H> for MemoryDB<H> {
fn get(&self, key: &H::Out) -> Result<Option<DBValue>, String> {
Ok(<Self as hash_db::HashDB<H, DBValue>>::get(self, key))
Ok(hash_db::PlainDB::get(self, key))
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/test-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ std = [
"runtime_version/std",
"consensus_aura/std",
]

[patch.crates-io]
hash-db = { git = "https://github.com/paritytech/trie" }
hash256-std-hasher = { git = "https://github.com/paritytech/trie" }
keccak-hasher = { git = "https://github.com/paritytech/trie" }
memory-db = { git = "https://github.com/paritytech/trie" }
trie-bench = { git = "https://github.com/paritytech/trie" }
trie-db = { git = "https://github.com/paritytech/trie" }
trie-root = { git = "https://github.com/paritytech/trie" }
trie-standardmap = { git = "https://github.com/paritytech/trie" }
Loading