1
// Copyright Moonsong Labs
2
// This file is part of Moonkit.
3

            
4
// Moonkit is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonkit is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonkit.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! A convenient interface over the digests used in nimbus.
18
//!
19
//! Currently Nimbus has two digests;
20
//! 1. A consensus digest that contains the block author identity
21
//!    This information is copied from the author inehrent.
22
//!    This may be replaced with a pre-runtime digest in the future.
23
//! 2. A seal digest that contains a signature over the rest of the
24
//!    block including the first digest.
25

            
26
use crate::{NimbusId, NimbusSignature, NIMBUS_ENGINE_ID};
27
use parity_scale_codec::Encode;
28
use sp_runtime::generic::DigestItem;
29

            
30
/// A digest item which is usable with aura consensus.
31
pub trait CompatibleDigestItem: Sized {
32
	/// Construct a pre-runtime digest from the given AuthorId
33
	fn nimbus_pre_digest(author: NimbusId) -> Self;
34

            
35
	/// If this item is a nimbus pre-runtime digest, return the author
36
	fn as_nimbus_pre_digest(&self) -> Option<NimbusId>;
37

            
38
	/// Construct a seal digest item from the given signature
39
	fn nimbus_seal(signature: NimbusSignature) -> Self;
40

            
41
	/// If this item is a nimbus seal, return the signature.
42
	fn as_nimbus_seal(&self) -> Option<NimbusSignature>;
43

            
44
	/// This will be deprecated in the future
45
	/// Construct a consensus digest from the given AuthorId
46
	fn nimbus_consensus_digest(author: NimbusId) -> Self;
47

            
48
	/// This will be deprecated in the future
49
	/// If this item is a nimbus consensus digest, return the author
50
	fn as_nimbus_consensus_digest(&self) -> Option<NimbusId>;
51
}
52

            
53
impl CompatibleDigestItem for DigestItem {
54
	fn nimbus_pre_digest(author: NimbusId) -> Self {
55
		DigestItem::PreRuntime(NIMBUS_ENGINE_ID, author.encode())
56
	}
57

            
58
	fn as_nimbus_pre_digest(&self) -> Option<NimbusId> {
59
		self.pre_runtime_try_to(&NIMBUS_ENGINE_ID)
60
	}
61

            
62
	fn nimbus_seal(signature: NimbusSignature) -> Self {
63
		DigestItem::Seal(NIMBUS_ENGINE_ID, signature.encode())
64
	}
65

            
66
	fn as_nimbus_seal(&self) -> Option<NimbusSignature> {
67
		self.seal_try_to(&NIMBUS_ENGINE_ID)
68
	}
69

            
70
	// Remove this once deprecated
71
	fn nimbus_consensus_digest(author: NimbusId) -> Self {
72
		DigestItem::Consensus(NIMBUS_ENGINE_ID, author.encode())
73
	}
74

            
75
	// Remove this once deprecated. I don't think it is used anyway.
76
	// Notice that it calls the pre_runtime helper function.
77
	fn as_nimbus_consensus_digest(&self) -> Option<NimbusId> {
78
		self.pre_runtime_try_to(&NIMBUS_ENGINE_ID)
79
	}
80
}