Make AuthorityId generic (#1296)

* BlockAuthorityId convenience type

* Rename AuthorityId -> Ed25519AuthorityId to make it more precise

* Generalize AuthorityId up to substrate-client

* Fix in client-db

* rename: BlockAuthorityId -> AuthorityIdFor

* typo: should be digest item

* Fix test-runtime authorityId mismatch

One states that AuthorityId is u64 while the other states that it's Ed25519AuthorityId.

* Fix more u64 - Ed25519AuthorityId mismatch

* Fix compile of most of the srml modules

* Continue to pin aura and grandpa with ed25519 and fix compile

* Add MaybeHash trait

* Fix node-runtime compile

* Fix network tests
This commit is contained in:
Wei Tang
2019-01-08 11:14:18 +01:00
committed by Benjamin Kampmann
parent 043831cfb0
commit 71d889b692
46 changed files with 234 additions and 216 deletions
+11 -11
View File
@@ -67,8 +67,8 @@ use client::ChainHead;
use client::block_builder::api::BlockBuilder as BlockBuilderApi;
use consensus_common::{ImportBlock, BlockOrigin};
use runtime_primitives::{generic, generic::BlockId, Justification, BasicInherentData};
use runtime_primitives::traits::{Block, Header, Digest, DigestItemFor, ProvideRuntimeApi};
use primitives::{AuthorityId, ed25519};
use runtime_primitives::traits::{Block, Header, Digest, DigestItemFor, DigestItem, ProvideRuntimeApi};
use primitives::{Ed25519AuthorityId, ed25519};
use futures::{Stream, Future, IntoFuture, future::{self, Either}};
use tokio::timer::Timeout;
@@ -91,7 +91,7 @@ pub trait Network: Clone {
}
/// Get slot author for given block along with authorities.
fn slot_author(slot_num: u64, authorities: &[AuthorityId]) -> Option<AuthorityId> {
fn slot_author(slot_num: u64, authorities: &[Ed25519AuthorityId]) -> Option<Ed25519AuthorityId> {
if authorities.is_empty() { return None }
let idx = slot_num % (authorities.len() as u64);
@@ -168,7 +168,7 @@ pub fn start_aura_thread<B, C, E, I, SO, Error>(
I: BlockImport<B> + Send + Sync + 'static,
Error: From<C::Error> + From<I::Error> + 'static,
SO: SyncOracle + Send + Clone + 'static,
DigestItemFor<B>: CompatibleDigestItem + 'static,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId> + 'static,
Error: ::std::error::Error + Send + From<::consensus_common::Error> + 'static,
{
use tokio::runtime::current_thread::Runtime;
@@ -211,7 +211,7 @@ pub fn start_aura<B, C, E, I, SO, Error>(
I: BlockImport<B>,
Error: From<C::Error> + From<I::Error>,
SO: SyncOracle + Send + Clone,
DigestItemFor<B>: CompatibleDigestItem,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
Error: ::std::error::Error + Send + 'static + From<::consensus_common::Error>,
{
let make_authorship = move || {
@@ -266,7 +266,7 @@ pub fn start_aura<B, C, E, I, SO, Error>(
slot_num, timestamp);
// we are the slot author. make a block and sign it.
let proposer = match env.init(&chain_head, &authorities, pair.clone()) {
let proposer = match env.init(&chain_head, &authorities) {
Ok(p) => p,
Err(e) => {
warn!("Unable to author block in slot {:?}: {:?}", slot_num, e);
@@ -373,7 +373,7 @@ enum CheckedHeader<H> {
/// if it's successful, returns the pre-header, the slot number, and the signat.
//
// FIXME: needs misbehavior types - https://github.com/paritytech/substrate/issues/1018
fn check_header<B: Block>(slot_now: u64, mut header: B::Header, hash: B::Hash, authorities: &[AuthorityId])
fn check_header<B: Block>(slot_now: u64, mut header: B::Header, hash: B::Hash, authorities: &[Ed25519AuthorityId])
-> Result<CheckedHeader<B::Header>, String>
where DigestItemFor<B>: CompatibleDigestItem
{
@@ -446,7 +446,7 @@ impl<B: Block> ExtraVerification<B> for NothingExtra {
impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E, MakeInherent> where
C: Authorities<B> + BlockImport<B> + ProvideRuntimeApi + Send + Sync,
C::Api: BlockBuilderApi<B, Inherent>,
DigestItemFor<B>: CompatibleDigestItem,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
E: ExtraVerification<B>,
MakeInherent: Fn(u64, u64) -> Inherent + Send + Sync,
{
@@ -456,7 +456,7 @@ impl<B: Block, C, E, MakeInherent, Inherent> Verifier<B> for AuraVerifier<C, E,
header: B::Header,
justification: Option<Justification>,
mut body: Option<Vec<B::Extrinsic>>,
) -> Result<(ImportBlock<B>, Option<Vec<AuthorityId>>), String> {
) -> Result<(ImportBlock<B>, Option<Vec<Ed25519AuthorityId>>), String> {
use runtime_primitives::CheckInherentError;
const MAX_TIMESTAMP_DRIFT_SECS: u64 = 60;
@@ -600,7 +600,7 @@ pub fn import_queue<B, C, E, MakeInherent, Inherent>(
B: Block,
C: Authorities<B> + BlockImport<B,Error=ConsensusError> + ProvideRuntimeApi + Send + Sync,
C::Api: BlockBuilderApi<B, Inherent>,
DigestItemFor<B>: CompatibleDigestItem,
DigestItemFor<B>: CompatibleDigestItem + DigestItem<AuthorityId=Ed25519AuthorityId>,
E: ExtraVerification<B>,
MakeInherent: Fn(u64, u64) -> Inherent + Send + Sync,
{
@@ -633,7 +633,7 @@ mod tests {
type Proposer = DummyProposer;
type Error = Error;
fn init(&self, parent_header: &<TestBlock as BlockT>::Header, _authorities: &[AuthorityId], _sign_with: Arc<ed25519::Pair>)
fn init(&self, parent_header: &<TestBlock as BlockT>::Header, _authorities: &[Ed25519AuthorityId])
-> Result<DummyProposer, Error>
{
Ok(DummyProposer(parent_header.number + 1, self.0.clone()))
@@ -16,8 +16,7 @@
//! Block import helpers.
use primitives::AuthorityId;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, DigestItemFor};
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Header as HeaderT, DigestItemFor};
use runtime_primitives::Justification;
use std::borrow::Cow;
@@ -146,6 +145,6 @@ pub trait BlockImport<B: BlockT> {
/// Import a Block alongside the new authorities valid form this block forward
fn import_block(&self,
block: ImportBlock<B>,
new_authorities: Option<Vec<AuthorityId>>
new_authorities: Option<Vec<AuthorityIdFor<B>>>
) -> Result<ImportResult, Self::Error>;
}
+2 -2
View File
@@ -44,13 +44,13 @@ error_chain! {
}
/// Error checking signature
InvalidSignature(s: ::primitives::ed25519::Signature, a: ::primitives::AuthorityId) {
InvalidSignature(s: ::primitives::ed25519::Signature, a: ::primitives::Ed25519AuthorityId) {
description("Message signature is invalid"),
display("Message signature {:?} by {:?} is invalid.", s, a),
}
/// Account is not an authority.
InvalidAuthority(a: ::primitives::AuthorityId) {
InvalidAuthority(a: ::primitives::Ed25519AuthorityId) {
description("Message sender is not a valid authority"),
display("Message sender {:?} is not a valid authority.", a),
}
@@ -29,10 +29,9 @@ use std::collections::{HashSet, VecDeque};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use parking_lot::{Condvar, Mutex, RwLock};
use primitives::AuthorityId;
use runtime_primitives::Justification;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero, AuthorityIdFor};
use error::Error as ConsensusError;
@@ -68,7 +67,7 @@ pub trait Verifier<B: BlockT>: Send + Sync + Sized {
header: B::Header,
justification: Option<Justification>,
body: Option<Vec<B::Extrinsic>>
) -> Result<(ImportBlock<B>, Option<Vec<AuthorityId>>), String>;
) -> Result<(ImportBlock<B>, Option<Vec<AuthorityIdFor<B>>>), String>;
}
/// Blocks import queue API.
+4 -6
View File
@@ -40,9 +40,8 @@ extern crate error_chain;
use std::sync::Arc;
use primitives::{ed25519, AuthorityId};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::Block;
use runtime_primitives::traits::{AuthorityIdFor, Block};
use futures::prelude::*;
pub mod offline_tracker;
@@ -60,7 +59,7 @@ pub use block_import::{BlockImport, ImportBlock, BlockOrigin, ImportResult, Fork
/// Trait for getting the authorities at a given block.
pub trait Authorities<B: Block> {
type Error: ::std::error::Error + Send + 'static; /// Get the authorities at the given block.
fn authorities(&self, at: &BlockId<B>) -> Result<Vec<AuthorityId>, Self::Error>;
fn authorities(&self, at: &BlockId<B>) -> Result<Vec<AuthorityIdFor<B>>, Self::Error>;
}
/// Environment producer for a Consensus instance. Creates proposer instance and communication streams.
@@ -71,9 +70,8 @@ pub trait Environment<B: Block, ConsensusData> {
type Error: From<Error>;
/// Initialize the proposal logic on top of a specific header. Provide
/// the authorities at that header, and a local key to sign any additional
/// consensus messages with as well.
fn init(&self, parent_header: &B::Header, authorities: &[AuthorityId], sign_with: Arc<ed25519::Pair>)
/// the authorities at that header.
fn init(&self, parent_header: &B::Header, authorities: &[AuthorityIdFor<B>])
-> Result<Self::Proposer, Self::Error>;
}
@@ -16,8 +16,6 @@
//! Tracks offline validators.
use primitives::AuthorityId;
use std::collections::HashMap;
use std::time::{Instant, Duration};
@@ -55,11 +53,11 @@ impl Observed {
}
/// Tracks offline validators and can issue a report for those offline.
pub struct OfflineTracker {
pub struct OfflineTracker<AuthorityId> {
observed: HashMap<AuthorityId, Observed>,
}
impl OfflineTracker {
impl<AuthorityId: Eq + Clone + std::hash::Hash> OfflineTracker<AuthorityId> {
/// Create a new tracker.
pub fn new() -> Self {
OfflineTracker { observed: HashMap::new() }
@@ -114,10 +112,11 @@ impl OfflineTracker {
#[cfg(test)]
mod tests {
use super::*;
use primitives::Ed25519AuthorityId;
#[test]
fn validator_offline() {
let mut tracker = OfflineTracker::new();
let mut tracker = OfflineTracker::<Ed25519AuthorityId>::new();
let v = [0; 32].into();
let v2 = [1; 32].into();
let v3 = [2; 32].into();