// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see .
//! Provides types and traits for creating and checking inherents.
//!
//! Each inherent is added to a produced block. Each runtime decides on which inherents it
//! wants to attach to its blocks. All data that is required for the runtime to create the inherents
//! is stored in the `InherentData`. This `InherentData` is constructed by the node and given to
//! the runtime.
//!
//! Types that provide data for inherents, should implement `InherentDataProvider` and need to be
//! registered at `InherentDataProviders`.
//!
//! In the runtime, modules need to implement `ProvideInherent` when they can create and/or check
//! inherents. By implementing `ProvideInherent`, a module is not enforced to create an inherent.
//! A module can also just check given inherents. For using a module as inherent provider, it needs
//! to be registered by the `construct_runtime!` macro. The macro documentation gives more
//! information on how that is done.
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
use codec::{Encode, Decode};
use sp_std::{collections::btree_map::{BTreeMap, IntoIter, Entry}, vec::Vec};
#[cfg(feature = "std")]
use parking_lot::RwLock;
#[cfg(feature = "std")]
use std::{sync::Arc, format};
/// An error that can occur within the inherent data system.
#[cfg(feature = "std")]
#[derive(Debug, Encode, Decode, derive_more::Display)]
pub struct Error(String);
#[cfg(feature = "std")]
impl> From for Error {
fn from(data: T) -> Error {
Self(data.into())
}
}
#[cfg(feature = "std")]
impl Error {
/// Convert this error into a `String`.
pub fn into_string(self) -> String {
self.0
}
}
/// An error that can occur within the inherent data system.
#[derive(Encode, sp_core::RuntimeDebug)]
#[cfg(not(feature = "std"))]
pub struct Error(&'static str);
#[cfg(not(feature = "std"))]
impl From<&'static str> for Error {
fn from(data: &'static str) -> Error {
Self(data)
}
}
/// An identifier for an inherent.
pub type InherentIdentifier = [u8; 8];
/// Inherent data to include in a block.
#[derive(Clone, Default, Encode, Decode)]
pub struct InherentData {
/// All inherent data encoded with parity-scale-codec and an identifier.
data: BTreeMap>
}
impl InherentData {
/// Create a new instance.
pub fn new() -> Self {
Self::default()
}
/// Put data for an inherent into the internal storage.
///
/// # Return
///
/// Returns `Ok(())` if the data could be inserted and no data for an inherent with the same
/// identifier existed, otherwise an error is returned.
///
/// Inherent identifiers need to be unique, otherwise decoding of these values will not work!
pub fn put_data(
&mut self,
identifier: InherentIdentifier,
inherent: &I,
) -> Result<(), Error> {
match self.data.entry(identifier) {
Entry::Vacant(entry) => {
entry.insert(inherent.encode());
Ok(())
},
Entry::Occupied(_) => {
Err("Inherent with same identifier already exists!".into())
}
}
}
/// Replace the data for an inherent.
///
/// If it does not exist, the data is just inserted.
pub fn replace_data(
&mut self,
identifier: InherentIdentifier,
inherent: &I,
) {
self.data.insert(identifier, inherent.encode());
}
/// Returns the data for the requested inherent.
///
/// # Return
///
/// - `Ok(Some(I))` if the data could be found and deserialized.
/// - `Ok(None)` if the data could not be found.
/// - `Err(_)` if the data could be found, but deserialization did not work.
pub fn get_data(
&self,
identifier: &InherentIdentifier,
) -> Result