Storage root for Ext.

This commit is contained in:
Gav
2018-01-30 19:21:54 +01:00
parent 06817aed51
commit 426486617a
5 changed files with 25 additions and 9 deletions
+7
View File
@@ -40,6 +40,9 @@ pub trait Backend {
/// Commit updates to the backend and get new state.
fn commit<I>(&mut self, changes: I) -> Committed
where I: IntoIterator<Item=Update>;
/// Get all key/value pairs into a Vec.
fn pairs(&self) -> Vec<(&[u8], &[u8])>;
}
/// Error impossible.
@@ -87,6 +90,10 @@ impl Backend for InMemory {
storage_tree_root,
}
}
fn pairs(&self) -> Vec<(&[u8], &[u8])> {
self.inner.storage.iter().map(|(k, v)| (&k[..], &v[..])).collect()
}
}
// TODO: DB-based backend
+10 -3
View File
@@ -17,7 +17,7 @@
//! Conrete externalities implementation.
use std::{error, fmt};
use triehash::trie_root;
use backend::Backend;
use {Externalities, ExternalitiesError, OverlayedChanges};
@@ -76,7 +76,14 @@ impl<'a, B: 'a> Externalities for Ext<'a, B>
42
}
fn commit(&self) -> [u8; 32] {
unimplemented!();
fn storage_root(&self) -> [u8; 32] {
let mut all_pairs = self.backend.pairs();
all_pairs.extend(
self.overlay.committed.storage.iter()
.chain(self.overlay.prospective.storage.iter())
.map(|(k, v)| (&k[..], &v[..]))
);
trie_root(all_pairs.into_iter().map(|(k, v)| (k.to_vec(), v.to_vec()))).0
}
}
+1 -1
View File
@@ -147,7 +147,7 @@ pub trait Externalities {
fn chain_id(&self) -> u64;
/// Get the trie root of the current storage map.
fn commit(&self) -> [u8; 32];
fn storage_root(&self) -> [u8; 32];
/// Get the current set of authorities from storage.
fn authorities(&self) -> Result<Vec<&[u8]>, ExternalitiesError> {
+5 -3
View File
@@ -23,11 +23,13 @@ use triehash::trie_root;
/// Simple HashMap based Externalities impl.
#[derive(Debug, Default)]
pub struct TestExternalities {
/// The storage.
pub storage: HashMap<Vec<u8>, Vec<u8>>,
}
impl TestExternalities {
fn new() -> Self {
/// Create a new instance with empty storage.
pub fn new() -> Self {
TestExternalities {
storage: HashMap::new(),
}
@@ -45,7 +47,7 @@ impl Externalities for TestExternalities {
fn chain_id(&self) -> u64 { 42 }
fn commit(&self) -> [u8; 32] {
fn storage_root(&self) -> [u8; 32] {
trie_root(self.storage.clone()).0
}
}
@@ -61,6 +63,6 @@ mod tests {
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!("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3");
assert_eq!(ext.commit(), ROOT);
assert_eq!(ext.storage_root(), ROOT);
}
}