Make Proposer consume its reference on propose (#6190)

* Make `Proposer` consume its reference on `propose`

A proposer must be created per new round, so it makes sense to have the
proposer consume its own reference.

* Remove `ProposerInner`
This commit is contained in:
Bastian Köcher
2020-05-29 18:50:56 +02:00
committed by GitHub
parent 78a72c12d7
commit 841aab512f
8 changed files with 24 additions and 32 deletions
@@ -91,16 +91,14 @@ impl<B, Block, C, A> ProposerFactory<A, B, C>
info!("🙌 Starting consensus session on top of parent {:?}", parent_hash);
let proposer = Proposer {
inner: Arc::new(ProposerInner {
client: self.client.clone(),
parent_hash,
parent_id: id,
parent_number: *parent_header.number(),
transaction_pool: self.transaction_pool.clone(),
now,
metrics: self.metrics.clone(),
_phantom: PhantomData,
}),
client: self.client.clone(),
parent_hash,
parent_id: id,
parent_number: *parent_header.number(),
transaction_pool: self.transaction_pool.clone(),
now,
metrics: self.metrics.clone(),
_phantom: PhantomData,
};
proposer
@@ -132,11 +130,6 @@ impl<A, B, Block, C> sp_consensus::Environment<Block> for
/// The proposer logic.
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<B, Block: BlockT, C, A: TransactionPool> {
client: Arc<C>,
parent_hash: <Block as BlockT>::Hash,
parent_id: BlockId<Block>,
@@ -165,22 +158,21 @@ impl<A, B, Block, C> sp_consensus::Proposer<Block> for
type Error = sp_blockchain::Error;
fn propose(
&mut self,
self,
inherent_data: InherentData,
inherent_digests: DigestFor<Block>,
max_duration: time::Duration,
record_proof: RecordProof,
) -> Self::Proposal {
let inner = self.inner.clone();
tokio_executor::blocking::run(move || {
// leave some time for evaluation and block finalization (33%)
let deadline = (inner.now)() + max_duration - max_duration / 3;
inner.propose_with(inherent_data, inherent_digests, deadline, record_proof)
let deadline = (self.now)() + max_duration - max_duration / 3;
self.propose_with(inherent_data, inherent_digests, deadline, record_proof)
})
}
}
impl<A, B, Block, C> ProposerInner<B, Block, C, A>
impl<A, B, Block, C> Proposer<B, Block, C, A>
where
A: TransactionPool<Block = Block>,
B: backend::Backend<Block> + Send + Sync + 'static,
@@ -191,7 +183,7 @@ impl<A, B, Block, C> ProposerInner<B, Block, C, A>
+ BlockBuilderApi<Block, Error = sp_blockchain::Error>,
{
fn propose_with(
&self,
self,
inherent_data: InherentData,
inherent_digests: DigestFor<Block>,
deadline: time::Instant,
@@ -399,7 +391,7 @@ mod tests {
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone(), None);
let cell = Mutex::new((false, time::Instant::now()));
let mut proposer = proposer_factory.init_with_now(
let proposer = proposer_factory.init_with_now(
&client.header(&BlockId::number(0)).unwrap().unwrap(),
Box::new(move || {
let mut value = cell.lock();
@@ -440,7 +432,7 @@ mod tests {
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone(), None);
let cell = Mutex::new((false, time::Instant::now()));
let mut proposer = proposer_factory.init_with_now(
let proposer = proposer_factory.init_with_now(
&client.header(&BlockId::number(0)).unwrap().unwrap(),
Box::new(move || {
let mut value = cell.lock();
@@ -489,7 +481,7 @@ mod tests {
let mut proposer_factory = ProposerFactory::new(client.clone(), txpool.clone(), None);
let mut proposer = proposer_factory.init_with_now(
let proposer = proposer_factory.init_with_now(
&client.header(&block_id).unwrap().unwrap(),
Box::new(move || time::Instant::now()),
);
@@ -560,7 +552,7 @@ mod tests {
expected_block_extrinsics,
expected_pool_transactions,
| {
let mut proposer = proposer_factory.init_with_now(
let proposer = proposer_factory.init_with_now(
&client.header(&BlockId::number(number)).unwrap().unwrap(),
Box::new(move || time::Instant::now()),
);
+1 -1
View File
@@ -38,7 +38,7 @@
//! );
//!
//! // The proposer is created asynchronously.
//! let mut proposer = futures::executor::block_on(proposer).unwrap();
//! let proposer = futures::executor::block_on(proposer).unwrap();
//!
//! // This `Proposer` allows us to create a block proposition.
//! // The proposer will grab transactions from the transaction pool, and put them into the block.
+1 -1
View File
@@ -904,7 +904,7 @@ mod tests {
type Proposal = future::Ready<Result<Proposal<TestBlock, Self::Transaction>, Error>>;
fn propose(
&mut self,
self,
_: InherentData,
digests: DigestFor<TestBlock>,
_: Duration,
+1 -1
View File
@@ -159,7 +159,7 @@ impl Proposer<TestBlock> for DummyProposer {
type Proposal = future::Ready<Result<Proposal<TestBlock, Self::Transaction>, Error>>;
fn propose(
&mut self,
mut self,
_: InherentData,
pre_digests: DigestFor<TestBlock>,
_: Duration,
@@ -108,7 +108,7 @@ pub async fn seal_new_block<B, SC, HB, E, T, P>(
None => select_chain.best_chain()?
};
let mut proposer = env.init(&header)
let proposer = env.init(&header)
.map_err(|err| Error::StringError(format!("{}", err))).await?;
let id = inherent_data_provider.create_inherent_data()?;
let inherents_len = id.len();
+1 -1
View File
@@ -610,7 +610,7 @@ fn mine_loop<B: BlockT, C, Algorithm, E, SO, S, CAW>(
continue 'outer
}
let mut proposer = futures::executor::block_on(env.init(&best_header))
let proposer = futures::executor::block_on(env.init(&best_header))
.map_err(|e| Error::Environment(format!("{:?}", e)))?;
let inherent_data = inherent_data_providers
+1 -1
View File
@@ -239,7 +239,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
let logs = self.pre_digest_data(slot_number, &claim);
// deadline our production to approx. the end of the slot
let proposing = awaiting_proposer.and_then(move |mut proposer| proposer.propose(
let proposing = awaiting_proposer.and_then(move |proposer| proposer.propose(
slot_info.inherent_data,
sp_runtime::generic::Digest {
logs,
@@ -157,7 +157,7 @@ pub trait Proposer<B: BlockT> {
///
/// Returns a future that resolves to a [`Proposal`] or to [`Error`].
fn propose(
&mut self,
self,
inherent_data: InherentData,
inherent_digests: DigestFor<B>,
max_duration: Duration,