Merge pull request #54 from gavofyork/gav

Spec with tested Morden genesis decoder and builtins.
This commit is contained in:
Gav Wood 2016-01-08 22:05:54 +01:00
commit 7cf9f0e22d
9 changed files with 488 additions and 69 deletions

View File

@ -9,11 +9,12 @@ authors = ["Ethcore <admin@ethcore.io>"]
[dependencies] [dependencies]
log = "0.3" log = "0.3"
env_logger = "0.3" env_logger = "0.3"
ethcore-util = "0.1.0" ethcore-util = { path = "../ethcore-util" }
rustc-serialize = "0.3" rustc-serialize = "0.3"
flate2 = "0.2" flate2 = "0.2"
rocksdb = "0.2.1" rocksdb = "0.2.1"
heapsize = "0.2.0" heapsize = "0.2.0"
rust-crypto = "0.2.34"
evmjit = { path = "rust-evmjit", optional = true } evmjit = { path = "rust-evmjit", optional = true }

34
res/morden.json Normal file
View File

@ -0,0 +1,34 @@
{
"engineName": "Ethash",
"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

@ -1,4 +1,15 @@
use std::cmp::min;
use std::fmt;
use util::uint::*; 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 crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
/// Definition of a contract whose implementation is built-in. /// Definition of a contract whose implementation is built-in.
pub struct Builtin { pub struct Builtin {
@ -8,3 +19,253 @@ pub struct Builtin {
/// being placed into the second. /// being placed into the second.
pub execute: Box<Fn(&[u8], &mut [u8])>, pub execute: Box<Fn(&[u8], &mut [u8])>,
} }
impl fmt::Debug for Builtin {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Builtin>")
}
}
impl Builtin {
/// Create a new object from components.
pub fn new(cost: Box<Fn(usize) -> U256>, execute: Box<Fn(&[u8], &mut [u8])>) -> Builtin {
Builtin {cost: cost, execute: execute}
}
/// Create a new object from a builtin-function name with a linear cost associated with input size.
pub fn from_named_linear(name: &str, base_cost: usize, word_cost: usize) -> Option<Builtin> {
new_builtin_exec(name).map(|b| {
let cost = Box::new(move|s: usize| -> U256 {
U256::from(base_cost) + U256::from(word_cost) * U256::from((s + 31) / 32)
});
Self::new(cost, b)
})
}
/// Create a builtin from JSON.
///
/// JSON must be of the form `{ "name": "identity", "linear": {"base": 10, "word": 20} }`.
pub fn from_json(json: &Json) -> Option<Builtin> {
// NICE: figure out a more convenient means of handing errors here.
if let Json::String(ref name) = json["name"] {
if let Json::Object(ref o) = json["linear"] {
if let Json::U64(ref word) = o["word"] {
if let Json::U64(ref base) = o["base"] {
return Self::from_named_linear(&name[..], *base as usize, *word as usize);
}
}
}
}
None
}
}
pub fn copy_to(src: &[u8], dest: &mut[u8]) {
// NICE: optimise
for i in 0..min(src.len(), dest.len()) {
dest[i] = src[i];
}
}
/// Create a new builtin executor according to `name`.
/// TODO: turn in to a factory with dynamic registration.
pub fn new_builtin_exec(name: &str) -> Option<Box<Fn(&[u8], &mut [u8])>> {
match name {
"identity" => Some(Box::new(move|input: &[u8], output: &mut[u8]| {
for i in 0..min(input.len(), output.len()) {
output[i] = input[i];
}
})),
"ecrecover" => Some(Box::new(move|input: &[u8], output: &mut[u8]| {
#[repr(packed)]
#[derive(Debug)]
struct InType {
hash: H256,
v: H256,
r: H256,
s: H256,
}
let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() };
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) {
Ok(p) => {
let r = p.as_slice().sha3();
// NICE: optimise and separate out into populate-like function
for i in 0..min(32, output.len()) {
output[i] = if i < 12 {0} else {r[i]};
}
}
_ => {}
};
}
}
})),
"sha256" => Some(Box::new(move|input: &[u8], output: &mut[u8]| {
let mut sha = Sha256::new();
sha.input(input);
if output.len() >= 32 {
sha.result(output);
} else {
let mut ret = H256::new();
sha.result(ret.as_slice_mut());
copy_to(&ret, output);
}
})),
"ripemd160" => Some(Box::new(move|input: &[u8], output: &mut[u8]| {
let mut sha = Ripemd160::new();
sha.input(input);
let mut ret = H256::new();
sha.result(&mut ret.as_slice_mut()[12..32]);
copy_to(&ret, output);
})),
_ => None
}
}
#[test]
fn identity() {
let f = new_builtin_exec("identity").unwrap();
let i = [0u8, 1, 2, 3];
let mut o2 = [255u8; 2];
f(&i[..], &mut o2[..]);
assert_eq!(i[0..2], o2);
let mut o4 = [255u8; 4];
f(&i[..], &mut o4[..]);
assert_eq!(i, o4);
let mut o8 = [255u8; 8];
f(&i[..], &mut o8[..]);
assert_eq!(i, o8[..4]);
assert_eq!([255u8; 4], o8[4..]);
}
#[test]
fn sha256() {
use rustc_serialize::hex::FromHex;
let f = new_builtin_exec("sha256").unwrap();
let i = [0u8; 0];
let mut o = [255u8; 32];
f(&i[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").unwrap())[..]);
let mut o8 = [255u8; 8];
f(&i[..], &mut o8[..]);
assert_eq!(&o8[..], &(FromHex::from_hex("e3b0c44298fc1c14").unwrap())[..]);
let mut o34 = [255u8; 34];
f(&i[..], &mut o34[..]);
assert_eq!(&o34[..], &(FromHex::from_hex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855ffff").unwrap())[..]);
}
#[test]
fn ripemd160() {
use rustc_serialize::hex::FromHex;
let f = new_builtin_exec("ripemd160").unwrap();
let i = [0u8; 0];
let mut o = [255u8; 32];
f(&i[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("0000000000000000000000009c1185a5c5e9fc54612808977ee8f548b2258d31").unwrap())[..]);
let mut o8 = [255u8; 8];
f(&i[..], &mut o8[..]);
assert_eq!(&o8[..], &(FromHex::from_hex("0000000000000000").unwrap())[..]);
let mut o34 = [255u8; 34];
f(&i[..], &mut o34[..]);
assert_eq!(&o34[..], &(FromHex::from_hex("0000000000000000000000009c1185a5c5e9fc54612808977ee8f548b2258d31ffff").unwrap())[..]);
}
#[test]
fn ecrecover() {
use rustc_serialize::hex::FromHex;
/*let k = KeyPair::from_secret(b"test".sha3()).unwrap();
let a: Address = From::from(k.public().sha3());
println!("Address: {}", a);
let m = b"hello world".sha3();
println!("Message: {}", m);
let s = k.sign(&m).unwrap();
println!("Signed: {}", s);*/
let f = new_builtin_exec("ecrecover").unwrap();
let i = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let mut o = [255u8; 32];
f(&i[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("000000000000000000000000c08b5542d177ac6686946920409741463a15dddb").unwrap())[..]);
let mut o8 = [255u8; 8];
f(&i[..], &mut o8[..]);
assert_eq!(&o8[..], &(FromHex::from_hex("0000000000000000").unwrap())[..]);
let mut o34 = [255u8; 34];
f(&i[..], &mut o34[..]);
assert_eq!(&o34[..], &(FromHex::from_hex("000000000000000000000000c08b5542d177ac6686946920409741463a15dddbffff").unwrap())[..]);
let i_bad = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001a650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);
let i_bad = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001b0000000000000000000000000000000000000000000000000000000000000000").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);
let i_bad = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001b").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);
let i_bad = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000001b").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);
let i_bad = FromHex::from_hex("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001bffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);
// TODO: Should this (corrupted version of the above) fail rather than returning some address?
/* let i_bad = FromHex::from_hex("48173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03").unwrap();
let mut o = [255u8; 32];
f(&i_bad[..], &mut o[..]);
assert_eq!(&o[..], &(FromHex::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap())[..]);*/
}
#[test]
fn from_named_linear() {
let b = Builtin::from_named_linear("identity", 10, 20).unwrap();
assert_eq!((*b.cost)(0), U256::from(10));
assert_eq!((*b.cost)(1), U256::from(30));
assert_eq!((*b.cost)(32), U256::from(30));
assert_eq!((*b.cost)(33), U256::from(50));
let i = [0u8, 1, 2, 3];
let mut o = [255u8; 4];
(*b.execute)(&i[..], &mut o[..]);
assert_eq!(i, o);
}
#[test]
fn from_json() {
let text = "{ \"name\": \"identity\", \"linear\": {\"base\": 10, \"word\": 20} }";
let json = Json::from_str(text).unwrap();
let b = Builtin::from_json(&json).unwrap();
assert_eq!((*b.cost)(0), U256::from(10));
assert_eq!((*b.cost)(1), U256::from(30));
assert_eq!((*b.cost)(32), U256::from(30));
assert_eq!((*b.cost)(33), U256::from(50));
let i = [0u8, 1, 2, 3];
let mut o = [255u8; 4];
(*b.execute)(&i[..], &mut o[..]);
assert_eq!(i, o);
}

View File

@ -57,7 +57,7 @@ pub trait Engine {
// TODO: consider including State in the params. // TODO: consider including State in the params.
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) } 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 // TODO: builtin 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. // from Spec into here and removing the Spec::builtins field.
/* fn is_builtin(&self, a: Address) -> bool; /* fn is_builtin(&self, a: Address) -> bool;
fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum; fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum;

22
src/ethash.rs Normal file
View File

@ -0,0 +1,22 @@
use engine::Engine;
use spec::Spec;
use evm_schedule::EvmSchedule;
use env_info::EnvInfo;
/// Engine using Ethash proof-of-work consensus algorithm, suitable for Ethereum
/// mainnet chains in the Olympic, Frontier and Homestead eras.
pub struct Ethash {
spec: Spec,
}
impl Ethash {
pub fn new_boxed(spec: Spec) -> Box<Engine> {
Box::new(Ethash{spec: spec})
}
}
impl Engine for Ethash {
fn name(&self) -> &str { "Ethash" }
fn spec(&self) -> &Spec { &self.spec }
fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() }
}

View File

@ -89,7 +89,7 @@ impl Genesis {
// ethash specific fields // ethash specific fields
let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap(); let mixhash = H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap();
let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap(); let nonce = H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap();
vec![mixhash.to_vec(), nonce.to_vec()] vec![encode(&mixhash), encode(&nonce)]
} }
}; };

View File

@ -3,12 +3,22 @@ use util::bytes::*;
use util::uint::*; use util::uint::*;
use util::rlp::*; use util::rlp::*;
pub static ZERO_ADDRESS: Address = Address([0x00; 20]); /// Type for a 2048-bit log-bloom, as used by our blocks.
pub static ZERO_H256: H256 = H256([0x00; 32]);
pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]);
pub type LogBloom = H2048; pub type LogBloom = H2048;
/// Constant address for point 0. Often used as a default.
pub static ZERO_ADDRESS: Address = Address([0x00; 20]);
/// Constant 256-bit datum for 0. Often used as a default.
pub static ZERO_H256: H256 = H256([0x00; 32]);
/// Constant 2048-bit datum for 0. Often used as a default.
pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]);
/// A block header.
///
/// Reflects the specific RLP fields of a block in the chain with additional room for the seal
/// which is non-specific.
///
/// Doesn't do all that much on its own.
#[derive(Debug)] #[derive(Debug)]
pub struct Header { pub struct Header {
pub parent_hash: H256, pub parent_hash: H256,
@ -31,6 +41,7 @@ pub struct Header {
} }
impl Header { impl Header {
/// Create a new, default-valued, header.
pub fn new() -> Header { pub fn new() -> Header {
Header { Header {
parent_hash: ZERO_H256.clone(), parent_hash: ZERO_H256.clone(),
@ -76,7 +87,7 @@ impl Decodable for Header {
}; };
for i in 13..d.len() { for i in 13..d.len() {
blockheader.seal.push(try!(Decodable::decode(&d[i]))); blockheader.seal.push(d[i].as_raw().to_vec());
} }
Ok(blockheader) Ok(blockheader)
@ -101,11 +112,33 @@ impl Encodable for Header {
self.extra_data.encode(e); self.extra_data.encode(e);
for b in self.seal.iter() { for b in self.seal.iter() {
b.encode(e); e.emit_raw(&b);
} }
}) })
} }
} }
/*
trait RlpStandard {
fn append(&self, s: &mut RlpStream);
}
impl RlpStandard for Header {
fn append(&self, s: &mut RlpStream) {
s.append_list(13);
s.append(self.parent_hash);
s.append_raw(self.seal[0]);
s.append_standard(self.x);
}
fn populate(&mut self, s: &Rlp) {
}
}
impl RlpStream {
fn append_standard<O>(&mut self, o: &O) where O: RlpStandard {
o.append(self);
}
}
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View File

@ -77,6 +77,7 @@ extern crate rustc_serialize;
extern crate flate2; extern crate flate2;
extern crate rocksdb; extern crate rocksdb;
extern crate heapsize; extern crate heapsize;
extern crate crypto;
extern crate env_logger; extern crate env_logger;
#[cfg(feature = "jit" )] #[cfg(feature = "jit" )]
@ -104,3 +105,4 @@ pub mod genesis;
pub mod views; pub mod views;
pub mod blockchain; pub mod blockchain;
pub mod extras; pub mod extras;
pub mod ethash;

View File

@ -17,11 +17,12 @@ use account::*;
use engine::Engine; use engine::Engine;
use builtin::Builtin; use builtin::Builtin;
use null_engine::NullEngine; use null_engine::NullEngine;
use ethash::Ethash;
use denominations::*; use denominations::*;
use header::*; use header::*;
/// Converts file from base64 gzipped bytes to json /// Converts file from base64 gzipped bytes to json
pub fn base_to_json(source: &[u8]) -> Json { pub fn gzip64res_to_json(source: &[u8]) -> Json {
// there is probably no need to store genesis in based64 gzip, // 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 // 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 = source.from_base64().expect("Genesis block is malformed!");
@ -34,11 +35,12 @@ pub fn base_to_json(source: &[u8]) -> Json {
/// Convert JSON value to equivlaent RLP representation. /// Convert JSON value to equivlaent RLP representation.
// TODO: handle container types. // TODO: handle container types.
pub fn json_to_rlp(json: &Json) -> Bytes { fn json_to_rlp(json: &Json) -> Bytes {
match json { match json {
&Json::Boolean(o) => encode(&(if o {1u64} else {0})),
&Json::I64(o) => encode(&(o as u64)), &Json::I64(o) => encode(&(o as u64)),
&Json::U64(o) => encode(&o), &Json::U64(o) => encode(&o),
&Json::String(ref s) if &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => { &Json::String(ref s) if s.len() >= 2 && &s[0..2] == "0x" && U256::from_str(&s[2..]).is_ok() => {
encode(&U256::from_str(&s[2..]).unwrap()) encode(&U256::from_str(&s[2..]).unwrap())
}, },
&Json::String(ref s) => { &Json::String(ref s) => {
@ -49,7 +51,7 @@ pub fn json_to_rlp(json: &Json) -> Bytes {
} }
/// Convert JSON to a string->RLP map. /// Convert JSON to a string->RLP map.
pub fn json_to_rlp_map(json: &Json) -> HashMap<String, Bytes> { fn json_to_rlp_map(json: &Json) -> HashMap<String, Bytes> {
json.as_object().unwrap().iter().map(|(k, v)| (k, json_to_rlp(v))).fold(HashMap::new(), |mut acc, kv| { 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.insert(kv.0.clone(), kv.1);
acc acc
@ -58,6 +60,7 @@ pub fn json_to_rlp_map(json: &Json) -> HashMap<String, Bytes> {
/// Parameters for a block chain; includes both those intrinsic to the design of the /// 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. /// chain and those to be interpreted by the active chain engine.
#[derive(Debug)]
pub struct Spec { pub struct Spec {
// What engine are we using for this? // What engine are we using for this?
pub engine_name: String, pub engine_name: String,
@ -91,6 +94,7 @@ impl Spec {
pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> { pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> {
match self.engine_name.as_ref() { match self.engine_name.as_ref() {
"NullEngine" => Ok(NullEngine::new_boxed(self)), "NullEngine" => Ok(NullEngine::new_boxed(self)),
"Ethash" => Ok(Ethash::new_boxed(self)),
_ => Err(EthcoreError::UnknownName) _ => Err(EthcoreError::UnknownName)
} }
} }
@ -98,25 +102,22 @@ impl Spec {
/// Return the state root for the genesis state, memoising accordingly. /// Return the state root for the genesis state, memoising accordingly.
pub fn state_root(&self) -> Ref<H256> { pub fn state_root(&self) -> Ref<H256> {
if self.state_root_memo.borrow().is_none() { if self.state_root_memo.borrow().is_none() {
*self.state_root_memo.borrow_mut() = Some(trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect())); *self.state_root_memo.borrow_mut() = Some(sec_trie_root(self.genesis_state.iter().map(|(k, v)| (k.to_vec(), v.rlp())).collect()));
} }
Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap()) Ref::map(self.state_root_memo.borrow(), |x|x.as_ref().unwrap())
} }
/// Compose the genesis block for this chain. fn genesis_header(&self) -> Header {
pub fn genesis_block(&self) -> Bytes { Header {
let empty_list = RlpStream::new_list(0).out();
let empty_list_sha3 = empty_list.sha3();
let header = Header {
parent_hash: self.parent_hash.clone(), parent_hash: self.parent_hash.clone(),
timestamp: self.timestamp.clone(), timestamp: self.timestamp.clone(),
number: U256::from(0u8), number: U256::from(0u8),
author: self.author.clone(), author: self.author.clone(),
transactions_root: SHA3_EMPTY.clone(), transactions_root: SHA3_NULL_RLP.clone(),
uncles_hash: empty_list_sha3.clone(), uncles_hash: RlpStream::new_list(0).out().sha3(),
extra_data: self.extra_data.clone(), extra_data: self.extra_data.clone(),
state_root: self.state_root().clone(), state_root: self.state_root().clone(),
receipts_root: SHA3_EMPTY.clone(), receipts_root: SHA3_NULL_RLP.clone(),
log_bloom: H2048::new().clone(), log_bloom: H2048::new().clone(),
gas_used: self.gas_used.clone(), gas_used: self.gas_used.clone(),
gas_limit: self.gas_limit.clone(), gas_limit: self.gas_limit.clone(),
@ -130,67 +131,90 @@ impl Spec {
let r = Rlp::new(&seal); 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).raw().to_vec()).collect()
}, },
}; }
}
/// Compose the genesis block for this chain.
pub fn genesis_block(&self) -> Bytes {
let empty_list = RlpStream::new_list(0).out();
let header = self.genesis_header();
let mut ret = RlpStream::new_list(3); let mut ret = RlpStream::new_list(3);
ret.append(&header); ret.append(&header);
ret.append_raw(&empty_list, 1); ret.append_raw(&empty_list, 1);
ret.append_raw(&empty_list, 1); ret.append_raw(&empty_list, 1);
ret.out() ret.out()
} }
}
impl Spec {
/// Loads a chain-specification from a json data structure /// Loads a chain-specification from a json data structure
pub fn from_json(json: Json) -> Spec { pub fn from_json(json: Json) -> Spec {
// once we commit ourselves to some json parsing library (serde?) // once we commit ourselves to some json parsing library (serde?)
// move it to proper data structure // move it to proper data structure
let mut state = HashMap::new(); let mut state = HashMap::new();
let accounts = json["alloc"].as_object().expect("Missing genesis state"); let mut builtins = HashMap::new();
if let Some(&Json::Object(ref accounts)) = json.find("accounts") {
for (address, acc) in accounts.iter() { for (address, acc) in accounts.iter() {
let addr = Address::from_str(address).unwrap(); let addr = Address::from_str(address).unwrap();
let o = acc.as_object().unwrap(); if let Some(ref builtin_json) = acc.find("builtin") {
let balance = U256::from_dec_str(o["balance"].as_string().unwrap()).unwrap(); if let Some(builtin) = Builtin::from_json(builtin_json) {
state.insert(addr, Account::new_basic(balance, U256::from(0))); builtins.insert(addr.clone(), builtin);
}
}
let balance = if let Some(&Json::String(ref b)) = acc.find("balance") {U256::from_dec_str(b).unwrap_or(U256::from(0))} else {U256::from(0)};
let nonce = if let Some(&Json::String(ref n)) = acc.find("nonce") {U256::from_dec_str(n).unwrap_or(U256::from(0))} else {U256::from(0)};
// TODO: handle code & data if they exist.
state.insert(addr, Account::new_basic(balance, nonce));
}
} }
let builtins = { let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON");
// TODO: populate from json.
HashMap::new()
};
let (seal_fields, seal_rlp) = { let (seal_fields, seal_rlp) = {
if json.find("mixhash").is_some() && json.find("nonce").is_some() { if genesis.find("mixHash").is_some() && genesis.find("nonce").is_some() {
let mut s = RlpStream::new(); let mut s = RlpStream::new();
s.append(&H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap()); s.append(&H256::from_str(&genesis["mixHash"].as_string().expect("mixHash not a string.")[2..]).expect("Invalid mixHash string value"));
s.append(&H64::from_str(&json["nonce"].as_string().unwrap()[2..]).unwrap()); s.append(&H64::from_str(&genesis["nonce"].as_string().expect("nonce not a string.")[2..]).expect("Invalid nonce string value"));
(2, s.out()) (2, s.out())
} else { } else {
// backup algo that will work with sealFields/sealRlp (and without). // 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()) (
usize::from_str(&genesis["sealFields"].as_string().unwrap_or("0x")[2..]).expect("Invalid sealFields integer data"),
genesis["sealRlp"].as_string().unwrap_or("0x")[2..].from_hex().expect("Invalid sealRlp hex data")
)
} }
}; };
Spec { Spec {
engine_name: json["engineName"].as_string().unwrap().to_string(), engine_name: json["engineName"].as_string().unwrap().to_string(),
engine_params: json_to_rlp_map(&json["params"]), engine_params: json_to_rlp_map(&json["params"]),
builtins: builtins, builtins: builtins,
parent_hash: H256::from_str(&json["parentHash"].as_string().unwrap()[2..]).unwrap(), parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(),
author: Address::from_str(&json["coinbase"].as_string().unwrap()[2..]).unwrap(), author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(),
difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(), difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(),
gas_limit: U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap(), gas_limit: U256::from_str(&genesis["gasLimit"].as_string().unwrap()[2..]).unwrap(),
gas_used: U256::from(0u8), gas_used: U256::from(0u8),
timestamp: U256::from_str(&json["timestamp"].as_string().unwrap()[2..]).unwrap(), timestamp: U256::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(),
extra_data: json["extraData"].as_string().unwrap()[2..].from_hex().unwrap(), extra_data: genesis["extraData"].as_string().unwrap()[2..].from_hex().unwrap(),
genesis_state: state, genesis_state: state,
seal_fields: seal_fields, seal_fields: seal_fields,
seal_rlp: seal_rlp, seal_rlp: seal_rlp,
state_root_memo: RefCell::new(json["stateRoot"].as_string().map(|s| H256::from_str(&s[2..]).unwrap())), state_root_memo: RefCell::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())),
} }
} }
/// 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. /// Creates the Olympic network chain spec.
pub fn olympic() -> Spec { pub fn new_like_olympic() -> Spec {
Spec { Spec {
engine_name: "Ethash".to_string(), engine_name: "Ethash".to_string(),
engine_params: vec![ engine_params: vec![
@ -207,7 +231,7 @@ impl Spec {
acc.insert(vec.0.to_string(), vec.1); acc.insert(vec.0.to_string(), vec.1);
acc acc
}), }),
builtins: HashMap::new(), // TODO: make correct builtins: Self::standard_builtins(),
parent_hash: H256::new(), parent_hash: H256::new(),
author: Address::new(), author: Address::new(),
difficulty: U256::from(131_072u64), difficulty: U256::from(131_072u64),
@ -222,13 +246,13 @@ impl Spec {
acc acc
}), }),
seal_fields: 2, seal_fields: 2,
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x2au64); r.append(&H256::new()); r.out() }, // TODO: make correct 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), state_root_memo: RefCell::new(None),
} }
} }
/// Creates the Frontier network chain spec. /// Creates the Frontier network chain spec, except for the genesis state, which is blank.
pub fn frontier() -> Spec { pub fn new_like_frontier() -> Spec {
Spec { Spec {
engine_name: "Ethash".to_string(), engine_name: "Ethash".to_string(),
engine_params: vec![ engine_params: vec![
@ -245,7 +269,7 @@ impl Spec {
acc.insert(vec.0.to_string(), vec.1); acc.insert(vec.0.to_string(), vec.1);
acc acc
}), }),
builtins: HashMap::new(), // TODO: make correct builtins: Self::standard_builtins(),
parent_hash: H256::new(), parent_hash: H256::new(),
author: Address::new(), author: Address::new(),
difficulty: U256::from(131_072u64), difficulty: U256::from(131_072u64),
@ -260,13 +284,13 @@ impl Spec {
acc acc
}), }),
seal_fields: 2, seal_fields: 2,
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x42u64); r.append(&H256::new()); r.out() }, seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&H256::new()); r.append(&0x42u64); r.out() },
state_root_memo: RefCell::new(None), state_root_memo: RefCell::new(None),
} }
} }
/// Creates the Morden network chain spec. /// Creates the actual Morden network chain spec.
pub fn morden() -> Spec { pub fn new_morden() -> Spec {
Spec { Spec {
engine_name: "Ethash".to_string(), engine_name: "Ethash".to_string(),
engine_params: vec![ engine_params: vec![
@ -283,24 +307,66 @@ impl Spec {
acc.insert(vec.0.to_string(), vec.1); acc.insert(vec.0.to_string(), vec.1);
acc acc
}), }),
builtins: HashMap::new(), // TODO: make correct builtins: Self::standard_builtins(),
parent_hash: H256::new(), parent_hash: H256::new(),
author: Address::new(), author: Address::new(),
difficulty: U256::from(131_072u64), difficulty: U256::from(0x20000u64),
gas_limit: U256::from(0u64), gas_limit: U256::from(0x2fefd8u64),
gas_used: U256::from(0u64), gas_used: U256::from(0u64),
timestamp: U256::from(0u64), timestamp: U256::from(0u64),
extra_data: vec![], extra_data: vec![],
genesis_state: vec![ // TODO: make correct genesis_state: {
(Address::new(), Account::new_basic(U256::from(1) << 200, U256::from(0))) let n = U256::from(1) << 20;
].into_iter().fold(HashMap::new(), | mut acc, vec | { 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.insert(vec.0, vec.1);
acc acc
}), }),
seal_fields: 2, seal_fields: 2,
seal_rlp: { let mut r = RlpStream::new_list(2); r.append(&0x00006d6f7264656eu64); r.append(&H256::from_str("00000000000000000000000000000000000000647572616c65787365646c6578").unwrap()); r.out() }, // TODO: make correct 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), state_root_memo: RefCell::new(None),
} }
} }
} }
#[cfg(test)]
mod tests {
use std::str::FromStr;
use util::hash::*;
use util::sha3::*;
use rustc_serialize::json::Json;
use views::*;
use super::*;
#[test]
fn all() {
let morden = Spec::new_morden();
// let engine = morden.to_engine(); // Ethash doesn't exist as an engine yet, so would fail.
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_res() {
let morden_json = Json::from_str(::std::str::from_utf8(include_bytes!("../res/morden.json")).unwrap()).expect("Json is invalid");
let morden = Spec::from_json(morden_json);
// let engine = morden.to_engine(); // Ethash doesn't exist as an engine yet, so would fail.
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());
}
}