Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions rust/cardano-blockchain-types/src/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,40 @@
//!
//! Note: This fork terminology is different from fork in blockchain.

use std::fmt;

use crate::conversion::from_saturating;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd)]
/// Counter that is incremented every time there is a roll-back in live-chain.
pub struct Fork(u64);

impl Fork {
/// Fork for data that read from the blockchain during a backfill on initial sync
pub const BACKFILL: Self = Self(1);
/// Fork count for the first live block.
pub const FIRST_LIVE: Self = Self(2);
/// Fork for immutable data. This indicates that there is no roll-back.
pub const IMMUTABLE: Self = Self(0);

/// Is the fork for immutable data.
#[must_use]
pub fn is_immutable(&self) -> bool {
self.0 == 0
}

/// Is the fork for backfill data.
#[must_use]
pub fn is_backfill(&self) -> bool {
self.0 == 1
}

/// Is the fork for live data.
#[must_use]
pub fn is_live(&self) -> bool {
self.0 > 1
}

/// Convert an `<T>` to `Fork` (saturate if out of range).
pub fn from_saturating<
T: Copy
Expand All @@ -38,6 +65,17 @@ impl Fork {
}
}

impl fmt::Display for Fork {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
0 => write!(f, "IMMUTABLE"),
1 => write!(f, "BACKFILL"),
// For live forks: 2 maps to LIVE:1, 3 maps to LIVE:2 etc.
2..=u64::MAX => write!(f, "LIVE:{}", self.0 - 1),
}
}
}

impl From<u64> for Fork {
fn from(value: u64) -> Self {
Self(value)
Expand Down
6 changes: 6 additions & 0 deletions rust/cardano-blockchain-types/src/multi_era_block_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ impl MultiEraBlock {

None
}

/// Get the auxiliary data of the block.
#[must_use]
pub fn aux_data(&self) -> &BlockAuxData {
&self.inner.aux_data
}
}

impl Display for MultiEraBlock {
Expand Down
Loading