From a422a14b527c2c2c1bcf4acfd70b4a379b5943e0 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Thu, 25 Oct 2018 18:00:46 +0200 Subject: [PATCH] use generic block number in GRANDPA --- substrate/Cargo.lock | 3 +- substrate/core/finality-grandpa/src/lib.rs | 64 ++++++++++++++-------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 0bbfc82ad0..df76975816 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -581,10 +581,11 @@ dependencies = [ [[package]] name = "finality-grandpa" version = "0.3.0" -source = "git+https://github.com/paritytech/finality-grandpa#fffce2dc450e72ff8b3f43818f8aceb5cfacc2d7" +source = "git+https://github.com/paritytech/finality-grandpa#f1ad8d7ca020e5db0e51e32cd62e8cd3c578d121" dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 2.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec-derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/substrate/core/finality-grandpa/src/lib.rs b/substrate/core/finality-grandpa/src/lib.rs index 6411fee6ad..faf1611e1a 100644 --- a/substrate/core/finality-grandpa/src/lib.rs +++ b/substrate/core/finality-grandpa/src/lib.rs @@ -58,7 +58,7 @@ use substrate_primitives::{ed25519, AuthorityId, Blake2Hasher}; use tokio::timer::Interval; use grandpa::Error as GrandpaError; -use grandpa::{voter, round::State as RoundState, Prevote, Precommit, Equivocation}; +use grandpa::{voter, round::State as RoundState, Equivocation, BlockNumberOps}; use std::collections::{VecDeque, HashMap}; use std::sync::Arc; @@ -72,12 +72,21 @@ const LAST_COMPLETED_KEY: &[u8] = b"grandpa_completed_round"; const AUTHORITY_SET_KEY: &[u8] = b"grandpa_voters"; /// round-number, round-state, set indicator -type LastCompleted = (u64, RoundState, u64); +type LastCompleted = (u64, RoundState, u64); /// A GRANDPA message for a substrate chain. -pub type Message = grandpa::Message<::Hash>; +pub type Message = grandpa::Message<::Hash, NumberFor>; /// A signed message. -pub type SignedMessage = grandpa::SignedMessage<::Hash, ed25519::Signature, AuthorityId>; +pub type SignedMessage = grandpa::SignedMessage< + ::Hash, + NumberFor, + ed25519::Signature, + AuthorityId, +>; +/// A prevote message for this chain's block type. +pub type Prevote = grandpa::Prevote<::Hash, NumberFor>; +/// A precommit message for this chain's block type. +pub type Precommit = grandpa::Precommit<::Hash, NumberFor>; /// Configuration for the GRANDPA service. pub struct Config { @@ -134,18 +143,17 @@ pub trait BlockStatus { /// Return `Ok(Some(number))` or `Ok(None)` depending on whether the block /// is definitely known and has been imported. /// If an unexpected error occurs, return that. - fn block_number(&self, hash: Block::Hash) -> Result, Error>; + fn block_number(&self, hash: Block::Hash) -> Result>, Error>; } impl BlockStatus for Arc> where B: Backend, E: CallExecutor, - NumberFor: As, + NumberFor: BlockNumberOps, { - fn block_number(&self, hash: Block::Hash) -> Result, Error> { + fn block_number(&self, hash: Block::Hash) -> Result>, Error> { self.block_number_from_id(&BlockId::Hash(hash)) .map_err(|e| Error::Blockchain(format!("{:?}", e))) - .map(|num| num.map(|n| n.as_())) } } @@ -415,13 +423,13 @@ pub struct Environment { network: N, } -impl grandpa::Chain for Environment where +impl grandpa::Chain> for Environment where Block: 'static, B: Backend + 'static, E: CallExecutor + 'static, N: Network + 'static, N::In: 'static, - NumberFor: As, + NumberFor: BlockNumberOps, DigestItemFor: CompatibleDigestItem>, { fn ancestry(&self, base: Block::Hash, block: Block::Hash) -> Result, GrandpaError> { @@ -450,13 +458,13 @@ impl grandpa::Chain for Environment Option<(Block::Hash, u32)> { + fn best_chain_containing(&self, block: Block::Hash) -> Option<(Block::Hash, NumberFor)> { match self.inner.best_containing(block, None) { Ok(Some(hash)) => { let header = self.inner.header(&BlockId::Hash(hash)).ok()? .expect("Header known to exist after `best_containing` call; qed"); - Some((hash, header.number().as_())) + Some((hash, header.number().clone())) } Ok(None) => None, Err(e) => { @@ -486,20 +494,26 @@ pub trait CompatibleDigestItem { fn scheduled_change(&self) -> Option> { None } } -impl voter::Environment for Environment where +impl voter::Environment> for Environment where Block: 'static, B: Backend + 'static, E: CallExecutor + 'static, N: Network + 'static, N::In: 'static, - NumberFor: As, + NumberFor: BlockNumberOps, DigestItemFor: CompatibleDigestItem>, { type Timer = Box>; type Id = AuthorityId; type Signature = ed25519::Signature; - type In = Box, Error = Self::Error>>; - type Out = Box, SinkError = Self::Error>>; + type In = Box, Self::Signature, Self::Id>, + Error = Self::Error, + >>; + type Out = Box>, + SinkError = Self::Error, + >>; type Error = Error; #[allow(unreachable_code)] @@ -560,7 +574,7 @@ impl voter::Environment for Environment) -> Result<(), Self::Error> { + fn completed(&self, round: u64, state: RoundState>) -> Result<(), Self::Error> { let encoded_state = (round, state).encode(); if let Err(e) = self.inner.backend() .insert_aux(&[(LAST_COMPLETED_KEY, &encoded_state[..])], &[]) @@ -572,7 +586,7 @@ impl voter::Environment for Environment Result<(), Self::Error> { + fn finalize_block(&self, hash: Block::Hash, number: NumberFor) -> Result<(), Self::Error> { // TODO: don't unconditionally notify. if let Err(e) = self.inner.finalize_block(BlockId::Hash(hash), true) { warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); @@ -586,7 +600,7 @@ impl voter::Environment for Environment, Self::Signature> + equivocation: ::grandpa::Equivocation, Self::Signature> ) { warn!(target: "afg", "Detected prevote equivocation in the finality worker: {:?}", equivocation); // nothing yet; this could craft misbehavior reports of some kind. @@ -595,7 +609,7 @@ impl voter::Environment for Environment, Self::Signature> + equivocation: Equivocation, Self::Signature> ) { warn!(target: "afg", "Detected precommit equivocation in the finality worker: {:?}", equivocation); // nothing yet @@ -658,19 +672,21 @@ pub fn run_grandpa( E: CallExecutor + 'static, N: Network + 'static, N::In: 'static, - NumberFor: As, + NumberFor: BlockNumberOps, DigestItemFor: CompatibleDigestItem>, { + use runtime_primitives::traits::Zero; + let chain_info = client.info()?; let genesis_hash = chain_info.chain.genesis_hash; let last_finalized = ( chain_info.chain.finalized_hash, - chain_info.chain.finalized_number.as_() + chain_info.chain.finalized_number, ); let (last_round_number, last_state) = match client.backend().get_aux(LAST_COMPLETED_KEY)? { - None => (0, RoundState::genesis((genesis_hash, 0))), - Some(raw) => <(u64, RoundState)>::decode(&mut &raw[..]) + None => (0, RoundState::genesis((genesis_hash, >::zero()))), + Some(raw) => <(u64, RoundState>)>::decode(&mut &raw[..]) .ok_or_else(|| ::client::error::ErrorKind::Backend( format!("Last GRANDPA round state kept in invalid format") ))?