Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1577 from ethcore/pv64
Browse files Browse the repository at this point in the history
bring snapshotting work into master
  • Loading branch information
NikVolf authored Jul 12, 2016
2 parents fbc0e00 + 4269867 commit d956b7c
Show file tree
Hide file tree
Showing 12 changed files with 1,049 additions and 4 deletions.
11 changes: 11 additions & 0 deletions devtools/src/random_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use std::path::*;
use std::fs;
use std::env;
use std::ops::{Deref, DerefMut};
use rand::random;

pub struct RandomTempPath {
Expand Down Expand Up @@ -93,6 +94,16 @@ impl<T> GuardedTempResult<T> {
}
}

impl<T> Deref for GuardedTempResult<T> {
type Target = T;

fn deref(&self) -> &T { self.result.as_ref().unwrap() }
}

impl<T> DerefMut for GuardedTempResult<T> {
fn deref_mut(&mut self) -> &mut T { self.result.as_mut().unwrap() }
}

#[test]
fn creates_dir() {
let temp = RandomTempPath::create_dir();
Expand Down
2 changes: 1 addition & 1 deletion ethcore/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use trace::Trace;
use evm::Factory as EvmFactory;

/// A block, encoded as it is on the block chain.
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Block {
/// The header of this block.
pub header: Header,
Expand Down
3 changes: 1 addition & 2 deletions ethcore/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

use std::path::PathBuf;
use std::collections::{HashSet, HashMap};
use std::ops::Deref;
use std::mem;
use std::collections::VecDeque;
use std::sync::*;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::fmt;
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering as AtomicOrdering};
use std::time::Instant;
Expand Down
18 changes: 18 additions & 0 deletions ethcore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ pub enum Error {
PowInvalid,
/// Error concerning TrieDBs
Trie(TrieError),
/// Io error.
Io(::std::io::Error),
/// Snappy error.
Snappy(::util::snappy::InvalidInput),
}

impl fmt::Display for Error {
Expand All @@ -246,6 +250,8 @@ impl fmt::Display for Error {
Error::PowHashInvalid => f.write_str("Invalid or out of date PoW hash."),
Error::PowInvalid => f.write_str("Invalid nonce or mishash"),
Error::Trie(ref err) => f.write_fmt(format_args!("{}", err)),
Error::Io(ref err) => f.write_fmt(format_args!("{}", err)),
Error::Snappy(ref err) => f.write_fmt(format_args!("{}", err)),
}
}
}
Expand Down Expand Up @@ -313,6 +319,18 @@ impl From<TrieError> for Error {
}
}

impl From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Error {
Error::Io(err)
}
}

impl From<::util::snappy::InvalidInput> for Error {
fn from(err: ::util::snappy::InvalidInput) -> Error {
Error::Snappy(err)
}
}

