Skip to content

Commit

Permalink
Fix/consensus (paritytech#97)
Browse files Browse the repository at this point in the history
* Fix pos round

* Adjust spawn block time
  • Loading branch information
gguoss committed Nov 16, 2018
1 parent a4f00a7 commit 4840b69
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
19 changes: 10 additions & 9 deletions consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,27 @@ impl<C: ChainXApi + Send + Sync> Proposer<C> {
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::<Vec<_>>();
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<AccountId> {
Expand Down Expand Up @@ -259,7 +260,7 @@ impl<C> bft::Proposer<Block> for Proposer<C>

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,
Expand All @@ -283,7 +284,7 @@ impl<C> bft::Proposer<Block> for Proposer<C>
}

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();

Expand Down
6 changes: 3 additions & 3 deletions consensus/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -36,15 +36,15 @@ 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],
};


// const MILLICENTS: u128 = 1_000_000_000;
// 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;
Expand Down

0 comments on commit 4840b69

Please sign in to comment.