referrerpolicy=no-referrer-when-downgrade

sp_consensus_sassafras/
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//! Sassafras digests structures and helpers.
19
20use crate::{
21	ticket::TicketClaim, vrf::VrfSignature, AuthorityId, AuthorityIndex, AuthoritySignature,
22	EpochConfiguration, Randomness, Slot, SASSAFRAS_ENGINE_ID,
23};
24
25use codec::{Decode, Encode, MaxEncodedLen};
26use scale_info::TypeInfo;
27
28#[cfg(not(feature = "std"))]
29use alloc::vec::Vec;
30use sp_runtime::{DigestItem, RuntimeDebug};
31
32/// Epoch slot claim digest entry.
33///
34/// This is mandatory for each block.
35#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
36pub struct SlotClaim {
37	/// Authority index that claimed the slot.
38	pub authority_idx: AuthorityIndex,
39	/// Corresponding slot number.
40	pub slot: Slot,
41	/// Slot claim VRF signature.
42	pub vrf_signature: VrfSignature,
43	/// Ticket auxiliary information for claim check.
44	pub ticket_claim: Option<TicketClaim>,
45}
46
47/// Information about the next epoch.
48///
49/// This is mandatory in the first block of each epoch.
50#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
51pub struct NextEpochDescriptor {
52	/// Randomness value.
53	pub randomness: Randomness,
54	/// Authorities list.
55	pub authorities: Vec<AuthorityId>,
56	/// Epoch configuration.
57	///
58	/// If not present previous epoch parameters are used.
59	pub config: Option<EpochConfiguration>,
60}
61
62/// Runtime digest entries.
63///
64/// Entries which may be generated by on-chain code.
65#[derive(Decode, Encode, Clone, PartialEq, Eq)]
66pub enum ConsensusLog {
67	/// Provides information about the next epoch parameters.
68	#[codec(index = 1)]
69	NextEpochData(NextEpochDescriptor),
70	/// Disable the authority with given index.
71	#[codec(index = 2)]
72	OnDisabled(AuthorityIndex),
73}
74
75impl TryFrom<&DigestItem> for SlotClaim {
76	type Error = ();
77	fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
78		item.pre_runtime_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
79	}
80}
81
82impl From<&SlotClaim> for DigestItem {
83	fn from(claim: &SlotClaim) -> Self {
84		DigestItem::PreRuntime(SASSAFRAS_ENGINE_ID, claim.encode())
85	}
86}
87
88impl TryFrom<&DigestItem> for AuthoritySignature {
89	type Error = ();
90	fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
91		item.seal_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
92	}
93}
94
95impl From<&AuthoritySignature> for DigestItem {
96	fn from(signature: &AuthoritySignature) -> Self {
97		DigestItem::Seal(SASSAFRAS_ENGINE_ID, signature.encode())
98	}
99}