From 6c785d28504aa1b6fc8471a6b19211b5db49e9f6 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 21 Dec 2015 17:40:48 +0000 Subject: [PATCH 1/5] Move all chain parameters into `engine_params` and let Engine impl provide them. --- src/engine.rs | 16 ++++++++++++++-- src/spec.rs | 36 +++++++++++++++--------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index edc1cddf7..b89b86f9e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,5 +1,7 @@ use std::collections::hash_map::*; use util::bytes::*; +use util::uint::*; +use util::rlp::*; use util::semantic_version::*; use util::error::*; use header::Header; @@ -30,6 +32,16 @@ pub trait Engine { /// Get the EVM schedule for 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, _env_info: &EnvInfo) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) } + // TODO: refactor in terms of `on_preseal_block` + fn block_reward(&self, _env_info: &EnvInfo) -> U256 { decode(&self.spec().engine_params.get("block_reward").unwrap()) } + + /// Block transformation functions, before and after the transactions. +// fn on_new_block(&self, _env_info: &EnvInfo, _block: &mut Block) -> Result<(), EthcoreError> {} +// fn on_preseal_block(&self, _env_info: &EnvInfo, _block: &mut Block) -> Result<(), EthcoreError> {} + /// Verify that `header` is valid. /// `parent` (the parent header) and `block` (the header's full block) may be provided for additional /// checks. Returns either a null `Ok` or a general error detailing the problem with import. @@ -42,8 +54,8 @@ pub trait Engine { /// Don't forget to call Super::populateFromParent when subclassing & overriding. fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) } - // TODO: buildin contract routing - this will require removing the built-in configuration reading logic from Spec - // into here and removing the Spec::builtins field. It's a big job. + // TODO: buildin contract routing - to do this properly, it will require removing the built-in configuration-reading logic + // from Spec into here and removing the Spec::builtins field. /* fn is_builtin(&self, a: Address) -> bool; fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum; fn execute_builtin(&self, a: Address, in: &[u8], out: &mut [u8]); diff --git a/src/spec.rs b/src/spec.rs index 2b0eb9496..b62da9620 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -19,11 +19,9 @@ pub struct Spec { // What engine are we using for this? pub engine_name: String, - // Various parameters for the chain operation. - pub block_reward: U256, - pub maximum_extra_data_size: U256, - pub account_start_nonce: U256, - pub misc: HashMap, + // Parameters concerning operation of the specific engine we're using. + // Name -> RLP-encoded value + pub engine_params: HashMap, // Builtin-contracts are here for now but would like to abstract into Engine API eventually. pub builtins: HashMap, @@ -62,10 +60,6 @@ impl Spec { Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap()) } - pub fn block_reward(&self) -> U256 { self.block_reward } - pub fn maximum_extra_data_size(&self) -> U256 { self.maximum_extra_data_size } - pub fn account_start_nonce(&self) -> U256 { self.account_start_nonce } - /// Compose the genesis block for this chain. pub fn genesis_block(&self) -> Bytes { // TODO @@ -78,10 +72,10 @@ impl Spec { pub fn olympic() -> Spec { Spec { engine_name: "Ethash".to_string(), - block_reward: finney() * U256::from(1500u64), - maximum_extra_data_size: U256::from(1024u64), - account_start_nonce: U256::from(0u64), - misc: vec![ + 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)), @@ -115,10 +109,10 @@ impl Spec { pub fn frontier() -> Spec { Spec { engine_name: "Ethash".to_string(), - block_reward: ether() * U256::from(5u64), - maximum_extra_data_size: U256::from(32u64), - account_start_nonce: U256::from(0u64), - misc: vec![ + 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)), @@ -152,10 +146,10 @@ impl Spec { pub fn morden() -> Spec { Spec { engine_name: "Ethash".to_string(), - block_reward: ether() * U256::from(5u64), - maximum_extra_data_size: U256::from(32u64), - account_start_nonce: U256::from(1u64) << 20, - misc: vec![ + 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)), From 07aef2499e5b1ed32b1645e8febfd9a689df9a17 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 21 Dec 2015 21:08:42 +0000 Subject: [PATCH 2/5] Spec can now read much from a JSON. Still todo builtins and params. --- src/genesis.rs | 24 +++++------ src/spec.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 120 insertions(+), 16 deletions(-) diff --git a/src/genesis.rs b/src/genesis.rs index d4829a70e..5b8bed1f8 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -34,7 +34,7 @@ impl Genesis { 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); + let (header, state) = Self::load_genesis_json(json, root); Self::new_from_header_and_state(header, state) } @@ -53,7 +53,7 @@ impl Genesis { } /// Loads genesis block from json file - fn load_genesis_json(json: &Json, state_root: &H256) -> (Header, HashMap) { + fn load_genesis_json(json: Json, state_root: H256) -> (Header, HashMap) { // once we commit ourselves to some json parsing library (serde?) // move it to proper data structure @@ -62,11 +62,20 @@ impl Genesis { 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.clone(), + state_root: state_root, transactions_root: empty_data_sha3.clone(), receipts_root: empty_data_sha3.clone(), log_bloom: H2048::new(), @@ -84,15 +93,6 @@ impl Genesis { } }; - 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))); - } - (header, state) } diff --git a/src/spec.rs b/src/spec.rs index b62da9620..d47a2a3c2 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -1,17 +1,41 @@ -use std::collections::hash_map::*; +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 flate2::read::GzDecoder; use util::uint::*; use util::hash::*; use util::bytes::*; use util::triehash::*; use util::error::*; use util::rlp::*; -use account::Account; +use util::sha3::*; +use account::*; use engine::Engine; use builtin::Builtin; use null_engine::NullEngine; use denominations::*; +use header::*; + +/// Converts file from base64 gzipped bytes to json +pub 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") +} + +/// Convert JSON to a string->RLP map. +pub fn json_to_rlp(_json: &Json) -> HashMap { + unimplemented!(); +} /// Parameters for a block chain; includes both those intrinsic to the design of the /// chain and those to be interpreted by the active chain engine. @@ -43,6 +67,9 @@ pub struct Spec { } impl Spec { +// pub fn new(engine_name: String, engine_params: HashMap, builtins: HashMap, genesis: Genesis) { +// } + /// Convert this object into a boxed Engine of the right underlying type. // TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead. pub fn to_engine(self) -> Result, EthcoreError> { @@ -62,13 +89,90 @@ impl Spec { /// Compose the genesis block for this chain. pub fn genesis_block(&self) -> Bytes { - // TODO - unimplemented!(); + let empty_list = RlpStream::new_list(0).out(); + let empty_list_sha3 = empty_list.sha3(); + let header = Header { + parent_hash: self.parent_hash.clone(), + timestamp: self.timestamp.clone(), + number: U256::from(0u8), + author: self.author.clone(), + transactions_root: SHA3_EMPTY.clone(), + uncles_hash: empty_list_sha3.clone(), + extra_data: self.extra_data.clone(), + state_root: self.state_root().clone(), + receipts_root: SHA3_EMPTY.clone(), + log_bloom: H2048::new().clone(), + gas_used: self.gas_used.clone(), + gas_limit: self.gas_limit.clone(), + difficulty: self.difficulty.clone(), + seal: { + let seal = { + let mut s = RlpStream::new_list(self.seal_fields); + s.append_raw(&self.seal_rlp, self.seal_fields); + s.out() + }; + let r = Rlp::new(&seal); + (0..self.seal_fields).map(|i| r.at(i).raw().to_vec()).collect() + }, + }; + let mut ret = RlpStream::new_list(3); + ret.append(&header); + ret.append_raw(&empty_list, 1); + ret.append_raw(&empty_list, 1); + ret.out() } } impl Spec { + /// Loads genesis block from json file + pub fn from_json(json: Json) -> Spec { + // once we commit ourselves to some json parsing library (serde?) + // move it to proper data structure + 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 builtins = { + // TODO: populate from json. + HashMap::new() + }; + + let (seal_fields, seal_rlp) = { + if json.find("mixhash").is_some() && json.find("nonce").is_some() { + let mut s = RlpStream::new(); + s.append(&H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap()); + s.append(&H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap()); + (2, s.out()) + } else { + // backup algo that will work with sealFields/sealRlp (and without). + (usize::from_str(&json["sealFields"].as_string().unwrap_or("0x")[2..]).unwrap(), json["sealRlp"].as_string().unwrap_or("0x")[2..].from_hex().unwrap()) + } + }; + + Spec { + engine_name: json["engineName"].as_string().unwrap().to_string(), + engine_params: json_to_rlp(&json["engineParams"]), + builtins: builtins, + parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(), + author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(), + difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(), + 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(), + genesis_state: state, + seal_fields: seal_fields, + seal_rlp: seal_rlp, + state_root_memo: RefCell::new(json["stateRoot"].as_string().map(|s| H256::from_str(&s[2..]).unwrap())), + } + } + pub fn olympic() -> Spec { Spec { engine_name: "Ethash".to_string(), From be3f8ffd4970130721705e29f4207deb7a9cfa64 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 23 Dec 2015 11:10:34 +0000 Subject: [PATCH 3/5] Additional docs. --- src/spec.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/spec.rs b/src/spec.rs index d47a2a3c2..9a7a56e23 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -67,9 +67,6 @@ pub struct Spec { } impl Spec { -// pub fn new(engine_name: String, engine_params: HashMap, builtins: HashMap, genesis: Genesis) { -// } - /// Convert this object into a boxed Engine of the right underlying type. // TODO avoid this hard-coded nastiness - use dynamic-linked plugin framework instead. pub fn to_engine(self) -> Result, EthcoreError> { @@ -125,7 +122,7 @@ impl Spec { impl Spec { - /// Loads genesis block from json file + /// Loads a chain-specification from a json data structure pub fn from_json(json: Json) -> Spec { // once we commit ourselves to some json parsing library (serde?) // move it to proper data structure @@ -157,7 +154,7 @@ impl Spec { Spec { engine_name: json["engineName"].as_string().unwrap().to_string(), - engine_params: json_to_rlp(&json["engineParams"]), + engine_params: json_to_rlp(&json["params"]), builtins: builtins, parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(), author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(), @@ -173,6 +170,7 @@ impl Spec { } } + /// Creates the Olympic network chain spec. pub fn olympic() -> Spec { Spec { engine_name: "Ethash".to_string(), @@ -210,6 +208,7 @@ impl Spec { } } + /// Creates the Frontier network chain spec. pub fn frontier() -> Spec { Spec { engine_name: "Ethash".to_string(), @@ -247,6 +246,7 @@ impl Spec { } } + /// Creates the Morden network chain spec. pub fn morden() -> Spec { Spec { engine_name: "Ethash".to_string(), From ff74e8239ddeab19fbd975e9f1e637f105f0d4da Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 23 Dec 2015 11:53:34 +0000 Subject: [PATCH 4/5] Spec's json_to_rlp. --- src/spec.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/spec.rs b/src/spec.rs index 9a7a56e23..b79624787 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -32,9 +32,28 @@ pub fn base_to_json(source: &[u8]) -> Json { Json::from_str(&s).expect("Json is invalid") } +/// Convert JSON value to equivlaent RLP representation. +// TODO: handle container types. +pub fn json_to_rlp(json: &Json) -> Bytes { + match json { + &Json::I64(o) => encode(&(o as u64)), + &Json::U64(o) => encode(&o), + &Json::String(ref s) if &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => { + encode(&U256::from_str(&s[2..]).unwrap()) + }, + &Json::String(ref s) => { + encode(s) + }, + _ => panic!() + } +} + /// Convert JSON to a string->RLP map. -pub fn json_to_rlp(_json: &Json) -> HashMap { - unimplemented!(); +pub fn json_to_rlp_map(json: &Json) -> HashMap { + json.as_object().unwrap().iter().map(|(k, v)| (k, json_to_rlp(v))).fold(HashMap::new(), |mut acc, kv| { + acc.insert(kv.0.clone(), kv.1); + acc + }) } /// Parameters for a block chain; includes both those intrinsic to the design of the @@ -154,7 +173,7 @@ impl Spec { Spec { engine_name: json["engineName"].as_string().unwrap().to_string(), - engine_params: json_to_rlp(&json["params"]), + engine_params: json_to_rlp_map(&json["params"]), builtins: builtins, parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(), author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(), From c85cabb598087ae0f69ef681ae590f0f1faea3a4 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 23 Dec 2015 11:56:38 +0000 Subject: [PATCH 5/5] Additional notes. --- src/engine.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/engine.rs b/src/engine.rs index b89b86f9e..d920f4bef 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -29,7 +29,7 @@ pub trait Engine { /// Get the general parameters of the chain. fn spec(&self) -> &Spec; - /// Get the EVM schedule for + /// Get the EVM schedule for the given `env_info`. fn evm_schedule(&self, env_info: &EnvInfo) -> EvmSchedule; /// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`. @@ -45,13 +45,16 @@ pub trait Engine { /// Verify that `header` is valid. /// `parent` (the parent header) and `block` (the header's full block) may be provided for additional /// checks. Returns either a null `Ok` or a general error detailing the problem with import. + // TODO: consider including State in the params. fn verify(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) } /// Additional verification for transactions in blocks. // TODO: Add flags for which bits of the transaction to check. + // TODO: consider including State in the params. fn verify_transaction(&self, _t: &Transaction, _header: &Header) -> Result<(), EthcoreError> { Ok(()) } /// Don't forget to call Super::populateFromParent when subclassing & overriding. + // TODO: consider including State in the params. fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) } // TODO: buildin contract routing - to do this properly, it will require removing the built-in configuration-reading logic @@ -60,4 +63,6 @@ pub trait Engine { fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum; fn execute_builtin(&self, a: Address, in: &[u8], out: &mut [u8]); */ + + // TODO: sealing stuff - though might want to leave this for later. }