jam_std_common/safrole/
ticket.rs1use crate::{bandersnatch::ring_vrf, Entropy};
4use bounded_collections::BoundedVec;
5use codec::{Decode, Encode, MaxEncodedLen};
6use jam_types::{opaque, EpochPeriod, TicketAttempt};
7
8opaque! { pub struct TicketId(pub [u8; 32]); }
15
16impl TicketId {
17 pub fn from(entropy: Entropy) -> TicketId {
18 TicketId(*entropy)
19 }
20}
21
22#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
24pub struct TicketBody {
25 pub id: TicketId,
27 #[codec(compact)]
29 pub attempt: TicketAttempt,
30}
31
32impl Ord for TicketBody {
33 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
34 self.id.cmp(&other.id)
35 }
36}
37
38impl PartialOrd for TicketBody {
39 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
40 Some(self.cmp(other))
41 }
42}
43
44pub type TicketSignature = ring_vrf::Signature;
46
47#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
49pub struct TicketEnvelope {
50 pub attempt: TicketAttempt,
52 pub signature: TicketSignature,
54}
55
56impl Ord for TicketEnvelope {
57 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
58 self.id().cmp(&other.id())
59 }
60}
61
62impl PartialOrd for TicketEnvelope {
63 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
64 Some(self.cmp(other))
65 }
66}
67
68impl TicketEnvelope {
69 pub fn id(&self) -> TicketId {
71 TicketId::from(self.signature.vrf_output())
72 }
73
74 pub fn body(&self) -> TicketBody {
76 TicketBody { id: self.id(), attempt: self.attempt }
77 }
78}
79
80pub type TicketBodies = BoundedVec<TicketBody, EpochPeriod>;