Merge branch 'master' of github.com:gavofyork/ethcore into network

This commit is contained in:
arkpar 2016-01-09 10:26:31 +01:00
commit 86dfa1b425
14 changed files with 27436 additions and 86 deletions

View File

@ -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 }

26713
res/frontier.json Normal file

File diff suppressed because it is too large Load Diff

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" }
}
}

160
src/block.rs Normal file
View File

@ -0,0 +1,160 @@
use std::collections::hash_set::*;
use util::hash::*;
use util::bytes::*;
use util::uint::*;
use util::error::*;
use util::overlaydb::*;
use transaction::*;
use receipt::*;
use blockchain::*;
use engine::*;
use header::*;
use env_info::*;
use state::*;
/// A transaction/receipt execution entry.
pub struct Entry {
transaction: Transaction,
receipt: Receipt,
}
/// Internal type for a block's common elements.
pub struct Block {
header: Header,
/// State is the most final state in the block.
state: State,
archive: Vec<Entry>,
archive_set: HashSet<H256>,
}
impl Block {
fn new(state: State) -> Block {
Block {
header: Header::new(),
state: state,
archive: Vec::new(),
archive_set: HashSet::new(),
}
}
pub fn state_mut(&mut self) -> &mut State { &mut self.state }
}
/// Trait for a object that is_a `Block`.
pub trait IsBlock {
/// Get the block associated with this object.
fn block(&self) -> &Block;
/// Get the header associated with this object's block.
fn header(&self) -> &Header { &self.block().header }
/// Get the final state associated with this object's block.
fn state(&self) -> &State { &self.block().state }
/// Get all information on transactions in this block.
fn archive(&self) -> &Vec<Entry> { &self.block().archive }
}
impl IsBlock for Block {
fn block(&self) -> &Block { self }
}
/// Block that is ready for transactions to be added.
///
/// It's a bit like a Vec<Transaction>, eccept that whenever a transaction is pushed, we execute it and
/// maintain the system `state()`. We also archive execution receipts in preparation for later block creation.
pub struct OpenBlock<'engine> {
block: Block,
engine: &'engine Engine,
last_hashes: LastHashes,
}
/// Just like OpenBlock, except that we've applied `Engine::on_close_block`, finished up the non-seal header fields,
/// and collected the uncles.
///
/// There is no function available to push a transaction. If you want that you'll need to `reopen()` it.
pub struct ClosedBlock<'engine> {
open_block: OpenBlock<'engine>,
_uncles: Vec<Header>,
}
/// A block that has a valid seal.
///
/// The block's header has valid seal arguments. The block cannot be reversed into a ClosedBlock or OpenBlock.
pub struct SealedBlock {
block: Block,
_bytes: Bytes,
}
impl<'engine> OpenBlock<'engine> {
/// Create a new OpenBlock ready for transaction pushing.
pub fn new<'a>(engine: &'a Engine, db: OverlayDB, parent: &Header, last_hashes: LastHashes) -> OpenBlock<'a> {
let mut r = OpenBlock {
block: Block::new(State::from_existing(db, parent.state_root.clone(), engine.account_start_nonce())),
engine: engine,
last_hashes: last_hashes,
};
engine.populate_from_parent(&mut r.block.header, parent);
engine.on_new_block(&mut r.block);
r
}
/// Get the environment info concerning this block.
pub fn env_info(&self) -> EnvInfo {
// TODO: memoise.
EnvInfo {
number: self.block.header.number.clone(),
author: self.block.header.author.clone(),
timestamp: self.block.header.timestamp.clone(),
difficulty: self.block.header.difficulty.clone(),
last_hashes: self.last_hashes.clone(),
gas_used: if let Some(ref t) = self.block.archive.last() {t.receipt.gas_used} else {U256::from(0)},
gas_limit: self.block.header.gas_limit.clone(),
}
}
/// Push a transaction into the block. It will be executed, and archived together with the receipt.
pub fn push_transaction(&mut self, t: Transaction, h: Option<H256>) -> Result<&Receipt, EthcoreError> {
let env_info = self.env_info();
match self.block.state.apply(&env_info, self.engine, &t, true) {
Ok(x) => {
self.block.archive_set.insert(h.unwrap_or_else(||t.sha3()));
self.block.archive.push(Entry { transaction: t, receipt: x.receipt });
Ok(&self.block.archive.last().unwrap().receipt)
}
Err(x) => Err(x)
}
}
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
pub fn close(self, _bc: &BlockChain) -> ClosedBlock { unimplemented!(); }
}
impl<'engine> IsBlock for OpenBlock<'engine> {
fn block(&self) -> &Block { &self.block }
}
impl<'engine> ClosedBlock<'engine> {
/// Get the hash of the header without seal arguments.
pub fn preseal_hash(&self) -> H256 { unimplemented!(); }
/// Turn this into a `ClosedBlock`. A BlockChain must be provided in order to figure ou the uncles.
pub fn seal(self, _seal_fields: Vec<Bytes>) -> SealedBlock { unimplemented!(); }
/// Turn this back into an `OpenBlock`.
pub fn reopen(self) -> OpenBlock<'engine> { unimplemented!(); }
}
impl<'engine> IsBlock for ClosedBlock<'engine> {
fn block(&self) -> &Block { &self.open_block.block }
}
impl SealedBlock {
}
impl IsBlock for SealedBlock {
fn block(&self) -> &Block { &self.block }
}

