jam_std_common/safrole/
ticket.rs1use crate::{bandersnatch::ring_vrf, Entropy, EpochPeriod, TicketAttempt};
4use bounded_collections::BoundedVec;
5use jam_types::opaque;
6use scale::{Decode, Encode, MaxEncodedLen};
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	pub attempt: TicketAttempt,
29}
30
31impl Ord for TicketBody {
32	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
33		self.id.cmp(&other.id)
34	}
35}
36
37impl PartialOrd for TicketBody {
38	fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
39		Some(self.cmp(other))
40	}
41}
42
43pub type TicketSignature = ring_vrf::Signature;
45
46#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
48pub struct TicketEnvelope {
49	pub attempt: TicketAttempt,
51	pub signature: TicketSignature,
53}
54
55impl Ord for TicketEnvelope {
56	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
57		self.id().cmp(&other.id())
58	}
59}
60
61impl PartialOrd for TicketEnvelope {
62	fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
63		Some(self.cmp(other))
64	}
65}
66
67impl TicketEnvelope {
68	pub fn id(&self) -> TicketId {
70		TicketId::from(self.signature.vrf_output())
71	}
72
73	pub fn body(&self) -> TicketBody {
75		TicketBody { id: self.id(), attempt: self.attempt }
76	}
77}
78
79pub type TicketBodies = BoundedVec<TicketBody, EpochPeriod>;