hotshot_new_protocol/
helpers.rs1use std::{fmt::Display, ops::RangeInclusive};
2
3use committable::{Commitment, Committable};
4use hotshot_types::{
5 data::{EpochNumber, Leaf2, ViewNumber},
6 simple_certificate::{LightClientStateUpdateCertificateV2, check_qc_state_cert_correspondence},
7 traits::node_implementation::NodeType,
8};
9
10use crate::message::Proposal;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct ViewEpoch(pub ViewNumber, pub EpochNumber);
20
21impl ViewEpoch {
22 pub const fn start(view: ViewNumber) -> Self {
24 Self(view, EpochNumber::new(0))
25 }
26
27 pub fn at_view(view: ViewNumber) -> RangeInclusive<Self> {
29 Self::start(view)..=Self(view, EpochNumber::new(u64::MAX))
30 }
31}
32
33impl Display for ViewEpoch {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 write!(f, "{}@{}", self.1, self.0)
36 }
37}
38
39pub fn proposal_commitment<T: NodeType>(proposal: &Proposal<T>) -> Commitment<Leaf2<T>> {
40 let leaf: Leaf2<T> = proposal.clone().into();
41 leaf.commit()
42}
43
44pub fn validated_state_cert<T: NodeType>(
57 proposal: &Proposal<T>,
58 epoch_height: u64,
59) -> Option<&LightClientStateUpdateCertificateV2<T>> {
60 let state_cert = proposal.state_cert.as_ref()?;
61 check_qc_state_cert_correspondence(&proposal.justify_qc, state_cert, epoch_height)
62 .then_some(state_cert)
63}
64
65#[cfg(test)]
76pub fn test_version() -> versions::Version {
77 if let Some(version) = PINNED_TEST_VERSION.get() {
78 return version;
79 }
80 match std::env::var("NP_TEST_VERSION").as_deref() {
81 Ok("0.7") => versions::TIMEOUT_EPOCH_VERSION,
82 _ => versions::NEW_PROTOCOL_VERSION,
83 }
84}
85
86#[cfg(test)]
87thread_local! {
88 static PINNED_TEST_VERSION: std::cell::Cell<Option<versions::Version>> =
89 const { std::cell::Cell::new(None) };
90}
91
92#[cfg(test)]
97pub fn pin_test_version(version: versions::Version) {
98 PINNED_TEST_VERSION.set(Some(version));
99}
100
101#[cfg(test)]
102pub fn test_upgrade_lock<T: NodeType>() -> hotshot_types::message::UpgradeLock<T> {
103 hotshot_types::message::UpgradeLock::new(versions::Upgrade::trivial(test_version()))
104}
105
106#[cfg(test)]
109pub fn test_timeout_epoch_lock<T: NodeType>() -> hotshot_types::message::UpgradeLock<T> {
110 use versions::{TIMEOUT_EPOCH_VERSION, Upgrade};
111
112 hotshot_types::message::UpgradeLock::new(Upgrade::trivial(TIMEOUT_EPOCH_VERSION))
113}