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

This commit is contained in:
arkpar 2016-01-09 18:50:45 +01:00
commit 28a658015d
22 changed files with 318 additions and 314 deletions

File diff suppressed because one or more lines are too long

34
res/null_morden.json Normal file
View File

@ -0,0 +1,34 @@
{
"engineName": "NullEngine",
"params": {
"accountStartNonce": "0x0100000",
"frontierCompatibilityModeLimit": "0xfffa2990",
"maximumExtraDataSize": "0x20",
"tieBreakingGas": false,
"minGasLimit": "0x1388",
"gasLimitBoundDivisor": "0x0400",
"minimumDifficulty": "0x020000",
"difficultyBoundDivisor": "0x0800",
"durationLimit": "0x0d",
"blockReward": "0x4563918244F40000",
"registrar": "",
"networkID" : "0x2"
},
"genesis": {
"nonce": "0x00006d6f7264656e",
"difficulty": "0x20000",
"mixHash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x2fefd8"
},
"accounts": {
"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "linear": { "base": 15, "word": 3 } } },
"102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" }
}
}

View File

@ -9,12 +9,12 @@ use util::hash::*;
use util::network::{NetworkService};
use ethcore::client::Client;
use ethcore::sync::EthSync;
use ethcore::spec::Spec;
use ethcore::ethereum;
fn main() {
let mut service = NetworkService::start().unwrap();
//TODO: replace with proper genesis and chain params.
let frontier = Spec::new_frontier();
let frontier = ethereum::new_frontier();
let mut dir = env::temp_dir();
dir.push(H32::random().hex());
let client = Arc::new(Client::new(&frontier.genesis_block(), &dir));

View File

@ -1,7 +1,6 @@
use util::*;
use transaction::*;
use receipt::*;
use blockchain::*;
use engine::*;
use header::*;
use env_info::*;
@ -125,7 +124,7 @@ impl<'engine> OpenBlock<'engine> {
}
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
pub fn close(self, _bc: &BlockChain) -> ClosedBlock { unimplemented!(); }
pub fn close(self, _uncles: Vec<Header>) -> ClosedBlock<'engine> { unimplemented!(); }
}
impl<'engine> IsBlock for OpenBlock<'engine> {
@ -153,3 +152,14 @@ impl SealedBlock {
impl IsBlock for SealedBlock {
fn block(&self) -> &Block { &self.block }
}
#[test]
fn open_block() {
use super::*;
use spec::*;
let engine = Spec::new_test().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

@ -76,18 +76,19 @@ impl BlockChain {
/// extern crate ethcore;
/// use std::env;
/// use std::str::FromStr;
/// use ethcore::genesis::*;
/// use ethcore::spec::*;
/// use ethcore::blockchain::*;
/// use ethcore::ethereum;
/// use util::hash::*;
/// use util::uint::*;
///
/// fn main() {
/// let genesis = Genesis::new_frontier();
/// let spec = ethereum::new_frontier();
///
/// let mut dir = env::temp_dir();
/// dir.push(H32::random().hex());
///
/// let bc = BlockChain::new(genesis.block(), &dir);
/// let bc = BlockChain::new(&spec.genesis_block(), &dir);
///
/// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3";
/// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap());

View File

@ -1,11 +1,6 @@
use std::sync::Arc;
use std::path::Path;
use util::uint::U256;
use util::hash::*;
use util::rlp::*;
use util::bytes::Bytes;
use util::*;
use blockchain::BlockChain;
use queue::BlockQueue;
use views::BlockView;
/// Status for a block in a queue.
@ -117,7 +112,6 @@ pub trait BlockChainClient : Sync {
/// Blockchain database client backed by a persistent database. Owns and manages a blockchain and a block queue.
pub struct Client {
chain: Arc<BlockChain>,
queue: BlockQueue,
}
impl Client {
@ -125,7 +119,6 @@ impl Client {
let chain = Arc::new(BlockChain::new(genesis, path));
Client {
chain: chain.clone(),
queue: BlockQueue::new(chain)
}
}
}
@ -185,7 +178,17 @@ impl BlockChainClient for Client {
}
fn import_block(&mut self, bytes: &[u8]) -> ImportResult {
self.queue.import_block(bytes)
//TODO: verify block
{
let block = BlockView::new(bytes);
let header = block.header_view();
let hash = header.sha3();
if self.chain.is_known(&hash) {
return ImportResult::Bad;
}
}
self.chain.insert_block(bytes);
ImportResult::Queued(QueueStatus::Known)
}
fn queue_status(&self) -> BlockQueueStatus {
@ -195,7 +198,6 @@ impl BlockChainClient for Client {
}
fn clear_queue(&mut self) {
self.queue.clear();
}
fn chain_info(&self) -> BlockChainInfo {

View File

@ -1,7 +1,6 @@
pub use util::*;
pub use env_info::*;
pub use evm_schedule::*;
pub use denominations::*;
pub use views::*;
pub use builtin::*;
pub use header::*;

View File

@ -25,8 +25,8 @@ pub trait Engine {
fn evm_schedule(&self, env_info: &EnvInfo) -> EvmSchedule;
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximum_extra_data_size").unwrap()) }
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) }
fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximumExtraDataSize").unwrap()) }
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("accountStartNonce").unwrap()) }
/// Block transformation functions, before and after the transactions.
fn on_new_block(&self, _block: &mut Block) {}

View File

@ -30,10 +30,17 @@ impl Engine for Ethash {
// TODO: test for on_close_block.
#[test]
fn playpen() {
use util::overlaydb::*;
let engine = Spec::new_morden().to_engine().unwrap();
use super::*;
use state::*;
let engine = 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()]);
assert!(SecTrieDB::new(&db, &genesis_header.state_root).contains(&address_from_hex("102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c")));
{
let s = State::from_existing(db.clone(), genesis_header.state_root.clone(), engine.account_start_nonce());
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000001")), U256::from(1u64));
}
let b = OpenBlock::new(engine.deref(), db, &genesis_header, vec![genesis_header.hash()]);
// let c = b.close();
}

72
src/ethereum/mod.rs Normal file
View File

@ -0,0 +1,72 @@
//! Ethereum protocol module.
//!
//! Contains all Ethereum network specific stuff, such as denominations and
//! consensus specifications.
pub mod ethash;
pub mod denominations;
pub use self::ethash::*;
pub use self::denominations::*;
use super::spec::*;
/// Create a new Olympic chain spec.
pub fn new_olympic() -> Spec { Spec::from_json_utf8(include_bytes!("res/olympic.json")) }
/// Create a new Frontier mainnet chain spec.
pub fn new_frontier() -> Spec { Spec::from_json_utf8(include_bytes!("res/frontier.json")) }
/// Create a new Frontier chain spec as though it never changes to Homestead.
pub fn new_frontier_test() -> Spec { Spec::from_json_utf8(include_bytes!("res/frontier_test.json")) }
/// Create a new Homestead chain spec as though it never changed from Frontier.
pub fn new_homestead_test() -> Spec { Spec::from_json_utf8(include_bytes!("res/homestead_test.json")) }
/// Create a new Morden chain spec.
pub fn new_morden() -> Spec { Spec::from_json_utf8(include_bytes!("res/morden.json")) }
#[cfg(test)]
mod tests {
use common::*;
use state::*;
use engine::*;
use super::*;
#[test]
fn ensure_db_good() {
let engine = 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 s = State::from_existing(db.clone(), genesis_header.state_root.clone(), engine.account_start_nonce());
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000001")), U256::from(1u64));
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000002")), U256::from(1u64));
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000003")), U256::from(1u64));
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000004")), U256::from(1u64));
assert_eq!(s.balance(&address_from_hex("102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c")), U256::from(1u64) << 200);
assert_eq!(s.balance(&address_from_hex("0000000000000000000000000000000000000000")), U256::from(0u64));
}
#[test]
fn morden() {
let morden = 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());
morden.to_engine();
}
#[test]
fn frontier() {
let frontier = new_frontier();
assert_eq!(*frontier.state_root(), H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap());
let genesis = frontier.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
frontier.to_engine();
}
}

View File

@ -0,0 +1,33 @@
{
"engineName": "Ethash",
"params": {
"accountStartNonce": "0x00",
"frontierCompatibilityModeLimit": "0xffffffffffffffff",
"maximumExtraDataSize": "0x20",
"tieBreakingGas": false,
"minGasLimit": "0x1388",
"gasLimitBoundDivisor": "0x0400",
"minimumDifficulty": "0x020000",
"difficultyBoundDivisor": "0x0800",
"durationLimit": "0x0d",
"blockReward": "0x4563918244F40000",
"registrar" : "0xc6d9d2cd449a754c494264e1809c50e34d64562b",
"networkID" : "0x1"
},
"genesis": {
"nonce": "0x0000000000000042",
"difficulty": "0x400000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1388"
},
"accounts": {
"0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
"0000000000000000000000000000000000000003": { "builtin": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
"0000000000000000000000000000000000000004": { "builtin": { "name": "identity", "linear": { "base": 15, "word": 3 } } }
}
}

View File

@ -0,0 +1,33 @@
{
"engineName": "Ethash",
"params": {
"accountStartNonce": "0x00",
"frontierCompatibilityModeLimit": "0xffffffff",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"tieBreakingGas": false,
"gasLimitBoundDivisor": "0x0400",
"minimumDifficulty": "0x020000",
"difficultyBoundDivisor": "0x0800",
"durationLimit": "0x0d",
"blockReward": "0x4563918244F40000",
"registrar" : "0xc6d9d2cd449a754c494264e1809c50e34d64562b",
"networkID" : "0x1"
},
"genesis": {
"nonce": "0x0000000000000042",
"difficulty": "0x400000000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x1388"
},
"accounts": {
"0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
"0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "linear": { "base": 15, "word": 3 } } }
}
}

View File

@ -0,0 +1,41 @@
{
"engineName": "Ethash",
"params": {
"accountStartNonce": "0x00",
"frontierCompatibilityModeLimit": "0xffffffff",
"maximumExtraDataSize": "0x0400",
"tieBreakingGas": false,
"minGasLimit": "125000",
"gasLimitBoundDivisor": "0x0400",
"minimumDifficulty": "0x020000",
"difficultyBoundDivisor": "0x0800",
"durationLimit": "0x08",
"blockReward": "0x14D1120D7B160000",
"registrar": "5e70c0bbcd5636e0f9f9316e9f8633feb64d4050",
"networkID" : "0x0"
},
"genesis": {
"nonce": "0x000000000000002a",
"difficulty": "0x20000",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x2fefd8"
},
"accounts": {
"0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },
"0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "linear": { "base": 600, "word": 120 } } },
"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "linear": { "base": 15, "word": 3 } } },
"dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"e6716f9544a56c530d868e4bfbacb172315bdead": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"b9c015918bdaba24b4ff057a92a3873d6eb201be": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"2ef47100e0787b915105fd5e3f4ff6752079d5cb": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"cd2a3d9f938e13cd947ec05abc7fe734df8dd826": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"6c386a4b26f73c802f34673f7248bb118f97424a": { "balance": "1606938044258990275541962092341162602522202993782792835301376" },
"e4157b34ea9615cfbde6b4fda419828124b70c78": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
}
}

View File

@ -1,115 +0,0 @@
use util::*;
use flate2::read::GzDecoder;
use account::*;
use header::*;
/// Converts file from base64 gzipped bytes to json
fn base_to_json(source: &[u8]) -> Json {
// there is probably no need to store genesis in based64 gzip,
// but that's what go does, and it was easy to load it this way
let data = source.from_base64().expect("Genesis block is malformed!");
let data_ref: &[u8] = &data;
let mut decoder = GzDecoder::new(data_ref).expect("Gzip is invalid");
let mut s: String = "".to_string();
decoder.read_to_string(&mut s).expect("Gzip is invalid");
Json::from_str(&s).expect("Json is invalid")
}
pub struct Genesis {
block: Vec<u8>,
state: HashMap<Address, Account>
}
impl Genesis {
/// Creates genesis block for frontier network
pub fn new_frontier() -> Genesis {
let root = H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap();
let json = base_to_json(include_bytes!("../res/genesis_frontier"));
let (header, state) = Self::load_genesis_json(json, root);
Self::new_from_header_and_state(header, state)
}
/// Creates genesis block from header and state hashmap
pub fn new_from_header_and_state(header: Header, state: HashMap<Address, Account>) -> Genesis {
let empty_list = RlpStream::new_list(0).out();
let mut stream = RlpStream::new_list(3);
stream.append(&header);
stream.append_raw(&empty_list, 1);
stream.append_raw(&empty_list, 1);
Genesis {
block: stream.out(),
state: state
}
}
/// Loads genesis block from json file
fn load_genesis_json(json: Json, state_root: H256) -> (Header, HashMap<Address, Account>) {
// once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure
let empty_list = RlpStream::new_list(0).out();
let empty_list_sha3 = empty_list.sha3();
let empty_data = encode(&"");
let empty_data_sha3 = empty_data.sha3();
let mut state = HashMap::new();
let accounts = json["alloc"].as_object().expect("Missing genesis state");
for (address, acc) in accounts.iter() {
let addr = Address::from_str(address).unwrap();
let o = acc.as_object().unwrap();
let balance = U256::from_dec_str(o["balance"].as_string().unwrap()).unwrap();
state.insert(addr, Account::new_basic(balance, U256::from(0)));
}
let header = Header {
parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(),
uncles_hash: empty_list_sha3.clone(),
author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(),
state_root: state_root,
transactions_root: empty_data_sha3.clone(),
receipts_root: empty_data_sha3.clone(),
log_bloom: H2048::new(),
difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(),
number: U256::from(0u8),
gas_limit: U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap(),
gas_used: U256::from(0u8),
timestamp: U256::from_str(&json["timestamp"].as_string().unwrap()[2..]).unwrap(),
extra_data: json["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
seal: {
// ethash specific fields
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)
}
/// Returns genesis block
pub fn block(&self) -> &[u8] {
&self.block
}
/// Returns genesis block state
pub fn state(&self) -> &HashMap<Address, Account> {
&self.state
}
// not sure if this one is needed
pub fn drain(self) -> (Vec<u8>, HashMap<Address, Account>) {
(self.block, self.state)
}
}
#[test]
fn test_genesis() {
use views::*;
let g = Genesis::new_frontier();
let view = BlockView::new(&g.block).header_view();
let genesis_hash = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap();
assert_eq!(view.sha3(), genesis_hash);
}

View File

@ -92,18 +92,14 @@ pub mod account;
pub mod header;
pub mod transaction;
pub mod receipt;
pub mod denominations;
pub mod null_engine;
pub mod evm_schedule;
pub mod builtin;
pub mod spec;
pub mod genesis;
pub mod views;
pub mod blockchain;
pub mod extras;
pub mod client;
pub mod sync;
pub mod verification;
pub mod queue;
pub mod ethash;
pub mod block;
pub mod ethereum;

View File

@ -2,7 +2,6 @@ use common::*;
use flate2::read::GzDecoder;
use engine::*;
use null_engine::*;
use ethash::*;
/// Converts file from base64 gzipped bytes to json
pub fn gzip64res_to_json(source: &[u8]) -> Json {
@ -77,7 +76,7 @@ impl Spec {
pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> {
match self.engine_name.as_ref() {
"NullEngine" => Ok(NullEngine::new_boxed(self)),
"Ethash" => Ok(Ethash::new_boxed(self)),
"Ethash" => Ok(super::ethereum::Ethash::new_boxed(self)),
_ => Err(EthcoreError::UnknownName)
}
}
@ -191,144 +190,6 @@ impl Spec {
}
}
/// Returns the builtins map for the standard network of Ethereum Olympic, Frontier and Homestead.
fn standard_builtins() -> HashMap<Address, Builtin> {
let mut ret = HashMap::new();
ret.insert(Address::from_str("0000000000000000000000000000000000000001").unwrap(), Builtin::from_named_linear("ecrecover", 3000, 0).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000002").unwrap(), Builtin::from_named_linear("sha256", 60, 12).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000003").unwrap(), Builtin::from_named_linear("ripemd160", 600, 120).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000004").unwrap(), Builtin::from_named_linear("identity", 15, 3).unwrap());
ret
}
/// Creates the Olympic network chain spec.
pub fn new_like_olympic() -> Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("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
}),
builtins: Self::standard_builtins(),
parent_hash: H256::new(),
author: Address::new(),
difficulty: U256::from(131_072u64),
gas_limit: U256::from(0u64),
gas_used: U256::from(0u64),
timestamp: U256::from(0u64),
extra_data: vec![],
genesis_state: vec![ // TODO: make correct
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0)))
].into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0, vec.1);
acc
}),
seal_fields: 2,
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&H256::new()); r.append(&0x2au64); r.out() }, // TODO: make correct
state_root_memo: RefCell::new(None),
}
}
/// Creates the Frontier network chain spec, except for the genesis state, which is blank.
pub fn new_like_frontier() -> Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("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
}),
builtins: Self::standard_builtins(),
parent_hash: H256::new(),
author: Address::new(),
difficulty: U256::from(131_072u64),
gas_limit: U256::from(0u64),
gas_used: U256::from(0u64),
timestamp: U256::from(0u64),
extra_data: vec![],
genesis_state: vec![ // TODO: make correct
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0)))
].into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0, vec.1);
acc
}),
seal_fields: 2,
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&H256::new()); r.append(&0x42u64); r.out() },
state_root_memo: RefCell::new(None),
}
}
/// Creates the actual Morden network chain spec.
pub fn new_morden_manual() -> Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
("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
}),
builtins: Self::standard_builtins(),
parent_hash: H256::new(),
author: Address::new(),
difficulty: U256::from(0x20000u64),
gas_limit: U256::from(0x2fefd8u64),
gas_used: U256::from(0u64),
timestamp: U256::from(0u64),
extra_data: vec![],
genesis_state: {
let n = U256::from(1) << 20;
vec![
(Address::from_str("0000000000000000000000000000000000000001").unwrap(), Account::new_basic(U256::from(1), n)),
(Address::from_str("0000000000000000000000000000000000000002").unwrap(), Account::new_basic(U256::from(1), n)),
(Address::from_str("0000000000000000000000000000000000000003").unwrap(), Account::new_basic(U256::from(1), n)),
(Address::from_str("0000000000000000000000000000000000000004").unwrap(), Account::new_basic(U256::from(1), n)),
(Address::from_str("102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c").unwrap(), Account::new_basic(U256::from(1) << 200, n))
]}.into_iter().fold(HashMap::new(), | mut acc, vec | {
acc.insert(vec.0, vec.1);
acc
}),
seal_fields: 2,
seal_rlp: {
let mut r = RlpStream::new();
r.append(&H256::from_str("00000000000000000000000000000000000000647572616c65787365646c6578").unwrap());
r.append(&FromHex::from_hex("00006d6f7264656e").unwrap());
r.out()
},
state_root_memo: RefCell::new(None),
}
}
/// 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()) {
@ -351,11 +212,8 @@ impl Spec {
Self::from_json(json)
}
/// Create a new Morden chain spec.
pub fn new_morden() -> Spec { Self::from_json_utf8(include_bytes!("../res/morden.json")) }
/// Create a new Frontier chain spec.
pub fn new_frontier() -> Spec { Self::from_json_utf8(include_bytes!("../res/frontier.json")) }
/// Create a new Spec which conforms to the Morden chain except that it's a NullEngine consensus.
pub fn new_test() -> Spec { Self::from_json_utf8(include_bytes!("../res/null_morden.json")) }
}
#[cfg(test)]
@ -367,22 +225,13 @@ mod tests {
use super::*;
#[test]
fn morden() {
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();
fn test_chain() {
let test_spec = Spec::new_test();
assert_eq!(*test_spec.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = test_spec.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
}
}
#[test]
fn frontier() {
let frontier = Spec::new_frontier();
assert_eq!(*frontier.state_root(), H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap());
let genesis = frontier.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
let engine = frontier.to_engine();
let _ = test_spec.to_engine();
}
}

View File

@ -27,7 +27,7 @@ impl State {
let mut root = H256::new();
{
// init trie and reset root too null
let _ = TrieDBMut::new(&mut db, &mut root);
let _ = SecTrieDBMut::new(&mut db, &mut root);
}
State {
@ -39,10 +39,10 @@ impl State {
}
/// Creates new state with existing state root
pub fn from_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
pub fn from_existing(db: OverlayDB, root: H256, account_start_nonce: U256) -> State {
{
// trie should panic! if root does not exist
let _ = TrieDB::new(&mut db, &mut root);
let _ = SecTrieDB::new(&db, &root);
}
State {
@ -142,7 +142,7 @@ impl State {
unimplemented!();
}
/// Commit accounts to TrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
/// Commit accounts to SecTrieDBMut. This is similar to cpp-ethereum's dev::eth::commit.
/// `accounts` is mutable because we may need to commit the code or storage and record that.
pub fn commit_into(db: &mut HashDB, mut root: H256, accounts: &mut HashMap<Address, Option<Account>>) -> H256 {
// first, commit the sub trees.
@ -158,7 +158,7 @@ impl State {
}
{
let mut trie = TrieDBMut::from_existing(db, &mut root);
let mut trie = SecTrieDBMut::from_existing(db, &mut root);
for (address, ref a) in accounts.iter() {
match a {
&&Some(ref account) => trie.insert(address, &account.rlp()),
@ -184,7 +184,7 @@ impl State {
/// `require_code` requires that the code be cached, too.
fn get(&self, a: &Address, require_code: bool) -> Ref<Option<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)));
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
if require_code {
if let Some(ref mut account) = self.cache.borrow_mut().get_mut(a).unwrap().as_mut() {
account.cache_code(&self.db);
@ -202,7 +202,7 @@ impl State {
/// 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)));
SecTrieDB::new(&self.db, &self.root).get(&a).map(|rlp| Account::from_rlp(rlp)));
let preexists = self.cache.borrow().get(a).unwrap().is_none();
if preexists {
self.cache.borrow_mut().insert(a.clone(), Some(default()));
@ -333,7 +333,7 @@ fn ensure_cached() {
let a = Address::from_str("0000000000000000000000000000000000000000").unwrap();
s.require(&a, false);
s.commit();
assert_eq!(s.root().hex(), "ec68b85fa2e0526dc0e821a5b33135459114f19173ce0479f5c09b21cc25b9a4");
assert_eq!(s.root().hex(), "0ce23f3c809de377b008a4a3ee94a0834aac8bec1f86e28ffe4fdb5a15b0c785");
}
#[test]

43
src/sync/io.rs Normal file
View File

@ -0,0 +1,43 @@
use client::BlockChainClient;
use util::network::{HandlerIo, PeerId, PacketId, Error as NetworkError};
pub trait SyncIo {
fn disable_peer(&mut self, peer_id: &PeerId);
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>;
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>;
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
}
pub struct NetSyncIo<'s, 'h> where 'h:'s {
network: &'s mut HandlerIo<'h>,
chain: &'s mut BlockChainClient
}
impl<'s, 'h> NetSyncIo<'s, 'h> {
pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> {
NetSyncIo {
network: network,
chain: chain,
}
}
}
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
fn disable_peer(&mut self, peer_id: &PeerId) {
self.network.disable_peer(*peer_id);
}
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>{
self.network.respond(packet_id, data)
}
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError>{
self.network.send(peer_id, packet_id, data)
}
fn chain<'a>(&'a mut self) -> &'a mut BlockChainClient {
self.chain
}
}

View File

@ -12,11 +12,11 @@
/// use util::network::NetworkService;
/// use ethcore::client::Client;
/// use ethcore::sync::EthSync;
/// use ethcore::spec::Spec;
/// use ethcore::ethereum;
///
/// fn main() {
/// let mut service = NetworkService::start().unwrap();
/// let frontier = Spec::new_frontier();
/// let frontier = ethereum::new_frontier();
/// let dir = env::temp_dir();
/// let client = Arc::new(Client::new(&frontier.genesis_block(), &dir));
/// EthSync::register(&mut service, client);