referrerpolicy=no-referrer-when-downgrade

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