Skip to content

Commit 78e828d

Browse files
bkchrgavofyork
authored andcommitted
Fix checking that get_heads exists (paritytech#657)
1 parent 0f7f218 commit 78e828d

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

availability-store/src/worker.rs

+46-30
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use std::thread;
2121

2222
use log::{error, info, trace, warn};
2323
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;
2626
use client::{
2727
BlockchainEvents, BlockBody,
2828
blockchain::ProvideCache,
@@ -206,30 +206,25 @@ where
206206
}
207207

208208

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>>>
211211
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>,
214214
{
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)
233228
}
234229

235230
/// Creates a task to prune entries in availability store upon block finalization.
@@ -244,12 +239,33 @@ where
244239
while let Some(notification) = finality_notification_stream.next().await {
245240
let hash = notification.hash;
246241
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+
247263
let candidate_hashes = match fetch_candidates(
248264
&*client,
249-
&BlockId::hash(hash),
265+
extrinsics,
250266
&BlockId::hash(parent_hash)
251267
) {
252-
Ok(Some(candidates)) => candidates.map(|c| c.hash()).collect(),
268+
Ok(Some(candidates)) => candidates.into_iter().map(|c| c.hash()).collect(),
253269
Ok(None) => {
254270
warn!(
255271
target: LOG_TARGET,
@@ -629,8 +645,7 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
629645
I: BlockImport<Block> + Send + Sync,
630646
I::Error: Into<ConsensusError>,
631647
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>,
634649
{
635650
type Error = ConsensusError;
636651

@@ -656,7 +671,7 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
656671
let our_id = self.our_id(&validators);
657672

658673
// 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)
660675
.map_err(|e| ConsensusError::ChainLookup(e.to_string()))?;
661676

662677
match candidates {
@@ -666,7 +681,8 @@ impl<I, P> BlockImport<Block> for AvailabilityBlockImport<I, P> where
666681
trace!(
667682
target: LOG_TARGET,
668683
"Our validator id is {}, the candidates included are {:?}",
669-
our_id, candidates
684+
our_id,
685+
candidates,
670686
);
671687

672688
for candidate in &candidates {

0 commit comments

Comments
 (0)