diff --git a/src/engine.rs b/src/engine.rs index edc1cddf7..d920f4bef 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; @@ -27,25 +29,40 @@ 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`. + 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. + // 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 - 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]); */ + + // TODO: sealing stuff - though might want to leave this for later. } 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 2b0eb9496..b79624787 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -1,17 +1,60 @@ -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 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_map(json: &Json) -> HashMap