From 4840b6945fcb9bae7c41bd2bfd6100ac00eaccaf Mon Sep 17 00:00:00 2001 From: Guanghua Guo Date: Wed, 14 Nov 2018 18:23:50 +0800 Subject: [PATCH] Fix/consensus (#97) * Fix pos round * Adjust spawn block time --- consensus/src/lib.rs | 19 ++++++++++--------- consensus/src/service.rs | 6 +++--- src/genesis_config.rs | 8 ++++---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/consensus/src/lib.rs b/consensus/src/lib.rs index 5ac9b72d02a7c..cd8b2f5c22778 100644 --- a/consensus/src/lib.rs +++ b/consensus/src/lib.rs @@ -165,26 +165,27 @@ impl Proposer { fn primary_index(&self, round_number: usize, _len: usize) -> usize { use primitives::uint::U256; + let validators_len = self.validators.len(); let mut stake_weight = self.validators.iter() .map(|account| { let weight = self.client.stake_weight(&self.parent_id, *account).unwrap(); if weight == 0 { 1 } else { weight } }) .collect::>(); - trace!("validator stake weight:{:?}", stake_weight); - for i in 1..self.validators.len() { + info!("validator stake weight:{:?}, round_numer:{:?}", stake_weight, round_number); + for i in 1..validators_len { stake_weight[i] = stake_weight[i] + stake_weight[i - 1]; } - let big_len = stake_weight[self.validators.len() - 1] as u128; + let big_len = stake_weight[validators_len - 1] as u128; let big_value = U256::from_big_endian(&self.random_seed.0).low_u64() as u128; - let offset = (big_value * big_value + round_number as u128) % big_len; + let offset = (big_value * big_value) % big_len; - for i in 0..stake_weight.len() { + for i in 0..validators_len { if offset < stake_weight[i] as u128 { - return i; + return (i + round_number) % validators_len; } } - return 0; + return round_number % validators_len; } fn primary_validator(&self, round_number: usize) -> Option { @@ -259,7 +260,7 @@ impl bft::Proposer for Proposer let block = block_builder.bake()?; - info!("Proposing block [number: {}; hash: {}; parent_hash: {}; extrinsics: [{}]]", + trace!("Proposing block [number: {}; hash: {}; parent_hash: {}; extrinsics: [{}]]", block.header.number, Hash::from(block.header.hash()), block.header.parent_hash, @@ -283,7 +284,7 @@ impl bft::Proposer for Proposer } fn evaluate(&self, unchecked_proposal: &Block) -> Self::Evaluate { - info!(target: "bft", "evaluating block on top of parent ({}, {:?})", self.parent_number, self.parent_hash); + trace!(target: "bft", "evaluating block on top of parent ({}, {:?})", self.parent_number, self.parent_hash); let current_timestamp = current_timestamp(); diff --git a/consensus/src/service.rs b/consensus/src/service.rs index 256ebdf652247..47a1890c1fbcf 100644 --- a/consensus/src/service.rs +++ b/consensus/src/service.rs @@ -27,8 +27,8 @@ use chainx_primitives::{Block, Header}; use chainx_api::ChainXApi; use TransactionPool; -const TIMER_DELAY_MS: Duration = Duration::from_millis(2000); -const TIMER_INTERVAL_MS: Duration = Duration::from_millis(200); +const TIMER_DELAY_MS: Duration = Duration::from_millis(3000); +const TIMER_INTERVAL_MS: Duration = Duration::from_millis(300); // spin up an instance of BFT agreement on the current thread's executor. // panics if there is no current thread executor. @@ -130,7 +130,7 @@ impl Service { let hash = best_block.hash(); if hash == prev_best { - debug!(target: "bft", "Starting consensus round after a timeout"); + trace!(target: "bft", "Starting consensus round after a timeout"); start_bft(best_block, s.clone()); } prev_best = hash; diff --git a/src/genesis_config.rs b/src/genesis_config.rs index e63e274fe45f9..871c6c36cdbba 100644 --- a/src/genesis_config.rs +++ b/src/genesis_config.rs @@ -24,8 +24,8 @@ use self::keys::DisplayLayout; pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { let alice = ed25519::Pair::from_seed(b"Alice ").public(); let bob = ed25519::Pair::from_seed(b"Bob ").public(); - let charlie = ed25519::Pair::from_seed(b"Charlie ").public(); - let dave = ed25519::Pair::from_seed(b"Dave ").public(); + let _charlie = ed25519::Pair::from_seed(b"Charlie ").public(); + let _dave = ed25519::Pair::from_seed(b"Dave ").public(); let gavin = ed25519::Pair::from_seed(b"Gavin ").public(); let satoshi = ed25519::Pair::from_seed(b"Satoshi ").public(); @@ -36,7 +36,7 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { let initial_authorities = match chainspec { ChainSpec::Dev => vec![auth1], ChainSpec::Local => vec![auth1, auth2], - ChainSpec::Multi => vec![auth1, auth2, auth3, auth4, charlie.into(), dave.into()], + ChainSpec::Multi => vec![auth1, auth2, auth3, auth4], }; @@ -44,7 +44,7 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { // const CENTS: u128 = 1_000 * MILLICENTS; // assume this is worth about a cent. // const DOLLARS: u128 = 100 * CENTS; - const SECS_PER_BLOCK: u64 = 1; + const SECS_PER_BLOCK: u64 = 3; const MINUTES: u64 = 60 / SECS_PER_BLOCK; const HOURS: u64 = MINUTES * 60; const DAYS: u64 = HOURS * 24;