Skip to content

Commit aa6bd36

Browse files
committed
fix(cardano-blockchain-types): point new should take type Slot and Blake2bHash
Signed-off-by: bkioshn <[email protected]>
1 parent c9185b5 commit aa6bd36

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ impl<const BYTES: usize> From<Hash<BYTES>> for Blake2bHash<BYTES> {
6262
}
6363
}
6464

65+
impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Vec<u8> {
66+
fn from(val: Blake2bHash<BYTES>) -> Self {
67+
val.0.to_vec()
68+
}
69+
}
70+
6571
impl<const BYTES: usize> TryFrom<&[u8]> for Blake2bHash<BYTES> {
6672
type Error = anyhow::Error;
6773

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl MultiEraBlock {
107107

108108
let slot = decoded_block.slot();
109109

110-
let point = Point::new(slot, decoded_block.hash().to_vec());
110+
let point = Point::new(slot.into(), decoded_block.hash().into());
111111

112112
let byron_block = matches!(
113113
decoded_block,
@@ -444,12 +444,12 @@ pub(crate) mod tests {
444444
pallas::ledger::traverse::MultiEraBlock::decode(test_block.raw.as_slice())?;
445445

446446
let previous_point = Point::new(
447-
pallas_block.slot().add(i as u64),
447+
pallas_block.slot().add(i as u64).into(),
448448
pallas_block
449449
.header()
450450
.previous_hash()
451451
.expect("cannot get previous hash")
452-
.to_vec(),
452+
.into(),
453453
);
454454

455455
let block =
@@ -468,7 +468,8 @@ pub(crate) mod tests {
468468
let pallas_block =
469469
pallas::ledger::traverse::MultiEraBlock::decode(test_block.raw.as_slice())?;
470470

471-
let previous_point = Point::new(pallas_block.slot() - 1, vec![0; 32]);
471+
let previous_point =
472+
Point::new((pallas_block.slot() - 1).into(), vec![0; 32].try_into()?);
472473

473474
let block =
474475
MultiEraBlock::new(Network::Preprod, test_block.raw.clone(), &previous_point, 1);
@@ -487,12 +488,12 @@ pub(crate) mod tests {
487488
pallas::ledger::traverse::MultiEraBlock::decode(test_block.raw.as_slice())?;
488489

489490
let previous_point = Point::new(
490-
pallas_block.slot() - 1,
491+
(pallas_block.slot() - 1).into(),
491492
pallas_block
492493
.header()
493494
.previous_hash()
494495
.expect("cannot get previous hash")
495-
.to_vec(),
496+
.into(),
496497
);
497498

498499
let block =
@@ -512,12 +513,12 @@ pub(crate) mod tests {
512513
let prev_point = pallas::ledger::traverse::MultiEraBlock::decode(block.as_slice())
513514
.map(|block| {
514515
Point::new(
515-
block.slot() - 1,
516+
(block.slot() - 1).into(),
516517
block
517518
.header()
518519
.previous_hash()
519520
.expect("cannot get previous hash")
520-
.to_vec(),
521+
.into(),
521522
)
522523
})
523524
.expect("cannot create point");
@@ -536,12 +537,12 @@ pub(crate) mod tests {
536537
pallas::ledger::traverse::MultiEraBlock::decode(block.as_slice())
537538
.map(|block| {
538539
Point::new(
539-
block.slot(),
540+
block.slot().into(),
540541
block
541542
.header()
542543
.previous_hash()
543544
.expect("cannot get previous hash")
544-
.to_vec(),
545+
.into(),
545546
)
546547
})
547548
.expect("cannot create point")

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use std::{
1010

1111
use pallas::crypto::hash::Hash;
1212

13+
use crate::{hashes::Blake2bHash, Slot};
14+
1315
/// A specific point in the blockchain. It can be used to
1416
/// identify a particular location within the blockchain, such as the tip (the
1517
/// most recent block) or any other block. It has special kinds of `Point`,
@@ -78,8 +80,8 @@ impl Point {
7880
///
7981
/// # Parameters
8082
///
81-
/// * `slot` - A `u64` value representing the slot number in the blockchain.
82-
/// * `hash` - A `Vec<u8>` containing the hash of the block at the specified slot.
83+
/// * `slot` - A `Slot` representing the slot number in the blockchain.
84+
/// * `hash` - A `Blake2bHash` size 32, block hash at the specified slot.
8385
///
8486
/// # Returns
8587
///
@@ -95,8 +97,11 @@ impl Point {
9597
/// let point = Point::new(slot, hash);
9698
/// ```
9799
#[must_use]
98-
pub fn new(slot: u64, hash: Vec<u8>) -> Self {
99-
Self(pallas::network::miniprotocols::Point::Specific(slot, hash))
100+
pub fn new(slot: Slot, hash: Blake2bHash<32>) -> Self {
101+
Self(pallas::network::miniprotocols::Point::Specific(
102+
slot.into(),
103+
hash.into(),
104+
))
100105
}
101106

102107
/// Creates a new `Point` instance representing a specific
@@ -106,7 +111,7 @@ impl Point {
106111
///
107112
/// # Parameters
108113
///
109-
/// * `slot` - A `u64` value representing the slot number in the blockchain.
114+
/// * `slot` - A `Slot` representing the slot number in the blockchain.
110115
///
111116
/// # Returns
112117
///
@@ -121,9 +126,9 @@ impl Point {
121126
/// let point = Point::fuzzy(slot);
122127
/// ```
123128
#[must_use]
124-
pub fn fuzzy(slot: u64) -> Self {
129+
pub fn fuzzy(slot: Slot) -> Self {
125130
Self(pallas::network::miniprotocols::Point::Specific(
126-
slot,
131+
slot.into(),
127132
Vec::new(),
128133
))
129134
}
@@ -137,7 +142,9 @@ impl Point {
137142
Self::TIP
138143
} else {
139144
match self.0 {
140-
pallas::network::miniprotocols::Point::Specific(slot, _) => Self::fuzzy(slot),
145+
pallas::network::miniprotocols::Point::Specific(slot, _) => {
146+
Self::fuzzy(slot.into())
147+
},
141148
pallas::network::miniprotocols::Point::Origin => Self::ORIGIN,
142149
}
143150
}
@@ -500,16 +507,16 @@ mod tests {
500507

501508
#[test]
502509
fn test_create_points() {
503-
let point1 = Point::new(100u64, vec![]);
504-
let fuzzy1 = Point::fuzzy(100u64);
510+
let point1 = Point::new(100u64.into(), Blake2bHash::new(&[]));
511+
let fuzzy1 = Point::fuzzy(100u64.into());
505512

506513
assert!(point1 == fuzzy1);
507514
}
508515

509516
#[test]
510517
fn test_cmp_hash_simple() {
511518
let origin1 = Point::ORIGIN;
512-
let point1 = Point::new(100u64, vec![8; 32]);
519+
let point1 = Point::new(100u64.into(), [8; 32].into());
513520

514521
assert!(!origin1.cmp_hash(&Some(Hash::new([0; 32]))));
515522
assert!(origin1.cmp_hash(&None));
@@ -520,16 +527,16 @@ mod tests {
520527

521528
#[test]
522529
fn test_get_hash_simple() {
523-
let point1 = Point::new(100u64, vec![8; 32]);
530+
let point1 = Point::new(100u64.into(), [8; 32].into());
524531

525532
assert_eq!(point1.hash_or_default(), vec![8; 32]);
526533
}
527534

528535
#[test]
529536
fn test_identical_compare() {
530-
let point1 = Point::new(100u64, vec![8; 32]);
531-
let point2 = Point::new(100u64, vec![8; 32]);
532-
let point3 = Point::new(999u64, vec![8; 32]);
537+
let point1 = Point::new(100u64.into(), [8; 32].into());
538+
let point2 = Point::new(100u64.into(), [8; 32].into());
539+
let point3 = Point::new(999u64.into(), [8; 32].into());
533540

534541
assert!(point1.strict_eq(&point2));
535542
assert!(!point1.strict_eq(&point3));
@@ -541,9 +548,9 @@ mod tests {
541548
let origin2 = Point::ORIGIN;
542549
let tip1 = Point::TIP;
543550
let tip2 = Point::TIP;
544-
let early_block = Point::new(100u64, vec![]);
545-
let late_block1 = Point::new(5000u64, vec![]);
546-
let late_block2 = Point::new(5000u64, vec![]);
551+
let early_block = Point::new(100u64.into(), Blake2bHash::new(&[]));
552+
let late_block1 = Point::new(5000u64.into(), Blake2bHash::new(&[]));
553+
let late_block2 = Point::new(5000u64.into(), Blake2bHash::new(&[]));
547554

548555
assert!(origin1 == origin2);
549556
assert!(origin1 < early_block);

0 commit comments

Comments
 (0)