jam-std-common 0.1.26

Common datatypes and utilities for the JAM nodes and tooling
Documentation
//! Types related to tickets.

use crate::{bandersnatch::ring_vrf, Entropy};
use bounded_collections::BoundedVec;
use codec::{Decode, Encode, MaxEncodedLen};
use jam_types::{opaque, EpochPeriod, TicketAttempt};

// Ticket identifier.
//
// Output of a VRF whose inputs cannot be controlled by the
// ticket's creator (refer to [`super::vrf::ticket_id_input`] parameters).
//
// The value is used as the ticket score to decide if the ticket is worth being considered.
opaque! { pub struct TicketId(pub [u8; 32]); }

impl TicketId {
	pub fn from(entropy: Entropy) -> TicketId {
		TicketId(*entropy)
	}
}

/// Ticket data on-chain.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct TicketBody {
	/// Ticket identifier
	pub id: TicketId,
	/// Attempt index.
	#[codec(compact)]
	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))
	}
}

/// Ticket ring vrf signature.
pub type TicketSignature = ring_vrf::Signature;

/// Ticket envelope used during submission.
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub struct TicketEnvelope {
	/// Ticket attempt.
	pub attempt: TicketAttempt,
	/// Ring signature of the ticket `body`.
	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 {
	/// Construct [`TicketId`] from [`TicketEnvelope`] signature.
	pub fn id(&self) -> TicketId {
		TicketId::from(self.signature.vrf_output())
	}

	/// Construct [`TicketBody`] from [`TicketEnvelope`] signature.
	pub fn body(&self) -> TicketBody {
		TicketBody { id: self.id(), attempt: self.attempt }
	}
}

/// A collection of ticket bodies.
pub type TicketBodies = BoundedVec<TicketBody, EpochPeriod>;