Remove genesis module, add more chain specs and separate out ethereum-specific stuff.

This commit is contained in:
Gav Wood 2016-01-09 17:15:55 +01:00
parent 20341072a9
commit a978cbad52
12 changed files with 138 additions and 273 deletions

33
res/frontier_test.json Normal file
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 } } }
}
}

File diff suppressed because one or more lines are too long

33
res/homestead_test.json Normal file
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 } } }
}
}

41
res/olympic.json Normal file
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

@ -75,18 +75,18 @@ impl BlockChain {
/// extern crate ethcore;
/// use std::env;
/// use std::str::FromStr;
/// use ethcore::genesis::*;
/// use ethcore::spec::*;
/// use ethcore::blockchain::*;
/// use util::hash::*;
/// use util::uint::*;
///
/// fn main() {
/// let genesis = Genesis::new_frontier();
/// let spec = Spec::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,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

@ -30,7 +30,6 @@ 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();

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

@ -0,0 +1,11 @@
//! 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::*;

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,14 +92,13 @@ 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 ethash;
pub mod block;
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,14 @@ 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 Olympic chain spec.
pub fn new_olympic() -> Spec { Self::from_json_utf8(include_bytes!("../res/olympic.json")) }
/// Create a new Frontier chain spec.
pub fn new_frontier() -> Spec { Self::from_json_utf8(include_bytes!("../res/frontier.json")) }
/// Create a new Morden chain spec.
pub fn new_morden() -> Spec { Self::from_json_utf8(include_bytes!("../res/morden.json")) }
}
#[cfg(test)]
@ -368,11 +232,13 @@ mod tests {
#[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();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
}
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());
morden.to_engine();
}
#[test]
@ -383,6 +249,6 @@ mod tests {
let genesis = frontier.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
let engine = frontier.to_engine();
frontier.to_engine();
}
}