use crate::{bandersnatch::ring_vrf, Entropy, EpochPeriod, TicketAttempt};
use bounded_collections::BoundedVec;
use jam_types::opaque;
use scale::{Decode, Encode, MaxEncodedLen};
opaque! { pub struct TicketId(pub [u8; 32]); }
impl TicketId {
pub fn from(entropy: Entropy) -> TicketId {
TicketId(*entropy)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct TicketBody {
pub id: TicketId,
pub attempt: TicketAttempt,
}
impl Ord for TicketBody {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.id.cmp(&other.id)
}
}
impl PartialOrd for TicketBody {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
pub type TicketSignature = ring_vrf::Signature;
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct TicketEnvelope {
pub attempt: TicketAttempt,
pub signature: TicketSignature,
}
impl Ord for TicketEnvelope {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.id().cmp(&other.id())
}
}
impl PartialOrd for TicketEnvelope {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl TicketEnvelope {
pub fn id(&self) -> TicketId {
TicketId::from(self.signature.vrf_output())
}
pub fn body(&self) -> TicketBody {
TicketBody { id: self.id(), attempt: self.attempt }
}
}
pub type TicketBodies = BoundedVec<TicketBody, EpochPeriod>;