Skip to main content

subsoil/consensus/aura/
inherents.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7/// Contains the inherents for the AURA module
8use crate::inherents::{Error, InherentData, InherentIdentifier};
9
10/// The Aura inherent identifier.
11pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
12
13/// The type of the Aura inherent.
14pub type InherentType = crate::consensus::slots::Slot;
15
16/// Auxiliary trait to extract Aura inherent data.
17pub trait AuraInherentData {
18	/// Get aura inherent data.
19	fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error>;
20	/// Replace aura inherent data.
21	fn aura_replace_inherent_data(&mut self, new: InherentType);
22}
23
24impl AuraInherentData for InherentData {
25	fn aura_inherent_data(&self) -> Result<Option<InherentType>, Error> {
26		self.get_data(&INHERENT_IDENTIFIER)
27	}
28
29	fn aura_replace_inherent_data(&mut self, new: InherentType) {
30		self.replace_data(INHERENT_IDENTIFIER, &new);
31	}
32}
33
34/// Provides the slot duration inherent data for `Aura`.
35// TODO: Remove in the future. https://github.com/paritytech/substrate/issues/8029
36#[cfg(feature = "std")]
37pub struct InherentDataProvider {
38	slot: InherentType,
39}
40
41#[cfg(feature = "std")]
42impl InherentDataProvider {
43	/// Create a new instance with the given slot.
44	pub fn new(slot: InherentType) -> Self {
45		Self { slot }
46	}
47
48	/// Creates the inherent data provider by calculating the slot from the given
49	/// `timestamp` and `duration`.
50	pub fn from_timestamp_and_slot_duration(
51		timestamp: crate::timestamp::Timestamp,
52		slot_duration: crate::consensus::slots::SlotDuration,
53	) -> Self {
54		let slot = InherentType::from_timestamp(timestamp, slot_duration);
55
56		Self { slot }
57	}
58}
59
60#[cfg(feature = "std")]
61impl core::ops::Deref for InherentDataProvider {
62	type Target = InherentType;
63
64	fn deref(&self) -> &Self::Target {
65		&self.slot
66	}
67}
68
69#[cfg(feature = "std")]
70#[async_trait::async_trait]
71impl crate::inherents::InherentDataProvider for InherentDataProvider {
72	async fn provide_inherent_data(&self, inherent_data: &mut InherentData) -> Result<(), Error> {
73		inherent_data.put_data(INHERENT_IDENTIFIER, &self.slot)
74	}
75
76	async fn try_handle_error(
77		&self,
78		_: &InherentIdentifier,
79		_: &[u8],
80	) -> Option<Result<(), Error>> {
81		// There is no error anymore
82		None
83	}
84}