jam_std_common/safrole/
mod.rs

1//! Primitives for Safrole consensus.
2
3use crate::{bandersnatch, ed25519, Entropy, EpochPeriod, ValKeyset};
4use jam_types::{BoundedVec, FixedVec, Slot};
5use scale::{Decode, Encode, MaxEncodedLen};
6
7pub mod ticket;
8
9/// Index of an epoch.
10pub type EpochIndex = Slot;
11
12//pub type ValBandersnatchKeys = FixedVec<bandersnatch::Public, jam_types::ValCount>;
13pub type EpochKeys = FixedVec<EpochKeySet, jam_types::ValCount>;
14pub type FallbackKeys = FixedVec<bandersnatch::Public, EpochPeriod>;
15pub type EpochTickets = FixedVec<ticket::TicketBody, EpochPeriod>;
16pub type EpochTicketsAccumulator = BoundedVec<ticket::TicketBody, EpochPeriod>;
17
18/// A pair of bandersnatch and ed25519 key for the validator.
19#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
20pub struct EpochKeySet {
21	/// Bandersnatch key.
22	pub bandersnatch: bandersnatch::Public,
23	/// Ed25519 key.
24	pub ed25519: ed25519::Public,
25}
26
27impl From<&ValKeyset> for EpochKeySet {
28	fn from(keyset: &ValKeyset) -> Self {
29		Self { bandersnatch: keyset.bandersnatch, ed25519: keyset.ed25519 }
30	}
31}
32
33/// Information about the next epoch.
34///
35/// This is mandatory in the first block of each epoch.
36#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
37pub struct NextEpochDescriptor {
38	/// Next epoch entropy value.
39	pub entropy: Entropy,
40	/// Entropy used to generate tickets for target epoch's slot.
41	pub tickets_entropy: Entropy,
42	/// Validators list.
43	pub validators: EpochKeys,
44}
45
46#[derive(Encode, Decode, Debug, Clone)]
47pub enum TicketOrKey {
48	Ticket(ticket::TicketBody),
49	Key(bandersnatch::Public),
50}
51
52impl TicketOrKey {
53	pub fn ticket(self) -> Option<ticket::TicketBody> {
54		match self {
55			TicketOrKey::Ticket(t) => Some(t),
56			TicketOrKey::Key(_) => None,
57		}
58	}
59}
60
61#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq)]
62pub enum TicketsOrKeys {
63	Tickets(EpochTickets),
64	Keys(FallbackKeys),
65}
66
67impl TicketsOrKeys {
68	pub fn tickets(&self) -> Option<&EpochTickets> {
69		match self {
70			Self::Tickets(t) => Some(t),
71			Self::Keys(_) => None,
72		}
73	}
74	pub fn get(&self, i: Slot) -> Option<TicketOrKey> {
75		Some(match self {
76			Self::Tickets(t) => TicketOrKey::Ticket(*t.get(i as usize)?),
77			Self::Keys(k) => TicketOrKey::Key(*k.get(i as usize)?),
78		})
79	}
80}