Fix difficulty calculation algo.

This commit is contained in:
Gav Wood 2016-01-28 20:34:06 +01:00
parent dec9c3a642
commit a2f4090c05
6 changed files with 32 additions and 10 deletions

View File

@ -3,7 +3,7 @@
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",
"frontierCompatibilityModeLimit": "0xDBBA0", "frontierCompatibilityModeLimit": "0xdbba0",
"maximumExtraDataSize": "0x20", "maximumExtraDataSize": "0x20",
"tieBreakingGas": false, "tieBreakingGas": false,
"minGasLimit": "0x1388", "minGasLimit": "0x1388",

View File

@ -3,7 +3,7 @@
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x00", "accountStartNonce": "0x00",
"frontierCompatibilityModeLimit": "0", "frontierCompatibilityModeLimit": 0,
"maximumExtraDataSize": "0x20", "maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388", "minGasLimit": "0x1388",
"tieBreakingGas": false, "tieBreakingGas": false,

View File

@ -3,7 +3,7 @@
"engineName": "Ethash", "engineName": "Ethash",
"params": { "params": {
"accountStartNonce": "0x0100000", "accountStartNonce": "0x0100000",
"frontierCompatibilityModeLimit": "0xDBBA0", "frontierCompatibilityModeLimit": "0xdbba0",
"maximumExtraDataSize": "0x20", "maximumExtraDataSize": "0x20",
"tieBreakingGas": false, "tieBreakingGas": false,
"minGasLimit": "0x1388", "minGasLimit": "0x1388",

View File

@ -59,9 +59,14 @@ impl Engine for Ethash {
} }
fn schedule(&self, env_info: &EnvInfo) -> Schedule { 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") { match env_info.number < self.u64_param("frontierCompatibilityModeLimit") {
true => Schedule::new_frontier(), true => {
_ => Schedule::new_homestead(), Schedule::new_frontier()
},
_ => {
Schedule::new_homestead()
},
} }
} }
@ -178,12 +183,15 @@ impl Ethash {
} }
} }
else { else {
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; let diff_inc = (header.timestamp - parent.timestamp) / 10;
if diff_inc <= 1 { parent.difficulty - parent.difficulty / From::from(2048) * From::from(min(diff_inc - 1, 99))
parent.difficulty + parent.difficulty / From::from(2048) * From::from(1 - diff_inc)
} }
else { 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); target = max(min_difficulty, target);

View File

@ -16,7 +16,7 @@ pub fn gzip64res_to_json(source: &[u8]) -> Json {
Json::from_str(&s).expect("Json is invalid") 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. // TODO: handle container types.
fn json_to_rlp(json: &Json) -> Bytes { fn json_to_rlp(json: &Json) -> Bytes {
match *json { match *json {

View File

@ -1,4 +1,6 @@
use std::env; use std::env;
use log::{LogLevelFilter};
use env_logger::LogBuilder;
use super::test_common::*; use super::test_common::*;
use client::{BlockChainClient,Client}; use client::{BlockChainClient,Client};
use pod_state::*; use pod_state::*;
@ -10,7 +12,19 @@ pub enum ChainEra {
Homestead, 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<String> { pub fn json_chain_test(json_data: &[u8], era: ChainEra) -> Vec<String> {
setup_log();
let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid"); let json = Json::from_str(::std::str::from_utf8(json_data).unwrap()).expect("Json is invalid");
let mut failed = Vec::new(); let mut failed = Vec::new();