Skip to main content

hotshot_types/
utils.rs

1// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
2// This file is part of the HotShot repository.
3
4// You should have received a copy of the MIT License
5// along with the HotShot repository. If not, see <https://mit-license.org/>.
6
7//! Utility functions, type aliases, helper structs and enum definitions.
8
9use std::{
10    hash::{Hash, Hasher},
11    ops::Deref,
12    sync::Arc,
13};
14
15use anyhow::{anyhow, ensure};
16use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
17use bincode::{
18    DefaultOptions, Options,
19    config::{
20        FixintEncoding, LittleEndian, RejectTrailing, WithOtherEndian, WithOtherIntEncoding,
21        WithOtherLimit, WithOtherTrailing,
22    },
23};
24use committable::{Commitment, Committable};
25use digest::OutputSizeUser;
26use serde::{Deserialize, Serialize};
27use sha2::Digest;
28use tagged_base64::tagged;
29use typenum::Unsigned;
30use vbs::version::Version;
31use versions::EPOCH_VERSION;
32
33use crate::{
34    data::{EpochNumber, Leaf2, VidCommitment, ViewNumber},
35    epoch_membership::EpochMembershipCoordinator,
36    message::UpgradeLock,
37    simple_certificate::QuorumCertificate2,
38    simple_vote::HasEpoch,
39    stake_table::StakeTableEntries,
40    traits::{ValidatedState, node_implementation::NodeType},
41    vote::{Certificate, HasViewNumber},
42};
43
44/// A view's state
45#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
46#[serde(bound = "")]
47pub enum ViewInner<TYPES: NodeType> {
48    /// A pending view with an available block but not leaf proposal yet.
49    ///
50    /// Storing this state allows us to garbage collect blocks for views where a proposal is never
51    /// made. This saves memory when a leader fails and subverts a DoS attack where malicious
52    /// leaders repeatedly request availability for blocks that they never propose.
53    Da {
54        /// Payload commitment to the available block.
55        payload_commitment: VidCommitment,
56        /// An epoch to which the data belongs to. Relevant for validating against the correct stake table
57        epoch: Option<EpochNumber>,
58    },
59    /// Undecided view
60    Leaf {
61        /// Proposed leaf
62        leaf: LeafCommitment<TYPES>,
63        /// Validated state.
64        state: Arc<TYPES::ValidatedState>,
65        /// Optional state delta.
66        delta: Option<Arc<<TYPES::ValidatedState as ValidatedState<TYPES>>::Delta>>,
67        /// An epoch to which the data belongs to. Relevant for validating against the correct stake table
68        epoch: Option<EpochNumber>,
69    },
70    /// Leaf has failed
71    Failed,
72}
73impl<TYPES: NodeType> Clone for ViewInner<TYPES> {
74    fn clone(&self) -> Self {
75        match self {
76            Self::Da {
77                payload_commitment,
78                epoch,
79            } => Self::Da {
80                payload_commitment: *payload_commitment,
81                epoch: *epoch,
82            },
83            Self::Leaf {
84                leaf,
85                state,
86                delta,
87                epoch,
88            } => Self::Leaf {
89                leaf: *leaf,
90                state: Arc::clone(state),
91                delta: delta.clone(),
92                epoch: *epoch,
93            },
94            Self::Failed => Self::Failed,
95        }
96    }
97}
98/// The hash of a leaf.
99pub type LeafCommitment<TYPES> = Commitment<Leaf2<TYPES>>;
100
101/// Optional validated state and state delta.
102pub type StateAndDelta<TYPES> = (
103    Option<Arc<<TYPES as NodeType>::ValidatedState>>,
104    Option<Arc<<<TYPES as NodeType>::ValidatedState as ValidatedState<TYPES>>::Delta>>,
105);
106
107pub async fn verify_leaf_chain<T: NodeType>(
108    mut leaf_chain: Vec<Leaf2<T>>,
109    coordinator: &EpochMembershipCoordinator<T>,
110    expected_height: u64,
111    upgrade_lock: &UpgradeLock<T>,
112) -> anyhow::Result<Leaf2<T>> {
113    let epoch_height = *coordinator.epoch_height();
114    // Derive each QC's epoch from the height of the leaf it certifies; trusting
115    // the epoch claimed in the QC would let a stale epoch's quorum pick the
116    // stake table that verifies its own signatures.
117    let validate_qc = |qc: QuorumCertificate2<T>, certified_height: u64| -> anyhow::Result<()> {
118        let epoch = EpochNumber::new(epoch_from_block_number(certified_height, epoch_height));
119        ensure!(
120            qc.data.epoch() == Some(epoch),
121            "QC claims epoch {:?} but certifies the leaf at height {certified_height} in epoch \
122             {epoch}",
123            qc.data.epoch(),
124        );
125        if let Some(block_number) = qc.data.block_number {
126            ensure!(
127                block_number == certified_height,
128                "QC claims block number {block_number} but certifies the leaf at height \
129                 {certified_height}"
130            );
131        }
132        let membership = coordinator
133            .stake_table_for_epoch(Some(epoch))
134            .map_err(|err| anyhow!("no stake table available for epoch {epoch}: {err:?}"))?;
135        let entries = StakeTableEntries::<T>::from_iter(membership.stake_table()).0;
136        qc.is_valid_cert(&entries, membership.success_threshold(), upgrade_lock)?;
137        Ok(())
138    };
139
140    // Sort the leaf chain by view number
141    leaf_chain.sort_by_key(|l| l.view_number());
142    // Reverse it
143    leaf_chain.reverse();
144
145    // Check we actually have a chain long enough for deciding
146    if leaf_chain.len() < 3 {
147        return Err(anyhow!("Leaf chain is not long enough for a decide"));
148    }
149
150    let newest_leaf = leaf_chain.first().unwrap();
151    let parent = &leaf_chain[1];
152    let grand_parent = &leaf_chain[2];
153
154    // Check if the leaves form a decide
155    if newest_leaf.justify_qc().view_number() != parent.view_number()
156        || parent.justify_qc().view_number() != grand_parent.view_number()
157    {
158        return Err(anyhow!("Leaf views do not chain"));
159    }
160    if newest_leaf.justify_qc().data.leaf_commit != parent.commit()
161        || parent.justify_qc().data().leaf_commit != grand_parent.commit()
162    {
163        return Err(anyhow!("Leaf commits do not chain"));
164    }
165    if parent.view_number() != grand_parent.view_number() + 1 {
166        return Err(anyhow::anyhow!(
167            "Decide rule failed, parent does not directly extend grandparent"
168        ));
169    }
170
171    // Verify the root is in the chain of decided leaves
172    validate_qc(newest_leaf.justify_qc(), parent.height())?;
173    let mut last_leaf = parent;
174    for leaf in leaf_chain.iter().skip(2) {
175        ensure!(last_leaf.justify_qc().view_number() == leaf.view_number());
176        ensure!(last_leaf.justify_qc().data().leaf_commit == leaf.commit());
177        validate_qc(last_leaf.justify_qc(), leaf.height())?;
178        if leaf.height() == expected_height {
179            return Ok(leaf.clone());
180        }
181        last_leaf = leaf;
182    }
183    Err(anyhow!("Epoch Root was not found in the decided chain"))
184}
185
186impl<TYPES: NodeType> ViewInner<TYPES> {
187    /// Return the underlying undecide leaf commitment and validated state if they exist.
188    #[must_use]
189    pub fn leaf_and_state(&self) -> Option<(LeafCommitment<TYPES>, &Arc<TYPES::ValidatedState>)> {
190        if let Self::Leaf { leaf, state, .. } = self {
191            Some((*leaf, state))
192        } else {
193            None
194        }
195    }
196
197    /// return the underlying leaf hash if it exists
198    #[must_use]
199    pub fn leaf_commitment(&self) -> Option<LeafCommitment<TYPES>> {
200        if let Self::Leaf { leaf, .. } = self {
201            Some(*leaf)
202        } else {
203            None
204        }
205    }
206
207    /// return the underlying validated state if it exists
208    #[must_use]
209    pub fn state(&self) -> Option<&Arc<TYPES::ValidatedState>> {
210        if let Self::Leaf { state, .. } = self {
211            Some(state)
212        } else {
213            None
214        }
215    }
216
217    /// Return the underlying validated state and state delta if they exist.
218    #[must_use]
219    pub fn state_and_delta(&self) -> StateAndDelta<TYPES> {
220        if let Self::Leaf { state, delta, .. } = self {
221            (Some(Arc::clone(state)), delta.clone())
222        } else {
223            (None, None)
224        }
225    }
226
227    /// return the underlying block payload commitment if it exists
228    #[must_use]
229    pub fn payload_commitment(&self) -> Option<VidCommitment> {
230        if let Self::Da {
231            payload_commitment, ..
232        } = self
233        {
234            Some(*payload_commitment)
235        } else {
236            None
237        }
238    }
239
240    /// Returns `Epoch` if possible
241    // #3967 REVIEW NOTE: This type is kinda ugly, should we Result<Option<Epoch>> instead?
242    pub fn epoch(&self) -> Option<Option<EpochNumber>> {
243        match self {
244            Self::Da { epoch, .. } | Self::Leaf { epoch, .. } => Some(*epoch),
245            Self::Failed => None,
246        }
247    }
248}
249
250impl<TYPES: NodeType> Deref for View<TYPES> {
251    type Target = ViewInner<TYPES>;
252
253    fn deref(&self) -> &Self::Target {
254        &self.view_inner
255    }
256}
257
258/// This exists so we can perform state transitions mutably
259#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
260#[serde(bound = "")]
261pub struct View<TYPES: NodeType> {
262    /// The view data. Wrapped in a struct so we can mutate
263    pub view_inner: ViewInner<TYPES>,
264}
265
266/// A struct containing information about a finished round.
267#[derive(Debug, Clone)]
268pub struct RoundFinishedEvent {
269    /// The round that finished
270    pub view_number: ViewNumber,
271}
272
273/// Whether or not to stop inclusively or exclusively when walking
274#[derive(Copy, Clone, Debug)]
275pub enum Terminator<T> {
276    /// Stop right before this view number
277    Exclusive(T),
278    /// Stop including this view number
279    Inclusive(T),
280}
281
282/// Type alias for byte array of SHA256 digest length
283type Sha256Digest = [u8; <sha2::Sha256 as OutputSizeUser>::OutputSize::USIZE];
284
285#[tagged("BUILDER_COMMITMENT")]
286#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize)]
287/// Commitment that builders use to sign block options.
288/// A thin wrapper around a Sha256 digest.
289pub struct BuilderCommitment(Sha256Digest);
290
291impl BuilderCommitment {
292    /// Create new commitment for `data`
293    pub fn from_bytes(data: impl AsRef<[u8]>) -> Self {
294        Self(sha2::Sha256::digest(data.as_ref()).into())
295    }
296
297    /// Create a new commitment from a raw Sha256 digest
298    pub fn from_raw_digest(digest: impl Into<Sha256Digest>) -> Self {
299        Self(digest.into())
300    }
301}
302
303impl AsRef<Sha256Digest> for BuilderCommitment {
304    fn as_ref(&self) -> &Sha256Digest {
305        &self.0
306    }
307}
308
309type BincodeOpts = WithOtherTrailing<
310    WithOtherIntEncoding<
311        WithOtherEndian<WithOtherLimit<DefaultOptions, bincode::config::Infinite>, LittleEndian>,
312        FixintEncoding,
313    >,
314    RejectTrailing,
315>;
316
317/// For the wire format, we use bincode with the following options:
318///   - No upper size limit
319///   - Little endian encoding
320///   - Varint encoding
321///   - Reject trailing bytes
322#[must_use]
323pub fn bincode_opts() -> BincodeOpts {
324    bincode::DefaultOptions::new()
325        .with_no_limit()
326        .with_little_endian()
327        .with_fixint_encoding()
328        .reject_trailing_bytes()
329}
330
331/// Returns an epoch number given a block number and an epoch height
332#[must_use]
333pub fn epoch_from_block_number(block_number: u64, epoch_height: u64) -> u64 {
334    if epoch_height == 0 {
335        0
336    } else if block_number == 0 {
337        1
338    } else if block_number.is_multiple_of(epoch_height) {
339        block_number / epoch_height
340    } else {
341        block_number / epoch_height + 1
342    }
343}
344
345/// Returns the block number of the epoch root in the given epoch
346///
347/// WARNING: This is NOT the root block for the given epoch.
348/// To find that root block number for epoch e, call `root_block_in_epoch(e-2,_)`.
349#[must_use]
350pub fn root_block_in_epoch(epoch: u64, epoch_height: u64) -> u64 {
351    if epoch_height == 0 || epoch < 1 {
352        0
353    } else {
354        epoch_height * epoch - 5
355    }
356}
357
358/// Get the block height of the transition block for the given epoch
359///
360/// This is the height at which we begin the transition to LEAVE the specified epoch
361#[must_use]
362pub fn transition_block_for_epoch(epoch: u64, epoch_height: u64) -> u64 {
363    if epoch_height == 0 || epoch < 1 {
364        0
365    } else {
366        epoch_height * epoch - 3
367    }
368}
369
370/// Returns an `Option<Epoch>` based on a boolean condition of whether or not epochs are enabled, a block number,
371/// and the epoch height. If epochs are disabled or the epoch height is zero, returns None.
372#[must_use]
373pub fn option_epoch_from_block_number(
374    with_epoch: bool,
375    block_number: u64,
376    epoch_height: u64,
377) -> Option<EpochNumber> {
378    if with_epoch {
379        if epoch_height == 0 {
380            None
381        } else if block_number == 0 {
382            Some(1u64)
383        } else if block_number.is_multiple_of(epoch_height) {
384            Some(block_number / epoch_height)
385        } else {
386            Some(block_number / epoch_height + 1)
387        }
388        .map(EpochNumber::new)
389    } else {
390        None
391    }
392}
393
394/// Returns Some(1) if epochs are enabled by `base`, otherwise returns None
395#[must_use]
396pub fn genesis_epoch_from_version(base: Version) -> Option<EpochNumber> {
397    (base >= EPOCH_VERSION).then(|| EpochNumber::new(1))
398}
399
400/// A function for generating a cute little user mnemonic from a hash
401#[must_use]
402pub fn mnemonic<H: Hash>(bytes: H) -> String {
403    let mut state = std::collections::hash_map::DefaultHasher::new();
404    bytes.hash(&mut state);
405    mnemonic::to_string(state.finish().to_le_bytes())
406}
407
408/// A helper enum to indicate whether a node is in the epoch transition
409/// A node is in epoch transition when its high QC is for the last block in an epoch
410#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
411pub enum EpochTransitionIndicator {
412    /// A node is currently in the epoch transition
413    InTransition,
414    /// A node is not in the epoch transition
415    NotInTransition,
416}
417
418/// Return true if the given block number is the final full block, the "transition block"
419#[must_use]
420pub fn is_transition_block(block_number: u64, epoch_height: u64) -> bool {
421    if block_number == 0 || epoch_height == 0 {
422        false
423    } else {
424        (block_number + 3).is_multiple_of(epoch_height)
425    }
426}
427/// returns true if it's the first transition block (epoch height - 2)
428#[must_use]
429pub fn is_first_transition_block(block_number: u64, epoch_height: u64) -> bool {
430    if block_number == 0 || epoch_height == 0 {
431        false
432    } else {
433        block_number % epoch_height == epoch_height - 2
434    }
435}
436/// Returns true if the block is part of the epoch transition (including the last non null block)
437#[must_use]
438pub fn is_epoch_transition(block_number: u64, epoch_height: u64) -> bool {
439    if block_number == 0 || epoch_height == 0 {
440        false
441    } else {
442        block_number % epoch_height >= epoch_height - 3 || block_number.is_multiple_of(epoch_height)
443    }
444}
445
446/// Returns true if the block is the last block in the epoch
447#[must_use]
448pub fn is_last_block(block_number: u64, epoch_height: u64) -> bool {
449    if block_number == 0 || epoch_height == 0 {
450        false
451    } else {
452        block_number.is_multiple_of(epoch_height)
453    }
454}
455
456/// Returns true if the block number is in trasntion but not the transition block
457/// or the last block in the epoch.
458///
459/// This function is useful for determining if a proposal extending this QC must follow
460/// the special rules for transition blocks.
461#[must_use]
462pub fn is_middle_transition_block(block_number: u64, epoch_height: u64) -> bool {
463    if block_number == 0 || epoch_height == 0 {
464        false
465    } else {
466        let blocks_left = epoch_height - (block_number % epoch_height);
467        blocks_left == 1 || blocks_left == 2
468    }
469}
470
471/// Returns true if the given block number is the third from the last in the epoch based on the
472/// given epoch height.
473#[must_use]
474pub fn is_epoch_root(block_number: u64, epoch_height: u64) -> bool {
475    if block_number == 0 || epoch_height == 0 {
476        false
477    } else {
478        (block_number + 5).is_multiple_of(epoch_height)
479    }
480}
481
482/// Returns true if the given block number is equal or greater than the epoch root block
483#[must_use]
484pub fn is_ge_epoch_root(block_number: u64, epoch_height: u64) -> bool {
485    if block_number == 0 || epoch_height == 0 {
486        false
487    } else {
488        block_number.is_multiple_of(epoch_height) || block_number % epoch_height >= epoch_height - 5
489    }
490}
491
492/// Returns true if the given block number is strictly greater than the epoch root block
493pub fn is_gt_epoch_root(block_number: u64, epoch_height: u64) -> bool {
494    if block_number == 0 || epoch_height == 0 {
495        false
496    } else {
497        block_number.is_multiple_of(epoch_height) || block_number % epoch_height > epoch_height - 5
498    }
499}
500
501#[cfg(test)]
502mod test {
503    use super::*;
504
505    #[test]
506    fn test_epoch_from_block_number() {
507        // block 0 is always epoch 1
508        let epoch = epoch_from_block_number(0, 10);
509        assert_eq!(1, epoch);
510
511        let epoch = epoch_from_block_number(1, 10);
512        assert_eq!(1, epoch);
513
514        let epoch = epoch_from_block_number(10, 10);
515        assert_eq!(1, epoch);
516
517        let epoch = epoch_from_block_number(11, 10);
518        assert_eq!(2, epoch);
519
520        let epoch = epoch_from_block_number(20, 10);
521        assert_eq!(2, epoch);
522
523        let epoch = epoch_from_block_number(21, 10);
524        assert_eq!(3, epoch);
525
526        let epoch = epoch_from_block_number(21, 0);
527        assert_eq!(0, epoch);
528    }
529
530    #[test]
531    fn test_is_last_block_in_epoch() {
532        assert!(!is_epoch_transition(5, 10));
533        assert!(!is_epoch_transition(6, 10));
534        assert!(is_epoch_transition(7, 10));
535        assert!(is_epoch_transition(8, 10));
536        assert!(is_epoch_transition(9, 10));
537        assert!(is_epoch_transition(10, 10));
538        assert!(!is_epoch_transition(11, 10));
539
540        assert!(!is_epoch_transition(10, 0));
541    }
542
543    #[test]
544    fn test_is_epoch_root() {
545        assert!(is_epoch_root(5, 10));
546        assert!(!is_epoch_root(6, 10));
547        assert!(!is_epoch_root(7, 10));
548        assert!(!is_epoch_root(8, 10));
549        assert!(!is_epoch_root(9, 10));
550        assert!(!is_epoch_root(10, 10));
551        assert!(!is_epoch_root(11, 10));
552
553        assert!(!is_epoch_transition(10, 0));
554    }
555
556    #[test]
557    fn test_root_block_in_epoch() {
558        // block 0 is always epoch 0
559        let epoch = 3;
560        let epoch_height = 10;
561        let epoch_root_block_number = root_block_in_epoch(3, epoch_height);
562
563        assert!(is_epoch_root(25, epoch_height));
564
565        assert_eq!(epoch_root_block_number, 25);
566
567        assert_eq!(
568            epoch,
569            epoch_from_block_number(epoch_root_block_number, epoch_height)
570        );
571    }
572}