@@ -21,8 +21,8 @@ use std::thread;
21
21
22
22
use log:: { error, info, trace, warn} ;
23
23
use sp_blockchain:: { Result as ClientResult } ;
24
- use sp_runtime:: traits:: { Header as HeaderT , ProvideRuntimeApi } ;
25
- use sp_api:: { ApiExt , ApiErrorFor } ;
24
+ use sp_runtime:: traits:: { Header as HeaderT , ProvideRuntimeApi , Block as BlockT } ;
25
+ use sp_api:: ApiExt ;
26
26
use client:: {
27
27
BlockchainEvents , BlockBody ,
28
28
blockchain:: ProvideCache ,
@@ -206,30 +206,25 @@ where
206
206
}
207
207
208
208
209
- fn fetch_candidates < P > ( client : & P , block : & BlockId , parent : & BlockId )
210
- -> ClientResult < Option < impl Iterator < Item = CandidateReceipt > > >
209
+ fn fetch_candidates < P > ( client : & P , extrinsics : Vec < < Block as BlockT > :: Extrinsic > , parent : & BlockId )
210
+ -> ClientResult < Option < Vec < CandidateReceipt > > >
211
211
where
212
- P : BlockBody < Block > + ProvideRuntimeApi ,
213
- P :: Api : ParachainHost < Block > + ApiExt < Block , Error = sp_blockchain:: Error > ,
212
+ P : ProvideRuntimeApi ,
213
+ P :: Api : ParachainHost < Block , Error = sp_blockchain:: Error > ,
214
214
{
215
- let extrinsics = client. block_body ( block) ?;
216
- Ok ( match extrinsics {
217
- Some ( extrinsics) => {
218
- let api = client. runtime_api ( ) ;
219
-
220
- if api. has_api_with :: < dyn ParachainHost < Block , Error = ApiErrorFor < P , Block > > , _ > (
221
- parent,
222
- |version| version >= 2 ,
223
- ) . map_err ( |_| ConsensusError :: ChainLookup ( "outdated runtime API" . into ( ) ) ) ? {
224
- api. get_heads ( & parent, extrinsics)
225
- . map_err ( |_| ConsensusError :: ChainLookup ( "" . into ( ) ) ) ?
226
- . map ( |v| v. into_iter ( ) )
227
- } else {
228
- None
229
- }
230
- }
231
- None => None ,
232
- } )
215
+ let api = client. runtime_api ( ) ;
216
+
217
+ let candidates = if api. has_api_with :: < dyn ParachainHost < Block , Error = ( ) > , _ > (
218
+ parent,
219
+ |version| version >= 2 ,
220
+ ) . map_err ( |e| ConsensusError :: ChainLookup ( e. to_string ( ) ) ) ? {
221
+ api. get_heads ( & parent, extrinsics)
222
+ . map_err ( |e| ConsensusError :: ChainLookup ( e. to_string ( ) ) ) ?
223
+ } else {
224
+ None
225
+ } ;
226
+
227
+ Ok ( candidates)
233
228
}
234
229
235
230
/// Creates a task to prune entries in availability store upon block finalization.
@@ -244,12 +239,33 @@ where
244
239
while let Some ( notification) = finality_notification_stream. next ( ) . await {
245
240
let hash = notification. hash ;
246
241
let parent_hash = notification. header . parent_hash ;
242
+ let extrinsics = match client. block_body ( & BlockId :: hash ( hash) ) {
243
+ Ok ( Some ( extrinsics) ) => extrinsics,
244
+ Ok ( None ) => {
245
+ error ! (
246
+ target: LOG_TARGET ,
247
+ "No block body found for imported block {:?}" ,
248
+ hash,
249
+ ) ;
250
+ continue ;
251
+ }
252
+ Err ( e) => {
253
+ error ! (
254
+ target: LOG_TARGET ,
255
+ "Failed to get block body for imported block {:?}: {:?}" ,
256
+ hash,
257
+ e,
258
+ ) ;
259
+ continue ;
260
+ }
261
+ } ;
262
+
247
263
let candidate_hashes = match fetch_candidates (
248
264
& * client,
249
- & BlockId :: hash ( hash ) ,
265
+ extrinsics ,
250
266
& BlockId :: hash ( parent_hash)
251
267
) {
252
- Ok ( Some ( candidates) ) => candidates. map ( |c| c. hash ( ) ) . collect ( ) ,
268
+ Ok ( Some ( candidates) ) => candidates. into_iter ( ) . map ( |c| c. hash ( ) ) . collect ( ) ,
253
269
Ok ( None ) => {
254
270
warn ! (
255
271
target: LOG_TARGET ,
@@ -629,8 +645,7 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
629
645
I : BlockImport < Block > + Send + Sync ,
630
646
I :: Error : Into < ConsensusError > ,
631
647
P : ProvideRuntimeApi + ProvideCache < Block > ,
632
- P :: Api : ParachainHost < Block > ,
633
- P :: Api : ApiExt < Block , Error = sp_blockchain:: Error > ,
648
+ P :: Api : ParachainHost < Block , Error = sp_blockchain:: Error > ,
634
649
{
635
650
type Error = ConsensusError ;
636
651
@@ -656,7 +671,7 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
656
671
let our_id = self . our_id ( & validators) ;
657
672
658
673
// Use a runtime API to extract all included erasure-roots from the imported block.
659
- let candidates = self . client . runtime_api ( ) . get_heads ( & parent_id , extrinsics. clone ( ) )
674
+ let candidates = fetch_candidates ( & * self . client , extrinsics. clone ( ) , & parent_id )
660
675
. map_err ( |e| ConsensusError :: ChainLookup ( e. to_string ( ) ) ) ?;
661
676
662
677
match candidates {
@@ -666,7 +681,8 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
666
681
trace ! (
667
682
target: LOG_TARGET ,
668
683
"Our validator id is {}, the candidates included are {:?}" ,
669
- our_id, candidates
684
+ our_id,
685
+ candidates,
670
686
) ;
671
687
672
688
for candidate in & candidates {
0 commit comments