referrerpolicy=no-referrer-when-downgrade

sp_inherents/
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 std::sync::Arc;
19
20use crate::{Error, InherentData, InherentIdentifier};
21use sp_runtime::traits::Block as BlockT;
22
23/// Something that can create inherent data providers.
24///
25/// It is possible for the caller to provide custom arguments to the callee by setting the
26/// `ExtraArgs` generic parameter.
27///
28/// The crate already provides some convenience implementations of this trait for
29/// `Box<dyn CreateInherentDataProviders>`, `Arc<dyn CreateInherentDataProviders>` and closures. So,
30/// it should not be required to implement this trait manually.
31#[async_trait::async_trait]
32pub trait CreateInherentDataProviders<Block: BlockT, ExtraArgs>: Send + Sync {
33	/// The inherent data providers that will be created.
34	type InherentDataProviders: InherentDataProvider;
35
36	/// Create the inherent data providers at the given `parent` block using the given `extra_args`.
37	async fn create_inherent_data_providers(
38		&self,
39		parent: Block::Hash,
40		extra_args: ExtraArgs,
41	) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>>;
42}
43
44#[async_trait::async_trait]
45impl<F, Block, IDP, ExtraArgs, Fut> CreateInherentDataProviders<Block, ExtraArgs> for F
46where
47	Block: BlockT,
48	F: Fn(Block::Hash, ExtraArgs) -> Fut + Sync + Send,
49	Fut: std::future::Future<Output = Result<IDP, Box<dyn std::error::Error + Send + Sync>>>
50		+ Send
51		+ 'static,
52	IDP: InherentDataProvider + 'static,
53	ExtraArgs: Send + 'static,
54{
55	type InherentDataProviders = IDP;
56
57	async fn create_inherent_data_providers(
58		&self,
59		parent: Block::Hash,
60		extra_args: ExtraArgs,
61	) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
62		(*self)(parent, extra_args).await
63	}
64}
65
66#[async_trait::async_trait]
67impl<Block: BlockT, ExtraArgs: Send, IDPS: InherentDataProvider>
68	CreateInherentDataProviders<Block, ExtraArgs>
69	for Box<dyn CreateInherentDataProviders<Block, ExtraArgs, InherentDataProviders = IDPS>>
70{
71	type InherentDataProviders = IDPS;
72
73	async fn create_inherent_data_providers(
74		&self,
75		parent: Block::Hash,
76		extra_args: ExtraArgs,
77	) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
78		(**self).create_inherent_data_providers(parent, extra_args).await
79	}
80}
81
82#[async_trait::async_trait]
83impl<Block: BlockT, ExtraArgs: Send, IDPS: InherentDataProvider>
84	CreateInherentDataProviders<Block, ExtraArgs>
85	for Arc<dyn CreateInherentDataProviders<Block, ExtraArgs, InherentDataProviders = IDPS>>
86{
87	type InherentDataProviders = IDPS;
88
89	async fn create_inherent_data_providers(
90		&self,
91		parent: Block::Hash,
92		extra_args: ExtraArgs,
93	) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>> {
94		(**self).create_inherent_data_providers(parent, extra_args).await
95	}
96}
97
98/// Something that provides inherent data.
99#[async_trait::async_trait]
100pub trait InherentDataProvider: Send + Sync {
101	/// Convenience function for creating [`InherentData`].
102	///
103	/// Basically maps around [`Self::provide_inherent_data`].
104	async fn create_inherent_data(&self) -> Result<InherentData, Error> {
105		let mut inherent_data = InherentData::new();
106		self.provide_inherent_data(&mut inherent_data).await?;
107		Ok(inherent_data)
108	}
109
110	/// Provide inherent data that should be included in a block.
111	///
112	/// The data should be stored in the given `InherentData` structure.
113	async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error>;
114
115	/// Convert the given encoded error to a string.
116	///
117	/// If the given error could not be decoded, `None` should be returned.
118	async fn try_handle_error(
119		&self,
120		identifier: &InherentIdentifier,
121		error: &[u8],
122	) -> Option<Result<(), Error>>;
123}
124
125#[impl_trait_for_tuples::impl_for_tuples(30)]
126#[async_trait::async_trait]
127impl InherentDataProvider for Tuple {
128	for_tuples!( where #( Tuple: Send + Sync )* );
129	async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
130		for_tuples!( #( Tuple.provide_inherent_data(inherent_data).await?; )* );
131		Ok(())
132	}
133
134	async fn try_handle_error(
135		&self,
136		identifier: &InherentIdentifier,
137		error: &[u8],
138	) -> Option<Result<(), Error>> {
139		for_tuples!( #(
140			if let Some(r) = Tuple.try_handle_error(identifier, error).await { return Some(r) }
141		)* );
142
143		None
144	}
145}