Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -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] );

Expand Down Expand Up @@ -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,
Expand All @@ -80,7 +72,7 @@ impl Account {

/// 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) {
pub fn init_code(&mut self, code: Bytes) {
assert!(self.code_hash.is_none());
self.code_cache = code;
}
Expand Down Expand Up @@ -224,7 +216,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()
};
Expand All @@ -241,7 +233,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()
};
Expand All @@ -267,7 +259,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");
Expand Down
7 changes: 1 addition & 6 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
13 changes: 1 addition & 12 deletions src/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
//! Fast access to blockchain data.

use std::collections::HashMap;
use std::cell::RefCell;
use std::path::Path;
use std::hash::Hash;
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::*;
Expand Down
14 changes: 3 additions & 11 deletions src/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,12 +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::*;
use crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
Expand Down Expand Up @@ -95,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
Expand Down
9 changes: 9 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -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::*;
2 changes: 1 addition & 1 deletion src/denominations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use util::uint::*;
use util::*;

#[inline]
pub fn ether() -> U256 { U256::exp10(18) }
Expand Down
12 changes: 1 addition & 11 deletions src/engine.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/env_info.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 13 additions & 6 deletions src/ethash.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -31,3 +28,13 @@ impl Engine for Ethash {
}

// TODO: test for on_close_block.
#[test]
fn playpen() {
use util::sha3::*;
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()]);
}
5 changes: 1 addition & 4 deletions src/extras.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
14 changes: 3 additions & 11 deletions src/genesis.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
use std::io::Read;
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::*;

Expand Down Expand Up @@ -90,7 +81,8 @@ impl Genesis {
let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap();
let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap();
vec![encode(&mixhash), encode(&nonce)]
}
},
hash: RefCell::new(None),
};

(header, state)
Expand Down
23 changes: 19 additions & 4 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use util::hash::*;
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;
Expand Down Expand Up @@ -38,6 +35,8 @@ pub struct Header {

pub difficulty: U256,
pub seal: Vec<Bytes>,

pub hash: RefCell<Option<H256>>, //TODO: make this private
}

impl Header {
Expand All @@ -61,6 +60,21 @@ impl Header {

difficulty: ZERO_U256.clone(),
seal: vec![],
hash: RefCell::new(None),
}
}

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()
}
}
}
}
Expand All @@ -84,6 +98,7 @@ impl Decodable for Header {
timestamp: try!(Decodable::decode(&d[11])),
extra_data: try!(Decodable::decode(&d[12])),
seal: vec![],
hash: RefCell::new(None),
};

for i in 13..d.len() {
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use util::hash::*;
use util::uint::*;
use util::*;

/// Information describing execution of a transaction.
pub struct Receipt {
Expand Down
61 changes: 23 additions & 38 deletions src/spec.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -129,8 +112,9 @@ impl Spec {
s.out()
};
let r = Rlp::new(&seal);
(0..self.seal_fields).map(|i| r.at(i).raw().to_vec()).collect()
(0..self.seal_fields).map(|i| r.at(i).as_raw().to_vec()).collect()
},
hash: RefCell::new(None),
}
}

Expand Down Expand Up @@ -342,6 +326,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())
Expand All @@ -365,26 +360,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]
Expand Down
Loading