Skip to content

Commit 64529b5

Browse files
authored
fix(rust/cardano-blockchain-types): Add const, display, and condition check for Fork (#146)
* fix(cardano-blockchain-types): add const to fork and impl display Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): add condition func Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): linter and format Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): expose aux data from multierablock Signed-off-by: bkioshn <[email protected]> * fix(cardano-blockchain-types): use const Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]>
1 parent 3b4c144 commit 64529b5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

rust/cardano-blockchain-types/src/fork.rs

+38
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,40 @@
66
//!
77
//! Note: This fork terminology is different from fork in blockchain.
88
9+
use std::fmt;
10+
911
use crate::conversion::from_saturating;
1012

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

1517
impl Fork {
18+
/// Fork for data that read from the blockchain during a backfill on initial sync
19+
pub const BACKFILL: Self = Self(1);
20+
/// Fork count for the first live block.
21+
pub const FIRST_LIVE: Self = Self(2);
22+
/// Fork for immutable data. This indicates that there is no roll-back.
23+
pub const IMMUTABLE: Self = Self(0);
24+
25+
/// Is the fork for immutable data.
26+
#[must_use]
27+
pub fn is_immutable(&self) -> bool {
28+
self == &Self::IMMUTABLE
29+
}
30+
31+
/// Is the fork for backfill data.
32+
#[must_use]
33+
pub fn is_backfill(&self) -> bool {
34+
self == &Self::BACKFILL
35+
}
36+
37+
/// Is the fork for live data.
38+
#[must_use]
39+
pub fn is_live(&self) -> bool {
40+
self >= &Self::FIRST_LIVE
41+
}
42+
1643
/// Convert an `<T>` to `Fork` (saturate if out of range).
1744
pub fn from_saturating<
1845
T: Copy
@@ -38,6 +65,17 @@ impl Fork {
3865
}
3966
}
4067

68+
impl fmt::Display for Fork {
69+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70+
match self.0 {
71+
0 => write!(f, "IMMUTABLE"),
72+
1 => write!(f, "BACKFILL"),
73+
// For live forks: 2 maps to LIVE:1, 3 maps to LIVE:2 etc.
74+
2..=u64::MAX => write!(f, "LIVE:{}", self.0 - 1),
75+
}
76+
}
77+
}
78+
4179
impl From<u64> for Fork {
4280
fn from(value: u64) -> Self {
4381
Self(value)

rust/cardano-blockchain-types/src/multi_era_block_data.rs

+6
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ impl MultiEraBlock {
281281

282282
None
283283
}
284+
285+
/// Get the auxiliary data of the block.
286+
#[must_use]
287+
pub fn aux_data(&self) -> &BlockAuxData {
288+
&self.inner.aux_data
289+
}
284290
}
285291

286292
impl Display for MultiEraBlock {

0 commit comments

Comments
 (0)