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

Commit 558e6d9

Browse files
authored
Async block import params (#10488)
* Make `SimpleSlotWorker::block_import_params()` return function that returns a future * Simplify `SimpleSlotWorker::block_import_params()` to just async method
1 parent 7f68a8b commit 558e6d9

File tree

3 files changed

+107
-129
lines changed

3 files changed

+107
-129
lines changed

client/consensus/aura/src/lib.rs

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -382,58 +382,51 @@ where
382382
vec![<DigestItem as CompatibleDigestItem<P::Signature>>::aura_pre_digest(slot)]
383383
}
384384

385-
fn block_import_params(
385+
async fn block_import_params(
386386
&self,
387-
) -> Box<
388-
dyn Fn(
389-
B::Header,
390-
&B::Hash,
391-
Vec<B::Extrinsic>,
392-
StorageChanges<sp_api::TransactionFor<C, B>, B>,
393-
Self::Claim,
394-
Self::EpochData,
395-
) -> Result<
396-
sc_consensus::BlockImportParams<B, sp_api::TransactionFor<C, B>>,
397-
sp_consensus::Error,
398-
> + Send
399-
+ 'static,
387+
header: B::Header,
388+
header_hash: &B::Hash,
389+
body: Vec<B::Extrinsic>,
390+
storage_changes: StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
391+
public: Self::Claim,
392+
_epoch: Self::EpochData,
393+
) -> Result<
394+
sc_consensus::BlockImportParams<B, <Self::BlockImport as BlockImport<B>>::Transaction>,
395+
sp_consensus::Error,
400396
> {
401-
let keystore = self.keystore.clone();
402-
Box::new(move |header, header_hash, body, storage_changes, public, _epoch| {
403-
// sign the pre-sealed hash of the block and then
404-
// add it to a digest item.
405-
let public_type_pair = public.to_public_crypto_pair();
406-
let public = public.to_raw_vec();
407-
let signature = SyncCryptoStore::sign_with(
408-
&*keystore,
409-
<AuthorityId<P> as AppKey>::ID,
410-
&public_type_pair,
411-
header_hash.as_ref(),
397+
// sign the pre-sealed hash of the block and then
398+
// add it to a digest item.
399+
let public_type_pair = public.to_public_crypto_pair();
400+
let public = public.to_raw_vec();
401+
let signature = SyncCryptoStore::sign_with(
402+
&*self.keystore,
403+
<AuthorityId<P> as AppKey>::ID,
404+
&public_type_pair,
405+
header_hash.as_ref(),
406+
)
407+
.map_err(|e| sp_consensus::Error::CannotSign(public.clone(), e.to_string()))?
408+
.ok_or_else(|| {
409+
sp_consensus::Error::CannotSign(
410+
public.clone(),
411+
"Could not find key in keystore.".into(),
412412
)
413-
.map_err(|e| sp_consensus::Error::CannotSign(public.clone(), e.to_string()))?
414-
.ok_or_else(|| {
415-
sp_consensus::Error::CannotSign(
416-
public.clone(),
417-
"Could not find key in keystore.".into(),
418-
)
419-
})?;
420-
let signature = signature
421-
.clone()
422-
.try_into()
423-
.map_err(|_| sp_consensus::Error::InvalidSignature(signature, public))?;
424-
425-
let signature_digest_item =
426-
<DigestItem as CompatibleDigestItem<P::Signature>>::aura_seal(signature);
427-
428-
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
429-
import_block.post_digests.push(signature_digest_item);
430-
import_block.body = Some(body);
431-
import_block.state_action =
432-
StateAction::ApplyChanges(sc_consensus::StorageChanges::Changes(storage_changes));
433-
import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
434-
435-
Ok(import_block)
436-
})
413+
})?;
414+
let signature = signature
415+
.clone()
416+
.try_into()
417+
.map_err(|_| sp_consensus::Error::InvalidSignature(signature, public))?;
418+
419+
let signature_digest_item =
420+
<DigestItem as CompatibleDigestItem<P::Signature>>::aura_seal(signature);
421+
422+
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
423+
import_block.post_digests.push(signature_digest_item);
424+
import_block.body = Some(body);
425+
import_block.state_action =
426+
StateAction::ApplyChanges(sc_consensus::StorageChanges::Changes(storage_changes));
427+
import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain);
428+
429+
Ok(import_block)
437430
}
438431

439432
fn force_authoring(&self) -> bool {

client/consensus/babe/src/lib.rs

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -772,60 +772,52 @@ where
772772
vec![<DigestItem as CompatibleDigestItem>::babe_pre_digest(claim.0.clone())]
773773
}
774774