impl From<BlockImportError> for Error {
fn from(err: BlockImportError) -> Error {
match err {
Expand Down
10 changes: 10 additions & 0 deletions ethcore/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ impl Header {

/// Set the number field of the header.
pub fn set_parent_hash(&mut self, a: H256) { self.parent_hash = a; self.note_dirty(); }
/// Set the uncles hash field of the header.
pub fn set_uncles_hash(&mut self, a: H256) { self.uncles_hash = a; self.note_dirty(); }
/// Set the state root field of the header.
pub fn set_state_root(&mut self, a: H256) { self.state_root = a; self.note_dirty(); }
/// Set the transactions root field of the header.
pub fn set_transactions_root(&mut self, a: H256) { self.transactions_root = a; self.note_dirty() }
/// Set the receipts root field of the header.
pub fn set_receipts_root(&mut self, a: H256) { self.receipts_root = a; self.note_dirty() }
/// Set the log bloom field of the header.
pub fn set_log_bloom(&mut self, a: LogBloom) { self.log_bloom = a; self.note_dirty() }
/// Set the timestamp field of the header.
pub fn set_timestamp(&mut self, a: u64) { self.timestamp = a; self.note_dirty(); }
/// Set the timestamp field of the header to the current time.
Expand Down
3 changes: 2 additions & 1 deletion ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ pub mod pod_state;
pub mod engine;
pub mod migrations;
pub mod miner;
#[macro_use] pub mod evm;
pub mod snapshot;
pub mod action_params;
#[macro_use] pub mod evm;

mod blooms;
mod db;
Expand Down
211 changes: 211 additions & 0 deletions ethcore/src/snapshot/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.

// Parity 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.

// Parity 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 Parity. If not, see <http://www.gnu.org/licenses/>.

//! Account state encoding and decoding
use account_db::{AccountDB, AccountDBMut};
use error::Error;

use util::{Bytes, HashDB, SHA3_EMPTY, TrieDB};
use util::hash::{FixedHash, H256};
use util::numbers::U256;
use util::rlp::{DecoderError, Rlp, RlpStream, Stream, UntrustedRlp, View};

// An alternate account structure from ::account::Account.
#[derive(PartialEq, Clone, Debug)]
pub struct Account {
nonce: U256,
balance: U256,
storage_root: H256,
code_hash: H256,
}

impl Account {
// decode the account from rlp.
pub fn from_thin_rlp(rlp: &[u8]) -> Self {
let r: Rlp = Rlp::new(rlp);

Account {
nonce: r.val_at(0),
balance: r.val_at(1),
storage_root: r.val_at(2),
code_hash: r.val_at(3),
}
}

// encode the account to a standard rlp.
pub fn to_thin_rlp(&self) -> Bytes {
let mut stream = RlpStream::new_list(4);
stream
.append(&self.nonce)
.append(&self.balance)
.append(&self.storage_root)
.append(&self.code_hash);

stream.out()
}

// walk the account's storage trie, returning an RLP item containing the
// account properties and the storage.
pub fn to_fat_rlp(&self, acct_db: &AccountDB) -> Result<Bytes, Error> {
let db = try!(TrieDB::new(acct_db, &self.storage_root));

let mut pairs = Vec::new();

for (k, v) in db.iter() {
pairs.push((k, v));
}

let mut stream = RlpStream::new_list(pairs.len());

for (k, v) in pairs {
stream.begin_list(2).append(&k).append(&v);
}

let pairs_rlp = stream.out();

let mut account_stream = RlpStream::new_list(5);
account_stream.append(&self.nonce)
.append(&self.balance);

// [has_code, code_hash].
if self.code_hash == SHA3_EMPTY {
account_stream.append(&false).append_empty_data();
} else {
match acct_db.get(&self.code_hash) {
Some(c) => {
account_stream.append(&true).append(&c);
}
None => {
warn!("code lookup failed during snapshot");
account_stream.append(&false).append_empty_data();
}
}
}

account_stream.append_raw(&pairs_rlp, 1);

Ok(account_stream.out())
}

// decode a fat rlp, and rebuild the storage trie as we go.
pub fn from_fat_rlp(acct_db: &mut AccountDBMut, rlp: UntrustedRlp) -> Result<Self, DecoderError> {
use util::{TrieDBMut, TrieMut};

let nonce = try!(rlp.val_at(0));
let balance = try!(rlp.val_at(1));
let code_hash = if try!(rlp.val_at(2)) {
let code: Bytes = try!(rlp.val_at(3));
acct_db.insert(&code)
} else {
SHA3_EMPTY
};

let mut storage_root = H256::zero();

{
let mut storage_trie = TrieDBMut::new(acct_db, &mut storage_root);
let pairs = try!(rlp.at(4));
for pair_rlp in pairs.iter() {
let k: Bytes = try!(pair_rlp.val_at(0));
let v: Bytes = try!(pair_rlp.val_at(1));

storage_trie.insert(&k, &v);
}
}
Ok(Account {
nonce: nonce,
balance: balance,
storage_root: storage_root,
code_hash: code_hash,
})
}
}

#[cfg(test)]
mod tests {
use account_db::{AccountDB, AccountDBMut};
use tests::helpers::get_temp_journal_db;

use util::{SHA3_NULL_RLP, SHA3_EMPTY};
use util::hash::{Address, FixedHash, H256};
use util::rlp::{UntrustedRlp, View};
use util::trie::{Alphabet, StandardMap, SecTrieDBMut, TrieMut, ValueMode};

use super::Account;

fn fill_storage(mut db: AccountDBMut) -> H256 {
let map = StandardMap {
alphabet: Alphabet::All,
min_key: 6,
journal_key: 6,
value_mode: ValueMode::Random,
count: 100
};

let mut root = H256::new();
{
let mut trie = SecTrieDBMut::new(&mut db, &mut root);
for (k, v) in map.make() {
trie.insert(&k, &v);
}
}
root
}

#[test]
fn encoding_basic() {
let mut db = get_temp_journal_db();
let mut db = &mut **db;
let addr = Address::random();

let account = Account {
nonce: 50.into(),
balance: 123456789.into(),
storage_root: SHA3_NULL_RLP,
code_hash: SHA3_EMPTY,
};

let thin_rlp = account.to_thin_rlp();
assert_eq!(Account::from_thin_rlp(&thin_rlp), account);

let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr)).unwrap();
let fat_rlp = UntrustedRlp::new(&fat_rlp);
assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap(), account);
}

#[test]
fn encoding_storage() {
let mut db = get_temp_journal_db();
let mut db = &mut **db;
let addr = Address::random();

let root = fill_storage(AccountDBMut::new(db.as_hashdb_mut(), &addr));
let account = Account {
nonce: 25.into(),
balance: 987654321.into(),
storage_root: root,
code_hash: SHA3_EMPTY,
};

let thin_rlp = account.to_thin_rlp();
assert_eq!(Account::from_thin_rlp(&thin_rlp), account);

let fat_rlp = account.to_fat_rlp(&AccountDB::new(db.as_hashdb(), &addr)).unwrap();
let fat_rlp = UntrustedRlp::new(&fat_rlp);
assert_eq!(Account::from_fat_rlp(&mut AccountDBMut::new(db.as_hashdb_mut(), &addr), fat_rlp).unwrap(), account);
}
}
Loading

0 comments on commit d956b7c

Please sign in to comment.