mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-07 23:38:00 +00:00
TestExternalities can commit.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user