referrerpolicy=no-referrer-when-downgrade

sp_consensus_aura/
digests.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//! Aura (Authority-Round) digests
19//!
20//! This implements the digests for AuRa, to allow the private
21//! `CompatibleDigestItem` trait to appear in public interfaces.
22
23use crate::AURA_ENGINE_ID;
24use codec::{Codec, Encode};
25use sp_consensus_slots::Slot;
26use sp_runtime::generic::DigestItem;
27
28/// A digest item which is usable with aura consensus.
29pub trait CompatibleDigestItem<Signature>: Sized {
30	/// Construct a digest item which contains a signature on the hash.
31	fn aura_seal(signature: Signature) -> Self;
32
33	/// If this item is an Aura seal, return the signature.
34	fn as_aura_seal(&self) -> Option<Signature>;
35
36	/// Construct a digest item which contains the slot number
37	fn aura_pre_digest(slot: Slot) -> Self;
38
39	/// If this item is an AuRa pre-digest, return the slot number
40	fn as_aura_pre_digest(&self) -> Option<Slot>;
41}
42
43impl<Signature> CompatibleDigestItem<Signature> for DigestItem
44where
45	Signature: Codec,
46{
47	fn aura_seal(signature: Signature) -> Self {
48		DigestItem::Seal(AURA_ENGINE_ID, signature.encode())
49	}
50
51	fn as_aura_seal(&self) -> Option<Signature> {
52		self.seal_try_to(&AURA_ENGINE_ID)
53	}
54
55	fn aura_pre_digest(slot: Slot) -> Self {
56		DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())
57	}
58
59	fn as_aura_pre_digest(&self) -> Option<Slot> {
60		self.pre_runtime_try_to(&AURA_ENGINE_ID)
61	}
62}