Skip to content

Commit

Permalink
fix(cardano-blockchain-types): point new should take type Slot and Bl…
Browse files Browse the repository at this point in the history
…ake2bHash

Signed-off-by: bkioshn <[email protected]>
  • Loading branch information
bkioshn committed Dec 17, 2024
1 parent c9185b5 commit aa6bd36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
6 changes: 6 additions & 0 deletions rust/cardano-blockchain-types/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl<const BYTES: usize> From<Hash<BYTES>> for Blake2bHash<BYTES> {
}
}

impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Vec<u8> {
fn from(val: Blake2bHash<BYTES>) -> Self {
val.0.to_vec()
}
}

impl<const BYTES: usize> TryFrom<&[u8]> for Blake2bHash<BYTES> {
type Error = anyhow::Error;

Expand Down
21 changes: 11 additions & 10 deletions rust/cardano-blockchain-types/src/multi_era_block_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl MultiEraBlock {

let slot = decoded_block.slot();

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

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

let previous_point = Point::new(
pallas_block.slot().add(i as u64),
pallas_block.slot().add(i as u64).into(),
pallas_block
.header()
.previous_hash()
.expect("cannot get previous hash")
.to_vec(),
.into(),
);

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

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

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

let previous_point = Point::new(
pallas_block.slot() - 1,
(pallas_block.slot() - 1).into(),
pallas_block
.header()
.previous_hash()
.expect("cannot get previous hash")
.to_vec(),
.into(),
);

let block =
Expand All @@ -512,12 +513,12 @@ pub(crate) mod tests {
let prev_point = pallas::ledger::traverse::MultiEraBlock::decode(block.as_slice())
.map(|block| {
Point::new(
block.slot() - 1,
(block.slot() - 1).into(),
block
.header()
.previous_hash()
.expect("cannot get previous hash")
.to_vec(),
.into(),
)
})
.expect("cannot create point");
Expand All @@ -536,12 +537,12 @@ pub(crate) mod tests {
pallas::ledger::traverse::MultiEraBlock::decode(block.as_slice())
.map(|block| {
Point::new(
block.slot(),
block.slot().into(),
block
.header()
.previous_hash()
.expect("cannot get previous hash")
.to_vec(),
.into(),
)
})
.expect("cannot create point")
Expand Down
43 changes: 25 additions & 18 deletions rust/cardano-blockchain-types/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::{

use pallas::crypto::hash::Hash;

use crate::{hashes::Blake2bHash, Slot};

/// A specific point in the blockchain. It can be used to
/// identify a particular location within the blockchain, such as the tip (the
/// most recent block) or any other block. It has special kinds of `Point`,
Expand Down Expand Up @@ -78,8 +80,8 @@ impl Point {
///
/// # Parameters
///
/// * `slot` - A `u64` value representing the slot number in the blockchain.
/// * `hash` - A `Vec<u8>` containing the hash of the block at the specified slot.
/// * `slot` - A `Slot` representing the slot number in the blockchain.
/// * `hash` - A `Blake2bHash` size 32, block hash at the specified slot.
///
/// # Returns
///
Expand All @@ -95,8 +97,11 @@ impl Point {
/// let point = Point::new(slot, hash);
/// ```
#[must_use]
pub fn new(slot: u64, hash: Vec<u8>) -> Self {
Self(pallas::network::miniprotocols::Point::Specific(slot, hash))
pub fn new(slot: Slot, hash: Blake2bHash<32>) -> Self {
Self(pallas::network::miniprotocols::Point::Specific(
slot.into(),
hash.into(),
))
}

/// Creates a new `Point` instance representing a specific
Expand All @@ -106,7 +111,7 @@ impl Point {
///
/// # Parameters
///
/// * `slot` - A `u64` value representing the slot number in the blockchain.
/// * `slot` - A `Slot` representing the slot number in the blockchain.
///
/// # Returns
///
Expand All @@ -121,9 +126,9 @@ impl Point {
/// let point = Point::fuzzy(slot);
/// ```
#[must_use]
pub fn fuzzy(slot: u64) -> Self {
pub fn fuzzy(slot: Slot) -> Self {
Self(pallas::network::miniprotocols::Point::Specific(
slot,
slot.into(),
Vec::new(),
))
}
Expand All @@ -137,7 +142,9 @@ impl Point {
Self::TIP
} else {
match self.0 {
pallas::network::miniprotocols::Point::Specific(slot, _) => Self::fuzzy(slot),
pallas::network::miniprotocols::Point::Specific(slot, _) => {
Self::fuzzy(slot.into())
},
pallas::network::miniprotocols::Point::Origin => Self::ORIGIN,
}
}
Expand Down Expand Up @@ -500,16 +507,16 @@ mod tests {

#[test]
fn test_create_points() {
let point1 = Point::new(100u64, vec![]);
let fuzzy1 = Point::fuzzy(100u64);
let point1 = Point::new(100u64.into(), Blake2bHash::new(&[]));
let fuzzy1 = Point::fuzzy(100u64.into());

assert!(point1 == fuzzy1);
}

#[test]
fn test_cmp_hash_simple() {
let origin1 = Point::ORIGIN;
let point1 = Point::new(100u64, vec![8; 32]);
let point1 = Point::new(100u64.into(), [8; 32].into());

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

#[test]
fn test_get_hash_simple() {
let point1 = Point::new(100u64, vec![8; 32]);
let point1 = Point::new(100u64.into(), [8; 32].into());

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

#[test]
fn test_identical_compare() {
let point1 = Point::new(100u64, vec![8; 32]);
let point2 = Point::new(100u64, vec![8; 32]);
let point3 = Point::new(999u64, vec![8; 32]);
let point1 = Point::new(100u64.into(), [8; 32].into());
let point2 = Point::new(100u64.into(), [8; 32].into());
let point3 = Point::new(999u64.into(), [8; 32].into());

assert!(point1.strict_eq(&point2));
assert!(!point1.strict_eq(&point3));
Expand All @@ -541,9 +548,9 @@ mod tests {
let origin2 = Point::ORIGIN;
let tip1 = Point::TIP;
let tip2 = Point::TIP;
let early_block = Point::new(100u64, vec![]);
let late_block1 = Point::new(5000u64, vec![]);
let late_block2 = Point::new(5000u64, vec![]);
let early_block = Point::new(100u64.into(), Blake2bHash::new(&[]));
let late_block1 = Point::new(5000u64.into(), Blake2bHash::new(&[]));
let late_block2 = Point::new(5000u64.into(), Blake2bHash::new(&[]));

assert!(origin1 == origin2);
assert!(origin1 < early_block);
Expand Down

0 comments on commit aa6bd36

Please sign in to comment.