sp_consensus_aura/
digests.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Aura (Authority-Round) digests
20//!
21//! This implements the digests for AuRa, to allow the private
22//! `CompatibleDigestItem` trait to appear in public interfaces.
23
24use crate::AURA_ENGINE_ID;
25use codec::{Codec, Encode};
26use sp_consensus_slots::Slot;
27use sp_runtime::generic::DigestItem;
28
29/// A digest item which is usable with aura consensus.
30pub trait CompatibleDigestItem<Signature>: Sized {
31	/// Construct a digest item which contains a signature on the hash.
32	fn aura_seal(signature: Signature) -> Self;
33
34	/// If this item is an Aura seal, return the signature.
35	fn as_aura_seal(&self) -> Option<Signature>;
36
37	/// Construct a digest item which contains the slot number
38	fn aura_pre_digest(slot: Slot) -> Self;
39
40	/// If this item is an AuRa pre-digest, return the slot number
41	fn as_aura_pre_digest(&self) -> Option<Slot>;
42}
43
44impl<Signature> CompatibleDigestItem<Signature> for DigestItem
45where
46	Signature: Codec,
47{
48	fn aura_seal(signature: Signature) -> Self {
49		DigestItem::Seal(AURA_ENGINE_ID, signature.encode())
50	}
51
52	fn as_aura_seal(&self) -> Option<Signature> {
53		self.seal_try_to(&AURA_ENGINE_ID)
54	}
55
56	fn aura_pre_digest(slot: Slot) -> Self {
57		DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())
58	}
59
60	fn as_aura_pre_digest(&self) -> Option<Slot> {
61		self.pre_runtime_try_to(&AURA_ENGINE_ID)
62	}
63}