tp_consensus_aura/
inherents.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2021 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
19
20use tp_inherents::{InherentIdentifier, InherentData, Error};
21
22#[cfg(feature = "std")]
23use tp_inherents::{InherentDataProviders, ProvideInherentData};
24
25/// The Aura inherent identifier.
26pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"auraslot";
27
28/// The type of the Aura inherent.
29pub type InherentType = tp_consensus_slots::Slot;
30
31/// Auxiliary trait to extract Aura inherent data.
32pub trait AuraInherentData {
33	/// Get aura inherent data.
34	fn aura_inherent_data(&self) ->Result<InherentType, Error>;
35	/// Replace aura inherent data.
36	fn aura_replace_inherent_data(&mut self, new: InherentType);
37}
38
39impl AuraInherentData for InherentData {
40	fn aura_inherent_data(&self) ->Result<InherentType, Error> {
41		self.get_data(&INHERENT_IDENTIFIER)
42			.and_then(|r| r.ok_or_else(|| "Aura inherent data not found".into()))
43	}
44
45	fn aura_replace_inherent_data(&mut self, new: InherentType) {
46		self.replace_data(INHERENT_IDENTIFIER, &new);
47	}
48}
49
50/// Provides the slot duration inherent data for `Aura`.
51#[cfg(feature = "std")]
52pub struct InherentDataProvider {
53	slot_duration: u64,
54}
55
56#[cfg(feature = "std")]
57impl InherentDataProvider {
58	pub fn new(slot_duration: u64) -> Self {
59		Self {
60			slot_duration
61		}
62	}
63}
64
65#[cfg(feature = "std")]
66impl ProvideInherentData for InherentDataProvider {
67	fn on_register(
68		&self,
69		providers: &InherentDataProviders,
70	) ->Result<(), Error> {
71		if !providers.has_provider(&tp_timestamp::INHERENT_IDENTIFIER) {
72			// Add the timestamp inherent data provider, as we require it.
73			providers.register_provider(tp_timestamp::InherentDataProvider)
74		} else {
75			Ok(())
76		}
77	}
78
79	fn inherent_identifier(&self) -> &'static InherentIdentifier {
80		&INHERENT_IDENTIFIER
81	}
82
83	fn provide_inherent_data(
84		&self,
85		inherent_data: &mut InherentData,
86	) ->Result<(), Error> {
87		use tp_timestamp::TimestampInherentData;
88
89		let timestamp = inherent_data.timestamp_inherent_data()?;
90		let slot = timestamp / self.slot_duration;
91		inherent_data.put_data(INHERENT_IDENTIFIER, &slot)
92	}
93
94	fn error_to_string(&self, error: &[u8]) -> Option<String> {
95		use codec::Decode;
96
97		tp_inherents::Error::decode(&mut &error[..]).map(|e| e.into_string()).ok()
98	}
99}