Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
30 changes: 27 additions & 3 deletions core/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ impl<B, E, Block, RA> CallRuntimeAt<Block> for Client<B, E, Block, RA> where
}
}

impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA> where
impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync,
Block: BlockT<Hash=H256>,
Expand All @@ -1447,7 +1447,7 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>
/// Import a checked and validated block. If a justification is provided in
/// `ImportBlock` then `finalized` *must* be true.
fn import_block(
&self,
&mut self,
import_block: ImportBlock<Block>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
Expand All @@ -1458,7 +1458,7 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>

/// Check block preconditions.
fn check_block(
&self,
&mut self,
hash: Block::Hash,
parent_hash: Block::Hash,
) -> Result<ImportResult, Self::Error> {
Expand All @@ -1482,6 +1482,30 @@ impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA>
}
}

impl<B, E, Block, RA> consensus::BlockImport<Block> for Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher> + Clone + Send + Sync,
Block: BlockT<Hash=H256>,
{
type Error = ConsensusError;

fn import_block(
&mut self,
import_block: ImportBlock<Block>,
new_cache: HashMap<CacheKeyId, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(&*self).import_block(import_block, new_cache)
}

fn check_block(
&mut self,
hash: Block::Hash,
parent_hash: Block::Hash,
) -> Result<ImportResult, Self::Error> {
(&*self).check_block(hash, parent_hash)
}
}

