snarkvm_ledger_narwhal_transmission_id/
lib.rs#![forbid(unsafe_code)]
#![warn(clippy::cast_possible_truncation)]
mod bytes;
mod serialize;
mod string;
use console::{network::TRANSACTION_PREFIX, prelude::*};
use ledger_puzzle::{SOLUTION_ID_PREFIX, SolutionID};
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub enum TransmissionID<N: Network> {
Ratification,
Solution(SolutionID<N>, N::TransmissionChecksum),
Transaction(N::TransactionID, N::TransmissionChecksum),
}
impl<N: Network> From<(SolutionID<N>, N::TransmissionChecksum)> for TransmissionID<N> {
fn from((solution_id, checksum): (SolutionID<N>, N::TransmissionChecksum)) -> Self {
Self::Solution(solution_id, checksum)
}
}
impl<N: Network> From<(&N::TransactionID, &N::TransmissionChecksum)> for TransmissionID<N> {
fn from((transaction_id, checksum): (&N::TransactionID, &N::TransmissionChecksum)) -> Self {
Self::Transaction(*transaction_id, *checksum)
}
}
impl<N: Network> TransmissionID<N> {
pub fn solution(&self) -> Option<SolutionID<N>> {
match self {
Self::Solution(solution_id, _) => Some(*solution_id),
_ => None,
}
}
pub fn transaction(&self) -> Option<N::TransactionID> {
match self {
Self::Transaction(transaction_id, _) => Some(*transaction_id),
_ => None,
}
}
pub fn checksum(&self) -> Option<N::TransmissionChecksum> {
match self {
Self::Ratification => None,
Self::Solution(_, checksum) => Some(*checksum),
Self::Transaction(_, checksum) => Some(*checksum),
}
}
}
#[cfg(any(test, feature = "test-helpers"))]
pub mod test_helpers {
use super::*;
use console::{
network::MainnetV0,
prelude::{Rng, TestRng, Uniform},
types::Field,
};
type CurrentNetwork = MainnetV0;
pub fn sample_transmission_ids(rng: &mut TestRng) -> Vec<TransmissionID<CurrentNetwork>> {
let mut sample = Vec::with_capacity(10);
for _ in 0..5 {
sample.push(TransmissionID::Solution(
SolutionID::from(rng.gen::<u64>()),
<CurrentNetwork as Network>::TransmissionChecksum::from(rng.gen::<u128>()),
));
}
for _ in 0..5 {
let id = TransmissionID::Transaction(
<CurrentNetwork as Network>::TransactionID::from(Field::rand(rng)),
<CurrentNetwork as Network>::TransmissionChecksum::from(rng.gen::<u128>()),
);
sample.push(id);
}
sample
}
}