referrerpolicy=no-referrer-when-downgrade

sp_consensus_aura/
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/// Contains the inherents for the AURA module
19use sp_inherents::{Error, InherentData, InherentIdentifier};
20
21/// The Aura inherent identifier.
22pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
23
24/// The type of the Aura inherent.
25pub type InherentType = sp_consensus_slots::Slot;
26
27/// Auxiliary trait to extract Aura inherent data.
28pub trait AuraInherentData {
29	/// Get aura inherent data.
30	fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error>;
31	/// Replace aura inherent data.
32	fn aura_replace_inherent_data(&mut self, new: InherentType);
33}
34
35impl AuraInherentData for InherentData {
36	fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error> {
37		self.get_data(&INHERENT_IDENTIFIER)
38	}
39
40	fn aura_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 `Aura`.
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 a new instance with 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
71#[cfg(feature = "std")]
72impl core::ops::Deref for InherentDataProvider {
73	type Target = InherentType;
74
75	fn deref(&self) -> &Self::Target {
76		&self.slot
77	}
78}
79
80#[cfg(feature = "std")]
81#[async_trait::async_trait]
82impl sp_inherents::InherentDataProvider for InherentDataProvider {
83	async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
84		inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
85	}
86
87	async fn try_handle_error(
88		&self,
89		_: &InherentIdentifier,
90		_: &[u8],
91	) -> Option<Result<(), Error>> {
92		// There is no error anymore
93		None
94	}
95}