use super::vrf::RingVrfSignature;
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
pub use crate::core::ed25519::{Public as EphemeralPublic, Signature as EphemeralSignature};
pub type TicketId = u128;
#[derive(
Debug, Clone, PartialEq, Eq, Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo,
)]
pub struct TicketBody {
pub attempt_idx: u32,
pub erased_public: EphemeralPublic,
pub revealed_public: EphemeralPublic,
}
pub type TicketSignature = RingVrfSignature;
#[derive(
Debug, Clone, PartialEq, Eq, Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo,
)]
pub struct TicketEnvelope {
pub body: TicketBody,
pub signature: TicketSignature,
}
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct TicketClaim {
pub erased_signature: EphemeralSignature,
}
pub fn ticket_id_threshold(
redundancy: u32,
slots: u32,
attempts: u32,
validators: u32,
) -> TicketId {
let num = redundancy as u64 * slots as u64;
let den = attempts as u64 * validators as u64;
TicketId::max_value()
.checked_div(den.into())
.unwrap_or_default()
.saturating_mul(num.into())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ticket_id_threshold_trivial_check() {
let redundancy = 2;
let slots = 1000;
let attempts = 100;
let validators = 500;
let threshold = ticket_id_threshold(redundancy, slots, attempts, validators);
let threshold = threshold as f64 / TicketId::MAX as f64;
let avt = ((attempts * validators) as f64 * threshold) as u32;
assert_eq!(avt, slots * redundancy);
println!("threshold: {}", threshold);
println!("avt = {}", avt);
}
}