Merge branch 'master' of github.com:gavofyork/ethcore into network

This commit is contained in:
arkpar 2016-01-09 14:41:04 +01:00
commit 8cd3aa4b43
18 changed files with 132 additions and 197 deletions

View File

@ -1,12 +1,4 @@
use std::collections::HashMap;
use util::hash::*;
use util::sha3::*;
use util::hashdb::*;
use util::bytes::*;
use util::trie::*;
use util::rlp::*;
use util::uint::*;
use std::cell::*;
use util::*;
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
@ -66,7 +58,7 @@ impl Account {
}
/// Create a new contract account.
/// NOTE: make sure you use `set_code` on this before `commit`ing.
/// NOTE: make sure you use `init_code` on this before `commit`ing.
pub fn new_contract(balance: U256) -> Account {
Account {
balance: balance,
@ -78,9 +70,16 @@ impl Account {
}
}
/// Reset this account to the status of a not-yet-initialised contract.
/// NOTE: Account should have `init_code()` called on it later.
pub fn reset_code(&mut self) {
self.code_hash = None;
self.code_cache = vec![];
}
/// Set this account's code to the given code.
/// NOTE: Account should have been created with `new_contract`.
pub fn set_code(&mut self, code: Bytes) {
/// NOTE: Account should have been created with `new_contract()` or have `reset_code()` called on it.
pub fn init_code(&mut self, code: Bytes) {
assert!(self.code_hash.is_none());
self.code_cache = code;
}
@ -224,7 +223,7 @@ fn storage_at() {
let mut a = Account::new_contract(U256::from(69u8));
a.set_storage(H256::from(&U256::from(0x00u64)), H256::from(&U256::from(0x1234u64)));
a.commit_storage(&mut db);
a.set_code(vec![]);
a.init_code(vec![]);
a.commit_code(&mut db);
a.rlp()
};
@ -241,7 +240,7 @@ fn note_code() {
let rlp = {
let mut a = Account::new_contract(U256::from(69u8));
a.set_code(vec![0x55, 0x44, 0xffu8]);
a.init_code(vec![0x55, 0x44, 0xffu8]);
a.commit_code(&mut db);
a.rlp()
};
@ -267,7 +266,7 @@ fn commit_storage() {
fn commit_code() {
let mut a = Account::new_contract(U256::from(69u8));
let mut db = OverlayDB::new_temp();
a.set_code(vec![0x55, 0x44, 0xffu8]);
a.init_code(vec![0x55, 0x44, 0xffu8]);
assert_eq!(a.code_hash(), SHA3_EMPTY);
a.commit_code(&mut db);
assert_eq!(a.code_hash().hex(), "af231e631776a517ca23125370d542873eca1fb4d613ed9b5d5335a46ae5b7eb");

View File

@ -1,9 +1,4 @@
use std::collections::hash_set::*;
use util::hash::*;
use util::bytes::*;
use util::uint::*;
use util::error::*;
use util::overlaydb::*;
use util::*;
use transaction::*;
use receipt::*;
use blockchain::*;

View File

@ -1,18 +1,8 @@
//! Fast access to blockchain data.
use std::collections::HashMap;
use std::path::Path;
use std::hash::Hash;
use std::sync::RwLock;
use std::sync::*;
use util::*;
use rocksdb::{DB, WriteBatch, Writable};
use heapsize::HeapSizeOf;
use util::hash::*;
use util::uint::*;
use util::rlp::*;
use util::hashdb::*;
use util::sha3::*;
use util::bytes::*;
use util::squeeze::*;
use header::*;
use extras::*;
use transaction::*;

View File

@ -1,13 +1,4 @@
use std::cmp::min;
use std::fmt;
use util::uint::*;
use util::hash::*;
use util::sha3::*;
use util::bytes::*;
use rustc_serialize::json::Json;
use std::io::Write;
use util::crypto::*;
use util::crypto::ec::*;
use util::*;
use crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
@ -96,8 +87,8 @@ pub fn new_builtin_exec(name: &str) -> Option<Box<Fn(&[u8], &mut [u8])>> {
it.copy_raw(input);
if it.v == H256::from(&U256::from(27)) || it.v == H256::from(&U256::from(28)) {
let s = Signature::from_rsv(&it.r, &it.s, it.v[31] - 27);
if is_valid(&s) {
match recover(&s, &it.hash) {
if ec::is_valid(&s) {
match ec::recover(&s, &it.hash) {
Ok(p) => {
let r = p.as_slice().sha3();
// NICE: optimise and separate out into populate-like function

9
src/common.rs Normal file
View File

@ -0,0 +1,9 @@
pub use util::*;
pub use env_info::*;
pub use evm_schedule::*;
pub use denominations::*;
pub use views::*;
pub use builtin::*;
pub use header::*;
pub use account::*;
pub use transaction::*;

View File

@ -1,4 +1,4 @@
use util::uint::*;
use util::*;
#[inline]
pub fn ether() -> U256 { U256::exp10(18) }

View File

@ -1,16 +1,6 @@
use std::collections::hash_map::*;
use util::bytes::*;
use util::hash::*;
use util::uint::*;
use util::rlp::*;
use util::semantic_version::*;
use util::error::*;
use header::Header;
use transaction::Transaction;
use common::*;
use block::Block;
use spec::Spec;
use evm_schedule::EvmSchedule;
use env_info::EnvInfo;
/// A consensus mechanism for the chain. Generally either proof-of-work or proof-of-stake-based.
/// Provides hooks into each of the major parts of block import.

View File

@ -1,5 +1,4 @@
use util::uint::*;
use util::hash::*;
use util::*;
/// Simple vector of hashes, should be at most 256 items large, can be smaller if being used
/// for a block whose number is less than 257.

View File

@ -1,10 +1,7 @@
//use util::error::*;
use util::rlp::decode;
use engine::Engine;
use spec::Spec;
use common::*;
use block::*;
use evm_schedule::EvmSchedule;
use env_info::EnvInfo;
use spec::*;
use engine::*;
/// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum
/// mainnet chains in the Olympic, Frontier and Homestead eras.
@ -31,3 +28,12 @@ impl Engine for Ethash {
}
// TODO: test for on_close_block.
#[test]
fn playpen() {
use util::overlaydb::*;
let engine = Spec::new_morden().to_engine().unwrap();
let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
// let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()]);
}

View File

@ -1,8 +1,5 @@
use heapsize::HeapSizeOf;
use util::*;
use rocksdb::{DB, Writable};
use util::uint::*;
use util::hash::*;
use util::rlp::*;
/// Represents index of extra data in database
#[derive(Copy, Clone)]

View File

@ -1,15 +1,5 @@
use std::io::Read;
use std::cell::RefCell;
use std::str::FromStr;
use std::collections::HashMap;
use rustc_serialize::base64::FromBase64;
use rustc_serialize::json::Json;
use rustc_serialize::hex::FromHex;
use util::*;
use flate2::read::GzDecoder;
use util::rlp::*;
use util::hash::*;
use util::uint::*;
use util::sha3::*;
use account::*;
use header::*;

View File

@ -1,9 +1,4 @@
use std::cell::RefCell;
use util::hash::*;
use util::sha3::*;
use util::bytes::*;
use util::uint::*;
use util::rlp::*;
use util::*;
/// Type for a 2048-bit log-bloom, as used by our blocks.
pub type LogBloom = H2048;
@ -70,16 +65,16 @@ impl Header {
}
pub fn hash(&self) -> H256 {
let mut hash = self.hash.borrow_mut();
match &mut *hash {
&mut Some(ref h) => h.clone(),
hash @ &mut None => {
let mut stream = RlpStream::new();
stream.append(self);
let h = stream.as_raw().sha3();
*hash = Some(h.clone());
h.clone()
}
let mut hash = self.hash.borrow_mut();
match &mut *hash {
&mut Some(ref h) => h.clone(),
hash @ &mut None => {
let mut stream = RlpStream::new();
stream.append(self);
let h = stream.as_raw().sha3();
*hash = Some(h.clone());
h.clone()
}
}
}
}

View File

@ -84,11 +84,7 @@ extern crate env_logger;
extern crate evmjit;
extern crate ethcore_util as util;
//use util::error::*;
pub use util::hash::*;
pub use util::uint::*;
pub use util::bytes::*;
pub mod common;
pub mod env_info;
pub mod engine;
pub mod state;

View File

@ -1,5 +1,4 @@
use util::hash::*;
use util::uint::*;
use util::*;
/// Information describing execution of a transaction.
pub struct Receipt {

View File

@ -1,25 +1,8 @@
use std::io::Read;
use std::collections::HashMap;
use std::cell::*;
use std::str::FromStr;
use rustc_serialize::base64::FromBase64;
use rustc_serialize::json::Json;
use rustc_serialize::hex::FromHex;
use common::*;
use flate2::read::GzDecoder;
use util::uint::*;
use util::hash::*;
use util::bytes::*;
use util::triehash::*;
use util::error::*;
use util::rlp::*;
use util::sha3::*;
use account::*;
use engine::Engine;
use builtin::Builtin;
use null_engine::NullEngine;
use ethash::Ethash;
use denominations::*;
use header::*;
use engine::*;
use null_engine::*;
use ethash::*;
/// Converts file from base64 gzipped bytes to json
pub fn gzip64res_to_json(source: &[u8]) -> Json {
@ -107,7 +90,7 @@ impl Spec {
Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap())
}
fn genesis_header(&self) -> Header {
pub fn genesis_header(&self) -> Header {
Header {
parent_hash: self.parent_hash.clone(),
timestamp: self.timestamp.clone(),
@ -223,15 +206,16 @@ impl Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("block_reward", encode(&(finney() * U256::from(1500u64)))),
("maximum_extra_data_size", encode(&U256::from(1024u64))),
("account_start_nonce", encode(&U256::from(0u64))),
("gas_limit_bounds_divisor", encode(&1024u64)),
("minimum_difficulty", encode(&131_072u64)),
("difficulty_bound_divisor", encode(&2048u64)),
("duration_limit", encode(&8u64)),
("min_gas_limit", encode(&125_000u64)),
("gas_floor_target", encode(&3_141_592u64)),
("blockReward", encode(&(finney() * U256::from(1500u64)))),
("frontierCompatibilityModeLimit", encode(&0xffffffffu64)),
("maximumExtraDataSize", encode(&U256::from(1024u64))),
("accountStartNonce", encode(&U256::from(0u64))),
("gasLimitBoundsDivisor", encode(&1024u64)),
("minimumDifficulty", encode(&131_072u64)),
("difficultyBoundDivisor", encode(&2048u64)),
("durationLimit", encode(&8u64)),
("minGasLimit", encode(&125_000u64)),
("gasFloorTarget", encode(&3_141_592u64)),
].into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0.to_string(), vec.1);
acc
@ -261,15 +245,16 @@ impl Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("block_reward", encode(&(ether() * U256::from(5u64)))),
("maximum_extra_data_size", encode(&U256::from(32u64))),
("account_start_nonce", encode(&U256::from(0u64))),
("gas_limit_bounds_divisor", encode(&1024u64)),
("minimum_difficulty", encode(&131_072u64)),
("difficulty_bound_divisor", encode(&2048u64)),
("duration_limit", encode(&13u64)),
("min_gas_limit", encode(&5000u64)),
("gas_floor_target", encode(&3_141_592u64)),
("blockReward", encode(&(ether() * U256::from(5u64)))),
("frontierCompatibilityModeLimit", encode(&0xfffa2990u64)),
("maximumExtraDataSize", encode(&U256::from(32u64))),
("accountStartNonce", encode(&U256::from(0u64))),
("gasLimitBoundsDivisor", encode(&1024u64)),
("minimumDifficulty", encode(&131_072u64)),
("difficultyBoundDivisor", encode(&2048u64)),
("durationLimit", encode(&13u64)),
("minGasLimit", encode(&5000u64)),
("gasFloorTarget", encode(&3_141_592u64)),
].into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0.to_string(), vec.1);
acc
@ -299,15 +284,16 @@ impl Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("block_reward", encode(&(ether() * U256::from(5u64)))),
("maximum_extra_data_size", encode(&U256::from(32u64))),
("account_start_nonce", encode(&(U256::from(1u64) << 20))),
("gas_limit_bounds_divisor", encode(&1024u64)),
("minimum_difficulty", encode(&131_072u64)),
("difficulty_bound_divisor", encode(&2048u64)),
("duration_limit", encode(&13u64)),
("min_gas_limit", encode(&5000u64)),
("gas_floor_target", encode(&3_141_592u64)),
("blockReward", encode(&(ether() * U256::from(5u64)))),
("frontierCompatibilityModeLimit", encode(&0xfffa2990u64)),
("maximumExtraDataSize", encode(&U256::from(32u64))),
("accountStartNonce", encode(&(U256::from(1u64) << 20))),
("gasLimitBoundsDivisor", encode(&1024u64)),
("minimumDifficulty", encode(&131_072u64)),
("difficultyBoundDivisor", encode(&2048u64)),
("durationLimit", encode(&13u64)),
("minGasLimit", encode(&5000u64)),
("gasFloorTarget", encode(&3_141_592u64)),
].into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0.to_string(), vec.1);
acc
@ -343,6 +329,17 @@ impl Spec {
}
}
/// Ensure that the given state DB has the trie nodes in for the genesis state.
pub fn ensure_db_good(&self, db: &mut HashDB) {
if !db.contains(&self.state_root()) {
let mut root = H256::new();
let mut t = SecTrieDBMut::new(db, &mut root);
for (address, account) in self.genesis_state.iter() {
t.insert(address.as_slice(), &account.rlp());
}
}
}
/// Create a new Spec from a JSON UTF-8 data resource `data`.
pub fn from_json_utf8(data: &[u8]) -> Spec {
Self::from_json_str(::std::str::from_utf8(data).unwrap())
@ -366,26 +363,16 @@ mod tests {
use std::str::FromStr;
use util::hash::*;
use util::sha3::*;
use rustc_serialize::json::Json;
use views::*;
use super::*;
#[test]
fn morden_manual() {
let morden = Spec::new_morden_manual();
assert_eq!(*morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = morden.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
}
#[test]
fn morden() {
let morden = Spec::new_morden();
assert_eq!(*morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = morden.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
for morden in [Spec::new_morden(), Spec::new_morden_manual()].into_iter() {
assert_eq!(*morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = morden.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
}
}
#[test]

View File

@ -1,14 +1,4 @@
use std::cell::*;
use std::ops::*;
use std::collections::HashMap;
use util::hash::*;
use util::hashdb::*;
use util::overlaydb::*;
use util::trie::*;
use util::bytes::*;
use util::rlp::*;
use util::uint::*;
use util::error::*;
use util::*;
use account::Account;
use transaction::Transaction;
use receipt::Receipt;
@ -83,6 +73,12 @@ impl State {
&mut self.db
}
/// Create a new contract at address `contract`. If there is already an account at the address
/// it will have its code reset, ready for `init_code()`.
pub fn new_contract(&mut self, contract: &Address) {
self.require_or_from(contract, false, || Account::new_contract(U256::from(0u8)), |r| r.reset_code());
}
/// Get the balance of account `a`.
pub fn balance(&self, a: &Address) -> U256 {
self.get(a, false).as_ref().map(|account| account.balance().clone()).unwrap_or(U256::from(0u8))
@ -129,9 +125,10 @@ impl State {
self.require(a, false).set_storage(key, value);
}
/// Mutate storage of account `a` so that it is `value` for `key`.
pub fn set_code(&mut self, a: &Address, code: Bytes) {
self.require_or_from(a, true, || Account::new_contract(U256::from(0u8))).set_code(code);
/// Initialise the code of account `a` so that it is `value` for `key`.
/// NOTE: Account should have been created with `new_contract`.
pub fn init_code(&mut self, a: &Address, code: Bytes) {
self.require_or_from(a, true, || Account::new_contract(U256::from(0u8)), |_|{}).init_code(code);
}
/// Execute a given transaction.
@ -197,18 +194,20 @@ impl State {
}
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
/// `force_create` creates a new, empty basic account if there is not currently an active account.
fn require(&self, a: &Address, require_code: bool) -> RefMut<Account> {
self.require_or_from(a, require_code, || Account::new_basic(U256::from(0u8), self.account_start_nonce))
self.require_or_from(a, require_code, || Account::new_basic(U256::from(0u8), self.account_start_nonce), |_|{})
}
/// Pull account `a` in our cache from the trie DB. `require_code` requires that the code be cached, too.
/// `force_create` creates a new, empty basic account if there is not currently an active account.
fn require_or_from<F: FnOnce() -> Account>(&self, a: &Address, require_code: bool, default: F) -> RefMut<Account> {
/// If it doesn't exist, make account equal the evaluation of `default`.
fn require_or_from<F: FnOnce() -> Account, G: FnOnce(&mut Account)>(&self, a: &Address, require_code: bool, default: F, not_default: G) -> RefMut<Account> {
self.cache.borrow_mut().entry(a.clone()).or_insert_with(||
TrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
if self.cache.borrow().get(a).unwrap().is_none() {
let preexists = self.cache.borrow().get(a).unwrap().is_none();
if preexists {
self.cache.borrow_mut().insert(a.clone(), Some(default()));
} else {
not_default(self.cache.borrow_mut().get_mut(a).unwrap().as_mut().unwrap());
}
let b = self.cache.borrow_mut();
@ -237,8 +236,8 @@ fn code_from_database() {
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
let (r, db) = {
let mut s = State::new_temp();
s.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)));
s.set_code(&a, vec![1, 2, 3]);
s.require_or_from(&a, false, ||Account::new_contract(U256::from(42u32)), |_|{});
s.init_code(&a, vec![1, 2, 3]);
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
s.commit();
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));

View File

@ -1,7 +1,4 @@
use util::hash::*;
use util::bytes::*;
use util::uint::*;
use util::rlp::*;
use util::*;
/// A set of information describing an externally-originating message call
/// or contract creation operation.

View File

@ -1,9 +1,5 @@
//! Block oriented views onto rlp.
use util::bytes::*;
use util::hash::*;
use util::uint::*;
use util::rlp::*;
use util::sha3::*;
use util::*;
use header::*;
use transaction::*;