From 957dd36328cb1daf85afa249758d8e9e0ad5df01 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 4 Jan 2016 21:47:45 +0000 Subject: [PATCH 01/19] Make builtins work. --- Cargo.toml | 2 +- src/builtin.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ src/spec.rs | 36 +++++++++++----- 3 files changed, 137 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a74bf589..81c7162b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ authors = ["Ethcore "] [dependencies] log = "0.3" env_logger = "0.3" -ethcore-util = "0.1.0" +ethcore-util = { path = "../ethcore-util" } rustc-serialize = "0.3" flate2 = "0.2" rocksdb = "0.2.1" diff --git a/src/builtin.rs b/src/builtin.rs index 12ed610a6..6a15c48b2 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,4 +1,6 @@ +use std::cmp::min; use util::uint::*; +use rustc_serialize::json::Json; /// Definition of a contract whose implementation is built-in. pub struct Builtin { @@ -8,3 +10,112 @@ pub struct Builtin { /// being placed into the second. pub execute: Box, } + +/* +ETH_REGISTER_PRECOMPILED(ecrecover)(bytesConstRef _in, bytesRef _out) +{ + struct inType + { + h256 hash; + h256 v; + h256 r; + h256 s; + } in; + + memcpy(&in, _in.data(), min(_in.size(), sizeof(in))); + + h256 ret; + u256 v = (u256)in.v; + if (v >= 27 && v <= 28) + { + SignatureStruct sig(in.r, in.s, (byte)((int)v - 27)); + if (sig.isValid()) + { + try + { + if (Public rec = recover(sig, in.hash)) + { + ret = dev::sha3(rec); + memset(ret.data(), 0, 12); + ret.ref().copyTo(_out); + } + } + catch (...) {} + } + } +} + +ETH_REGISTER_PRECOMPILED(sha256)(bytesConstRef _in, bytesRef _out) +{ + dev::sha256(_in).ref().copyTo(_out); +} + +ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out) +{ + h256(dev::ripemd160(_in), h256::AlignRight).ref().copyTo(_out); +} +*/ + +impl Builtin { + /// Create a new object from components. + pub fn new(cost: Box U256>, execute: Box) -> 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 { + 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 { + // 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 + } +} + +// TODO: turn in to a factory with dynamic registration. +pub fn new_builtin_exec(name: &str) -> Option> { + 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]| { + unimplemented!(); + })), + "sha256" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { + unimplemented!(); + })), + "ripemd160" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { + unimplemented!(); + })), + _ => None + } +} + +#[test] +fn identity() { + let f = new_builtin_exec("identity").unwrap(); + let i = [0u8, 1, 2, 3]; + for osize in [2, 4, 8].iter() { + let mut o = vec![255u8; *osize]; + f(&i[..], &mut o[..]); + assert_eq!(i[..], o[..]); + } +} \ No newline at end of file diff --git a/src/spec.rs b/src/spec.rs index b79624787..8fd42faec 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -20,6 +20,8 @@ use null_engine::NullEngine; use denominations::*; use header::*; +// TODO: need a JSON->cost closure converter. + /// 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, @@ -139,26 +141,29 @@ impl Spec { } } - impl Spec { /// 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 let mut state = HashMap::new(); + let mut builtins = 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))); + if let Json::Object(_) = o["precompiled"] { + if let Some(b) = Builtin::from_json(&o["precompiled"]) { + builtins.insert(addr.clone(), b); + } + } + let balance = U256::from_dec_str(o["balance"].as_string().unwrap_or("0")).unwrap(); + let nonce = U256::from_dec_str(o["nonce"].as_string().unwrap_or("0")).unwrap(); + // TODO: handle code & data is they exist. + state.insert(addr, Account::new_basic(balance, nonce)); } - 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(); @@ -189,6 +194,15 @@ impl Spec { } } + fn standard_builtins() -> HashMap { + 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 olympic() -> Spec { Spec { @@ -207,7 +221,7 @@ impl Spec { acc.insert(vec.0.to_string(), vec.1); acc }), - builtins: HashMap::new(), // TODO: make correct + builtins: Self::standard_builtins(), parent_hash: H256::new(), author: Address::new(), difficulty: U256::from(131_072u64), @@ -245,7 +259,7 @@ impl Spec { acc.insert(vec.0.to_string(), vec.1); acc }), - builtins: HashMap::new(), // TODO: make correct + builtins: Self::standard_builtins(), parent_hash: H256::new(), author: Address::new(), difficulty: U256::from(131_072u64), @@ -283,7 +297,7 @@ impl Spec { acc.insert(vec.0.to_string(), vec.1); acc }), - builtins: HashMap::new(), // TODO: make correct + builtins: Self::standard_builtins(), parent_hash: H256::new(), author: Address::new(), difficulty: U256::from(131_072u64), From e369042ff756366707193ebf67bb58425d622afb Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 4 Jan 2016 21:53:36 +0000 Subject: [PATCH 02/19] Correct naming and fix for Morden. --- src/spec.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/spec.rs b/src/spec.rs index 8fd42faec..7386e55e9 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -194,6 +194,7 @@ impl Spec { } } + /// Returns the builtins map for the standard network of Ethereum Olympic, Frontier and Homestead. fn standard_builtins() -> HashMap { let mut ret = HashMap::new(); ret.insert(Address::from_str("0000000000000000000000000000000000000001").unwrap(), Builtin::from_named_linear("ecrecover", 3000, 0).unwrap()); @@ -204,7 +205,7 @@ impl Spec { } /// Creates the Olympic network chain spec. - pub fn olympic() -> Spec { + pub fn new_like_olympic() -> Spec { Spec { engine_name: "Ethash".to_string(), engine_params: vec![ @@ -241,8 +242,8 @@ impl Spec { } } - /// Creates the Frontier network chain spec. - pub fn frontier() -> Spec { + /// 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![ @@ -280,7 +281,7 @@ impl Spec { } /// Creates the Morden network chain spec. - pub fn morden() -> Spec { + pub fn new_morden() -> Spec { Spec { engine_name: "Ethash".to_string(), engine_params: vec![ @@ -305,8 +306,8 @@ impl Spec { 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))) + genesis_state: vec![ + (Address::from_str("102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c").unwrap(), 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 From dabb90bf4073cb7f343e4b27a1bc9be33035880f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 5 Jan 2016 19:11:41 +0100 Subject: [PATCH 03/19] Fix tests for builtin. --- src/builtin.rs | 82 +++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/src/builtin.rs b/src/builtin.rs index 6a15c48b2..48558c608 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -11,6 +11,38 @@ pub struct Builtin { pub execute: Box, } +impl Builtin { + /// Create a new object from components. + pub fn new(cost: Box U256>, execute: Box) -> 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 { + 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 { + // 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 + } +} + /* ETH_REGISTER_PRECOMPILED(ecrecover)(bytesConstRef _in, bytesRef _out) { @@ -56,38 +88,6 @@ ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out) } */ -impl Builtin { - /// Create a new object from components. - pub fn new(cost: Box U256>, execute: Box) -> 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 { - 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 { - // 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 - } -} - // TODO: turn in to a factory with dynamic registration. pub fn new_builtin_exec(name: &str) -> Option> { match name { @@ -113,9 +113,17 @@ pub fn new_builtin_exec(name: &str) -> Option> { fn identity() { let f = new_builtin_exec("identity").unwrap(); let i = [0u8, 1, 2, 3]; - for osize in [2, 4, 8].iter() { - let mut o = vec![255u8; *osize]; - f(&i[..], &mut o[..]); - assert_eq!(i[..], o[..]); - } + + 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..]); } \ No newline at end of file From 6585910c2a5afb89adc850133ae0dff57377d4f4 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 5 Jan 2016 19:12:19 +0100 Subject: [PATCH 04/19] Whitespace fixes. --- src/spec.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/spec.rs b/src/spec.rs index 7386e55e9..872c41541 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -20,8 +20,6 @@ use null_engine::NullEngine; use denominations::*; use header::*; -// TODO: need a JSON->cost closure converter. - /// 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, @@ -53,9 +51,9 @@ pub fn json_to_rlp(json: &Json) -> Bytes { /// Convert JSON to a string->RLP map. 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 - }) + acc.insert(kv.0.clone(), kv.1); + acc + }) } /// Parameters for a block chain; includes both those intrinsic to the design of the From e98452b0c7deeffcca51b7ddccd17caa59b47902 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Tue, 5 Jan 2016 19:23:40 +0100 Subject: [PATCH 05/19] Tests for Builtin. --- src/builtin.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/builtin.rs b/src/builtin.rs index 48558c608..2b7d4199c 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -20,7 +20,9 @@ impl Builtin { /// 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 { 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)}); + 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) }) } @@ -126,4 +128,34 @@ fn identity() { f(&i[..], &mut o8[..]); assert_eq!(i, o8[..4]); assert_eq!([255u8; 4], o8[4..]); +} + +#[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); } \ No newline at end of file From db23ee765a112042c399ad2a87155cec1750a56e Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 6 Jan 2016 15:57:17 +0100 Subject: [PATCH 06/19] Fixes and a test! Now correctly builds Morden's genesis block. --- src/header.rs | 23 +++++++++++---- src/spec.rs | 77 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 73 insertions(+), 27 deletions(-) diff --git a/src/header.rs b/src/header.rs index 28e568e8e..6f9cd8f14 100644 --- a/src/header.rs +++ b/src/header.rs @@ -3,12 +3,22 @@ use util::bytes::*; use util::uint::*; use util::rlp::*; -pub static ZERO_ADDRESS: Address = Address([0x00; 20]); -pub static ZERO_H256: H256 = H256([0x00; 32]); -pub static ZERO_LOGBLOOM: LogBloom = H2048([0x00; 256]); - +/// Type for a 2048-bit log-bloom, as used by our blocks. 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)] pub struct Header { pub parent_hash: H256, @@ -31,6 +41,7 @@ pub struct Header { } impl Header { + /// Create a new, default-valued, header. pub fn new() -> Header { Header { parent_hash: ZERO_H256.clone(), @@ -76,7 +87,7 @@ impl Decodable for Header { }; 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) @@ -101,7 +112,7 @@ impl Encodable for Header { self.extra_data.encode(e); for b in self.seal.iter() { - b.encode(e); + e.emit_raw(&b); } }) } diff --git a/src/spec.rs b/src/spec.rs index 872c41541..b0cffc901 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -34,7 +34,7 @@ pub fn base_to_json(source: &[u8]) -> Json { /// Convert JSON value to equivlaent RLP representation. // TODO: handle container types. -pub fn json_to_rlp(json: &Json) -> Bytes { +fn json_to_rlp(json: &Json) -> Bytes { match json { &Json::I64(o) => encode(&(o as u64)), &Json::U64(o) => encode(&o), @@ -49,7 +49,7 @@ pub fn json_to_rlp(json: &Json) -> Bytes { } /// Convert JSON to a string->RLP map. -pub fn json_to_rlp_map(json: &Json) -> HashMap { +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 @@ -98,25 +98,22 @@ impl Spec { /// Return the state root for the genesis state, memoising accordingly. pub fn state_root(&self) -> Ref { 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()) } - /// Compose the genesis block for this chain. - pub fn genesis_block(&self) -> Bytes { - let empty_list = RlpStream::new_list(0).out(); - let empty_list_sha3 = empty_list.sha3(); - let header = Header { + fn genesis_header(&self) -> 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(), + transactions_root: SHA3_NULL_RLP.clone(), + uncles_hash: RlpStream::new_list(0).out().sha3(), extra_data: self.extra_data.clone(), state_root: self.state_root().clone(), - receipts_root: SHA3_EMPTY.clone(), + receipts_root: SHA3_NULL_RLP.clone(), log_bloom: H2048::new().clone(), gas_used: self.gas_used.clone(), gas_limit: self.gas_limit.clone(), @@ -130,7 +127,13 @@ impl Spec { let r = Rlp::new(&seal); (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); ret.append(&header); ret.append_raw(&empty_list, 1); @@ -235,7 +238,7 @@ impl Spec { acc }), 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), } } @@ -273,12 +276,12 @@ impl Spec { acc }), 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), } } - /// Creates the Morden network chain spec. + /// Creates the actual Morden network chain spec. pub fn new_morden() -> Spec { Spec { engine_name: "Ethash".to_string(), @@ -299,21 +302,53 @@ impl Spec { builtins: Self::standard_builtins(), parent_hash: H256::new(), author: Address::new(), - difficulty: U256::from(131_072u64), - gas_limit: U256::from(0u64), + difficulty: U256::from(0x20000u64), + gas_limit: U256::from(0x2fefd8u64), gas_used: U256::from(0u64), timestamp: U256::from(0u64), extra_data: vec![], - genesis_state: vec![ - (Address::from_str("102e61f5d8f9bc71d0ad4a084df4e65e05ce0e1c").unwrap(), Account::new_basic(U256::from(1) << 200, U256::from(0))) - ].into_iter().fold(HashMap::new(), | mut acc, 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_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), } } } +mod tests { + use std::str::FromStr; + use util::uint::*; + use util::hash::*; + use util::bytes::*; + use util::trie::*; + use util::sha3::*; + use views::*; + use util::memorydb::*; + 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()); + } +} \ No newline at end of file From f9b3e269344ecd07b2d20fe197948ebb3fb251e7 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 6 Jan 2016 16:00:42 +0100 Subject: [PATCH 07/19] Fix warnings. --- src/spec.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/spec.rs b/src/spec.rs index b0cffc901..bdee6e62f 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -331,15 +331,12 @@ impl Spec { } } +#[cfg(test)] mod tests { use std::str::FromStr; - use util::uint::*; use util::hash::*; - use util::bytes::*; - use util::trie::*; use util::sha3::*; use views::*; - use util::memorydb::*; use super::*; #[test] From 810bff1ea9ac20419e45a8b5433c9bdf6f48e68d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jan 2016 19:10:29 +0100 Subject: [PATCH 08/19] Morden genesis tested and working. --- res/morden.json | 34 +++++++++++++++++++++ src/blockchain.rs | 2 +- src/builtin.rs | 27 +++++++++++++++++ src/genesis.rs | 2 +- src/header.rs | 22 ++++++++++++++ src/spec.rs | 77 +++++++++++++++++++++++++++++------------------ 6 files changed, 132 insertions(+), 32 deletions(-) create mode 100644 res/morden.json diff --git a/res/morden.json b/res/morden.json new file mode 100644 index 000000000..033c0651c --- /dev/null +++ b/res/morden.json @@ -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" } + } +} \ No newline at end of file diff --git a/src/blockchain.rs b/src/blockchain.rs index df729787a..6bb8c2674 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -99,7 +99,7 @@ impl BlockChain { /// /// let bc = BlockChain::new(genesis.block(), &dir); /// - /// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; + /// let genesis_hash = "347db3ae87cf4703f948676de5858af1a2a336cbe2e6e56c5041dd80bed3071f"; /// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap()); /// assert!(bc.is_known(&bc.genesis_hash())); /// assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap()); diff --git a/src/builtin.rs b/src/builtin.rs index 2b7d4199c..cabc076b4 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,6 +1,8 @@ use std::cmp::min; +use std::fmt; use util::uint::*; use rustc_serialize::json::Json; +//use crypto::recover; /// Definition of a contract whose implementation is built-in. pub struct Builtin { @@ -11,6 +13,12 @@ pub struct Builtin { pub execute: Box, } +impl fmt::Debug for Builtin { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "") + } +} + impl Builtin { /// Create a new object from components. pub fn new(cost: Box U256>, execute: Box) -> Builtin { @@ -99,6 +107,25 @@ pub fn new_builtin_exec(name: &str) -> Option> { } })), "ecrecover" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { +/* #[repr(packed)] + struct InType { + hash: H256, + v: H256, + r: H256, + s: H256, + } + let it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; + unsafe { + transmute() + } + let hash = H256::from_slice(input[0..32]); + let v = H256::from_slice(input[32..64]); + let r = H256::from_slice(input[64..96]); + let s = H256::from_slice(input[96..128]); + if v == U256::from(27).hash() || v == U256::from(28).hash() { + v[31] + } + recover()*/ unimplemented!(); })), "sha256" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { diff --git a/src/genesis.rs b/src/genesis.rs index 5b8bed1f8..93527e417 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -118,6 +118,6 @@ fn test_genesis() { let g = Genesis::new_frontier(); let view = BlockView::new(&g.block).header_view(); - let genesis_hash = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); + let genesis_hash = H256::from_str("347db3ae87cf4703f948676de5858af1a2a336cbe2e6e56c5041dd80bed3071f").unwrap(); assert_eq!(view.sha3(), genesis_hash); } diff --git a/src/header.rs b/src/header.rs index 6f9cd8f14..ab84d5c58 100644 --- a/src/header.rs +++ b/src/header.rs @@ -117,6 +117,28 @@ impl Encodable for Header { }) } } +/* +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(&mut self, o: &O) where O: RlpStandard { + o.append(self); + } +} +*/ #[cfg(test)] mod tests { diff --git a/src/spec.rs b/src/spec.rs index bdee6e62f..59b7f7789 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -21,7 +21,7 @@ use denominations::*; use header::*; /// 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, // 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!"); @@ -36,9 +36,10 @@ pub fn base_to_json(source: &[u8]) -> Json { // TODO: handle container types. fn json_to_rlp(json: &Json) -> Bytes { match json { + &Json::Boolean(o) => encode(&(if o {1u64} else {0})), &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() => { + &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()) }, &Json::String(ref s) => { @@ -58,6 +59,7 @@ fn json_to_rlp_map(json: &Json) -> HashMap { /// 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. +#[derive(Debug)] pub struct Spec { // What engine are we using for this? pub engine_name: String, @@ -140,9 +142,7 @@ impl Spec { ret.append_raw(&empty_list, 1); ret.out() } -} -impl Spec { /// 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?) @@ -150,48 +150,54 @@ impl Spec { let mut state = HashMap::new(); let mut builtins = 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(); - if let Json::Object(_) = o["precompiled"] { - if let Some(b) = Builtin::from_json(&o["precompiled"]) { - builtins.insert(addr.clone(), b); + if let Some(&Json::Object(ref accounts)) = json.find("accounts") { + for (address, acc) in accounts.iter() { + let addr = Address::from_str(address).unwrap(); + if let Some(ref builtin_json) = acc.find("builtin") { + if let Some(builtin) = Builtin::from_json(builtin_json) { + 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 balance = U256::from_dec_str(o["balance"].as_string().unwrap_or("0")).unwrap(); - let nonce = U256::from_dec_str(o["nonce"].as_string().unwrap_or("0")).unwrap(); - // TODO: handle code & data is they exist. - state.insert(addr, Account::new_basic(balance, nonce)); } + let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON"); + 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(); - s.append(&H256::from_str(&json["mixhash"].as_string().unwrap()[2..]).unwrap()); - s.append(&H64::from_str(&json["nonce"].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(&genesis["nonce"].as_string().expect("nonce not a string.")[2..]).expect("Invalid nonce string value")); (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()) + ( + 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 { engine_name: json["engineName"].as_string().unwrap().to_string(), 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(), - difficulty: U256::from_str(&json["difficulty"].as_string().unwrap()[2..]).unwrap(), - gas_limit: U256::from_str(&json["gasLimit"].as_string().unwrap()[2..]).unwrap(), + parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(), + author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(), + difficulty: U256::from_str(&genesis["difficulty"].as_string().unwrap()[2..]).unwrap(), + gas_limit: U256::from_str(&genesis["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(), + timestamp: U256::from_str(&genesis["timestamp"].as_string().unwrap()[2..]).unwrap(), + extra_data: genesis["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())), + state_root_memo: RefCell::new(genesis.find("stateRoot").and_then(|_| genesis["stateRoot"].as_string()).map(|s| H256::from_str(&s[2..]).unwrap())), } } @@ -315,10 +321,10 @@ impl Spec { (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 - }), + ]}.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(); @@ -336,6 +342,7 @@ mod tests { use std::str::FromStr; use util::hash::*; use util::sha3::*; + use rustc_serialize::json::Json; use views::*; use super::*; @@ -348,4 +355,14 @@ mod tests { 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); + + 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()); + } } \ No newline at end of file From 14d77d522b66611d4f485c9bf11bb34c15fcdb58 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jan 2016 19:25:51 +0100 Subject: [PATCH 09/19] Minor whitespacing. --- src/spec.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/spec.rs b/src/spec.rs index 59b7f7789..82a8cbc3f 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -349,7 +349,7 @@ mod tests { #[test] fn all() { let morden = Spec::new_morden(); - // let engine = morden.to_engine(); // Ethash doesn't exist as an engine yet, so would fail. +// 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(); @@ -361,6 +361,8 @@ mod tests { 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()); From 3cb3c2a2bd65848c9f47b42a014c7ff017b19e19 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jan 2016 19:42:59 +0100 Subject: [PATCH 10/19] Fix for Genesis. --- src/blockchain.rs | 2 +- src/genesis.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/blockchain.rs b/src/blockchain.rs index 6bb8c2674..df729787a 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -99,7 +99,7 @@ impl BlockChain { /// /// let bc = BlockChain::new(genesis.block(), &dir); /// - /// let genesis_hash = "347db3ae87cf4703f948676de5858af1a2a336cbe2e6e56c5041dd80bed3071f"; + /// let genesis_hash = "d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"; /// assert_eq!(bc.genesis_hash(), H256::from_str(genesis_hash).unwrap()); /// assert!(bc.is_known(&bc.genesis_hash())); /// assert_eq!(bc.genesis_hash(), bc.block_hash(&U256::from(0u8)).unwrap()); diff --git a/src/genesis.rs b/src/genesis.rs index 93527e417..63a0caf81 100644 --- a/src/genesis.rs +++ b/src/genesis.rs @@ -89,7 +89,7 @@ impl Genesis { // 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![mixhash.to_vec(), nonce.to_vec()] + vec![encode(&mixhash), encode(&nonce)] } }; @@ -118,6 +118,6 @@ fn test_genesis() { let g = Genesis::new_frontier(); let view = BlockView::new(&g.block).header_view(); - let genesis_hash = H256::from_str("347db3ae87cf4703f948676de5858af1a2a336cbe2e6e56c5041dd80bed3071f").unwrap(); + let genesis_hash = H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap(); assert_eq!(view.sha3(), genesis_hash); } From 183682ffbc6266870bee9f4452f10ec4a096dcb5 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 7 Jan 2016 23:55:14 +0100 Subject: [PATCH 11/19] ecrecover builtin, tests and docs. --- src/builtin.rs | 114 ++++++++++++++++++++++++++++++++++++++++++------- src/engine.rs | 2 +- 2 files changed, 100 insertions(+), 16 deletions(-) diff --git a/src/builtin.rs b/src/builtin.rs index cabc076b4..c3c3b25e4 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,8 +1,14 @@ use std::cmp::min; use std::fmt; +use std::mem; +use std::slice; use util::uint::*; +use util::hash::*; +use util::sha3::*; +use util::bytes::*; use rustc_serialize::json::Json; -//use crypto::recover; +use std::io::Write; +use util::crypto::*; /// Definition of a contract whose implementation is built-in. pub struct Builtin { @@ -98,7 +104,22 @@ ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out) } */ -// TODO: turn in to a factory with dynamic registration. +/// Simple trait to allow for raw population of a Sized object from a byte slice. +trait Populatable { + /// Populate self from byte slice `d` in a raw fashion. + fn populate_raw(&mut self, d: &[u8]); +} + +impl Populatable for T where T: Sized { + fn populate_raw(&mut self, d: &[u8]) { + unsafe { + slice::from_raw_parts_mut(self as *mut T as *mut u8, mem::size_of::()) + }.write(&d).unwrap(); + } +} + +/// 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> { match name { "identity" => Some(Box::new(move|input: &[u8], output: &mut[u8]| { @@ -107,26 +128,31 @@ pub fn new_builtin_exec(name: &str) -> Option> { } })), "ecrecover" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { -/* #[repr(packed)] + #[repr(packed)] + #[derive(Debug)] struct InType { hash: H256, v: H256, r: H256, s: H256, } - let it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; - unsafe { - transmute() + let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; + it.populate_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]}; + } + } + _ => {} + }; + } } - let hash = H256::from_slice(input[0..32]); - let v = H256::from_slice(input[32..64]); - let r = H256::from_slice(input[64..96]); - let s = H256::from_slice(input[96..128]); - if v == U256::from(27).hash() || v == U256::from(28).hash() { - v[31] - } - recover()*/ - unimplemented!(); })), "sha256" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { unimplemented!(); @@ -157,6 +183,64 @@ fn identity() { assert_eq!([255u8; 4], o8[4..]); } +#[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(); diff --git a/src/engine.rs b/src/engine.rs index d920f4bef..9632ba090 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -57,7 +57,7 @@ pub trait Engine { // 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 + // 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. /* fn is_builtin(&self, a: Address) -> bool; fn cost_of_builtin(&self, a: Address, in: &[u8]) -> bignum; From 8f4637ecbc36cff0a2d854c7d69f0df59bffe64d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 00:00:07 +0100 Subject: [PATCH 12/19] API sync (moved Populatable to util). --- src/builtin.rs | 49 ------------------------------------------------- 1 file changed, 49 deletions(-) diff --git a/src/builtin.rs b/src/builtin.rs index c3c3b25e4..5cf06c8a7 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,7 +1,5 @@ use std::cmp::min; use std::fmt; -use std::mem; -use std::slice; use util::uint::*; use util::hash::*; use util::sha3::*; @@ -60,39 +58,6 @@ impl Builtin { } /* -ETH_REGISTER_PRECOMPILED(ecrecover)(bytesConstRef _in, bytesRef _out) -{ - struct inType - { - h256 hash; - h256 v; - h256 r; - h256 s; - } in; - - memcpy(&in, _in.data(), min(_in.size(), sizeof(in))); - - h256 ret; - u256 v = (u256)in.v; - if (v >= 27 && v <= 28) - { - SignatureStruct sig(in.r, in.s, (byte)((int)v - 27)); - if (sig.isValid()) - { - try - { - if (Public rec = recover(sig, in.hash)) - { - ret = dev::sha3(rec); - memset(ret.data(), 0, 12); - ret.ref().copyTo(_out); - } - } - catch (...) {} - } - } -} - ETH_REGISTER_PRECOMPILED(sha256)(bytesConstRef _in, bytesRef _out) { dev::sha256(_in).ref().copyTo(_out); @@ -104,20 +69,6 @@ ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out) } */ -/// Simple trait to allow for raw population of a Sized object from a byte slice. -trait Populatable { - /// Populate self from byte slice `d` in a raw fashion. - fn populate_raw(&mut self, d: &[u8]); -} - -impl Populatable for T where T: Sized { - fn populate_raw(&mut self, d: &[u8]) { - unsafe { - slice::from_raw_parts_mut(self as *mut T as *mut u8, mem::size_of::()) - }.write(&d).unwrap(); - } -} - /// 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> { From d94fcbd8395eddc33fd0b09c771becc24ca1f44f Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 00:26:52 +0100 Subject: [PATCH 13/19] sha256 builtin + tests. --- Cargo.toml | 1 + src/builtin.rs | 59 +++++++++++++++++++++++++++++++++++--------------- src/lib.rs | 1 + 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 81c7162b0..64b49663f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ rustc-serialize = "0.3" flate2 = "0.2" rocksdb = "0.2.1" heapsize = "0.2.0" +rust-crypto = "0.2.34" evmjit = { path = "rust-evmjit", optional = true } diff --git a/src/builtin.rs b/src/builtin.rs index 5cf06c8a7..fd3e30f19 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -7,6 +7,8 @@ use util::bytes::*; use rustc_serialize::json::Json; use std::io::Write; use util::crypto::*; +use crypto::sha2::Sha256; +use crypto::digest::Digest; /// Definition of a contract whose implementation is built-in. pub struct Builtin { @@ -57,18 +59,13 @@ impl Builtin { } } -/* -ETH_REGISTER_PRECOMPILED(sha256)(bytesConstRef _in, bytesRef _out) -{ - dev::sha256(_in).ref().copyTo(_out); +pub fn copy_to(src: &[u8], dest: &mut[u8]) { + // NICE: optimise + for i in 0..min(src.len(), dest.len()) { + dest[i] = src[i]; + } } -ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out) -{ - h256(dev::ripemd160(_in), h256::AlignRight).ref().copyTo(_out); -} -*/ - /// 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> { @@ -78,7 +75,7 @@ pub fn new_builtin_exec(name: &str) -> Option> { output[i] = input[i]; } })), - "ecrecover" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { + "ecrecover" => Some(Box::new(move|input: &[u8], output: &mut[u8]| { #[repr(packed)] #[derive(Debug)] struct InType { @@ -88,7 +85,7 @@ pub fn new_builtin_exec(name: &str) -> Option> { s: H256, } let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; - it.populate_raw(_input); + it.populate_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) { @@ -96,8 +93,8 @@ pub fn new_builtin_exec(name: &str) -> Option> { 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]}; + for i in 0..min(32, output.len()) { + output[i] = if i < 12 {0} else {r[i]}; } } _ => {} @@ -105,11 +102,20 @@ pub fn new_builtin_exec(name: &str) -> Option> { } } })), - "sha256" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { - unimplemented!(); + "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); + } + // dev::sha256(_in).ref().copyTo(_out); })), "ripemd160" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { - unimplemented!(); + // h256(dev::ripemd160(_in), h256::AlignRight).ref().copyTo(_out); })), _ => None } @@ -134,6 +140,25 @@ fn identity() { 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 ecrecover() { use rustc_serialize::hex::FromHex; diff --git a/src/lib.rs b/src/lib.rs index 41cdc9515..e0f80fb70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,6 +77,7 @@ extern crate rustc_serialize; extern crate flate2; extern crate rocksdb; extern crate heapsize; +extern crate crypto; extern crate env_logger; #[cfg(feature = "jit" )] From 7e873a8bbec9d71adb84ae11a1e8075fd7769272 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 00:31:21 +0100 Subject: [PATCH 14/19] ripemd160 builtin and tests. --- src/builtin.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/builtin.rs b/src/builtin.rs index fd3e30f19..caf7c9963 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -8,6 +8,7 @@ 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. @@ -112,10 +113,13 @@ pub fn new_builtin_exec(name: &str) -> Option> { sha.result(ret.as_slice_mut()); copy_to(&ret, output); } - // dev::sha256(_in).ref().copyTo(_out); })), - "ripemd160" => Some(Box::new(move|_input: &[u8], _output: &mut[u8]| { - // h256(dev::ripemd160(_in), h256::AlignRight).ref().copyTo(_out); + "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 } @@ -159,6 +163,25 @@ fn sha256() { 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; From e6439dde99f0f79262758d28eb4b27d4b51c24cf Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 11:03:58 +0100 Subject: [PATCH 15/19] Minor API update. --- src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtin.rs b/src/builtin.rs index caf7c9963..f299f0b81 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -86,7 +86,7 @@ pub fn new_builtin_exec(name: &str) -> Option> { s: H256, } let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; - it.populate_raw(input); + it.copy_from_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) { From 6837899eba9f1a21390057f45e170e5cca0178e3 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 11:53:07 +0100 Subject: [PATCH 16/19] Fixups for new bytes.rs API. --- src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtin.rs b/src/builtin.rs index f299f0b81..33c19dfb7 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -86,7 +86,7 @@ pub fn new_builtin_exec(name: &str) -> Option> { s: H256, } let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; - it.copy_from_raw(input); + it.fax_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) { From 648207d8e889877e680aee53ef4e9cb058aa81dc Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 12:05:58 +0100 Subject: [PATCH 17/19] Rename fax->copy. --- src/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtin.rs b/src/builtin.rs index 33c19dfb7..b6eb2591b 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -86,7 +86,7 @@ pub fn new_builtin_exec(name: &str) -> Option> { s: H256, } let mut it: InType = InType { hash: H256::new(), v: H256::new(), r: H256::new(), s: H256::new() }; - it.fax_raw(input); + 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) { From 3cbaf64c516cb89b7ac89b881c860b2f576ee150 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 12:15:59 +0100 Subject: [PATCH 18/19] Beginnings of Ethash engine. --- src/ethash.rs | 22 ++++++++++++++++++++++ src/spec.rs | 1 + 2 files changed, 23 insertions(+) create mode 100644 src/ethash.rs diff --git a/src/ethash.rs b/src/ethash.rs new file mode 100644 index 000000000..26fdc7cc9 --- /dev/null +++ b/src/ethash.rs @@ -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 { + Box::new(NullEngine{spec: spec}) + } +} + +impl Engine for NullEngine { + fn name(&self) -> &str { "Ethash" } + fn spec(&self) -> &Spec { &self.spec } + fn evm_schedule(&self, _env_info: &EnvInfo) -> EvmSchedule { EvmSchedule::new_frontier() } +} diff --git a/src/spec.rs b/src/spec.rs index 82a8cbc3f..0550fd409 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -93,6 +93,7 @@ impl Spec { pub fn to_engine(self) -> Result, EthcoreError> { match self.engine_name.as_ref() { "NullEngine" => Ok(NullEngine::new_boxed(self)), + "Ethash" => Ok(Ethash::new_boxed(self)), _ => Err(EthcoreError::UnknownName) } } From 1c716407775cc07b870b3f2b93916d8905aae35c Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Fri, 8 Jan 2016 12:27:00 +0100 Subject: [PATCH 19/19] Build fixes for Ethash. --- src/ethash.rs | 4 ++-- src/lib.rs | 1 + src/spec.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ethash.rs b/src/ethash.rs index 26fdc7cc9..27d39bdaa 100644 --- a/src/ethash.rs +++ b/src/ethash.rs @@ -11,11 +11,11 @@ pub struct Ethash { impl Ethash { pub fn new_boxed(spec: Spec) -> Box { - Box::new(NullEngine{spec: spec}) + Box::new(Ethash{spec: spec}) } } -impl Engine for NullEngine { +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() } diff --git a/src/lib.rs b/src/lib.rs index e0f80fb70..f426a4b87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,3 +105,4 @@ pub mod genesis; pub mod views; pub mod blockchain; pub mod extras; +pub mod ethash; diff --git a/src/spec.rs b/src/spec.rs index 0550fd409..d7455b072 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -17,6 +17,7 @@ use account::*; use engine::Engine; use builtin::Builtin; use null_engine::NullEngine; +use ethash::Ethash; use denominations::*; use header::*;