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