diff --git a/rust/cardano-blockchain-types/src/fork.rs b/rust/cardano-blockchain-types/src/fork.rs index d65a08196a..9679fb576a 100644 --- a/rust/cardano-blockchain-types/src/fork.rs +++ b/rust/cardano-blockchain-types/src/fork.rs @@ -6,6 +6,8 @@ //! //! 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)] @@ -13,6 +15,31 @@ use crate::conversion::from_saturating; 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 == &Self::IMMUTABLE + } + + /// Is the fork for backfill data. + #[must_use] + pub fn is_backfill(&self) -> bool { + self == &Self::BACKFILL + } + + /// Is the fork for live data. + #[must_use] + pub fn is_live(&self) -> bool { + self >= &Self::FIRST_LIVE + } + /// Convert an `` to `Fork` (saturate if out of range). pub fn from_saturating< T: Copy @@ -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 for Fork { fn from(value: u64) -> Self { Self(value) diff --git a/rust/cardano-blockchain-types/src/multi_era_block_data.rs b/rust/cardano-blockchain-types/src/multi_era_block_data.rs index ee06c26fa9..612fa32f38 100644 --- a/rust/cardano-blockchain-types/src/multi_era_block_data.rs +++ b/rust/cardano-blockchain-types/src/multi_era_block_data.rs @@ -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 {