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_basic_authorship (#5050)
Browse files Browse the repository at this point in the history
* removes use of sc-client from sc-basic-authorship

* refactor use of ProposerFactory

* correct dep path
  • Loading branch information
seunlanlege authored Feb 27, 2020
1 parent 06fae63 commit 8c22368
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 112 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ pub fn new_full(config: Configuration<GenesisConfig>)
.build()?;

if participates_in_consensus {
let proposer = sc_basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
let proposer = sc_basic_authorship::ProposerFactory::new(
service.client(),
service.transaction_pool()
);

let client = service.client();
let select_chain = service.select_chain()
Expand Down
16 changes: 8 additions & 8 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ macro_rules! new_full {
($with_startup_data)(&block_import, &babe_link);

if participates_in_consensus {
let proposer = sc_basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
let proposer = sc_basic_authorship::ProposerFactory::new(
service.client(),
service.transaction_pool()
);

let client = service.client();
let select_chain = service.select_chain()
Expand Down Expand Up @@ -501,10 +501,10 @@ mod tests {
let parent_header = service.client().header(&parent_id).unwrap().unwrap();
let parent_hash = parent_header.hash();
let parent_number = *parent_header.number();
let mut proposer_factory = sc_basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
let mut proposer_factory = sc_basic_authorship::ProposerFactory::new(
service.client(),
service.transaction_pool()
);

let epoch = babe_link.epoch_changes().lock().epoch_for_child_of(
descendent_query(&*service.client()),
Expand Down
1 change: 0 additions & 1 deletion client/basic-authorship/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ sp-api = { version = "2.0.0-alpha.2", path = "../../primitives/api" }
sp-runtime = { version = "2.0.0-alpha.2", path = "../../primitives/runtime" }
sp-core = { version = "2.0.0-alpha.2", path = "../../primitives/core" }
sp-blockchain = { version = "2.0.0-alpha.2", path = "../../primitives/blockchain" }
sc-client = { version = "0.8.0-alpha.2", path = "../" }
sc-client-api = { version = "2.0.0-alpha.2", path = "../api" }
sp-consensus = { version = "0.8.0-alpha.2", path = "../../primitives/consensus/common" }
sp-inherents = { version = "2.0.0-alpha.2", path = "../../primitives/inherents" }
Expand Down
106 changes: 54 additions & 52 deletions client/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// FIXME #1021 move this into sp-consensus

use std::{time, sync::Arc};
use sc_client_api::{CallExecutor, backend};
use sc_client::Client as SubstrateClient;
use sc_client_api::backend;
use codec::Decode;
use sp_consensus::{evaluation, Proposal, RecordProof};
use sp_inherents::InherentData;
Expand All @@ -32,35 +31,47 @@ use sp_runtime::{
};
use sp_transaction_pool::{TransactionPool, InPoolTransaction};
use sc_telemetry::{telemetry, CONSENSUS_INFO};
use sc_block_builder::BlockBuilderApi;
use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider};
use sp_api::{ProvideRuntimeApi, ApiExt};
use futures::prelude::*;
use sp_blockchain::HeaderBackend;
use std::marker::PhantomData;

/// Proposer factory.
pub struct ProposerFactory<C, A> where A: TransactionPool {
pub struct ProposerFactory<A, B, C> {
/// The client instance.
pub client: Arc<C>,
client: Arc<C>,
/// The transaction pool.
pub transaction_pool: Arc<A>,
transaction_pool: Arc<A>,
/// phantom member to pin the `Backend` type.
_phantom: PhantomData<B>,
}

impl<A, B, C> ProposerFactory<A, B, C> {
pub fn new(client: Arc<C>, transaction_pool: Arc<A>) -> Self {
ProposerFactory {
client,
transaction_pool,
_phantom: PhantomData,
}
}
}

impl<B, E, Block, RA, A> ProposerFactory<SubstrateClient<B, E, Block, RA>, A>
impl<B, Block, C, A> ProposerFactory<A, B, C>
where
A: TransactionPool<Block = Block> + 'static,
B: backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + Clone + 'static,
Block: BlockT,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
BlockBuilderApi<Block, Error = sp_blockchain::Error> +
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
pub fn init_with_now(
&mut self,
parent_header: &<Block as BlockT>::Header,
now: Box<dyn Fn() -> time::Instant + Send + Sync>,
) -> Proposer<Block, SubstrateClient<B, E, Block, RA>, A> {
) -> Proposer<B, Block, C, A> {
let parent_hash = parent_header.hash();

let id = BlockId::hash(parent_hash);
Expand All @@ -75,28 +86,27 @@ impl<B, E, Block, RA, A> ProposerFactory<SubstrateClient<B, E, Block, RA>, A>
parent_number: *parent_header.number(),
transaction_pool: self.transaction_pool.clone(),
now,
_phantom: PhantomData,
}),
};

proposer
}
}

impl<B, E, Block, RA, A> sp_consensus::Environment<Block> for
ProposerFactory<SubstrateClient<B, E, Block, RA>, A>
impl<A, B, Block, C> sp_consensus::Environment<Block> for
ProposerFactory<A, B, C>
where
A: TransactionPool<Block = Block> + 'static,
B: backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + Clone + 'static,
Block: BlockT,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
BlockBuilderApi<Block, Error = sp_blockchain::Error> +
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
type CreateProposer = future::Ready<Result<Self::Proposer, Self::Error>>;
type Proposer = Proposer<Block, SubstrateClient<B, E, Block, RA>, A>;
type Proposer = Proposer<B, Block, C, A>;
type Error = sp_blockchain::Error;

fn init(
Expand All @@ -108,32 +118,31 @@ impl<B, E, Block, RA, A> sp_consensus::Environment<Block> for
}

/// The proposer logic.
pub struct Proposer<Block: BlockT, C, A: TransactionPool> {
inner: Arc<ProposerInner<Block, C, A>>,
pub struct Proposer<B, Block: BlockT, C, A: TransactionPool> {
inner: Arc<ProposerInner<B, Block, C, A>>,
}

/// Proposer inner, to wrap parameters under Arc.
struct ProposerInner<Block: BlockT, C, A: TransactionPool> {
struct ProposerInner<B, Block: BlockT, C, A: TransactionPool> {
client: Arc<C>,
parent_hash: <Block as BlockT>::Hash,
parent_id: BlockId<Block>,
parent_number: <<Block as BlockT>::Header as HeaderT>::Number,
transaction_pool: Arc<A>,
now: Box<dyn Fn() -> time::Instant + Send + Sync>,
_phantom: PhantomData<B>,
}

impl<B, E, Block, RA, A> sp_consensus::Proposer<Block> for
Proposer<Block, SubstrateClient<B, E, Block, RA>, A>
impl<A, B, Block, C> sp_consensus::Proposer<Block> for
Proposer<B, Block, C, A>
where
A: TransactionPool<Block = Block> + 'static,
B: backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + Clone + 'static,
Block: BlockT,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
BlockBuilderApi<Block, Error = sp_blockchain::Error> +
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
type Transaction = backend::TransactionFor<B, Block>;
type Proposal = tokio_executor::blocking::Blocking<
Expand All @@ -157,16 +166,15 @@ impl<B, E, Block, RA, A> sp_consensus::Proposer<Block> for
}
}

impl<Block, B, E, RA, A> ProposerInner<Block, SubstrateClient<B, E, Block, RA>, A> where
A: TransactionPool<Block = Block>,
B: sc_client_api::backend::Backend<Block> + Send + Sync + 'static,
E: CallExecutor<Block> + Send + Sync + Clone + 'static,
Block: BlockT,
RA: Send + Sync + 'static,
SubstrateClient<B, E, Block, RA>: ProvideRuntimeApi<Block>,
<SubstrateClient<B, E, Block, RA> as ProvideRuntimeApi<Block>>::Api:
BlockBuilderApi<Block, Error = sp_blockchain::Error> +
ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>,
impl<A, B, Block, C> ProposerInner<B, Block, C, A>
where
A: TransactionPool<Block = Block>,
B: backend::Backend<Block> + Send + Sync + 'static,
Block: BlockT,
C: BlockBuilderProvider<B, Block, C> + HeaderBackend<Block> + ProvideRuntimeApi<Block>
+ Send + Sync + 'static,
C::Api: ApiExt<Block, StateBackend = backend::StateBackendFor<B, Block>>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
fn propose_with(
&self,
Expand Down Expand Up @@ -315,10 +323,7 @@ mod tests {
txpool.submit_at(&BlockId::number(0), vec![extrinsic(0), extrinsic(1)])
).unwrap();

let mut proposer_factory = ProposerFactory {
client: client.clone(),
transaction_pool: txpool.clone(),
};
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone());

let cell = Mutex::new(time::Instant::now());
let mut proposer = proposer_factory.init_with_now(
Expand Down Expand Up @@ -359,10 +364,7 @@ mod tests {
txpool.submit_at(&BlockId::number(0), vec![extrinsic(0)]),
).unwrap();

let mut proposer_factory = ProposerFactory {
client: client.clone(),
transaction_pool: txpool.clone(),
};
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone());

let mut proposer = proposer_factory.init_with_now(
&client.header(&block_id).unwrap().unwrap(),
Expand Down
5 changes: 1 addition & 4 deletions client/basic-authorship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
//! # let client = Arc::new(substrate_test_runtime_client::new());
//! # let txpool = Arc::new(BasicPool::new(Default::default(), Arc::new(FullChainApi::new(client.clone()))).0);
//! // The first step is to create a `ProposerFactory`.
//! let mut proposer_factory = ProposerFactory {
//! client: client.clone(),
//! transaction_pool: txpool.clone(),
//! };
//! let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone());
//!
//! // From this factory, we create a `Proposer`.
//! let proposer = proposer_factory.init(
Expand Down
21 changes: 21 additions & 0 deletions client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ impl<Block: BlockT, StateBackend: backend::StateBackend<HasherFor<Block>>> Built
}
}

/// Block builder provider
pub trait BlockBuilderProvider<B, Block, RA>
where
Block: BlockT,
B: backend::Backend<Block>,
Self: Sized,
RA: ProvideRuntimeApi<Block>,
{
/// Create a new block, built on top of `parent`.
///
/// When proof recording is enabled, all accessed trie nodes are saved.
/// These recorded trie nodes can be used by a third party to proof the
/// output of this block builder without having access to the full storage.
fn new_block_at<R: Into<RecordProof>>(
&self,
parent: &BlockId<Block>,
inherent_digests: DigestFor<Block>,
record_proof: R,
) -> sp_blockchain::Result<BlockBuilder<Block, RA, B>>;
}

/// Utility for building new (valid) blocks from a stream of extrinsics.
pub struct BlockBuilder<'a, Block: BlockT, A: ProvideRuntimeApi<Block>, B> {
extrinsics: Vec<Block::Extrinsic>,
Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use super::*;
use authorship::claim_slot;

use sp_consensus_babe::{AuthorityPair, SlotNumber};
use sc_block_builder::BlockBuilder;
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
use sp_consensus::{
NoNetwork as DummyOracle, Proposal, RecordProof,
import_queue::{BoxBlockImport, BoxJustificationImport, BoxFinalityProofImport},
Expand Down
24 changes: 12 additions & 12 deletions client/consensus/manual-seal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ mod tests {
let select_chain = LongestChain::new(backend.clone());
let inherent_data_providers = InherentDataProviders::new();
let pool = Arc::new(BasicPool::new(Options::default(), api()).0);
let env = ProposerFactory {
transaction_pool: pool.clone(),
client: client.clone(),
};
let env = ProposerFactory::new(
client.clone(),
pool.clone()
);
// this test checks that blocks are created as soon as transactions are imported into the pool.
let (sender, receiver) = futures::channel::oneshot::channel();
let mut sender = Arc::new(Some(sender));
Expand Down Expand Up @@ -309,10 +309,10 @@ mod tests {
let select_chain = LongestChain::new(backend.clone());
let inherent_data_providers = InherentDataProviders::new();
let pool = Arc::new(BasicPool::new(Options::default(), api()).0);
let env = ProposerFactory {
transaction_pool: pool.clone(),
client: client.clone(),
};
let env = ProposerFactory::new(
client.clone(),
pool.clone()
);
// this test checks that blocks are created as soon as an engine command is sent over the stream.
let (mut sink, stream) = futures::channel::mpsc::channel(1024);
let future = run_manual_seal(
Expand Down Expand Up @@ -378,10 +378,10 @@ mod tests {
let inherent_data_providers = InherentDataProviders::new();
let pool_api = api();
let pool = Arc::new(BasicPool::new(Options::default(), pool_api.clone()).0);
let env = ProposerFactory {
transaction_pool: pool.clone(),
client: client.clone(),
};
let env = ProposerFactory::new(
client.clone(),
pool.clone(),
);
// this test checks that blocks are created as soon as an engine command is sent over the stream.
let (mut sink, stream) = futures::channel::mpsc::channel(1024);
let future = run_manual_seal(
Expand Down
1 change: 1 addition & 0 deletions client/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sc-network = { version = "0.8.0-alpha.2", path = "../network" }
sc-network-gossip = { version = "0.8.0-alpha.2", path = "../network-gossip" }
sp-finality-tracker = { version = "2.0.0-alpha.2", path = "../../primitives/finality-tracker" }
sp-finality-grandpa = { version = "2.0.0-alpha.2", path = "../../primitives/finality-grandpa" }
sc-block-builder = { version = "0.8.0-alpha.2", path = "../block-builder" }
finality-grandpa = { version = "0.11.1", features = ["derive-codec"] }
pin-project = "0.4.6"

Expand Down
1 change: 1 addition & 0 deletions client/finality-grandpa/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use finality_proof::{
FinalityProofProvider, AuthoritySetForFinalityProver, AuthoritySetForFinalityChecker,
};
use consensus_changes::ConsensusChanges;
use sc_block_builder::BlockBuilderProvider;

type PeerData =
Mutex<
Expand Down
2 changes: 1 addition & 1 deletion client/network/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sc_client_api::{
FinalityNotification,
backend::{TransactionFor, AuxStore, Backend, Finalizer},
};
use sc_block_builder::BlockBuilder;
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
use sc_client::LongestChain;
use sc_network::config::Roles;
use sp_consensus::block_validation::DefaultBlockAnnounceValidator;
Expand Down
Loading

0 comments on commit 8c22368

Please sign in to comment.