Skip to content

Commit

Permalink
Refactored in-mem backed to use the actual trie (paritytech#5730)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar authored Apr 23, 2020
1 parent 5a0754d commit 8a47b4f
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 304 deletions.
13 changes: 9 additions & 4 deletions client/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ pub struct BlockImportOperation<Block: BlockT> {
pending_block: Option<PendingBlock<Block>>,
pending_cache: HashMap<CacheKeyId, Vec<u8>>,
old_state: InMemoryBackend<HashFor<Block>>,
new_state: Option<InMemoryBackend<HashFor<Block>>>,
new_state: Option<<InMemoryBackend<HashFor<Block>> as StateBackend<HashFor<Block>>>::Transaction>,
aux: Vec<(Vec<u8>, Option<Vec<u8>>)>,
finalized_blocks: Vec<(BlockId<Block>, Option<Justification>)>,
set_head: Option<BlockId<Block>>,
Expand Down Expand Up @@ -502,7 +502,7 @@ impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperatio
&mut self,
update: <InMemoryBackend<HashFor<Block>> as StateBackend<HashFor<Block>>>::Transaction,
) -> sp_blockchain::Result<()> {
self.new_state = Some(self.old_state.update(update));
self.new_state = Some(update);
Ok(())
}

Expand All @@ -525,7 +525,7 @@ impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperatio
child_delta
);

self.new_state = Some(InMemoryBackend::from(transaction));
self.new_state = Some(transaction);
Ok(root)
}

Expand Down Expand Up @@ -638,7 +638,12 @@ impl<Block: BlockT> backend::Backend<Block> for Backend<Block> where Block::Hash

let hash = header.hash();

self.states.write().insert(hash, operation.new_state.unwrap_or_else(|| old_state.clone()));
let new_state = match operation.new_state {
Some(state) => old_state.update_backend(*header.state_root(), state),
None => old_state.clone(),
};

self.states.write().insert(hash, new_state);

self.blockchain.insert(hash, header, justification, body, pending_block.state)?;
}
Expand Down
2 changes: 1 addition & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ mod tests {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("489ae9b57a19bb4733a264dc64bbcae9b140a904657a681ed3bb5fbbe8cf412b").into(),
state_root: hex!("409fb5a14aeb8b8c59258b503396a56dee45a0ee28a78de3e622db957425e275").into(),
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
digest: Digest { logs: vec![], },
},
Expand Down
10 changes: 5 additions & 5 deletions primitives/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

//! State machine backends. These manage the code and storage of contracts.
use log::warn;
use hash_db::Hasher;
use codec::{Decode, Encode};

use sp_core::{traits::RuntimeCode, storage::{ChildInfo, well_known_keys}};
use sp_trie::{TrieMut, MemoryDB, trie_types::TrieDBMut};

use crate::{
trie_backend::TrieBackend,
Expand Down Expand Up @@ -334,17 +331,20 @@ impl<H: Hasher, KF: sp_trie::KeyFunction<H>> Consolidate for sp_trie::GenericMem
}

/// Insert input pairs into memory db.
pub(crate) fn insert_into_memory_db<H, I>(mdb: &mut MemoryDB<H>, input: I) -> Option<H::Out>
#[cfg(test)]
pub(crate) fn insert_into_memory_db<H, I>(mdb: &mut sp_trie::MemoryDB<H>, input: I) -> Option<H::Out>
where
H: Hasher,
I: IntoIterator<Item=(StorageKey, StorageValue)>,
{
use sp_trie::{TrieMut, trie_types::TrieDBMut};

let mut root = <H as Hasher>::Out::default();
{
let mut trie = TrieDBMut::<H>::new(mdb, &mut root);
for (key, value) in input {
if let Err(e) = trie.insert(&key, &value) {
warn!(target: "trie", "Failed to write to trie: {}", e);
log::warn!(target: "trie", "Failed to write to trie: {}", e);
return None;
}
}
Expand Down
5 changes: 2 additions & 3 deletions primitives/state-machine/src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use std::{
collections::BTreeMap, any::{TypeId, Any}, iter::FromIterator, ops::Bound
};
use crate::{Backend, InMemoryBackend, StorageKey, StorageValue};
use crate::{Backend, StorageKey, StorageValue};
use hash_db::Hasher;
use sp_trie::{TrieConfiguration, empty_child_trie_root};
use sp_trie::trie_types::Layout;
Expand Down Expand Up @@ -284,8 +284,7 @@ impl Externalities for BasicExternalities {
) -> Vec<u8> {
if let Some(child) = self.inner.children_default.get(child_info.storage_key()) {
let delta = child.data.clone().into_iter().map(|(k, v)| (k, Some(v)));

InMemoryBackend::<Blake2Hasher>::default()
crate::in_memory_backend::new_in_mem::<Blake2Hasher>()
.child_storage_root(&child.child_info, delta).0
} else {
empty_child_trie_root::<Layout<Blake2Hasher>>()
Expand Down
Loading

0 comments on commit 8a47b4f

Please sign in to comment.