new way of loading PodState

This commit is contained in:
debris 2016-03-17 15:51:40 +01:00
parent 1f03ae54d6
commit c695b83e52
8 changed files with 66 additions and 15 deletions

View File

@ -101,13 +101,13 @@ pub mod spec;
pub mod transaction;
pub mod views;
pub mod receipt;
pub mod pod_state;
mod common;
mod basic_types;
#[macro_use] mod evm;
mod env_info;
mod pod_account;
mod pod_state;
mod account_diff;
mod state_diff;
mod engine;

View File

@ -17,6 +17,7 @@
use util::*;
use account::*;
use account_db::*;
use ethjson;
#[derive(Debug,Clone,PartialEq,Eq)]
/// An account, expressed as Plain-Old-Data (hence the name).
@ -73,6 +74,21 @@ impl PodAccount {
}
}
impl From<ethjson::blockchain::Account> for PodAccount {
fn from(a: ethjson::blockchain::Account) -> Self {
PodAccount {
balance: a.balance.into(),
nonce: a.nonce.into(),
code: a.code.into(),
storage: a.storage.into_iter().fold(BTreeMap::new(), |mut acc, (key, value)| {
let key: U256 = key.into();
acc.insert(H256::from(key), value.into());
acc
})
}
}
}
impl fmt::Display for PodAccount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(bal={}; nonce={}; code={} bytes, #{}; storage={} items)", self.balance, self.nonce, self.code.len(), self.code.sha3(), self.storage.len())

View File

@ -14,11 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! State of all accounts in the system expressed in Plain Old Data.
use util::*;
use pod_account::*;
use ethjson;
#[derive(Debug,Clone,PartialEq,Eq,Default)]
/// State of all accounts in the system expressed in Plain Old Data.
#[derive(Debug,Clone,PartialEq,Eq,Default)]
pub struct PodState (BTreeMap<Address, PodAccount>);
impl PodState {
@ -64,6 +67,15 @@ impl FromJson for PodState {
}
}
impl From<ethjson::blockchain::State> for PodState {
fn from(s: ethjson::blockchain::State) -> PodState {
PodState(s.0.into_iter().fold(BTreeMap::new(), |mut acc, (key, value)| {
acc.insert(key.into(), PodAccount::from(value));
acc
}))
}
}
impl fmt::Display for PodState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (add, acc) in &self.0 {

View File

@ -20,3 +20,4 @@ mod genesis;
pub mod spec;
pub use self::spec::*;
pub use self::genesis::Genesis;

View File

@ -19,14 +19,19 @@
use std::collections::BTreeMap;
use uint::Uint;
use bytes::Bytes;
use hash::H256;
/// Blockchain test account deserializer.
#[derive(Debug, PartialEq, Deserialize)]
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct Account {
balance: Uint,
code: Bytes,
nonce: Uint,
storage: BTreeMap<Uint, Bytes>,
/// Balance.
pub balance: Uint,
/// Code.
pub code: Bytes,
/// Nonce.
pub nonce: Uint,
/// Storage.
pub storage: BTreeMap<Uint, H256>,
}
#[cfg(test)]
@ -35,7 +40,7 @@ mod tests {
use blockchain::account::Account;
#[test]
fn header_deserialization() {
fn account_deserialization() {
let s = r#"{
"balance" : "0x09184e72a078",
"code" : "0x600140600155",

View File

@ -25,15 +25,20 @@ use spec::Genesis;
/// Blockchain deserialization.
#[derive(Debug, PartialEq, Deserialize)]
pub struct BlockChain {
/// Genesis block header.
#[serde(rename="genesisBlockHeader")]
genesis_block: Header,
pub genesis_block: Header,
/// Genesis block rlp.
#[serde(rename="genesisRLP")]
genesis_rlp: Bytes,
blocks: Vec<Block>,
pub genesis_rlp: Bytes,
/// Blocks.
pub blocks: Vec<Block>,
/// Post state.
#[serde(rename="postState")]
post_state: State,
pub post_state: State,
/// Pre state.
#[serde(rename="pre")]
pre_state: State,
pub pre_state: State,
}
impl BlockChain {

View File

@ -22,8 +22,8 @@ use hash::Address;
use blockchain::account::Account;
/// Blockchain test state deserializer.
#[derive(Debug, PartialEq, Deserialize)]
pub struct State(BTreeMap<Address, Account>);
#[derive(Debug, PartialEq, Deserialize, Clone)]
pub struct State(pub BTreeMap<Address, Account>);
impl Deref for State {
type Target = BTreeMap<Address, Account>;

View File

@ -18,11 +18,15 @@ extern crate docopt;
extern crate rustc_serialize;
extern crate serde_json;
extern crate ethjson;
extern crate ethcore;
use std::process;
use std::fs::File;
use std::path::Path;
use docopt::Docopt;
use ethcore::spec::Genesis;
use ethcore::pod_state::PodState;
use ethcore::ethereum;
const USAGE: &'static str = r#"
Parity rpctest client.
@ -70,7 +74,15 @@ impl Configuration {
process::exit(3);
});
let genesis = Genesis::from(blockchain.genesis());
let state = PodState::from(blockchain.pre_state.clone());
let mut spec = ethereum::new_frontier_test();
spec.set_genesis_state(state);
spec.overwrite_genesis_params(genesis);
assert!(spec.is_state_root_valid());
//let temp = RandomTempPath::new();
//spec.
}
}