Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
removes use of sc_client::Client from sc_consensus_babe
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Feb 20, 2020
1 parent 5135844 commit 0157730
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 86 deletions.
6 changes: 1 addition & 5 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,13 @@ macro_rules! new_full_start {
sc_consensus_babe::Config::get_or_compute(&*client)?,
grandpa_block_import,
client.clone(),
client.clone(),
)?;

let import_queue = sc_consensus_babe::import_queue(
babe_link.clone(),
block_import.clone(),
Some(Box::new(justification_import)),
None,
client.clone(),
client,
inherent_data_providers.clone(),
)?;
Expand Down Expand Up @@ -342,7 +340,6 @@ pub fn new_light(config: NodeConfiguration)
sc_consensus_babe::Config::get_or_compute(&*client)?,
grandpa_block_import,
client.clone(),
client.clone(),
)?;

let import_queue = sc_consensus_babe::import_queue(
Expand All @@ -351,7 +348,6 @@ pub fn new_light(config: NodeConfiguration)
None,
Some(Box::new(finality_proof_import)),
client.clone(),
client,
inherent_data_providers.clone(),
)?;

Expand Down Expand Up @@ -500,7 +496,7 @@ mod tests {
|config| {
let mut setup_handles = None;
new_full!(config, |
block_import: &sc_consensus_babe::BabeBlockImport<_, _, Block, _, _, _>,
block_import: &sc_consensus_babe::BabeBlockImport<Block, _, _>,
babe_link: &sc_consensus_babe::BabeLink<Block>,
| {
setup_handles = Some((block_import.clone(), babe_link.clone()));
Expand Down
1 change: 0 additions & 1 deletion client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ mod tests {
config.clone(),
client.clone(),
client.clone(),
client.clone(),
).expect("can initialize block-import");

let epoch_changes = link.epoch_changes().clone();
Expand Down
119 changes: 48 additions & 71 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,9 @@ use sp_consensus_babe::inherents::BabeInherentData;
use sp_timestamp::{TimestampInherentData, InherentType as TimestampInherent};
use sp_consensus::import_queue::{Verifier, BasicQueue, CacheKeyId};
use sc_client_api::{
backend::{AuxStore, Backend},
call_executor::CallExecutor,
backend::AuxStore,
BlockchainEvents, ProvideUncles,
};
use sc_client::Client;

use sp_block_builder::BlockBuilder as BlockBuilderApi;

use futures::prelude::*;
Expand Down Expand Up @@ -655,27 +652,28 @@ impl<Block: BlockT> BabeLink<Block> {
}

/// A verifier for Babe blocks.
pub struct BabeVerifier<B, E, Block: BlockT, RA, PRA> {
client: Arc<Client<B, E, Block, RA>>,
api: Arc<PRA>,
pub struct BabeVerifier<Block: BlockT, Client> {
client: Arc<Client>,
inherent_data_providers: sp_inherents::InherentDataProviders,
config: Config,
epoch_changes: SharedEpochChanges<Block, Epoch>,
time_source: TimeSource,
}

impl<B, E, Block: BlockT, RA, PRA> BabeVerifier<B, E, Block, RA, PRA> {
impl<Block, Client> BabeVerifier<Block, Client>
where
Block: BlockT,
Client: HeaderBackend<Block> + HeaderMetadata<Block> + ProvideRuntimeApi<Block>,
Client::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
fn check_inherents(
&self,
block: Block,
block_id: BlockId<Block>,
inherent_data: InherentData,
) -> Result<(), Error<Block>>
where
PRA: ProvideRuntimeApi<Block>,
PRA::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error>
{
let inherent_res = self.api.runtime_api().check_inherents(
let inherent_res = self.client.runtime_api().check_inherents(
&block_id,
block,
inherent_data,
Expand All @@ -693,14 +691,11 @@ impl<B, E, Block: BlockT, RA, PRA> BabeVerifier<B, E, Block, RA, PRA> {
}
}

impl<B, E, Block, RA, PRA> Verifier<Block> for BabeVerifier<B, E, Block, RA, PRA> where
impl<Block, Client> Verifier<Block> for BabeVerifier<Block, Client> where
Block: BlockT,
B: Backend<Block> + 'static,
E: CallExecutor<Block> + 'static + Clone + Send + Sync,
RA: Send + Sync,
PRA: ProvideRuntimeApi<Block> + Send + Sync + AuxStore + ProvideCache<Block>,
PRA::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error>
+ BabeApi<Block, Error = sp_blockchain::Error>,
Client: HeaderMetadata<Block, Error = sp_blockchain::Error> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + AuxStore + ProvideCache<Block>,
Client::Api: BlockBuilderApi<Block, Error = sp_blockchain::Error> + BabeApi<Block, Error = sp_blockchain::Error>,
{
fn verify(
&mut self,
Expand Down Expand Up @@ -769,7 +764,7 @@ impl<B, E, Block, RA, PRA> Verifier<Block> for BabeVerifier<B, E, Block, RA, PRA
// the header is valid but let's check if there was something else already
// proposed at the same slot by the given author
if let Some(equivocation_proof) = check_equivocation(
&*self.api,
&*self.client,
slot_now,
babe_pre_digest.slot_number(),
&header,
Expand Down Expand Up @@ -857,57 +852,50 @@ fn register_babe_inherent_data_provider(
/// it is missing.
///
/// The epoch change tree should be pruned as blocks are finalized.
pub struct BabeBlockImport<B, E, Block: BlockT, I, RA, PRA> {
pub struct BabeBlockImport<Block: BlockT, Client, I> {
inner: I,
client: Arc<Client<B, E, Block, RA>>,
api: Arc<PRA>,
client: Arc<Client>,
epoch_changes: SharedEpochChanges<Block, Epoch>,
config: Config,
}

impl<B, E, Block: BlockT, I: Clone, RA, PRA> Clone for BabeBlockImport<B, E, Block, I, RA, PRA> {
impl<Block: BlockT, I: Clone, Client> Clone for BabeBlockImport<Block, Client, I> {
fn clone(&self) -> Self {
BabeBlockImport {
inner: self.inner.clone(),
client: self.client.clone(),
api: self.api.clone(),
epoch_changes: self.epoch_changes.clone(),
config: self.config.clone(),
}
}
}

impl<B, E, Block: BlockT, I, RA, PRA> BabeBlockImport<B, E, Block, I, RA, PRA> {
impl<Block: BlockT, Client, I> BabeBlockImport<Block, Client, I> {
fn new(
client: Arc<Client<B, E, Block, RA>>,
api: Arc<PRA>,
client: Arc<Client>,
epoch_changes: SharedEpochChanges<Block, Epoch>,
block_import: I,
config: Config,
) -> Self {
BabeBlockImport {
client,
api,
inner: block_import,
epoch_changes,
config,
}
}
}

impl<B, E, Block, I, RA, PRA> BlockImport<Block> for BabeBlockImport<B, E, Block, I, RA, PRA> where
impl<Block, Client, Inner> BlockImport<Block> for BabeBlockImport<Block, Client, Inner> where
Block: BlockT,
I: BlockImport<Block, Transaction = sp_api::TransactionFor<PRA, Block>> + Send + Sync,
I::Error: Into<ConsensusError>,
B: Backend<Block> + 'static,
E: CallExecutor<Block> + 'static + Clone + Send + Sync,
Client<B, E, Block, RA>: AuxStore,
RA: Send + Sync,
PRA: ProvideRuntimeApi<Block> + ProvideCache<Block>,
PRA::Api: BabeApi<Block> + ApiExt<Block, StateBackend = B::State>,
Inner: BlockImport<Block, Transaction = sp_api::TransactionFor<Client, Block>> + Send + Sync,
Inner::Error: Into<ConsensusError>,
Client: HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>
+ AuxStore + ProvideRuntimeApi<Block> + ProvideCache<Block> + Send + Sync,
Client::Api: BabeApi<Block> + ApiExt<Block>,
{
type Error = ConsensusError;
type Transaction = sp_api::TransactionFor<PRA, Block>;
type Transaction = sp_api::TransactionFor<Client, Block>;

fn import_block(
&mut self,
Expand All @@ -931,7 +919,7 @@ impl<B, E, Block, I, RA, PRA> BlockImport<Block> for BabeBlockImport<B, E, Block
let slot_number = pre_digest.slot_number();

let parent_hash = *block.header.parent_hash();
let parent_header = self.client.header(&BlockId::Hash(parent_hash))
let parent_header = self.client.header(BlockId::Hash(parent_hash))
.map_err(|e| ConsensusError::ChainLookup(e.to_string()))?
.ok_or_else(|| ConsensusError::ChainLookup(babe_err(
Error::<Block>::ParentUnavailable(parent_hash, hash)
Expand Down Expand Up @@ -1003,7 +991,7 @@ impl<B, E, Block, I, RA, PRA> BlockImport<Block> for BabeBlockImport<B, E, Block
// this way we can revert it if there's any error
let mut old_epoch_changes = None;

let info = self.client.chain_info();
let info = self.client.info();

if let Some(next_epoch_descriptor) = next_epoch_digest {
let next_epoch = epoch.increment(next_epoch_descriptor);
Expand All @@ -1023,7 +1011,7 @@ impl<B, E, Block, I, RA, PRA> BlockImport<Block> for BabeBlockImport<B, E, Block
// imported.
let prune_and_import = || {
prune_finalized(
&self.client,
self.client.clone(),
&mut epoch_changes,
)?;

Expand Down Expand Up @@ -1109,19 +1097,17 @@ impl<B, E, Block, I, RA, PRA> BlockImport<Block> for BabeBlockImport<B, E, Block
}

/// Gets the best finalized block and its slot, and prunes the given epoch tree.
fn prune_finalized<B, E, Block, RA>(
client: &Client<B, E, Block, RA>,
fn prune_finalized<Block, Client>(
client: Arc<Client>,
epoch_changes: &mut EpochChangesFor<Block, Epoch>,
) -> Result<(), ConsensusError> where
Block: BlockT,
E: CallExecutor<Block> + Send + Sync,
B: Backend<Block>,
RA: Send + Sync,
Client: HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>,
{
let info = client.chain_info();
let info = client.info();

let finalized_slot = {
let finalized_header = client.header(&BlockId::Hash(info.finalized_hash))
let finalized_header = client.header(BlockId::Hash(info.finalized_hash))
.map_err(|e| ConsensusError::ClientImport(format!("{:?}", e)))?
.expect("best finalized hash was given by client; \
finalized headers must exist in db; qed");
Expand All @@ -1147,16 +1133,12 @@ fn prune_finalized<B, E, Block, RA>(
///
/// Also returns a link object used to correctly instantiate the import queue
/// and background worker.
pub fn block_import<B, E, Block: BlockT, I, RA, PRA>(
pub fn block_import<Client, Block: BlockT, I>(
config: Config,
wrapped_block_import: I,
client: Arc<Client<B, E, Block, RA>>,
api: Arc<PRA>,
) -> ClientResult<(BabeBlockImport<B, E, Block, I, RA, PRA>, BabeLink<Block>)> where
B: Backend<Block>,
E: CallExecutor<Block> + Send + Sync,
RA: Send + Sync,
Client<B, E, Block, RA>: AuxStore,
client: Arc<Client>,
) -> ClientResult<(BabeBlockImport<Block, Client, I>, BabeLink<Block>)> where
Client: AuxStore + HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>,
{
let epoch_changes = aux_schema::load_epoch_changes::<Block, _>(&*client)?;
let link = BabeLink {
Expand All @@ -1169,13 +1151,12 @@ pub fn block_import<B, E, Block: BlockT, I, RA, PRA>(
// epoch tree it is useful as a migration, so that nodes prune long trees on
// startup rather than waiting until importing the next epoch change block.
prune_finalized(
&client,
client.clone(),
&mut epoch_changes.lock(),
)?;

let import = BabeBlockImport::new(
client,
api,
epoch_changes,
wrapped_block_import,
config,
Expand All @@ -1193,28 +1174,24 @@ pub fn block_import<B, E, Block: BlockT, I, RA, PRA>(
///
/// The block import object provided must be the `BabeBlockImport` or a wrapper
/// of it, otherwise crucial import logic will be omitted.
pub fn import_queue<B, E, Block: BlockT, I, RA, PRA>(
pub fn import_queue<Block: BlockT, Client, Inner>(
babe_link: BabeLink<Block>,
block_import: I,
block_import: Inner,
justification_import: Option<BoxJustificationImport<Block>>,
finality_proof_import: Option<BoxFinalityProofImport<Block>>,
client: Arc<Client<B, E, Block, RA>>,
api: Arc<PRA>,
client: Arc<Client>,
inherent_data_providers: InherentDataProviders,
) -> ClientResult<BabeImportQueue<Block, sp_api::TransactionFor<PRA, Block>>> where
B: Backend<Block> + 'static,
I: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<PRA, Block>>
) -> ClientResult<BabeImportQueue<Block, sp_api::TransactionFor<Client, Block>>> where
Inner: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<Client, Block>>
+ Send + Sync + 'static,
E: CallExecutor<Block> + Clone + Send + Sync + 'static,
RA: Send + Sync + 'static,
PRA: ProvideRuntimeApi<Block> + ProvideCache<Block> + Send + Sync + AuxStore + 'static,
PRA::Api: BlockBuilderApi<Block> + BabeApi<Block> + ApiExt<Block, Error = sp_blockchain::Error>,
Client: ProvideRuntimeApi<Block> + ProvideCache<Block> + Send + Sync + AuxStore + 'static,
Client: HeaderBackend<Block> + HeaderMetadata<Block, Error = sp_blockchain::Error>,
Client::Api: BlockBuilderApi<Block> + BabeApi<Block> + ApiExt<Block, Error = sp_blockchain::Error>,
{
register_babe_inherent_data_provider(&inherent_data_providers, babe_link.config.slot_duration)?;

let verifier = BabeVerifier {
client: client.clone(),
api,
inherent_data_providers,
config: babe_link.config,
epoch_changes: babe_link.epoch_changes,
Expand Down
10 changes: 1 addition & 9 deletions client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,7 @@ type TestHeader = <TestBlock as BlockT>::Header;
type TestExtrinsic = <TestBlock as BlockT>::Extrinsic;

pub struct TestVerifier {
inner: BabeVerifier<
substrate_test_runtime_client::Backend,
substrate_test_runtime_client::Executor,
TestBlock,
substrate_test_runtime_client::runtime::RuntimeApi,
PeersFullClient,
>,
inner: BabeVerifier<TestBlock, PeersFullClient>,
mutator: Mutator,
}

Expand Down Expand Up @@ -271,7 +265,6 @@ impl TestNetFactory for BabeTestNet {
config,
client.clone(),
client.clone(),
client.clone(),
).expect("can initialize block-import");

let block_import = PanickingBlockImport(block_import);
Expand Down Expand Up @@ -305,7 +298,6 @@ impl TestNetFactory for BabeTestNet {
TestVerifier {
inner: BabeVerifier {
client: client.clone(),
api: client,
inherent_data_providers: data.inherent_data_providers.clone(),
config: data.link.config.clone(),
epoch_changes: data.link.epoch_changes.clone(),
Expand Down

0 comments on commit 0157730

Please sign in to comment.