775-
fn block_import_params(
775+
async fn block_import_params(
776776
&self,
777-
) -> Box<
778-
dyn Fn(
779-
B::Header,
780-
&B::Hash,
781-
Vec<B::Extrinsic>,
782-
StorageChanges<I::Transaction, B>,
783-
Self::Claim,
784-
Self::EpochData,
785-
) -> Result<sc_consensus::BlockImportParams<B, I::Transaction>, sp_consensus::Error>
786-
+ Send
787-
+ 'static,
777+
header: B::Header,
778+
header_hash: &B::Hash,
779+
body: Vec<B::Extrinsic>,
780+
storage_changes: StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
781+
(_, public): Self::Claim,
782+
epoch_descriptor: Self::EpochData,
783+
) -> Result<
784+
sc_consensus::BlockImportParams<B, <Self::BlockImport as BlockImport<B>>::Transaction>,
785+
sp_consensus::Error,
788786
> {
789-
let keystore = self.keystore.clone();
790-
Box::new(
791-
move |header, header_hash, body, storage_changes, (_, public), epoch_descriptor| {
792-
// sign the pre-sealed hash of the block and then
793-
// add it to a digest item.
794-
let public_type_pair = public.clone().into();
795-
let public = public.to_raw_vec();
796-
let signature = SyncCryptoStore::sign_with(
797-
&*keystore,
798-
<AuthorityId as AppKey>::ID,
799-
&public_type_pair,
800-
header_hash.as_ref(),
801-
)
802-
.map_err(|e| sp_consensus::Error::CannotSign(public.clone(), e.to_string()))?
803-
.ok_or_else(|| {
804-
sp_consensus::Error::CannotSign(
805-
public.clone(),
806-
"Could not find key in keystore.".into(),
807-
)
808-
})?;
809-
let signature: AuthoritySignature = signature
810-
.clone()
811-
.try_into()
812-
.map_err(|_| sp_consensus::Error::InvalidSignature(signature, public))?;
813-
let digest_item = <DigestItem as CompatibleDigestItem>::babe_seal(signature.into());
814-
815-
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
816-
import_block.post_digests.push(digest_item);
817-
import_block.body = Some(body);
818-
import_block.state_action = StateAction::ApplyChanges(
819-
sc_consensus::StorageChanges::Changes(storage_changes),
820-
);
821-
import_block.intermediates.insert(
822-
Cow::from(INTERMEDIATE_KEY),
823-
Box::new(BabeIntermediate::<B> { epoch_descriptor }) as Box<_>,
824-
);
825-
826-
Ok(import_block)
827-
},
787+
// sign the pre-sealed hash of the block and then
788+
// add it to a digest item.
789+
let public_type_pair = public.clone().into();
790+
let public = public.to_raw_vec();
791+
let signature = SyncCryptoStore::sign_with(
792+
&*self.keystore,
793+
<AuthorityId as AppKey>::ID,
794+
&public_type_pair,
795+
header_hash.as_ref(),
828796
)
797+
.map_err(|e| sp_consensus::Error::CannotSign(public.clone(), e.to_string()))?
798+
.ok_or_else(|| {
799+
sp_consensus::Error::CannotSign(
800+
public.clone(),
801+
"Could not find key in keystore.".into(),
802+
)
803+
})?;
804+
let signature: AuthoritySignature = signature
805+
.clone()
806+
.try_into()
807+
.map_err(|_| sp_consensus::Error::InvalidSignature(signature, public))?;
808+
let digest_item = <DigestItem as CompatibleDigestItem>::babe_seal(signature.into());
809+
810+
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
811+
import_block.post_digests.push(digest_item);
812+
import_block.body = Some(body);
813+
import_block.state_action =
814+
StateAction::ApplyChanges(sc_consensus::StorageChanges::Changes(storage_changes));
815+
import_block.intermediates.insert(
816+
Cow::from(INTERMEDIATE_KEY),
817+
Box::new(BabeIntermediate::<B> { epoch_descriptor }) as Box<_>,
818+
);
819+
820+
Ok(import_block)
829821
}
830822

831823
fn force_authoring(&self) -> bool {

client/consensus/slots/src/lib.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,17 @@ pub trait SimpleSlotWorker<B: BlockT> {
144144
fn pre_digest_data(&self, slot: Slot, claim: &Self::Claim) -> Vec<sp_runtime::DigestItem>;
145145

146146
/// Returns a function which produces a `BlockImportParams`.
147-
fn block_import_params(
147+
async fn block_import_params(
148148
&self,
149-
) -> Box<
150-
dyn Fn(
151-
B::Header,
152-
&B::Hash,
153-
Vec<B::Extrinsic>,
154-
StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
155-
Self::Claim,
156-
Self::EpochData,
157-
) -> Result<
158-
sc_consensus::BlockImportParams<
159-
B,
160-
<Self::BlockImport as BlockImport<B>>::Transaction,
161-
>,
162-
sp_consensus::Error,
163-
> + Send
164-
+ 'static,
149+
header: B::Header,
150+
header_hash: &B::Hash,
151+
body: Vec<B::Extrinsic>,
152+
storage_changes: StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
153+
public: Self::Claim,
154+
epoch: Self::EpochData,
155+
) -> Result<
156+
sc_consensus::BlockImportParams<B, <Self::BlockImport as BlockImport<B>>::Transaction>,
157+
sp_consensus::Error,
165158
>;
166159

167160
/// Whether to force authoring if offline.
@@ -342,23 +335,23 @@ pub trait SimpleSlotWorker<B: BlockT> {
342335
},
343336
};
344337

345-
let block_import_params_maker = self.block_import_params();
346-
let block_import = self.block_import();
347-
348338
let (block, storage_proof) = (proposal.block, proposal.proof);
349339
let (header, body) = block.deconstruct();
350340
let header_num = *header.number();
351341
let header_hash = header.hash();
352342
let parent_hash = *header.parent_hash();
353343

354-
let block_import_params = match block_import_params_maker(
355-
header,
356-
&header_hash,
357-
body.clone(),
358-
proposal.storage_changes,
359-
claim,
360-
epoch_data,
361-
) {
344+
let block_import_params = match self
345+
.block_import_params(
346+
header,
347+
&header_hash,
348+
body.clone(),
349+
proposal.storage_changes,
350+
claim,
351+
epoch_data,
352+
)
353+
.await
354+
{
362355
Ok(bi) => bi,
363356
Err(err) => {
364357
warn!(target: logging_target, "Failed to create block import params: {:?}", err);
@@ -385,7 +378,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
385378
);
386379

387380
let header = block_import_params.post_header();
388-
match block_import.import_block(block_import_params, Default::default()).await {
381+
match self.block_import().import_block(block_import_params, Default::default()).await {
389382
Ok(res) => {
390383
res.handle_justification(
391384
&header.hash(),

0 commit comments

Comments
 (0)