View File

@ -1,4 +1,16 @@
use std::cmp::min;
use std::fmt;
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 util::crypto::ec::*;
use crypto::sha2::Sha256;
use crypto::ripemd160::Ripemd160;
use crypto::digest::Digest;
/// Definition of a contract whose implementation is built-in.
pub struct Builtin {
@ -8,3 +20,259 @@ pub struct Builtin {
/// being placed into the second.
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
}
/// Simple forwarder for cost.
pub fn cost(&self, s: usize) -> U256 { (*self.cost)(s) }
/// Simple forwarder for execute.
pub fn execute(&self, input: &[u8], output: &mut[u8]) { (*self.execute)(input, output); }
}
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

@ -1,11 +1,13 @@
use std::collections::hash_map::*;
use util::bytes::*;
use util::hash::*;
use util::uint::*;
use util::rlp::*;
use util::semantic_version::*;
use util::error::*;
use header::Header;
use transaction::Transaction;
use block::Block;
use spec::Spec;
use evm_schedule::EvmSchedule;
use env_info::EnvInfo;
@ -34,19 +36,17 @@ pub trait Engine {
/// Some intrinsic operation parameters; by default they take their value from the `spec()`'s `engine_params`.
fn maximum_extra_data_size(&self, _env_info: &EnvInfo) -> usize { decode(&self.spec().engine_params.get("maximum_extra_data_size").unwrap()) }
fn account_start_nonce(&self, _env_info: &EnvInfo) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) }
// TODO: refactor in terms of `on_preseal_block`
fn block_reward(&self, _env_info: &EnvInfo) -> U256 { decode(&self.spec().engine_params.get("block_reward").unwrap()) }
fn account_start_nonce(&self) -> U256 { decode(&self.spec().engine_params.get("account_start_nonce").unwrap()) }
/// Block transformation functions, before and after the transactions.
// fn on_new_block(&self, _env_info: &EnvInfo, _block: &mut Block) -> Result<(), EthcoreError> {}
// fn on_preseal_block(&self, _env_info: &EnvInfo, _block: &mut Block) -> Result<(), EthcoreError> {}
fn on_new_block(&self, _block: &mut Block) {}
fn on_close_block(&self, _block: &mut Block) {}
/// Verify that `header` is valid.
/// `parent` (the parent header) and `block` (the header's full block) may be provided for additional
/// checks. Returns either a null `Ok` or a general error detailing the problem with import.
// TODO: consider including State in the params.
fn verify(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) }
fn verify_block(&self, _header: &Header, _parent: Option<&Header>, _block: Option<&[u8]>) -> Result<(), EthcoreError> { Ok(()) }
/// Additional verification for transactions in blocks.
// TODO: Add flags for which bits of the transaction to check.
@ -55,14 +55,13 @@ pub trait Engine {
/// Don't forget to call Super::populateFromParent when subclassing & overriding.
// TODO: consider including State in the params.
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) -> Result<(), EthcoreError> { Ok(()) }
fn populate_from_parent(&self, _header: &mut Header, _parent: &Header) {}
// 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;
fn execute_builtin(&self, a: Address, in: &[u8], out: &mut [u8]);
*/
fn is_builtin(&self, a: &Address) -> bool { self.spec().builtins.contains_key(a) }
fn cost_of_builtin(&self, a: &Address, input: &[u8]) -> U256 { self.spec().builtins.get(a).unwrap().cost(input.len()) }
fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut [u8]) { self.spec().builtins.get(a).unwrap().execute(input, output); }
// TODO: sealing stuff - though might want to leave this for later.
}

