Skip to content

Commit e027d8b

Browse files
committed
fix(cardano-blockchain-types): Fork type
Signed-off-by: bkioshn <[email protected]>
1 parent aa6bd36 commit e027d8b

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Fork count is a counter that is incremented every time there is a roll-back in
2+
//! live-chain It is used to help followers determine how far to roll-back to
3+
//! resynchronize without storing full block history. The fork count starts at 1 for live
4+
//! blocks and increments if the live chain tip is purged due to a detected fork, but it
5+
//! does not track the exact number of forks reported by peers.
6+
//!
7+
//! Note: This fork terminology is different from fork in blockchain.
8+
9+
use crate::conversion::from_saturating;
10+
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
12+
/// Counter that is incremented every time there is a roll-back in live-chain
13+
pub struct Fork(u64);
14+
15+
impl Fork {
16+
/// Convert an `<T>` to Fork. (saturate if out of range.)
17+
pub fn from_saturating<
18+
T: Copy
19+
+ TryInto<u64>
20+
+ std::ops::Sub<Output = T>
21+
+ std::cmp::PartialOrd<T>
22+
+ num_traits::identities::Zero,
23+
>(
24+
value: T,
25+
) -> Self {
26+
let value: u64 = from_saturating(value);
27+
Self(value)
28+
}
29+
}
30+
31+
impl From<u64> for Fork {
32+
fn from(value: u64) -> Self {
33+
Self(value)
34+
}
35+
}
36+
37+
impl From<Fork> for u64 {
38+
fn from(val: Fork) -> Self {
39+
val.0
40+
}
41+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
mod auxdata;
44
pub mod conversion;
5+
mod fork;
56
pub mod hashes;
67
mod multi_era_block_data;
78
mod network;

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::{
1818
auxdata::{
1919
block::BlockAuxData, metadatum_label::MetadatumLabel, metadatum_value::MetadatumValue,
2020
},
21+
fork::Fork,
2122
network::Network,
2223
point::Point,
2324
txn_index::TxnIndex,
@@ -80,7 +81,7 @@ pub struct MultiEraBlock {
8081
/// It starts at 1 for live blocks, and is only incremented if the live-chain tip is
8182
/// purged because of a detected fork based on data received from the peer node.
8283
/// It does NOT count the strict number of forks reported by the peer node.
83-
fork: u64,
84+
fork: Fork,
8485
/// The Immutable decoded data about the block itself.
8586
inner: Arc<MultiEraBlockInner>,
8687
}
@@ -92,7 +93,7 @@ impl MultiEraBlock {
9293
///
9394
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
9495
fn new_block(
95-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: u64,
96+
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
9697
) -> anyhow::Result<Self> {
9798
let builder = SelfReferencedMultiEraBlockTryBuilder {
9899
raw_data,
@@ -155,13 +156,13 @@ impl MultiEraBlock {
155156
///
156157
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
157158
pub fn new(
158-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: u64,
159+
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
159160
) -> anyhow::Result<Self> {
160161
MultiEraBlock::new_block(chain, raw_data, previous, fork)
161162
}
162163

163164
/// Remake the block on a new fork.
164-
pub fn set_fork(&mut self, fork: u64) {
165+
pub fn set_fork(&mut self, fork: Fork) {
165166
self.fork = fork;
166167
}
167168

@@ -212,7 +213,7 @@ impl MultiEraBlock {
212213
/// `true` if the block is immutable, `false` otherwise.
213214
#[must_use]
214215
pub fn immutable(&self) -> bool {
215-
self.fork == 0
216+
self.fork == 0.into()
216217
}
217218

218219
/// What fork is the block from.
@@ -226,7 +227,7 @@ impl MultiEraBlock {
226227
/// # Returns
227228
/// The fork the block was found on.
228229
#[must_use]
229-
pub fn fork(&self) -> u64 {
230+
pub fn fork(&self) -> Fork {
230231
self.fork
231232
}
232233

@@ -288,7 +289,7 @@ impl Display for MultiEraBlock {
288289
let fork = if self.immutable() {
289290
"Immutable".to_string()
290291
} else {
291-
format!("Fork: {fork}")
292+
format!("Fork: {fork:?}")
292293
};
293294

294295
let block_era = match block {
@@ -452,8 +453,12 @@ pub(crate) mod tests {
452453
.into(),
453454
);
454455

455-
let block =
456-
MultiEraBlock::new(Network::Preprod, test_block.raw.clone(), &previous_point, 1);
456+
let block = MultiEraBlock::new(
457+
Network::Preprod,
458+
test_block.raw.clone(),
459+
&previous_point,
460+
1.into(),
461+
);
457462

458463
assert!(block.is_err());
459464
}
@@ -471,8 +476,12 @@ pub(crate) mod tests {
471476
let previous_point =
472477
Point::new((pallas_block.slot() - 1).into(), vec![0; 32].try_into()?);
473478

474-
let block =
475-
MultiEraBlock::new(Network::Preprod, test_block.raw.clone(), &previous_point, 1);
479+
let block = MultiEraBlock::new(
480+
Network::Preprod,
481+
test_block.raw.clone(),
482+
&previous_point,
483+
1.into(),
484+
);
476485

477486
assert!(block.is_err());
478487
}
@@ -496,8 +505,12 @@ pub(crate) mod tests {
496505
.into(),
497506
);
498507

499-
let block =
500-
MultiEraBlock::new(Network::Preprod, test_block.raw.clone(), &previous_point, 1)?;
508+
let block = MultiEraBlock::new(
509+
Network::Preprod,
510+
test_block.raw.clone(),
511+
&previous_point,
512+
1.into(),
513+
)?;
501514

502515
assert_eq!(block.decode().hash(), pallas_block.hash());
503516
}
@@ -523,7 +536,7 @@ pub(crate) mod tests {
523536
})
524537
.expect("cannot create point");
525538

526-
MultiEraBlock::new(Network::Preprod, block.clone(), &prev_point, 1)
539+
MultiEraBlock::new(Network::Preprod, block.clone(), &prev_point, 1.into())
527540
.expect("cannot create multi-era block")
528541
})
529542
.collect()
@@ -720,7 +733,7 @@ pub(crate) mod tests {
720733
Network::Preprod,
721734
test_block.raw.clone(),
722735
&test_block.previous,
723-
1,
736+
1.into(),
724737
);
725738

726739
assert!(block.is_ok());

0 commit comments

Comments
 (0)