Expose commit in externalities.

Also refactor `TestExternalities` into a single place.
This commit is contained in:
Gav
2018-01-30 18:29:26 +01:00
parent 8e554129ec
commit e2a2936408
14 changed files with 77 additions and 74 deletions
+4
View File
@@ -75,4 +75,8 @@ impl<'a, B: 'a> Externalities for Ext<'a, B>
fn chain_id(&self) -> u64 {
42
}
fn commit(&self) -> [u8; 32] {
unimplemented!();
}
}
+7 -18
View File
@@ -35,6 +35,9 @@ use primitives::contract::{CallData};
pub mod backend;
mod ext;
mod testing;
pub use testing::TestExternalities;
/// Updates to be committed to the state.
pub enum Update {
@@ -141,6 +144,9 @@ pub trait Externalities {
/// Get the identity of the chain.
fn chain_id(&self) -> u64;
/// Get the trie root of the current storage map.
fn commit(&self) -> [u8; 32];
/// Get the current set of authorities from storage.
fn authorities(&self) -> Result<Vec<&[u8]>, ExternalitiesError> {
(0..self.storage(b"con:aut:len")?.into_iter()
@@ -210,8 +216,7 @@ pub fn execute<B: backend::Backend, Exec: CodeExecutor>(
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::{OverlayedChanges, Externalities, ExternalitiesError};
use super::{OverlayedChanges, Externalities, TestExternalities};
#[test]
fn overlayed_storage_works() {
@@ -238,22 +243,6 @@ mod tests {
assert!(overlayed.storage(&key).is_none());
}
#[derive(Debug, Default)]
struct TestExternalities {
storage: HashMap<Vec<u8>, Vec<u8>>,
}
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))
}
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.storage.insert(key, value);
}
fn chain_id(&self) -> u64 { 42 }
}
#[test]
fn authorities_call_works() {
let mut ext = TestExternalities::default();
+42
View File
@@ -0,0 +1,42 @@
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test implementation for Externalities.
use std::collections::HashMap;
use super::{Externalities, ExternalitiesError};
/// Simple HashMap based Externalities impl.
#[derive(Debug, Default)]
pub struct TestExternalities {
pub storage: HashMap<Vec<u8>, Vec<u8>>,
}
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))
}
fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
self.storage.insert(key, value);
}
fn chain_id(&self) -> u64 { 42 }
fn commit(&self) -> [u8; 32] {
unimplemented!();
}
}