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
+2 -1
View File
@@ -41,8 +41,9 @@ impl CodeExecutor for NativeExecutor {
#[cfg(test)]
mod tests {
use super::*;
use runtime_std::TestExternalities;
use native_runtime::codec::KeyedVec;
use native_runtime::support::{TestExternalities, one, two, StaticHexInto};
use native_runtime::support::{one, two, StaticHexInto};
use native_runtime::runtime::staking::balance;
use primitives::twox_128;
+2 -2
View File
@@ -279,8 +279,8 @@ mod tests {
use super::*;
use rustc_hex::FromHex;
use primitives::{blake2_256, twox_128};
use runtime_std;
use native_runtime::support::{one, two, StaticHexInto, TestExternalities};
use runtime_std::{self, TestExternalities};
use native_runtime::support::{one, two, StaticHexInto};
use native_runtime::codec::KeyedVec;
use native_runtime::runtime::staking::balance;
+8 -18
View File
@@ -39,7 +39,7 @@ pub mod prelude {
pub use std::boxed::Box;
}
pub use polkadot_state_machine::{Externalities, ExternalitiesError};
pub use polkadot_state_machine::{Externalities, ExternalitiesError, TestExternalities};
use primitives::hexdisplay::HexDisplay;
// TODO: use the real error, not NoError.
@@ -92,6 +92,13 @@ pub fn chain_id() -> u64 {
).unwrap_or(0)
}
/// "Commit" all existing operations and get the resultant storage root.
pub fn commit() -> [u8; 32] {
ext::with(|ext|
ext.commit()
).unwrap_or([0u8; 32])
}
/// Conduct a Keccak-256 hash of the given data.
pub use primitives::{blake2_256, twox_128, twox_256};
@@ -140,23 +147,6 @@ macro_rules! impl_stubs {
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
#[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 }
}
macro_rules! map {
($( $name:expr => $value:expr ),*) => (
+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!();
}
}
@@ -114,9 +114,9 @@ pub mod internal {
#[cfg(test)]
mod tests {
use super::*;
use runtime_std::{with_externalities, twox_128};
use runtime_std::{with_externalities, twox_128, TestExternalities};
use codec::{KeyedVec, Joiner};
use support::{one, two, TestExternalities, with_env};
use support::{one, two, with_env};
use primitives::{AccountID, InternalFunction};
use runtime::{staking, session};
@@ -132,9 +132,9 @@ mod tests {
use super::public::*;
use super::privileged::*;
use super::internal::*;
use runtime_std::{with_externalities, twox_128};
use runtime_std::{with_externalities, twox_128, TestExternalities};
use codec::{KeyedVec, Joiner};
use support::{one, two, TestExternalities, with_env};
use support::{one, two, with_env};
use primitives::AccountID;
use runtime::{consensus, session};
@@ -206,9 +206,9 @@ mod tests {
use super::public::*;
use super::privileged::*;
use runtime_std::{with_externalities, twox_128};
use runtime_std::{with_externalities, twox_128, TestExternalities};
use codec::{KeyedVec, Joiner};
use support::{one, two, TestExternalities, with_env};
use support::{one, two, with_env};
use primitives::AccountID;
use runtime::{staking, session};
@@ -127,9 +127,9 @@ mod tests {
use super::*;
use super::internal::*;
use runtime_std::{with_externalities, twox_128};
use runtime_std::{with_externalities, twox_128, TestExternalities};
use codec::{Joiner, KeyedVec, Slicable};
use support::{StaticHexInto, TestExternalities, HexDisplay, one, two};
use support::{StaticHexInto, HexDisplay, one, two};
use primitives::{UncheckedTransaction, Transaction, Function};
use runtime::staking;
@@ -42,10 +42,9 @@ mod tests {
use super::*;
use super::public::*;
use runtime_std::{with_externalities, twox_128};
use runtime_std::{with_externalities, twox_128, TestExternalities};
use runtime::timestamp;
use codec::{Joiner, KeyedVec};
use support::TestExternalities;
#[test]
fn timestamp_works() {
@@ -31,4 +31,4 @@ pub use self::hashable::Hashable;
#[cfg(feature = "with-std")]
pub use self::statichex::{StaticHexConversion, StaticHexInto};
#[cfg(feature = "with-std")]
pub use self::testing::{AsBytesRef, HexDisplay, TestExternalities, one, two};
pub use self::testing::{AsBytesRef, HexDisplay, one, two};
@@ -146,9 +146,8 @@ pub trait StorageVec {
mod tests {
use super::*;
use std::collections::HashMap;
use runtime_std::with_externalities;
use support::{TestExternalities, HexDisplay};
use runtime_std::{storage, twox_128};
use support::HexDisplay;
use runtime_std::{storage, twox_128, TestExternalities, with_externalities};
#[test]
fn integers_can_be_stored() {
@@ -16,30 +16,9 @@
//! Testing helpers.
use std::collections::HashMap;
use runtime_std::{Externalities, ExternalitiesError};
use primitives::AccountID;
use super::statichex::StaticHexInto;
#[derive(Debug, Default)]
/// Simple externaties implementation.
pub struct TestExternalities {
/// The storage map.
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 }
}
#[macro_export]
macro_rules! map {
($( $name:expr => $value:expr ),*) => (