jam_std_common/safrole/
mod.rs1use crate::{bandersnatch, ed25519, Entropy, EpochPeriod, ValKeyset};
4use jam_types::{BoundedVec, FixedVec, Slot};
5use scale::{Decode, Encode, MaxEncodedLen};
6
7pub mod ticket;
8
9pub type EpochIndex = Slot;
11
12pub 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#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
20pub struct EpochKeySet {
21 pub bandersnatch: bandersnatch::Public,
23 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#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
37pub struct NextEpochDescriptor {
38 pub entropy: Entropy,
40 pub tickets_entropy: Entropy,
42 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}