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 3 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
16 changes: 8 additions & 8 deletions Cargo.lock

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

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 @@ -47,7 +47,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 @@ -73,7 +73,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 @@ -112,7 +112,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 @@ -143,7 +143,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
74 changes: 59 additions & 15 deletions core/state-machine/src/trie_backend_essence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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 @@ -143,6 +143,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 @@ -166,47 +177,80 @@ 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() // TODO: iterate backing
hash_db::PlainDB::keys(self.overlay)
}

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::HashDB<H, DBValue>
for Ephemeral<'a, S, H>
where H::Out: HeapSizeOf
{
fn keys(&self) -> HashMap<H::Out, i32> {
hash_db::HashDB::keys(self.overlay) // TODO: iterate backing
}

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)
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/test-runtime/wasm/Cargo.lock

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

Loading