initial merge

This commit is contained in:
Robert Habermeier
2018-02-06 14:12:54 +01:00
21 changed files with 839 additions and 191 deletions
+2 -18
View File
@@ -17,16 +17,11 @@
//! State machine backends. These manage the code and storage of contracts.
use std::{error, fmt};
use primitives::hash::H256;
use triehash::sec_trie_root;
use super::{Update, MemoryState};
/// Output of a commit.
pub struct Committed {
/// Root of the storage tree after changes committed.
pub storage_tree_root: H256,
}
pub struct Committed {}
/// A state backend is used to read state data and can have changes committed
/// to it.
@@ -67,7 +62,6 @@ pub struct InMemory {
inner: MemoryState, // keeps all the state in memory.
}
#[cfg(test)]
impl InMemory {
/// Create a new instance from a given storage map.
pub fn from(storage: ::std::collections::HashMap<Vec<u8>, Vec<u8>>) -> Self {
@@ -90,17 +84,7 @@ impl Backend for InMemory {
where I: IntoIterator<Item=Update>
{
self.inner.update(changes);
// fully recalculate trie roots.
let storage_tree_root = H256(sec_trie_root(
self.inner.storage.iter()
.map(|(k, v)| (k.to_vec(), v.clone()))
.collect()
).0);
Committed {
storage_tree_root,
}
Committed {}
}
fn pairs(&self) -> Vec<(&[u8], &[u8])> {
+14 -8
View File
@@ -34,13 +34,14 @@ extern crate byteorder;
use std::collections::HashMap;
use std::fmt;
use primitives::contract::{CallData};
use primitives::contract::CallData;
pub mod backend;
mod ext;
mod testing;
pub use testing::TestExternalities;
pub use ext::Ext;
/// Updates to be committed to the state.
pub enum Update {
@@ -152,12 +153,17 @@ pub trait Externalities {
/// Get the current set of authorities from storage.
fn authorities(&self) -> Result<Vec<&[u8]>, ExternalitiesError> {
(0..self.storage(b"con:aut:len")?.into_iter()
(0..self.storage(b":auth:len")?.into_iter()
.rev()
.fold(0, |acc, &i| (acc << 8) + (i as u32)))
.map(|i| self.storage(&to_keyed_vec(i, b"con:aut:".to_vec())))
.map(|i| self.storage(&to_keyed_vec(i, b":auth:".to_vec())))
.collect()
}
/// Get the runtime code.
fn code(&self) -> Result<&[u8], ExternalitiesError> {
self.storage(b":code")
}
}
/// Code execution engine.
@@ -254,17 +260,17 @@ mod tests {
assert_eq!(ext.authorities(), Ok(vec![]));
ext.set_storage(b"con:aut:len".to_vec(), vec![0u8; 4]);
ext.set_storage(b":auth:len".to_vec(), vec![0u8; 4]);
assert_eq!(ext.authorities(), Ok(vec![]));
ext.set_storage(b"con:aut:len".to_vec(), vec![1u8, 0, 0, 0]);
ext.set_storage(b":auth:len".to_vec(), vec![1u8, 0, 0, 0]);
assert_eq!(ext.authorities(), Ok(vec![&[][..]]));
ext.set_storage(b"con:aut:\0\0\0\0".to_vec(), b"first".to_vec());
ext.set_storage(b":auth:\0\0\0\0".to_vec(), b"first".to_vec());
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..]]));
ext.set_storage(b"con:aut:len".to_vec(), vec![2u8, 0, 0, 0]);
ext.set_storage(b"con:aut:\x01\0\0\0".to_vec(), b"second".to_vec());
ext.set_storage(b":auth:len".to_vec(), vec![2u8, 0, 0, 0]);
ext.set_storage(b":auth:\x01\0\0\0".to_vec(), b"second".to_vec());
assert_eq!(ext.authorities(), Ok(vec![&b"first"[..], &b"second"[..]]));
}