From a2f4090c059597afc42551e5543c3b55db701d1a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 28 Jan 2016 20:34:06 +0100 Subject: [PATCH] Fix difficulty calculation algo. --- res/ethereum/frontier.json | 2 +- res/ethereum/homestead_test.json | 2 +- res/ethereum/morden.json | 2 +- src/ethereum/ethash.rs | 20 ++++++++++++++------ src/spec.rs | 2 +- src/tests/chain.rs | 14 ++++++++++++++ 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/res/ethereum/frontier.json b/res/ethereum/frontier.json index 9cb456ce8..43be680d2 100644 --- a/res/ethereum/frontier.json +++ b/res/ethereum/frontier.json @@ -3,7 +3,7 @@ "engineName": "Ethash", "params": { "accountStartNonce": "0x00", - "frontierCompatibilityModeLimit": "0xDBBA0", + "frontierCompatibilityModeLimit": "0xdbba0", "maximumExtraDataSize": "0x20", "tieBreakingGas": false, "minGasLimit": "0x1388", diff --git a/res/ethereum/homestead_test.json b/res/ethereum/homestead_test.json index b11ef9740..1fb5dff80 100644 --- a/res/ethereum/homestead_test.json +++ b/res/ethereum/homestead_test.json @@ -3,7 +3,7 @@ "engineName": "Ethash", "params": { "accountStartNonce": "0x00", - "frontierCompatibilityModeLimit": "0", + "frontierCompatibilityModeLimit": 0, "maximumExtraDataSize": "0x20", "minGasLimit": "0x1388", "tieBreakingGas": false, diff --git a/res/ethereum/morden.json b/res/ethereum/morden.json index 32fed0cab..cdcf8c7dc 100644 --- a/res/ethereum/morden.json +++ b/res/ethereum/morden.json @@ -3,7 +3,7 @@ "engineName": "Ethash", "params": { "accountStartNonce": "0x0100000", - "frontierCompatibilityModeLimit": "0xDBBA0", + "frontierCompatibilityModeLimit": "0xdbba0", "maximumExtraDataSize": "0x20", "tieBreakingGas": false, "minGasLimit": "0x1388", diff --git a/src/ethereum/ethash.rs b/src/ethereum/ethash.rs index 019d764df..2ececec9b 100644 --- a/src/ethereum/ethash.rs +++ b/src/ethereum/ethash.rs @@ -59,9 +59,14 @@ impl Engine for Ethash { } fn schedule(&self, env_info: &EnvInfo) -> Schedule { + trace!(target: "client", "Creating schedule. param={:?}, fCML={}", self.spec().engine_params.get("frontierCompatibilityModeLimit"), self.u64_param("frontierCompatibilityModeLimit")); match env_info.number < self.u64_param("frontierCompatibilityModeLimit") { - true => Schedule::new_frontier(), - _ => Schedule::new_homestead(), + true => { + Schedule::new_frontier() + }, + _ => { + Schedule::new_homestead() + }, } } @@ -178,12 +183,15 @@ impl Ethash { } } else { - let diff_inc = (header.timestamp - parent.timestamp) / 10; - if diff_inc <= 1 { - parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc) + trace!(target: "ethash", "Calculating difficulty parent.difficulty={}, header.timestamp={}, parent.timestamp={}", parent.difficulty, header.timestamp, parent.timestamp); + //block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10, -99) + if header.timestamp >= parent.timestamp + 10 { + let diff_inc = (header.timestamp - parent.timestamp) / 10; + parent.difficulty - parent.difficulty / From::from(2048) * From::from(min(diff_inc - 1, 99)) } else { - parent.difficulty - parent.difficulty / From::from(2048) * From::from(max(diff_inc - 1, 99)) + let minus_diff_inc = if parent.timestamp > header.timestamp {(parent.timestamp - header.timestamp) / 10} else {0}; + parent.difficulty + parent.difficulty / From::from(2048) * From::from(minus_diff_inc + 1) } }; target = max(min_difficulty, target); diff --git a/src/spec.rs b/src/spec.rs index f4166eab2..77073eedd 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -16,7 +16,7 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json { Json::from_str(&s).expect("Json is invalid") } -/// Convert JSON value to equivlaent RLP representation. +/// Convert JSON value to equivalent RLP representation. // TODO: handle container types. fn json_to_rlp(json: &Json) -> Bytes { match *json { diff --git a/src/tests/chain.rs b/src/tests/chain.rs index ca7884315..6518c546d 100644 --- a/src/tests/chain.rs +++ b/src/tests/chain.rs @@ -1,4 +1,6 @@ use std::env; +use log::{LogLevelFilter}; +use env_logger::LogBuilder; use super::test_common::*; use client::{BlockChainClient,Client}; use pod_state::*; @@ -10,7 +12,19 @@ pub enum ChainEra { Homestead, } +fn setup_log() { + let mut builder = LogBuilder::new(); + builder.filter(None, LogLevelFilter::Info); + + if env::var("RUST_LOG").is_ok() { + builder.parse(&env::var("RUST_LOG").unwrap()); + } + + builder.init().unwrap(); +} + pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec { + setup_log(); let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); let mut failed = Vec::new();