Spec name; uncle reward calculation fixed

This commit is contained in:
arkpar 2016-01-14 22:38:49 +01:00
parent 00868488cf
commit 77c5b315df
13 changed files with 70 additions and 14 deletions

View File

@ -1,4 +1,5 @@
{ {
"name": "Frontier",
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",

View File

@ -1,4 +1,5 @@
{ {
"engineName": "Frontier (Test)",
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",

View File

@ -1,4 +1,5 @@
{ {
"name": "Homestead (Test)",
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",

View File

@ -1,4 +1,5 @@
{ {
"name": "Morden",
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x0100000", "accountStartNonce": "0x0100000",

View File

@ -1,4 +1,5 @@
{ {
"name": "Olympic",
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",

View File

@ -1,4 +1,5 @@
{ {
"name": "Morden",
"engineName": "NullEngine", "engineName": "NullEngine",
"params": { "params": {
"accountStartNonce": "0x0100000", "accountStartNonce": "0x0100000",

View File

@ -1,15 +1,34 @@
extern crate ethcore_util as util; extern crate ethcore_util as util;
extern crate ethcore; extern crate ethcore;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate log;
extern crate env_logger; extern crate env_logger;
use std::io::*; use std::io::*;
use std::env;
use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;
use util::hash::*; use util::hash::*;
use ethcore::service::ClientService; use ethcore::service::ClientService;
use ethcore::ethereum; use ethcore::ethereum;
fn setup_log() {
let format = |record: &LogRecord| {
format!("{} - {}", record.level(), record.args())
};
let mut builder = LogBuilder::new();
builder.format(format).filter(None, LogLevelFilter::Info);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init().unwrap();
}
fn main() { fn main() {
::env_logger::init().ok(); setup_log();
let spec = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut _service = ClientService::start(spec).unwrap(); let mut _service = ClientService::start(spec).unwrap();
loop { loop {

View File

@ -297,7 +297,7 @@ fn enact_block() {
let mut db = OverlayDB::new_temp(); let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db); engine.spec().ensure_db_good(&mut db);
let e = enact(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap(); let e = enact_and_seal(&orig_bytes, engine.deref(), db, &genesis_header, &vec![genesis_header.hash()]).unwrap();
assert_eq!(e.rlp_bytes(), orig_bytes); assert_eq!(e.rlp_bytes(), orig_bytes);

View File

@ -113,6 +113,7 @@ impl Client {
let db = DB::open_default(state_path.to_str().unwrap()).unwrap(); let db = DB::open_default(state_path.to_str().unwrap()).unwrap();
let mut state_db = OverlayDB::new(db); let mut state_db = OverlayDB::new(db);
engine.spec().ensure_db_good(&mut state_db); engine.spec().ensure_db_good(&mut state_db);
state_db.commit().expect("Error commiting genesis state to state DB");
Ok(Client { Ok(Client {
chain: chain.clone(), chain: chain.clone(),
@ -161,6 +162,7 @@ impl Client {
}; };
if let Err(e) = verify_block_final(&header, result.block().header()) { if let Err(e) = verify_block_final(&header, result.block().header()) {
warn!(target: "client", "Stage 4 block verification failed for {}\nError: {:?}", header.hash(), e); warn!(target: "client", "Stage 4 block verification failed for {}\nError: {:?}", header.hash(), e);
return;
} }
self.chain.write().unwrap().insert_block(&bytes); //TODO: err here? self.chain.write().unwrap().insert_block(&bytes); //TODO: err here?

View File

@ -41,8 +41,9 @@ impl Engine for Ethash {
// Bestow uncle rewards // Bestow uncle rewards
let current_number = fields.header.number(); let current_number = fields.header.number();
for u in fields.uncles.iter() { for u in fields.uncles.iter() {
fields.state.add_balance(u.author(), &(reward * U256::from((8 + u.number() - current_number) / 8))); fields.state.add_balance(u.author(), &(reward * U256::from(8 + u.number() - current_number) / U256::from(8)));
} }
fields.state.commit();
} }
@ -128,4 +129,23 @@ fn on_close_block() {
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap()); assert_eq!(b.state().balance(&Address::zero()), U256::from_str("4563918244f40000").unwrap());
} }
#[test]
fn on_close_block_with_uncle() {
use super::*;
let engine = new_morden().to_engine().unwrap();
let genesis_header = engine.spec().genesis_header();
let mut db = OverlayDB::new_temp();
engine.spec().ensure_db_good(&mut db);
let last_hashes = vec![genesis_header.hash()];
let mut b = OpenBlock::new(engine.deref(), db, &genesis_header, &last_hashes, Address::zero(), vec![]);
let mut uncle = Header::new();
let uncle_author = address_from_hex("ef2d6d194084c2de36e0dabfce45d046b37d1106");
uncle.author = uncle_author.clone();
b.push_uncle(uncle).unwrap();
let b = b.close();
assert_eq!(b.state().balance(&Address::zero()), U256::from_str("478eae0e571ba000").unwrap());
assert_eq!(b.state().balance(&uncle_author), U256::from_str("3cb71f51fc558000").unwrap());
}
// TODO: difficulty test // TODO: difficulty test

View File

@ -20,6 +20,7 @@ impl ClientService {
pub fn start(spec: Spec) -> Result<ClientService, Error> { pub fn start(spec: Spec) -> Result<ClientService, Error> {
let mut net_service = try!(NetworkService::start()); let mut net_service = try!(NetworkService::start());
info!("Starting {}", net_service.host_info()); info!("Starting {}", net_service.host_info());
info!("Configured for {} using {} engine", spec.name, spec.engine_name);
let mut dir = env::home_dir().unwrap(); let mut dir = env::home_dir().unwrap();
dir.push(".parity"); dir.push(".parity");
dir.push(H64::from(spec.genesis_header().hash()).hex()); dir.push(H64::from(spec.genesis_header().hash()).hex());

View File

@ -65,6 +65,8 @@ impl GenesisAccount {
/// chain and those to be interpreted by the active chain engine. /// chain and those to be interpreted by the active chain engine.
#[derive(Debug)] #[derive(Debug)]
pub struct Spec { pub struct Spec {
// User friendly spec name
pub name: String,
// What engine are we using for this? // What engine are we using for this?
pub engine_name: String, pub engine_name: String,
@ -196,6 +198,7 @@ impl FromJson for Spec {
Spec { Spec {
name: json["name"].as_string().unwrap().to_string(),
engine_name: json["engineName"].as_string().unwrap().to_string(), engine_name: json["engineName"].as_string().unwrap().to_string(),
engine_params: json_to_rlp_map(&json["params"]), engine_params: json_to_rlp_map(&json["params"]),
builtins: builtins, builtins: builtins,
@ -218,11 +221,16 @@ impl Spec {
/// Ensure that the given state DB has the trie nodes in for the genesis state. /// Ensure that the given state DB has the trie nodes in for the genesis state.
pub fn ensure_db_good(&self, db: &mut HashDB) { pub fn ensure_db_good(&self, db: &mut HashDB) {
if !db.contains(&self.state_root()) { if !db.contains(&self.state_root()) {
info!("Populating genesis state...");
let mut root = H256::new(); let mut root = H256::new();
let mut t = SecTrieDBMut::new(db, &mut root); {
for (address, account) in self.genesis_state.iter() { let mut t = SecTrieDBMut::new(db, &mut root);
t.insert(address.as_slice(), &account.rlp()); for (address, account) in self.genesis_state.iter() {
t.insert(address.as_slice(), &account.rlp());
}
} }
assert!(db.contains(&self.state_root()));
info!("Genesis state is ready");
} }
} }

View File

@ -187,7 +187,7 @@ impl<'p> TestIo<'p> {
} }
impl<'p> SyncIo for TestIo<'p> { impl<'p> SyncIo for TestIo<'p> {
fn disable_peer(&mut self, _peer_id: &PeerId) { fn disable_peer(&mut self, _peer_id: PeerId) {
} }
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> { fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), UtilError> {
@ -257,7 +257,7 @@ impl TestNet {
for client in 0..self.peers.len() { for client in 0..self.peers.len() {
if peer != client { if peer != client {
let mut p = self.peers.get_mut(peer).unwrap(); let mut p = self.peers.get_mut(peer).unwrap();
p.sync.on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), &(client as PeerId)); p.sync.on_peer_connected(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(client as PeerId)), client as PeerId);
} }
} }
} }
@ -269,7 +269,7 @@ impl TestNet {
Some(packet) => { Some(packet) => {
let mut p = self.peers.get_mut(packet.recipient).unwrap(); let mut p = self.peers.get_mut(packet.recipient).unwrap();
trace!("--- {} -> {} ---", peer, packet.recipient); trace!("--- {} -> {} ---", peer, packet.recipient);
p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), &(peer as PeerId), packet.packet_id, &packet.data); p.sync.on_packet(&mut TestIo::new(&mut p.chain, &mut p.queue, Some(peer as PeerId)), peer as PeerId, packet.packet_id, &packet.data);
trace!("----------------"); trace!("----------------");
}, },
None => {} None => {}