From 2017cd4cdb5581fbd50fc3603f8030df65f056fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= Date: Mon, 22 Jul 2019 13:17:22 +0100 Subject: [PATCH] babe: style fixes --- core/consensus/babe/src/lib.rs | 49 +++++++++++++++++++++++----------- core/test-runtime/src/lib.rs | 2 +- srml/babe/src/lib.rs | 9 ++++--- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/core/consensus/babe/src/lib.rs b/core/consensus/babe/src/lib.rs index 07d144b47db40..3cb21a164f081 100644 --- a/core/consensus/babe/src/lib.rs +++ b/core/consensus/babe/src/lib.rs @@ -17,6 +17,7 @@ //! # BABE consensus //! //! BABE (Blind Assignment for Blockchain Extension) consensus in Substrate. + #![forbid(unsafe_code, missing_docs, unused_must_use, unused_imports, unused_variables)] #![cfg_attr(not(test), forbid(dead_code))] pub use babe_primitives::*; @@ -136,7 +137,7 @@ impl SlotCompatible for BabeLink { /// Parameters for BABE. pub struct BabeParams { - /// The configuration for BABE. Includes the slot duration, threshold, and + /// The configuration for BABE. Includes the slot duration, threshold, and /// other parameters. pub config: Config, @@ -271,10 +272,13 @@ impl SlotWorker for BabeWorker w return Box::new(future::ok(())); } }; + let Epoch { ref authorities, randomness, epoch_index, .. } = epoch; + if authorities.is_empty() { error!(target: "babe", "No authorities at block {:?}", chain_head.hash()); } + if !self.force_authoring && self.sync_oracle.is_offline() && authorities.len() > 1 { debug!(target: "babe", "Skipping proposal slot. Waiting for the network."); telemetry!(CONSENSUS_DEBUG; "babe.skipping_proposal_slot"; @@ -350,7 +354,7 @@ impl SlotWorker for BabeWorker w telemetry!(CONSENSUS_INFO; "babe.discarding_proposal_took_too_long"; "slot" => slot_number ); - return + return; } let (header, body) = b.deconstruct(); @@ -489,6 +493,7 @@ fn check_header( slot_number, epoch_index, ); + schnorrkel::PublicKey::from_bytes(author.as_slice()).and_then(|p| { p.vrf_verify(transcript, vrf_output, vrf_proof) }).map_err(|s| { @@ -572,22 +577,26 @@ fn median_algorithm( let num_timestamps = time_source.1.len(); if num_timestamps as u64 >= median_required_blocks && median_required_blocks > 0 { let mut new_list: Vec<_> = time_source.1.iter().map(|&(t, sl)| { - let offset: u128 = u128::from(slot_duration) - .checked_mul(1_000_000u128) // self.config.get() returns *milliseconds* - .and_then(|x| x.checked_mul(u128::from(slot_number).saturating_sub(u128::from(sl)))) - .expect("we cannot have timespans long enough for this to overflow; qed"); - const NANOS_PER_SEC: u32 = 1_000_000_000; - let nanos = (offset % u128::from(NANOS_PER_SEC)) as u32; - let secs = (offset / u128::from(NANOS_PER_SEC)) as u64; - t + Duration::new(secs, nanos) - }).collect(); + let offset: u128 = u128::from(slot_duration) + .checked_mul(1_000_000u128) // self.config.get() returns *milliseconds* + .and_then(|x| x.checked_mul(u128::from(slot_number).saturating_sub(u128::from(sl)))) + .expect("we cannot have timespans long enough for this to overflow; qed"); + + const NANOS_PER_SEC: u32 = 1_000_000_000; + let nanos = (offset % u128::from(NANOS_PER_SEC)) as u32; + let secs = (offset / u128::from(NANOS_PER_SEC)) as u64; + + t + Duration::new(secs, nanos) + }).collect(); + // FIXME #2926: use a selection algorithm instead of a full sorting algorithm. new_list.sort_unstable(); + let &median = new_list .get(num_timestamps / 2) .expect("we have at least one timestamp, so this is a valid index; qed"); + time_source.1.clear(); - // FIXME #2927: pass this to the block authoring logic somehow time_source.0.replace(Instant::now() - median); } else { time_source.1.push((Instant::now(), slot_now)) @@ -619,17 +628,20 @@ impl Verifier for BabeVerifier where .inherent_data_providers .create_inherent_data() .map_err(String::from)?; + let (_, slot_now, _) = self.time_source.extract_timestamp_and_slot(&inherent_data) .map_err(|e| format!("Could not extract timestamp and slot: {:?}", e))?; + let hash = header.hash(); let parent_hash = *header.parent_hash(); let Epoch { authorities, randomness, epoch_index, .. } = epoch(self.api.as_ref(), &BlockId::Hash(parent_hash)) - .map_err(|e| format!("Could not fetch epoch at {:?}: {:?}", parent_hash, e))?; + .map_err(|e| format!("Could not fetch epoch at {:?}: {:?}", parent_hash, e))?; + let authorities: Vec<_> = authorities.into_iter().map(|(s, _)| s).collect(); - // we add one to allow for some small drift. - // FIXME #1019 in the future, alter this queue to allow deferring of - // headers + + // We add one to allow for some small drift. + // FIXME #1019 in the future, alter this queue to allow deferring of headers let checked_header = check_header::( &self.api, slot_now + 1, @@ -640,6 +652,7 @@ impl Verifier for BabeVerifier where epoch_index, self.config.threshold(), )?; + match checked_header { CheckedHeader::Checked(pre_header, (pre_digest, seal)) => { let BabePreDigest { slot_number, .. } = pre_digest.as_babe_pre_digest() @@ -684,6 +697,8 @@ impl Verifier for BabeVerifier where auxiliary: Vec::new(), fork_choice: ForkChoiceStrategy::LongestChain, }; + + // FIXME: this should eventually be moved to BabeBlockImport median_algorithm( self.config.0.median_required_blocks, self.config.get(), @@ -691,6 +706,7 @@ impl Verifier for BabeVerifier where slot_now, &mut *self.time_source.0.lock(), ); + // FIXME #1019 extract authorities Ok((import_block, maybe_keys)) } @@ -823,6 +839,7 @@ fn initialize_authorities_cache(client: &C) -> Result<(), ConsensusError> "Error initializing authorities cache: {}", error, ))); + let genesis_epoch = epoch(client, &genesis_id)?; cache.initialize(&well_known_cache_keys::AUTHORITIES, genesis_epoch.encode()) .map_err(map_err) diff --git a/core/test-runtime/src/lib.rs b/core/test-runtime/src/lib.rs index fa6838c15e985..e1699bc09c2d1 100644 --- a/core/test-runtime/src/lib.rs +++ b/core/test-runtime/src/lib.rs @@ -689,7 +689,7 @@ cfg_if! { assert!(!authorities.is_empty(), "no authorities!"); consensus_babe::Epoch { authorities, - randomness: srml_babe::randomness::(), + randomness: >::randomness(), epoch_index: 1, duration: 20, } diff --git a/srml/babe/src/lib.rs b/srml/babe/src/lib.rs index cd83da05470f2..577bda8c0142b 100644 --- a/srml/babe/src/lib.rs +++ b/srml/babe/src/lib.rs @@ -176,7 +176,7 @@ impl FindAuthor for Module { return Some(RawBabePreDigest::decode(&mut data)?.authority_index); } } - return None + return None; } } @@ -245,11 +245,14 @@ impl session::OneSessionHandler { use staking::BalanceOf; - let to_votes = |b: BalanceOf| - , u64>>::convert(b); + let to_votes = |b: BalanceOf| { + , u64>>::convert(b) + }; + let epoch_index = EpochIndex::get() .checked_add(1) .expect("epoch indices will never reach 2^64 before the death of the universe; qed"); + EpochIndex::put(epoch_index); // *Next* epoch's authorities.