impl<B, E, Block, RA> CurrentHeight for Client<B, E, Block, RA> where
B: backend::Backend<Block, Blake2Hasher>,
E: CallExecutor<Block, Blake2Hasher>,
Expand Down
21 changes: 11 additions & 10 deletions core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use consensus_common::{self, BlockImport, Environment, Proposer,
SelectChain, well_known_cache_keys::{self, Id as CacheKeyId}
};
use consensus_common::import_queue::{
Verifier, BasicQueue, SharedBlockImport, SharedJustificationImport, SharedFinalityProofImport,
SharedFinalityProofRequestBuilder,
Verifier, BasicQueue, BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport,
BoxFinalityProofRequestBuilder,
};
use client::{
block_builder::api::BlockBuilder as BlockBuilderApi,
Expand All @@ -54,6 +54,7 @@ use primitives::Pair;
use inherents::{InherentDataProviders, InherentData};

use futures::{Future, IntoFuture, future};
use parking_lot::Mutex;
use tokio_timer::Timeout;
use log::{error, warn, debug, info, trace};

Expand Down Expand Up @@ -134,7 +135,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
local_key: Arc<P>,
client: Arc<C>,
select_chain: SC,
block_import: Arc<I>,
block_import: I,
env: Arc<E>,
sync_oracle: SO,
inherent_data_providers: InherentDataProviders,
Expand All @@ -157,7 +158,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
{
let worker = AuraWorker {
client: client.clone(),
block_import,
block_import: Arc::new(Mutex::new(block_import)),
env,
local_key,
sync_oracle: sync_oracle.clone(),
Expand All @@ -179,7 +180,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(

struct AuraWorker<C, E, I, P, SO> {
client: Arc<C>,
block_import: Arc<I>,
block_import: Arc<Mutex<I>>,
env: Arc<E>,
local_key: Arc<P>,
sync_oracle: SO,
Expand Down Expand Up @@ -339,7 +340,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
"hash_previously" => ?header_hash
);

if let Err(e) = block_import.import_block(import_block, Default::default()) {
if let Err(e) = block_import.lock().import_block(import_block, Default::default()) {
warn!(target: "aura", "Error with block built on {:?}: {:?}",
parent_hash, e);
telemetry!(CONSENSUS_WARN; "aura.err_with_block_built_on";
Expand Down Expand Up @@ -673,10 +674,10 @@ fn register_aura_inherent_data_provider(
/// Start an import queue for the Aura consensus algorithm.
pub fn import_queue<B, C, P>(
slot_duration: SlotDuration,
block_import: SharedBlockImport<B>,
justification_import: Option<SharedJustificationImport<B>>,
finality_proof_import: Option<SharedFinalityProofImport<B>>,
finality_proof_request_builder: Option<SharedFinalityProofRequestBuilder<B>>,
block_import: BoxBlockImport<B>,
justification_import: Option<BoxJustificationImport<B>>,
finality_proof_import: Option<BoxFinalityProofImport<B>>,
finality_proof_request_builder: Option<BoxFinalityProofRequestBuilder<B>>,
client: Arc<C>,
inherent_data_providers: InherentDataProviders,
) -> Result<AuraImportQueue<B>, consensus_common::Error> where
Expand Down
20 changes: 10 additions & 10 deletions core/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub use digest::{BabePreDigest, BABE_VRF_PREFIX};
pub use babe_primitives::*;
pub use consensus_common::SyncOracle;
use consensus_common::import_queue::{
SharedBlockImport, SharedJustificationImport, SharedFinalityProofImport,
SharedFinalityProofRequestBuilder,
BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport,
BoxFinalityProofRequestBuilder,
};
use consensus_common::well_known_cache_keys::Id as CacheKeyId;
use runtime_primitives::{generic, generic::{BlockId, OpaqueDigestItemId}, Justification};
Expand Down Expand Up @@ -152,7 +152,7 @@ pub struct BabeParams<C, E, I, SO, SC> {
pub select_chain: SC,

/// A block importer
pub block_import: Arc<I>,
pub block_import: I,

/// The environment
pub env: Arc<E>,
Expand Down Expand Up @@ -200,7 +200,7 @@ pub fn start_babe<B, C, SC, E, I, SO, Error, H>(BabeParams {
{
let worker = BabeWorker {
client: client.clone(),
block_import,
block_import: Arc::new(Mutex::new(block_import)),
env,
local_key,
sync_oracle: sync_oracle.clone(),
Expand All @@ -220,7 +220,7 @@ pub fn start_babe<B, C, SC, E, I, SO, Error, H>(BabeParams {

struct BabeWorker<C, E, I, SO> {
client: Arc<C>,
block_import: Arc<I>,
block_import: Arc<Mutex<I>>,
env: Arc<E>,
local_key: Arc<sr25519::Pair>,
sync_oracle: SO,
Expand Down Expand Up @@ -397,7 +397,7 @@ impl<Hash, H, B, C, E, I, Error, SO> SlotWorker<B> for BabeWorker<C, E, I, SO> w
"hash_previously" => ?header_hash,
);

if let Err(e) = block_import.import_block(import_block, Default::default()) {
if let Err(e) = block_import.lock().import_block(import_block, Default::default()) {
warn!(target: "babe", "Error with block built on {:?}: {:?}",
parent_hash, e);
telemetry!(CONSENSUS_WARN; "babe.err_with_block_built_on";
Expand Down Expand Up @@ -829,10 +829,10 @@ fn initialize_authorities_cache<B, C>(client: &C) -> Result<(), ConsensusError>
/// Start an import queue for the Babe consensus algorithm.
pub fn import_queue<B, C, E>(
config: Config,
block_import: SharedBlockImport<B>,
justification_import: Option<SharedJustificationImport<B>>,
finality_proof_import: Option<SharedFinalityProofImport<B>>,
finality_proof_request_builder: Option<SharedFinalityProofRequestBuilder<B>>,
block_import: BoxBlockImport<B>,
justification_import: Option<BoxJustificationImport<B>>,
finality_proof_import: Option<BoxFinalityProofImport<B>>,
finality_proof_request_builder: Option<BoxFinalityProofRequestBuilder<B>>,
client: Arc<C>,
inherent_data_providers: InherentDataProviders,
) -> Result<(BabeImportQueue<B>, BabeLink), consensus_common::Error> where
Expand Down
37 changes: 30 additions & 7 deletions core/consensus/common/src/block_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use runtime_primitives::traits::{Block as BlockT, DigestItemFor, Header as Heade
use runtime_primitives::Justification;
use std::borrow::Cow;
use std::collections::HashMap;
use std::sync::Arc;
use crate::well_known_cache_keys;

use crate::import_queue::Verifier;
Expand Down Expand Up @@ -175,7 +176,7 @@ pub trait BlockImport<B: BlockT> {

/// Check block preconditions.
fn check_block(
&self,
&mut self,
hash: B::Hash,
parent_hash: B::Hash,
) -> Result<ImportResult, Self::Error>;
Expand All @@ -184,23 +185,45 @@ pub trait BlockImport<B: BlockT> {
///
/// Cached data can be accessed through the blockchain cache.
fn import_block(
&self,
&mut self,
block: ImportBlock<B>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error>;
}

impl<B: BlockT, T, E: ::std::error::Error + Send + 'static> BlockImport<B> for Arc<T>
where for<'r> &'r T: BlockImport<B, Error = E>
{
type Error = E;

fn check_block(
&mut self,
hash: B::Hash,
parent_hash: B::Hash,
) -> Result<ImportResult, Self::Error> {
(&**self).check_block(hash, parent_hash)
}

fn import_block(
&mut self,
block: ImportBlock<B>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
(&**self).import_block(block, cache)
}
}

/// Justification import trait
pub trait JustificationImport<B: BlockT> {
type Error: ::std::error::Error + Send + 'static;

/// Called by the import queue when it is started. Returns a list of justifications to request
/// from the network.
fn on_start(&self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }
fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }

/// Import a Block justification and finalize the given block.
fn import_justification(
&self,
&mut self,
hash: B::Hash,
number: NumberFor<B>,
justification: Justification,
Expand All @@ -213,11 +236,11 @@ pub trait FinalityProofImport<B: BlockT> {

/// Called by the import queue when it is started. Returns a list of finality proofs to request
/// from the network.
fn on_start(&self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }
fn on_start(&mut self) -> Vec<(B::Hash, NumberFor<B>)> { Vec::new() }

/// Import a Block justification and finalize the given block. Returns finalized block or error.
fn import_finality_proof(
&self,
&mut self,
hash: B::Hash,
number: NumberFor<B>,
finality_proof: Vec<u8>,
Expand All @@ -228,5 +251,5 @@ pub trait FinalityProofImport<B: BlockT> {
/// Finality proof request builder.
pub trait FinalityProofRequestBuilder<B: BlockT>: Send {
/// Build data blob, associated with the request.
fn build_request_data(&self, hash: &B::Hash) -> Vec<u8>;
fn build_request_data(&mut self, hash: &B::Hash) -> Vec<u8>;
}
12 changes: 6 additions & 6 deletions core/consensus/common/src/import_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ mod basic_queue;
pub mod buffered_link;

/// Shared block import struct used by the queue.
pub type SharedBlockImport<B> = Arc<dyn BlockImport<B, Error = ConsensusError> + Send + Sync>;
pub type BoxBlockImport<B> = Box<dyn BlockImport<B, Error = ConsensusError> + Send + Sync>;

/// Shared justification import struct used by the queue.
pub type SharedJustificationImport<B> = Arc<dyn JustificationImport<B, Error=ConsensusError> + Send + Sync>;
pub type BoxJustificationImport<B> = Box<dyn JustificationImport<B, Error=ConsensusError> + Send + Sync>;

/// Shared finality proof import struct used by the queue.
pub type SharedFinalityProofImport<B> = Arc<dyn FinalityProofImport<B, Error=ConsensusError> + Send + Sync>;
pub type BoxFinalityProofImport<B> = Box<dyn FinalityProofImport<B, Error=ConsensusError> + Send + Sync>;

/// Shared finality proof request builder struct used by the queue.
pub type SharedFinalityProofRequestBuilder<B> = Arc<dyn FinalityProofRequestBuilder<B> + Send + Sync>;
pub type BoxFinalityProofRequestBuilder<B> = Box<dyn FinalityProofRequestBuilder<B> + Send + Sync>;

/// Maps to the Origin used by the network.
pub type Origin = libp2p::PeerId;
Expand Down Expand Up @@ -140,7 +140,7 @@ pub trait Link<B: BlockT>: Send {
/// Request a finality proof for the given block.
fn request_finality_proof(&mut self, _hash: &B::Hash, _number: NumberFor<B>) {}
/// Remember finality proof request builder on start.
fn set_finality_proof_request_builder(&mut self, _request_builder: SharedFinalityProofRequestBuilder<B>) {}
fn set_finality_proof_request_builder(&mut self, _request_builder: BoxFinalityProofRequestBuilder<B>) {}
/// Adjusts the reputation of the given peer.
fn report_peer(&mut self, _who: Origin, _reputation_change: i32) {}
/// Restart sync.
Expand Down Expand Up @@ -173,7 +173,7 @@ pub enum BlockImportError {

/// Single block import function.
pub fn import_single_block<B: BlockT, V: Verifier<B>>(
import_handle: &dyn BlockImport<B, Error = ConsensusError>,
import_handle: &mut dyn BlockImport<B, Error = ConsensusError>,
block_origin: BlockOrigin,
block: IncomingBlock<B>,
verifier: Arc<V>,
Expand Down
Loading