referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Sassafras digests structures and helpers.

use crate::{
	ticket::TicketClaim, vrf::VrfSignature, AuthorityId, AuthorityIndex, AuthoritySignature,
	EpochConfiguration, Randomness, Slot, SASSAFRAS_ENGINE_ID,
};

use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use sp_runtime::{DigestItem, RuntimeDebug};

/// Epoch slot claim digest entry.
///
/// This is mandatory for each block.
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct SlotClaim {
	/// Authority index that claimed the slot.
	pub authority_idx: AuthorityIndex,
	/// Corresponding slot number.
	pub slot: Slot,
	/// Slot claim VRF signature.
	pub vrf_signature: VrfSignature,
	/// Ticket auxiliary information for claim check.
	pub ticket_claim: Option<TicketClaim>,
}

/// Information about the next epoch.
///
/// This is mandatory in the first block of each epoch.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct NextEpochDescriptor {
	/// Randomness value.
	pub randomness: Randomness,
	/// Authorities list.
	pub authorities: Vec<AuthorityId>,
	/// Epoch configuration.
	///
	/// If not present previous epoch parameters are used.
	pub config: Option<EpochConfiguration>,
}

/// Runtime digest entries.
///
/// Entries which may be generated by on-chain code.
#[derive(Decode, Encode, Clone, PartialEq, Eq)]
pub enum ConsensusLog {
	/// Provides information about the next epoch parameters.
	#[codec(index = 1)]
	NextEpochData(NextEpochDescriptor),
	/// Disable the authority with given index.
	#[codec(index = 2)]
	OnDisabled(AuthorityIndex),
}

impl TryFrom<&DigestItem> for SlotClaim {
	type Error = ();
	fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
		item.pre_runtime_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
	}
}

impl From<&SlotClaim> for DigestItem {
	fn from(claim: &SlotClaim) -> Self {
		DigestItem::PreRuntime(SASSAFRAS_ENGINE_ID, claim.encode())
	}
}

impl TryFrom<&DigestItem> for AuthoritySignature {
	type Error = ();
	fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
		item.seal_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
	}
}

impl From<&AuthoritySignature> for DigestItem {
	fn from(signature: &AuthoritySignature) -> Self {
		DigestItem::Seal(SASSAFRAS_ENGINE_ID, signature.encode())
	}
}