33
src/ethash.rs Normal file
View File

@ -0,0 +1,33 @@
//use util::error::*;
use util::rlp::decode;
use engine::Engine;
use spec::Spec;
use block::*;
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() }
/// Apply the block reward on finalisation of the block.
fn on_close_block(&self, block: &mut Block) {
let a = block.header().author.clone();
block.state_mut().add_balance(&a, &decode(&self.spec().engine_params.get("block_reward").unwrap()));
}
}
// TODO: test for on_close_block.

View File

@ -90,7 +90,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)]
},
hash: RefCell::new(None)
};

View File

@ -5,12 +5,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,
@ -35,6 +45,7 @@ pub struct Header {
}
impl Header {
/// Create a new, default-valued, header.
pub fn new() -> Header {
Header {
parent_hash: ZERO_H256.clone(),
@ -121,11 +132,33 @@ impl Encodable for Header {
self.extra_data.encode(e);
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)]
mod tests {

View File

@ -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" )]
@ -108,4 +109,5 @@ pub mod client;
pub mod sync;
pub mod verification;
pub mod queue;
pub mod ethash;
pub mod block;

View File

@ -1,7 +1,9 @@
use util::hash::*;
use util::uint::*;
/// Information describing execution of a transaction.
pub struct Receipt {
// TODO
pub state_root: H256,
pub gas_used: U256,
}

View File

@ -17,11 +17,12 @@ use account::*;
use engine::Engine;
use builtin::Builtin;
use null_engine::NullEngine;
use ethash::Ethash;
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!");
@ -34,11 +35,12 @@ 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::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) => {
@ -49,7 +51,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<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| {
acc.insert(kv.0.clone(), kv.1);
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
/// 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,
@ -91,6 +94,7 @@ impl Spec {
pub fn to_engine(self) -> Result<Box<Engine>, EthcoreError> {
match self.engine_name.as_ref() {
"NullEngine" => Ok(NullEngine::new_boxed(self)),
"Ethash" => Ok(Ethash::new_boxed(self)),
_ => Err(EthcoreError::UnknownName)
}
}
@ -98,25 +102,22 @@ impl Spec {
/// Return the state root for the genesis state, memoising accordingly.
pub fn state_root(&self) -> Ref<H256> {
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(),
@ -131,67 +132,94 @@ impl Spec {
(0..self.seal_fields).map(|i| r.at(i).as_raw().to_vec()).collect()
},
hash: RefCell::new(None)
};
}
}
/// 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);
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?)
// move it to proper data structure
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() {
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 Some(ref builtin_json) = acc.find("builtin") {
if let Some(builtin) = Builtin::from_json(builtin_json) {
builtins.insert(addr.clone(), builtin);
}
}
let balance = acc.find("balance").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
let nonce = acc.find("nonce").and_then(|x| match x { &Json::String(ref b) => U256::from_dec_str(b).ok(), _ => None });
// 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.
if balance.is_some() || nonce.is_some() {
state.insert(addr, Account::new_basic(balance.unwrap_or(U256::from(0)), nonce.unwrap_or(U256::from(0))));
}
}
}
let builtins = {
// TODO: populate from json.
HashMap::new()
};
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())),
}
}
/// Returns the builtins map for the standard network of Ethereum Olympic, Frontier and Homestead.
fn standard_builtins() -> HashMap<Address, Builtin> {
let mut ret = HashMap::new();
ret.insert(Address::from_str("0000000000000000000000000000000000000001").unwrap(), Builtin::from_named_linear("ecrecover", 3000, 0).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000002").unwrap(), Builtin::from_named_linear("sha256", 60, 12).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000003").unwrap(), Builtin::from_named_linear("ripemd160", 600, 120).unwrap());
ret.insert(Address::from_str("0000000000000000000000000000000000000004").unwrap(), Builtin::from_named_linear("identity", 15, 3).unwrap());
ret
}
/// Creates the Olympic network chain spec.
pub fn olympic() -> Spec {
pub fn new_like_olympic() -> Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
@ -208,7 +236,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),
@ -223,13 +251,13 @@ 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),
}
}
/// 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![
@ -246,7 +274,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),
@ -261,13 +289,13 @@ 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.
pub fn morden() -> Spec {
/// Creates the actual Morden network chain spec.
pub fn new_morden_manual() -> Spec {
Spec {
engine_name: "Ethash".to_string(),
engine_params: vec![
@ -284,24 +312,90 @@ 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),
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![ // TODO: make correct
(Address::new(), 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),
}
}
/// Create a new Spec from a JSON UTF-8 data resource `data`.
pub fn from_json_utf8(data: &[u8]) -> Spec {
Self::from_json_str(::std::str::from_utf8(data).unwrap())
}
/// Create a new Spec from a JSON string.
pub fn from_json_str(s: &str) -> Spec {
let json = Json::from_str(s).expect("Json is invalid");
Self::from_json(json)
}
/// Create a new Morden chain spec.
pub fn new_morden() -> Spec { Self::from_json_utf8(include_bytes!("../res/morden.json")) }
/// Create a new Frontier chain spec.
pub fn new_frontier() -> Spec { Self::from_json_utf8(include_bytes!("../res/frontier.json")) }
}
#[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 morden_manual() {
let morden = Spec::new_morden_manual();
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() {
let morden = Spec::new_morden();
assert_eq!(*morden.state_root(), H256::from_str("f3f4696bbf3b3b07775128eb7a3763279a394e382130f27c21e70233e04946a9").unwrap());
let genesis = morden.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303").unwrap());
}
#[test]
fn frontier() {
let frontier = Spec::new_frontier();
assert_eq!(*frontier.state_root(), H256::from_str("d7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544").unwrap());
let genesis = frontier.genesis_block();
assert_eq!(BlockView::new(&genesis).header_view().sha3(), H256::from_str("d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3").unwrap());
let engine = frontier.to_engine();
}
}

