referrerpolicy=no-referrer-when-downgrade

sp_block_builder/
client_side.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use crate::BlockBuilder;
19
20use sp_inherents::{InherentData, InherentDataProvider, InherentIdentifier};
21use sp_runtime::traits::Block as BlockT;
22
23/// Errors that occur when creating and checking on the client side.
24#[derive(Debug)]
25pub enum CheckInherentsError {
26	/// Create inherents error.
27	CreateInherentData(sp_inherents::Error),
28	/// Client Error
29	Client(sp_api::ApiError),
30	/// Check inherents error
31	CheckInherents(sp_inherents::Error),
32	/// Unknown inherent error for identifier
33	CheckInherentsUnknownError(InherentIdentifier),
34}
35
36/// Create inherent data and check that the inherents are valid.
37pub async fn check_inherents<Block: BlockT, Client: sp_api::ProvideRuntimeApi<Block>>(
38	client: std::sync::Arc<Client>,
39	at_hash: Block::Hash,
40	block: Block,
41	inherent_data_providers: &impl InherentDataProvider,
42) -> Result<(), CheckInherentsError>
43where
44	Client::Api: BlockBuilder<Block>,
45{
46	let inherent_data = inherent_data_providers
47		.create_inherent_data()
48		.await
49		.map_err(CheckInherentsError::CreateInherentData)?;
50
51	check_inherents_with_data(client, at_hash, block, inherent_data_providers, inherent_data).await
52}
53
54/// Check that the inherents are valid.
55pub async fn check_inherents_with_data<Block: BlockT, Client: sp_api::ProvideRuntimeApi<Block>>(
56	client: std::sync::Arc<Client>,
57	at_hash: Block::Hash,
58	block: Block,
59	inherent_data_provider: &impl InherentDataProvider,
60	inherent_data: InherentData,
61) -> Result<(), CheckInherentsError>
62where
63	Client::Api: BlockBuilder<Block>,
64{
65	let res = client
66		.runtime_api()
67		.check_inherents(at_hash, block, inherent_data)
68		.map_err(CheckInherentsError::Client)?;
69
70	if !res.ok() {
71		for (id, error) in res.into_errors() {
72			match inherent_data_provider.try_handle_error(&id, &error).await {
73				Some(res) => res.map_err(CheckInherentsError::CheckInherents)?,
74				None => return Err(CheckInherentsError::CheckInherentsUnknownError(id)),
75			}
76		}
77	}
78
79	Ok(())
80}