sp_inherents/lib.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
18//! Substrate Inherent Extrinsics
19//!
20//! Inherent extrinsics are extrinsics that are inherently added to each block. However, it is up to
21//! the runtime implementation to require an inherent for each block or to make it optional.
22//! Inherents are mainly used to pass data from the block producer to the runtime. So, inherents
23//! require some part that is running on the client side and some part that is running on the
24//! runtime side. Any data that is required by an inherent is passed as [`InherentData`] from the
25//! client to the runtime when the inherents are constructed.
26//!
27//! The process of constructing and applying inherents is the following:
28//!
29//! 1. The block producer first creates the [`InherentData`] by using the inherent data providers
30//! that are created by [`CreateInherentDataProviders`].
31//!
32//! 2. The [`InherentData`] is passed to the `inherent_extrinsics` function of the `BlockBuilder`
33//! runtime api. This will call the runtime which will create all the inherents that should be
34//! applied to the block.
35//!
36//! 3. Apply each inherent to the block like any normal extrinsic.
37//!
38//! On block import the inherents in the block are checked by calling the `check_inherents` runtime
39//! API. This will also pass an instance of [`InherentData`] which the runtime can use to validate
40//! all inherents. If some inherent data isn't required for validating an inherent, it can be
41//! omitted when providing the inherent data providers for block import.
42//!
43//! # Providing inherent data
44//!
45//! To provide inherent data from the client side, [`InherentDataProvider`] should be implemented.
46//!
47//! ```
48//! use codec::Decode;
49//! use sp_inherents::{InherentIdentifier, InherentData};
50//!
51//! // This needs to be unique for the runtime.
52//! const INHERENT_IDENTIFIER: InherentIdentifier = *b"testinh0";
53//!
54//! /// Some custom inherent data provider
55//! struct InherentDataProvider;
56//!
57//! #[async_trait::async_trait]
58//! impl sp_inherents::InherentDataProvider for InherentDataProvider {
59//! async fn provide_inherent_data(
60//! &self,
61//! inherent_data: &mut InherentData,
62//! ) -> Result<(), sp_inherents::Error> {
63//! // We can insert any data that implements [`codec::Encode`].
64//! inherent_data.put_data(INHERENT_IDENTIFIER, &"hello")
65//! }
66//!
67//! /// When validating the inherents, the runtime implementation can throw errors. We support
68//! /// two error modes, fatal and non-fatal errors. A fatal error means that the block is invalid
69//! /// and this function here should return `Err(_)` to not import the block. Non-fatal errors
70//! /// are allowed to be handled here in this function and the function should return `Ok(())`
71//! /// if it could be handled. A non-fatal error is for example that a block is in the future
72//! /// from the point of view of the local node. In such a case the block import for example
73//! /// should be delayed until the block is valid.
74//! ///
75//! /// If this functions returns `None`, it means that it is not responsible for this error or
76//! /// that the error could not be interpreted.
77//! async fn try_handle_error(
78//! &self,
79//! identifier: &InherentIdentifier,
80//! mut error: &[u8],
81//! ) -> Option<Result<(), sp_inherents::Error>> {
82//! // Check if this error belongs to us.
83//! if *identifier != INHERENT_IDENTIFIER {
84//! return None;
85//! }
86//!
87//! // For demonstration purposes we are using a `String` as error type. In real
88//! // implementations it is advised to not use `String`.
89//! Some(Err(
90//! sp_inherents::Error::Application(Box::from(String::decode(&mut error).ok()?))
91//! ))
92//! }
93//! }
94//! ```
95//!
96//! In the service the relevant inherent data providers need to be passed the block production and
97//! the block import. As already highlighted above, the providers can be different between import
98//! and production.
99//!
100//! ```
101//! # use sp_runtime::testing::{MockCallU64, TestXt};
102//! # use sp_inherents::{InherentIdentifier, InherentData};
103//! # use futures::FutureExt;
104//! # type Block = sp_runtime::testing::Block<TestXt<MockCallU64, ()>>;
105//! # const INHERENT_IDENTIFIER: InherentIdentifier = *b"testinh0";
106//! # struct InherentDataProvider;
107//! # #[async_trait::async_trait]
108//! # impl sp_inherents::InherentDataProvider for InherentDataProvider {
109//! # async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), sp_inherents::Error> {
110//! # inherent_data.put_data(INHERENT_IDENTIFIER, &"hello")
111//! # }
112//! # async fn try_handle_error(
113//! # &self,
114//! # _: &InherentIdentifier,
115//! # _: &[u8],
116//! # ) -> Option<Result<(), sp_inherents::Error>> {
117//! # None
118//! # }
119//! # }
120//!
121//! async fn cool_consensus_block_production(
122//! // The second parameter to the trait are parameters that depend on what the caller
123//! // can provide on extra data.
124//! _: impl sp_inherents::CreateInherentDataProviders<Block, ()>,
125//! ) {
126//! // do cool stuff
127//! }
128//!
129//! async fn cool_consensus_block_import(
130//! _: impl sp_inherents::CreateInherentDataProviders<Block, ()>,
131//! ) {
132//! // do cool stuff
133//! }
134//!
135//! async fn build_service(is_validator: bool) {
136//! // For block import we don't pass any inherent data provider, because our runtime
137//! // does not need any inherent data to validate the inherents.
138//! let block_import = cool_consensus_block_import(|_parent, ()| async { Ok(()) });
139//!
140//! let block_production = if is_validator {
141//! // For block production we want to provide our inherent data provider
142//! cool_consensus_block_production(|_parent, ()| async {
143//! Ok(InherentDataProvider)
144//! }).boxed()
145//! } else {
146//! futures::future::pending().boxed()
147//! };
148//!
149//! futures::pin_mut!(block_import);
150//!
151//! futures::future::select(block_import, block_production).await;
152//! }
153//! ```
154//!
155//! # Creating the inherent
156//!
157//! As the inherents are created by the runtime, it depends on the runtime implementation on how
158//! to create the inherents. As already described above the client side passes the [`InherentData`]
159//! and expects the runtime to construct the inherents out of it. When validating the inherents,
160//! [`CheckInherentsResult`] is used to communicate the result client side.
161
162#![cfg_attr(not(feature = "std"), no_std)]
163#![warn(missing_docs)]
164
165extern crate alloc;
166
167use codec::{Decode, Encode};
168
169use alloc::{
170 collections::btree_map::{BTreeMap, Entry, IntoIter},
171 vec::Vec,
172};
173
174#[cfg(feature = "std")]
175mod client_side;
176
177#[cfg(feature = "std")]
178pub use client_side::*;
179
180/// Errors that occur in context of inherents.
181#[derive(Debug)]
182#[cfg_attr(feature = "std", derive(thiserror::Error))]
183#[allow(missing_docs)]
184pub enum Error {
185 #[cfg_attr(
186 feature = "std",
187 error("Inherent data already exists for identifier: {}", "String::from_utf8_lossy(_0)")
188 )]
189 InherentDataExists(InherentIdentifier),
190 #[cfg_attr(
191 feature = "std",
192 error("Failed to decode inherent data for identifier: {}", "String::from_utf8_lossy(_1)")
193 )]
194 DecodingFailed(#[cfg_attr(feature = "std", source)] codec::Error, InherentIdentifier),
195 #[cfg_attr(
196 feature = "std",
197 error("There was already a fatal error reported and no other errors are allowed")
198 )]
199 FatalErrorReported,
200 #[cfg(feature = "std")]
201 #[error(transparent)]
202 Application(#[from] Box<dyn std::error::Error + Send + Sync>),
203}
204
205/// An identifier for an inherent.
206pub type InherentIdentifier = [u8; 8];
207
208/// Inherent data to include in a block.
209#[derive(Clone, Default, Encode, Decode, scale_info::TypeInfo)]
210pub struct InherentData {
211 /// All inherent data encoded with parity-scale-codec and an identifier.
212 data: BTreeMap<InherentIdentifier, Vec<u8>>,
213}
214
215impl InherentData {
216 /// Create a new instance.
217 pub fn new() -> Self {
218 Self::default()
219 }
220
221 /// Put data for an inherent into the internal storage.
222 ///
223 /// # Return
224 ///
225 /// Returns `Ok(())` if the data could be inserted and no data for an inherent with the same
226 /// identifier existed, otherwise an error is returned.
227 ///
228 /// Inherent identifiers need to be unique, otherwise decoding of these values will not work!
229 pub fn put_data<I: codec::Encode>(
230 &mut self,
231 identifier: InherentIdentifier,
232 inherent: &I,
233 ) -> Result<(), Error> {
234 match self.data.entry(identifier) {
235 Entry::Vacant(entry) => {
236 entry.insert(inherent.encode());
237 Ok(())
238 },
239 Entry::Occupied(_) => Err(Error::InherentDataExists(identifier)),
240 }
241 }
242
243 /// Replace the data for an inherent.
244 ///
245 /// If it does not exist, the data is just inserted.
246 pub fn replace_data<I: codec::Encode>(&mut self, identifier: InherentIdentifier, inherent: &I) {
247 self.data.insert(identifier, inherent.encode());
248 }
249
250 /// Returns the data for the requested inherent.
251 ///
252 /// # Return
253 ///
254 /// - `Ok(Some(I))` if the data could be found and deserialized.
255 /// - `Ok(None)` if the data could not be found.
256 /// - `Err(_)` if the data could be found, but deserialization did not work.
257 pub fn get_data<I: codec::Decode>(
258 &self,
259 identifier: &InherentIdentifier,
260 ) -> Result<Option<I>, Error> {
261 match self.data.get(identifier) {
262 Some(inherent) => I::decode(&mut &inherent[..])
263 .map_err(|e| Error::DecodingFailed(e, *identifier))
264 .map(Some),
265 None => Ok(None),
266 }
267 }
268
269 /// Get the number of inherents in this instance
270 pub fn len(&self) -> usize {
271 self.data.len()
272 }
273}
274
275/// The result of checking inherents.
276///
277/// It either returns okay for all checks, stores all occurred errors or just one fatal error.
278///
279/// When a fatal error occurs, all other errors are removed and the implementation needs to
280/// abort checking inherents.
281#[derive(Encode, Decode, Clone, scale_info::TypeInfo)]
282pub struct CheckInherentsResult {
283 /// Did the check succeed?
284 okay: bool,
285 /// Did we encounter a fatal error?
286 fatal_error: bool,
287 /// We use the `InherentData` to store our errors.
288 errors: InherentData,
289}
290
291impl Default for CheckInherentsResult {
292 fn default() -> Self {
293 Self { okay: true, errors: InherentData::new(), fatal_error: false }
294 }
295}
296
297impl CheckInherentsResult {
298 /// Create a new instance.
299 pub fn new() -> Self {
300 Self::default()
301 }
302
303 /// Put an error into the result.
304 ///
305 /// This makes this result resolve to `ok() == false`.
306 ///
307 /// # Parameters
308 ///
309 /// - identifier - The identifier of the inherent that generated the error.
310 /// - error - The error that will be encoded.
311 pub fn put_error<E: codec::Encode + IsFatalError>(
312 &mut self,
313 identifier: InherentIdentifier,
314 error: &E,
315 ) -> Result<(), Error> {
316 // Don't accept any other error
317 if self.fatal_error {
318 return Err(Error::FatalErrorReported)
319 }
320
321 self.okay = false;
322 if error.is_fatal_error() {
323 self.fatal_error = true;
324 // remove the other errors.
325 self.errors.data.clear();
326 }
327 self.errors.put_data(identifier, error)?;
328
329 Ok(())
330 }
331
332 /// Get an error out of the result.
333 ///
334 /// # Return
335 ///
336 /// - `Ok(Some(I))` if the error could be found and deserialized.
337 /// - `Ok(None)` if the error could not be found.
338 /// - `Err(_)` if the error could be found, but deserialization did not work.
339 pub fn get_error<E: codec::Decode>(
340 &self,
341 identifier: &InherentIdentifier,
342 ) -> Result<Option<E>, Error> {
343 self.errors.get_data(identifier)
344 }
345
346 /// Convert into an iterator over all contained errors.
347 pub fn into_errors(self) -> IntoIter<InherentIdentifier, Vec<u8>> {
348 self.errors.data.into_iter()
349 }
350
351 /// Is this result ok?
352 pub fn ok(&self) -> bool {
353 self.okay
354 }
355
356 /// Is this a fatal error?
357 pub fn fatal_error(&self) -> bool {
358 self.fatal_error
359 }
360}
361
362#[cfg(feature = "std")]
363impl PartialEq for CheckInherentsResult {
364 fn eq(&self, other: &Self) -> bool {
365 self.fatal_error == other.fatal_error &&
366 self.okay == other.okay &&
367 self.errors.data == other.errors.data
368 }
369}
370
371/// Did we encounter a fatal error while checking an inherent?
372///
373/// A fatal error is everything that fails while checking an inherent error, e.g. the inherent
374/// was not found, could not be decoded etc.
375/// Then there are cases where you not want the inherent check to fail, but report that there is an
376/// action required. For example a timestamp of a block is in the future, the timestamp is still
377/// correct, but it is required to verify the block at a later time again and then the inherent
378/// check will succeed.
379pub trait IsFatalError {
380 /// Is this a fatal error?
381 fn is_fatal_error(&self) -> bool;
382}
383
384/// Auxiliary to make any given error resolve to `is_fatal_error() == true` for [`IsFatalError`].
385#[derive(codec::Encode)]
386pub struct MakeFatalError<E>(E);
387
388impl<E: codec::Encode> From<E> for MakeFatalError<E> {
389 fn from(err: E) -> Self {
390 MakeFatalError(err)
391 }
392}
393
394impl<E: codec::Encode> IsFatalError for MakeFatalError<E> {
395 fn is_fatal_error(&self) -> bool {
396 true
397 }
398}
399
400#[cfg(test)]
401mod tests {
402 use super::*;
403 use codec::{Decode, Encode};
404
405 const TEST_INHERENT_0: InherentIdentifier = *b"testinh0";
406 const TEST_INHERENT_1: InherentIdentifier = *b"testinh1";
407
408 #[derive(Encode)]
409 struct NoFatalError<E: codec::Encode>(E);
410 impl<E: codec::Encode> IsFatalError for NoFatalError<E> {
411 fn is_fatal_error(&self) -> bool {
412 false
413 }
414 }
415
416 #[test]
417 fn inherent_data_encodes_and_decodes() {
418 let inherent_0 = vec![1, 2, 3];
419 let inherent_1: u32 = 7;
420
421 let mut data = InherentData::new();
422 data.put_data(TEST_INHERENT_0, &inherent_0).unwrap();
423 data.put_data(TEST_INHERENT_1, &inherent_1).unwrap();
424
425 let encoded = data.encode();
426
427 let decoded = InherentData::decode(&mut &encoded[..]).unwrap();
428
429 assert_eq!(decoded.get_data::<Vec<u32>>(&TEST_INHERENT_0).unwrap().unwrap(), inherent_0);
430 assert_eq!(decoded.get_data::<u32>(&TEST_INHERENT_1).unwrap().unwrap(), inherent_1);
431 }
432
433 #[test]
434 fn adding_same_inherent_returns_an_error() {
435 let mut data = InherentData::new();
436 data.put_data(TEST_INHERENT_0, &8).unwrap();
437 assert!(data.put_data(TEST_INHERENT_0, &10).is_err());
438 }
439
440 #[derive(Clone)]
441 struct TestInherentDataProvider;
442
443 const ERROR_TO_STRING: &str = "Found error!";
444
445 #[async_trait::async_trait]
446 impl InherentDataProvider for TestInherentDataProvider {
447 async fn provide_inherent_data(&self, data: &mut InherentData) -> Result<(), Error> {
448 data.put_data(TEST_INHERENT_0, &42)
449 }
450
451 async fn try_handle_error(
452 &self,
453 _: &InherentIdentifier,
454 _: &[u8],
455 ) -> Option<Result<(), Error>> {
456 Some(Err(Error::Application(Box::from(ERROR_TO_STRING))))
457 }
458 }
459
460 #[test]
461 fn create_inherent_data() {
462 let provider = TestInherentDataProvider;
463
464 let inherent_data = futures::executor::block_on(provider.create_inherent_data()).unwrap();
465
466 assert_eq!(inherent_data.get_data::<u32>(&TEST_INHERENT_0).unwrap().unwrap(), 42u32);
467 }
468
469 #[test]
470 fn check_inherents_result_encodes_and_decodes() {
471 let mut result = CheckInherentsResult::new();
472 assert!(result.ok());
473
474 result.put_error(TEST_INHERENT_0, &NoFatalError(2u32)).unwrap();
475 assert!(!result.ok());
476 assert!(!result.fatal_error());
477
478 let encoded = result.encode();
479
480 let decoded = CheckInherentsResult::decode(&mut &encoded[..]).unwrap();
481
482 assert_eq!(decoded.get_error::<u32>(&TEST_INHERENT_0).unwrap().unwrap(), 2);
483 assert!(!decoded.ok());
484 assert!(!decoded.fatal_error());
485 }
486
487 #[test]
488 fn check_inherents_result_removes_other_errors_on_fatal_error() {
489 let mut result = CheckInherentsResult::new();
490 assert!(result.ok());
491
492 result.put_error(TEST_INHERENT_0, &NoFatalError(2u32)).unwrap();
493 assert!(!result.ok());
494 assert!(!result.fatal_error());
495
496 result.put_error(TEST_INHERENT_1, &MakeFatalError(4u32)).unwrap();
497 assert!(!result.ok());
498 assert!(result.fatal_error());
499
500 assert!(result.put_error(TEST_INHERENT_0, &NoFatalError(5u32)).is_err());
501
502 result.into_errors().for_each(|(i, e)| match i {
503 TEST_INHERENT_1 => assert_eq!(u32::decode(&mut &e[..]).unwrap(), 4),
504 _ => panic!("There should be no other error!"),
505 });
506 }
507}