Skip to content

Commit 347045a

Browse files
committed
fix(cardano-blockchain-types): cleanup
Signed-off-by: bkioshn <[email protected]>
1 parent 0b0152d commit 347045a

File tree

4 files changed

+43
-74
lines changed

4 files changed

+43
-74
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ impl Fork {
2929

3030
/// Increment the fork count.
3131
pub fn incr(&mut self) {
32-
self.0 += 1;
32+
self.0 = self.0.saturating_add(1);
3333
}
3434

3535
/// Decrement the fork count.
3636
pub fn decr(&mut self) {
37-
self.0 -= 1;
37+
self.0 = self.0.saturating_sub(1);
3838
}
3939
}
4040

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl<const BYTES: usize> From<Blake2bHash<BYTES>> for Vec<u8> {
6868
}
6969
}
7070

71+
/// Convert hash in a form of byte array into the `Blake2bHash` type.
7172
impl<const BYTES: usize> TryFrom<&[u8]> for Blake2bHash<BYTES> {
7273
type Error = anyhow::Error;
7374

@@ -133,12 +134,8 @@ impl<'a, C, const BYTES: usize> minicbor::Decode<'a, C> for Blake2bHash<BYTES> {
133134
d: &mut minicbor::Decoder<'a>, _ctx: &mut C,
134135
) -> Result<Self, minicbor::decode::Error> {
135136
let bytes = d.bytes()?;
136-
if bytes.len() == BYTES {
137-
let mut hash = [0; BYTES];
138-
hash.copy_from_slice(bytes);
139-
Ok(hash.into())
140-
} else {
141-
Err(minicbor::decode::Error::message("Invalid hash size"))
142-
}
137+
bytes.try_into().map_err(|_| {
138+
minicbor::decode::Error::message("Invalid hash size for Blake2bHash cbor decode")
139+
})
143140
}
144141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl MultiEraBlock {
127127
// Special case, when the previous block is actually UNKNOWN, we can't check it.
128128
if !previous.is_unknown()
129129
// Otherwise, we make sure the hash chain is intact
130-
&& !previous.cmp_hash(&decoded_block.header().previous_hash())
130+
&& previous != &decoded_block.header().previous_hash()
131131
{
132132
debug!("{}, {:?}", previous, decoded_block.header().previous_hash());
133133

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

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Point {
9393
/// use cardano_chain_follower::Point;
9494
///
9595
/// let slot = 42;
96-
/// let hash = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
96+
/// let hash = vec![0; 32];
9797
/// let point = Point::new(slot, hash);
9898
/// ```
9999
#[must_use]
@@ -244,55 +244,6 @@ impl Point {
244244
}
245245
}
246246

247-
/// Compares the hash stored in the `Point` with a known hash.
248-
/// It returns `true` if the hashes match and `false` otherwise. If the
249-
/// provided hash is `None`, the function checks if the `Point` has an
250-
/// empty hash.
251-
///
252-
/// # Parameters
253-
///
254-
/// * `hash` - An `Option<Hash<32>>` containing the hash to compare against. If
255-
/// `Some`, the contained hash is compared with the `Point`'s hash. If `None`, the
256-
/// function checks if the `Point`'s hash is empty.
257-
///
258-
/// # Returns
259-
///
260-
/// A `bool` indicating whether the hashes match.
261-
///
262-
/// # Examples
263-
///
264-
/// ```rs
265-
/// use cardano_chain_follower::Point;
266-
///
267-
/// use pallas::crypto::hash::Hash;
268-
///
269-
/// let point = Point::new(42, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
270-
/// let hash = Some(Hash::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
271-
/// assert!(point.cmp_hash(&hash));
272-
///
273-
/// let empty_point = Point::fuzzy(42);
274-
/// assert!(empty_point.cmp_hash(&None));
275-
/// ```
276-
#[must_use]
277-
pub fn cmp_hash(&self, hash: &Option<Hash<32>>) -> bool {
278-
match hash {
279-
Some(cmp_hash) => {
280-
match self.0 {
281-
pallas::network::miniprotocols::Point::Specific(_, ref hash) => {
282-
**hash == **cmp_hash
283-
},
284-
pallas::network::miniprotocols::Point::Origin => false,
285-
}
286-
},
287-
None => {
288-
match self.0 {
289-
pallas::network::miniprotocols::Point::Specific(_, ref hash) => hash.is_empty(),
290-
pallas::network::miniprotocols::Point::Origin => true,
291-
}
292-
},
293-
}
294-
}
295-
296247
/// Retrieves the slot number from the `Point`. If the `Point`
297248
/// is the origin, it returns a default slot number.
298249
///
@@ -306,15 +257,15 @@ impl Point {
306257
/// ```rs
307258
/// use cardano_chain_follower::{Point, ORIGIN_POINT};
308259
///
309-
/// let specific_point = Point::new(42, vec![1, 2, 3]);
260+
/// let specific_point = Point::new(42, vec![0; 32]);
310261
/// assert_eq!(specific_point.slot_or_default(), 42);
311262
///
312263
/// let origin_point = ORIGIN_POINT;
313-
/// assert_eq!(origin_point.slot_or_default(), 0); // assuming 0 is the default
264+
/// assert_eq!(origin_point.slot_or_default(), 0.into()); // assuming 0 is the default
314265
/// ```
315266
#[must_use]
316-
pub fn slot_or_default(&self) -> u64 {
317-
self.0.slot_or_default()
267+
pub fn slot_or_default(&self) -> Slot {
268+
self.0.slot_or_default().into()
318269
}
319270

320271
/// Retrieves the hash from the `Point`. If the `Point` is
@@ -374,6 +325,31 @@ impl Point {
374325
}
375326
}
376327

328+
impl PartialEq<Option<Hash<32>>> for Point {
329+
/// Compares the hash stored in the `Point` with a known hash.
330+
/// It returns `true` if the hashes match and `false` otherwise. If the
331+
/// provided hash is `None`, the function checks if the `Point` has an
332+
/// empty hash.
333+
fn eq(&self, other: &Option<Hash<32>>) -> bool {
334+
match other {
335+
Some(cmp_hash) => {
336+
match self.0 {
337+
pallas::network::miniprotocols::Point::Specific(_, ref hash) => {
338+
**hash == **cmp_hash
339+
},
340+
pallas::network::miniprotocols::Point::Origin => false,
341+
}
342+
},
343+
None => {
344+
match self.0 {
345+
pallas::network::miniprotocols::Point::Specific(_, ref hash) => hash.is_empty(),
346+
pallas::network::miniprotocols::Point::Origin => true,
347+
}
348+
},
349+
}
350+
}
351+
}
352+
377353
impl Display for Point {
378354
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
379355
if *self == Self::ORIGIN {
@@ -387,9 +363,9 @@ impl Display for Point {
387363
let slot = self.slot_or_default();
388364
let hash = self.hash_or_default();
389365
if hash.is_empty() {
390-
return write!(f, "Point @ Probe:{slot}");
366+
return write!(f, "Point @ Probe:{slot:?}");
391367
}
392-
write!(f, "Point @ {slot}:{}", hex::encode(hash))
368+
write!(f, "Point @ {slot:?}:{}", hex::encode(hash))
393369
}
394370
}
395371

@@ -422,10 +398,6 @@ impl Ord for Point {
422398
}
423399

424400
impl PartialEq<u64> for Point {
425-
/// Allows to compare a `SnapshotID` against `u64` (Just the Immutable File Number).
426-
///
427-
/// Equality ONLY checks the Immutable File Number, not the path.
428-
/// This is because the Filename is already the Immutable File Number.
429401
fn eq(&self, other: &u64) -> bool {
430402
self.0.slot_or_default() == *other
431403
}
@@ -510,11 +482,11 @@ mod tests {
510482
let origin1 = Point::ORIGIN;
511483
let point1 = Point::new(100u64.into(), [8; 32].into());
512484

513-
assert!(!origin1.cmp_hash(&Some(Hash::new([0; 32]))));
514-
assert!(origin1.cmp_hash(&None));
485+
assert!(origin1 != Some(Hash::new([0; 32])));
486+
assert!(origin1 == None::<Hash<32>>);
515487

516-
assert!(point1.cmp_hash(&Some(Hash::new([8; 32]))));
517-
assert!(!point1.cmp_hash(&None));
488+
assert!(point1 == Some(Hash::new([8; 32])));
489+
assert!(point1 != None::<Hash<32>>);
518490
}
519491

520492
#[test]

0 commit comments

Comments
 (0)