TestExternalities can commit.

This commit is contained in:
Gav
2018-01-30 18:41:32 +01:00
parent e2a2936408
commit 06817aed51
6 changed files with 63 additions and 1 deletions
+2
View File
@@ -19,6 +19,8 @@
#![warn(missing_docs)]
extern crate polkadot_primitives as primitives;
#[macro_use]
extern crate hex_literal;
extern crate hashdb;
extern crate memorydb;
+25 -1
View File
@@ -18,6 +18,7 @@
use std::collections::HashMap;
use super::{Externalities, ExternalitiesError};
use triehash::trie_root;
/// Simple HashMap based Externalities impl.
#[derive(Debug, Default)]
@@ -25,6 +26,14 @@ pub struct TestExternalities {
pub storage: HashMap<Vec<u8>, Vec<u8>>,
}
impl TestExternalities {
fn new() -> Self {
TestExternalities {
storage: HashMap::new(),
}
}
}
impl Externalities for TestExternalities {
fn storage(&self, key: &[u8]) -> Result<&[u8], ExternalitiesError> {
Ok(self.storage.get(&key.to_vec()).map_or(&[] as &[u8], Vec::as_slice))
@@ -37,6 +46,21 @@ impl Externalities for TestExternalities {
fn chain_id(&self) -> u64 { 42 }
fn commit(&self) -> [u8; 32] {
unimplemented!();
trie_root(self.storage.clone()).0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn commit_should_work() {
let mut ext = TestExternalities::new();
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
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);
}
}