use crate::{bandersnatch, ed25519, Entropy, ValKeyset};
use codec::{Decode, Encode, MaxEncodedLen};
use jam_types::{BoundedVec, EpochPeriod, FixedVec, Slot};
pub mod ticket;
pub type EpochIndex = Slot;
pub type EpochKeys = FixedVec<EpochKeySet, jam_types::ValCount>;
pub type FallbackKeys = FixedVec<bandersnatch::Public, EpochPeriod>;
pub type EpochTickets = FixedVec<ticket::TicketBody, EpochPeriod>;
pub type EpochTicketsAccumulator = BoundedVec<ticket::TicketBody, EpochPeriod>;
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct EpochKeySet {
pub bandersnatch: bandersnatch::Public,
pub ed25519: ed25519::Public,
}
impl From<&ValKeyset> for EpochKeySet {
fn from(keyset: &ValKeyset) -> Self {
Self { bandersnatch: keyset.bandersnatch, ed25519: keyset.ed25519 }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct NextEpochDescriptor {
pub entropy: Entropy,
pub tickets_entropy: Entropy,
pub validators: EpochKeys,
}
#[derive(Encode, Decode, Debug, Clone)]
pub enum TicketOrKey {
Ticket(ticket::TicketBody),
Key(bandersnatch::Public),
}
impl TicketOrKey {
pub fn ticket(self) -> Option<ticket::TicketBody> {
match self {
TicketOrKey::Ticket(t) => Some(t),
TicketOrKey::Key(_) => None,
}
}
}
#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq)]
pub enum TicketsOrKeys {
Tickets(EpochTickets),
Keys(FallbackKeys),
}
impl TicketsOrKeys {
pub fn tickets(&self) -> Option<&EpochTickets> {
match self {
Self::Tickets(t) => Some(t),
Self::Keys(_) => None,
}
}
pub fn get(&self, i: Slot) -> Option<TicketOrKey> {
Some(match self {
Self::Tickets(t) => TicketOrKey::Ticket(*t.get(i as usize)?),
Self::Keys(k) => TicketOrKey::Key(*k.get(i as usize)?),
})
}
}