Move authorities interface from Core to consensus (#1412)

* Move authorities interface from Core to consensus

f

* notify all caches of block insert + create with up-to-date best_fin

* merged authorities_are_cached from light_grandpa_import2

* Add ProvideCache trait

* Create helper function for 'get_cache'

* Fix some formatting

* Bump impl version

* Resolve wasm conflicts

* Apply review comments

* Use try_for_each

* Move authorities interface from Core to consensus

f

* notify all caches of block insert + create with up-to-date best_fin

* merged authorities_are_cached from light_grandpa_import2

* Add ProvideCache trait

* Create helper function for 'get_cache'

* Fix some formatting

* Bump impl version

* Resolve wasm conflicts

* Apply review comments

* Use try_for_each

* Move authorities interface from Core to consensus

f

* notify all caches of block insert + create with up-to-date best_fin

* merged authorities_are_cached from light_grandpa_import2

* Add ProvideCache trait

* Create helper function for 'get_cache'

* Fix some formatting

* Bump impl version

* Resolve wasm conflicts

* Apply review comments

* Use try_for_each

* Increment impl_version

* Update lib.rs
This commit is contained in:
Stanislav Tkach
2019-03-29 18:41:22 +02:00
committed by Gav Wood
parent 55788fdf77
commit cbfc36b39f
44 changed files with 650 additions and 409 deletions
@@ -16,9 +16,10 @@
//! Block import helpers.
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor};
use runtime_primitives::traits::{Block as BlockT, DigestItemFor, Header as HeaderT, NumberFor};
use runtime_primitives::Justification;
use std::borrow::Cow;
use std::collections::HashMap;
/// Block import result.
#[derive(Debug, PartialEq, Eq)]
@@ -175,11 +176,13 @@ pub trait BlockImport<B: BlockT> {
parent_hash: B::Hash,
) -> Result<ImportResult, Self::Error>;
/// Import a Block alongside the new authorities valid from this block forward
/// Import a block.
///
/// Cached data can be accessed through the blockchain cache.
fn import_block(
&self,
block: ImportBlock<B>,
new_authorities: Option<Vec<AuthorityIdFor<B>>>,
cache: HashMap<Vec<u8>, Vec<u8>>,
) -> Result<ImportResult, Self::Error>;
}
@@ -58,6 +58,12 @@ error_chain! {
display("Message signature {:?} by {:?} is invalid.", s, a),
}
/// Invalid authorities set received from the runtime.
InvalidAuthoritiesSet {
description("authorities set is invalid"),
display("Current state of blockchain has invalid authorities set"),
}
/// Account is not an authority.
InvalidAuthority(a: Public) {
description("Message sender is not a valid authority"),
@@ -28,6 +28,7 @@ use crate::block_import::{
BlockImport, BlockOrigin, ImportBlock, ImportedAux, ImportResult, JustificationImport,
};
use crossbeam_channel::{self as channel, Receiver, Sender};
use parity_codec::Encode;
use std::sync::Arc;
use std::thread;
@@ -38,6 +39,7 @@ use runtime_primitives::traits::{
use runtime_primitives::Justification;
use crate::error::Error as ConsensusError;
use parity_codec::alloc::collections::hash_map::HashMap;
/// Shared block import struct used by the queue.
pub type SharedBlockImport<B> = Arc<dyn BlockImport<B, Error = ConsensusError> + Send + Sync>;
@@ -566,7 +568,12 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
BlockImportError::VerificationFailed(peer.clone(), msg)
})?;
import_error(import_handle.import_block(import_block, new_authorities))
let mut cache = HashMap::new();
if let Some(authorities) = new_authorities {
cache.insert(crate::well_known_cache_keys::AUTHORITIES.to_vec(), authorities.encode());
}
import_error(import_handle.import_block(import_block, cache))
}
#[cfg(test)]
+9 -1
View File
@@ -53,7 +53,9 @@ pub use block_import::{
/// 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.
type Error: std::error::Error + Send + 'static;
/// Get the authorities at the given block.
fn authorities(&self, at: &BlockId<B>) -> Result<Vec<AuthorityIdFor<B>>, Self::Error>;
}
@@ -115,3 +117,9 @@ impl<T: SyncOracle> SyncOracle for Arc<T> {
T::is_offline(&*self)
}
}
/// A list of all well known keys in the cache.
pub mod well_known_cache_keys {
/// A list of authorities.
pub const AUTHORITIES: &'static [u8] = b"auth";
}