@@ -10,7 +10,10 @@ use std::{
1010
1111use pallas:: crypto:: hash:: Hash ;
1212
13- use crate :: { hashes:: Blake2bHash , Slot } ;
13+ use crate :: {
14+ hashes:: { Blake2b256Hash , Blake2bHash } ,
15+ Slot ,
16+ } ;
1417
1518/// A specific point in the blockchain. It can be used to
1619/// identify a particular location within the blockchain, such as the tip (the
@@ -81,7 +84,7 @@ impl Point {
8184 /// # Parameters
8285 ///
8386 /// * `slot` - A `Slot` representing the slot number in the blockchain.
84- /// * `hash` - A `Blake2bHash` size 32 , block hash at the specified slot.
87+ /// * `hash` - A `Blake2b256Hash` , block hash at the specified slot.
8588 ///
8689 /// # Returns
8790 ///
@@ -97,7 +100,7 @@ impl Point {
97100 /// let point = Point::new(slot.into(), hash.into());
98101 /// ```
99102 #[ must_use]
100- pub fn new ( slot : Slot , hash : Blake2bHash < 32 > ) -> Self {
103+ pub fn new ( slot : Slot , hash : Blake2b256Hash ) -> Self {
101104 Self ( pallas:: network:: miniprotocols:: Point :: Specific (
102105 slot. into ( ) ,
103106 hash. into ( ) ,
@@ -269,29 +272,29 @@ impl Point {
269272 }
270273
271274 /// Retrieves the hash from the `Point`. If the `Point` is
272- /// the origin, it returns a default hash value, which is an empty `Vec<u8> `.
275+ /// the origin, it returns `None `.
273276 ///
274277 /// # Returns
275278 ///
276- /// A `Vec<u8> ` representing the hash. If the `Point` is the `Origin`, it
277- /// returns an empty vector .
279+ /// A `Blake2b256Hash ` representing the hash. If the `Point` is the `Origin`, it
280+ /// returns `None` .
278281 ///
279282 /// # Examples
280283 ///
281284 /// ```
282- /// use cardano_blockchain_types::Point;
285+ /// use cardano_blockchain_types::{ Point, hashes::Blake2bHash} ;
283286 ///
284287 /// let specific_point = Point::new(42.into(), [0; 32].into());
285- /// assert_eq!(specific_point.hash_or_default(), Some([0; 32].into( )));
288+ /// assert_eq!(specific_point.hash_or_default(), Some(Blake2bHash::new(& [0; 32])));
286289 ///
287290 /// let origin_point = Point::ORIGIN;
288291 /// assert_eq!(origin_point.hash_or_default(), None);
289292 /// ```
290293 #[ must_use]
291- pub fn hash_or_default ( & self ) -> Option < Hash < 32 > > {
294+ pub fn hash_or_default ( & self ) -> Option < Blake2b256Hash > {
292295 match & self . 0 {
293296 pallas:: network:: miniprotocols:: Point :: Specific ( _, hash) => {
294- Some ( Hash :: from ( hash. as_slice ( ) ) )
297+ Some ( Blake2bHash :: new ( hash) )
295298 } ,
296299 // Origin has empty hash, so set it to None
297300 pallas:: network:: miniprotocols:: Point :: Origin => None ,
@@ -328,8 +331,34 @@ impl Point {
328331 }
329332}
330333
334+ impl PartialEq < Option < Blake2b256Hash > > for Point {
335+ /// Compares the hash stored in the `Point` with a `Blake2b256Hash`.
336+ /// It returns `true` if the hashes match and `false` otherwise. If the
337+ /// provided hash is `None`, the function checks if the `Point` has an
338+ /// empty hash.
339+ fn eq ( & self , other : & Option < Blake2b256Hash > ) -> bool {
340+ match other {
341+ Some ( cmp_hash) => {
342+ match self . 0 {
343+ pallas:: network:: miniprotocols:: Point :: Specific ( _, ref hash) => {
344+ // Compare vec to vec
345+ * hash == <Blake2b256Hash as Into < Vec < u8 > > >:: into ( * cmp_hash)
346+ } ,
347+ pallas:: network:: miniprotocols:: Point :: Origin => false ,
348+ }
349+ } ,
350+ None => {
351+ match self . 0 {
352+ pallas:: network:: miniprotocols:: Point :: Specific ( _, ref hash) => hash. is_empty ( ) ,
353+ pallas:: network:: miniprotocols:: Point :: Origin => true ,
354+ }
355+ } ,
356+ }
357+ }
358+ }
359+
331360impl PartialEq < Option < Hash < 32 > > > for Point {
332- /// Compares the hash stored in the `Point` with a known hash .
361+ /// Compares the hash stored in the `Point` with a Pallas `Hash` .
333362 /// It returns `true` if the hashes match and `false` otherwise. If the
334363 /// provided hash is `None`, the function checks if the `Point` has an
335364 /// empty hash.
@@ -366,7 +395,13 @@ impl Display for Point {
366395 let slot = self . slot_or_default ( ) ;
367396 let hash = self . hash_or_default ( ) ;
368397 match hash {
369- Some ( hash) => write ! ( f, "Point @ {slot:?}:{}" , hex:: encode( hash) ) ,
398+ Some ( hash) => {
399+ write ! (
400+ f,
401+ "Point @ {slot:?}:{}" ,
402+ hex:: encode( <Blake2b256Hash as Into <Vec <u8 >>>:: into( hash) )
403+ )
404+ } ,
370405 None => write ! ( f, "Point @ {slot:?}" ) ,
371406 }
372407 }
@@ -407,20 +442,16 @@ impl PartialEq<u64> for Point {
407442}
408443
409444impl PartialOrd < u64 > for Point {
410- /// Allows to compare a `Point` against a `u64` (Just the Immutable File Number).
411- ///
412- /// Equality ONLY checks the Immutable File Number, not the path.
413- /// This is because the Filename is already the Immutable File Number.
445+ /// Allows to compare a `Point` against a `u64`
414446 fn partial_cmp ( & self , other : & u64 ) -> Option < Ordering > {
415447 self . 0 . slot_or_default ( ) . partial_cmp ( other)
416448 }
417449}
418450
419451impl PartialEq < Option < Point > > for Point {
420- /// Allows to compare a `SnapshotID` against `u64` (Just the Immutable File Number).
421- ///
422- /// Equality ONLY checks the Immutable File Number, not the path.
423- /// This is because the Filename is already the Immutable File Number.
452+ /// Allows for direct comparison between a `Point` and an `Option<Point>`,
453+ /// returning `true` only if the `Option` contains a `Point` that is equal to the
454+ /// `self` instance.
424455 fn eq ( & self , other : & Option < Point > ) -> bool {
425456 if let Some ( other) = other {
426457 * self == * other
@@ -431,11 +462,8 @@ impl PartialEq<Option<Point>> for Point {
431462}
432463
433464impl PartialOrd < Option < Point > > for Point {
434- /// Allows to compare a `Point` against a `u64` (Just the Immutable File Number).
435- ///
436- /// Equality ONLY checks the Immutable File Number, not the path.
437- /// This is because the Filename is already the Immutable File Number.
438- /// Any point is greater than None.
465+ /// Allows comparing a `Point` with an `Option<Point>`, where a `Point` is always
466+ /// considered greater than `None`.
439467 fn partial_cmp ( & self , other : & Option < Point > ) -> Option < Ordering > {
440468 if let Some ( other) = other {
441469 self . partial_cmp ( other)
@@ -476,19 +504,16 @@ fn cmp_point(
476504
477505#[ cfg( test) ]
478506mod tests {
479- use pallas:: crypto:: hash:: Hash ;
480-
481507 use crate :: point:: * ;
482508
483509 #[ test]
484510 fn test_cmp_hash_simple ( ) {
485511 let origin1 = Point :: ORIGIN ;
486512 let point1 = Point :: new ( 100u64 . into ( ) , [ 8 ; 32 ] . into ( ) ) ;
487513
488- assert ! ( origin1 != Some ( Hash :: new( [ 0 ; 32 ] ) ) ) ;
489- assert ! ( origin1 == None :: <Hash <32 >>) ;
490-
491- assert ! ( point1 == Some ( Hash :: new( [ 8 ; 32 ] ) ) ) ;
514+ assert ! ( origin1 != Some ( Blake2bHash :: <32 >:: new( & [ 0 ; 32 ] ) ) ) ;
515+ assert ! ( origin1 == None :: <Blake2b256Hash >) ;
516+ assert ! ( point1 == Some ( Hash :: <32 >:: new( [ 8 ; 32 ] ) ) ) ;
492517 assert ! ( point1 != None :: <Hash <32 >>) ;
493518 }
494519
0 commit comments