View File

@ -8,6 +8,7 @@ use util::trie::*;
use util::bytes::*;
use util::rlp::*;
use util::uint::*;
use util::error::*;
use account::Account;
use transaction::Transaction;
use receipt::Receipt;
@ -15,7 +16,11 @@ use env_info::EnvInfo;
use engine::Engine;
/// Information concerning the result of the `State::apply` operation.
pub struct ApplyResult; // TODO
pub struct ApplyInfo {
pub receipt: Receipt,
}
pub type ApplyResult = Result<ApplyInfo, EthcoreError>;
/// Representation of the entire state of all accounts in the system.
pub struct State {
@ -44,7 +49,7 @@ impl State {
}
/// Creates new state with existing state root
pub fn new_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
pub fn from_existing(mut db: OverlayDB, mut root: H256, account_start_nonce: U256) -> State {
{
// trie should panic! if root does not exist
let _ = TrieDB::new(&mut db, &mut root);
@ -131,7 +136,7 @@ impl State {
/// Execute a given transaction.
/// This will change the state accordingly.
pub fn apply(_env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> (ApplyResult, Receipt) {
pub fn apply(&mut self, _env_info: &EnvInfo, _engine: &Engine, _t: &Transaction, _is_permanent: bool) -> ApplyResult {
unimplemented!();
}
@ -156,7 +161,7 @@ impl State {
}
{
let mut trie = TrieDBMut::new_existing(db, &mut root);
let mut trie = TrieDBMut::from_existing(db, &mut root);
for (address, ref a) in accounts.iter() {
match a {
&&Some(ref account) => trie.insert(address, &account.rlp()),
@ -240,7 +245,7 @@ fn code_from_database() {
s.drop()
};
let s = State::new_existing(db, r, U256::from(0u8));
let s = State::from_existing(db, r, U256::from(0u8));
assert_eq!(s.code(&a), Some([1u8, 2, 3].to_vec()));
}
@ -254,7 +259,7 @@ fn storage_at_from_database() {
s.drop()
};
let s = State::new_existing(db, r, U256::from(0u8));
let s = State::from_existing(db, r, U256::from(0u8));
assert_eq!(s.storage_at(&a, &H256::from(&U256::from(01u64))), H256::from(&U256::from(69u64)));
}
@ -270,7 +275,7 @@ fn get_from_database() {
s.drop()
};
let s = State::new_existing(db, r, U256::from(0u8));
let s = State::from_existing(db, r, U256::from(0u8));
assert_eq!(s.balance(&a), U256::from(69u64));
assert_eq!(s.nonce(&a), U256::from(1u64));
}

View File

@ -19,10 +19,16 @@ impl Transaction {
pub fn is_contract_creation(&self) -> bool {
self.to.is_none()
}
/// Is this transaction meant to send a message?
pub fn is_message_call(&self) -> bool {
!self.is_contract_creation()
}
/// Get the hash of this transaction.
pub fn sha3(&self) -> H256 {
unimplemented!();
}
}
impl Encodable for Transaction {