Skip to content

Commit 48ca937

Browse files
committed
fix(cardano-blockchain-types): cleanup
Signed-off-by: bkioshn <[email protected]>
1 parent d17adf7 commit 48ca937

File tree

5 files changed

+77
-49
lines changed

5 files changed

+77
-49
lines changed

rust/cardano-blockchain-types/deps.tmp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
rbac-registration = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.8" }
32

43
thiserror = "1.0.64"

rust/cardano-blockchain-types/src/auxdata/aux_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ impl TransactionAuxData {
158158

159159
let script_array = ScriptArray::decode(d, &mut ctx)?;
160160
if scripts.insert(script_type, script_array).is_some() {
161-
return Err(minicbor::decode::Error::message(
161+
return Err(minicbor::decode::Error::message(format!(
162162
"Multiple Alonzo+ Script entries of type {script_type} found. Invalid.",
163-
));
163+
)));
164164
}
165165
}
166166

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ struct SelfReferencedMultiEraBlock {
4545
/// Multi-era block - inner.
4646
#[derive(Debug)]
4747
struct MultiEraBlockInner {
48-
/// What blockchain was the block produced on.
49-
//#[allow(dead_code)]
50-
pub chain: Network,
48+
/// What blockchain network was the block produced on.
49+
network: Network,
5150
/// The Point on the blockchain this block can be found.
5251
point: Point,
5352
/// The previous point on the blockchain before this block.
@@ -93,7 +92,7 @@ impl MultiEraBlock {
9392
///
9493
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
9594
fn new_block(
96-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
95+
network: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
9796
) -> anyhow::Result<Self> {
9897
let builder = SelfReferencedMultiEraBlockTryBuilder {
9998
raw_data,
@@ -140,7 +139,7 @@ impl MultiEraBlock {
140139
Ok(Self {
141140
fork,
142141
inner: Arc::new(MultiEraBlockInner {
143-
chain,
142+
network,
144143
point,
145144
previous: previous.clone(),
146145
data: self_ref_block,
@@ -156,9 +155,9 @@ impl MultiEraBlock {
156155
///
157156
/// If the given bytes cannot be decoded as a multi-era block, an error is returned.
158157
pub fn new(
159-
chain: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
158+
network: Network, raw_data: Vec<u8>, previous: &Point, fork: Fork,
160159
) -> anyhow::Result<Self> {
161-
MultiEraBlock::new_block(chain, raw_data, previous, fork)
160+
MultiEraBlock::new_block(network, raw_data, previous, fork)
162161
}
163162

164163
/// Remake the block on a new fork.
@@ -212,7 +211,7 @@ impl MultiEraBlock {
212211
/// # Returns
213212
/// `true` if the block is immutable, `false` otherwise.
214213
#[must_use]
215-
pub fn immutable(&self) -> bool {
214+
pub fn is_immutable(&self) -> bool {
216215
self.fork == 0.into()
217216
}
218217

@@ -231,13 +230,13 @@ impl MultiEraBlock {
231230
self.fork
232231
}
233232

234-
/// What chain was the block from
233+
/// What blockchain network was the block from
235234
///
236235
/// # Returns
237-
/// - The chain that this block originated on.
236+
/// - The network that this block originated on.
238237
#[must_use]
239-
pub fn chain(&self) -> Network {
240-
self.inner.chain
238+
pub fn network(&self) -> Network {
239+
self.inner.network
241240
}
242241

243242
/// Get The Metadata fora a transaction and known label from the block
@@ -286,7 +285,7 @@ impl Display for MultiEraBlock {
286285
let txns = block.tx_count();
287286
let aux_data = block.has_aux_data();
288287

289-
let fork = if self.immutable() {
288+
let fork = if self.is_immutable() {
290289
"Immutable".to_string()
291290
} else {
292291
format!("Fork: {fork:?}")
@@ -461,6 +460,10 @@ pub(crate) mod tests {
461460
);
462461

463462
assert!(block.is_err());
463+
assert!(block
464+
.unwrap_err()
465+
.to_string()
466+
.contains("Previous slot is not less than current slot"));
464467
}
465468

466469
Ok(())

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

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use std::{
1010

1111
use 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+
331360
impl 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

409444
impl 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

419451
impl 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

433464
impl 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)]
478506
mod 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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ impl TxnWitness {
2424
///
2525
/// # Errors
2626
///
27-
/// Only if the witness map does not contain a valid ED25519 public key
27+
/// If the witness map does not contain a valid ED25519 public key,
28+
/// or unsupported transaction era.
2829
pub fn new(txs: &[MultiEraTx]) -> anyhow::Result<Self> {
2930
/// Update the temporary map with the witnesses.
3031
fn update_map(
@@ -67,7 +68,7 @@ impl TxnWitness {
6768
}
6869
},
6970
_ => {
70-
return Err(anyhow::anyhow!("Unsupported transaction type"));
71+
return Err(anyhow::anyhow!("Unsupported transaction Era"));
7172
},
7273
};
7374
}

0 commit comments

Comments
 (0)