sp_consensus_babe/
inherents.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//! Inherents for BABE
19
20use sp_inherents::{Error, InherentData, InherentIdentifier};
21
22/// The BABE inherent identifier.
23pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"babeslot";
24
25/// The type of the BABE inherent.
26pub type InherentType = sp_consensus_slots::Slot;
27/// Auxiliary trait to extract BABE inherent data.
28pub trait BabeInherentData {
29	/// Get BABE inherent data.
30	fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error>;
31	/// Replace BABE inherent data.
32	fn babe_replace_inherent_data(&mut self, new: InherentType);
33}
34
35impl BabeInherentData for InherentData {
36	fn babe_inherent_data(&self) -> Result<Option<InherentType>, Error> {
37		self.get_data(&INHERENT_IDENTIFIER)
38	}
39
40	fn babe_replace_inherent_data(&mut self, new: InherentType) {
41		self.replace_data(INHERENT_IDENTIFIER, &new);
42	}
43}
44
45/// Provides the slot duration inherent data for BABE.
46// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
47#[cfg(feature = "std")]
48pub struct InherentDataProvider {
49	slot: InherentType,
50}
51
52#[cfg(feature = "std")]
53impl InherentDataProvider {
54	/// Create new inherent data provider from the given `slot`.
55	pub fn new(slot: InherentType) -> Self {
56		Self { slot }
57	}
58
59	/// Creates the inherent data provider by calculating the slot from the given
60	/// `timestamp` and `duration`.
61	pub fn from_timestamp_and_slot_duration(
62		timestamp: sp_timestamp::Timestamp,
63		slot_duration: sp_consensus_slots::SlotDuration,
64	) -> Self {
65		let slot = InherentType::from_timestamp(timestamp, slot_duration);
66
67		Self { slot }
68	}
69
70	/// Returns the `slot` of this inherent data provider.
71	pub fn slot(&self) -> InherentType {
72		self.slot
73	}
74}
75
76#[cfg(feature = "std")]
77impl core::ops::Deref for InherentDataProvider {
78	type Target = InherentType;
79
80	fn deref(&self) -> &Self::Target {
81		&self.slot
82	}
83}
84
85#[cfg(feature = "std")]
86#[async_trait::async_trait]
87impl sp_inherents::InherentDataProvider for InherentDataProvider {
88	async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
89		inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
90	}
91
92	async fn try_handle_error(
93		&self,
94		_: &InherentIdentifier,
95		_: &[u8],
96	) -> Option<Result<(), Error>> {
97		// There is no error anymore
98		None
99	}
100}