jam-std-common 0.1.26

Common datatypes and utilities for the JAM nodes and tooling
Documentation
//! Primitives for Safrole consensus.

use crate::{bandersnatch, ed25519, Entropy, ValKeyset};
use codec::{Decode, Encode, MaxEncodedLen};
use jam_types::{BoundedVec, EpochPeriod, FixedVec, Slot};

pub mod ticket;

/// Index of an epoch.
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>;

/// A pair of bandersnatch and ed25519 key for the validator.
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct EpochKeySet {
	/// Bandersnatch key.
	pub bandersnatch: bandersnatch::Public,
	/// Ed25519 key.
	pub ed25519: ed25519::Public,
}

impl From<&ValKeyset> for EpochKeySet {
	fn from(keyset: &ValKeyset) -> Self {
		Self { bandersnatch: keyset.bandersnatch, ed25519: keyset.ed25519 }
	}
}

/// Information about the next epoch.
///
/// This is mandatory in the first block of each epoch.
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct NextEpochDescriptor {
	/// Next epoch entropy value.
	pub entropy: Entropy,
	/// Entropy used to generate tickets for target epoch's slot.
	pub tickets_entropy: Entropy,
	/// Validators list.
	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)?),
		})
	}
}