Skip to content

Commit 0e16500

Browse files
bkioshnstevenj
andauthored
fix(rust/cardano-blockchain-types): Make cardano-blockchain-types WASM compatible (#478)
* feat: add deps Signed-off-by: bkioshn <[email protected]> * fix: move hash from cat-types Signed-off-by: bkioshn <[email protected]> * fix: import pallas Signed-off-by: bkioshn <[email protected]> * fix: format Signed-off-by: bkioshn <[email protected]> * fix: bump minor version Signed-off-by: bkioshn <[email protected]> * fix: update re export pallas Signed-off-by: bkioshn <[email protected]> * fix: expose hashes Signed-off-by: bkioshn <[email protected]> * fix: add pallas codec Signed-off-by: bkioshn <[email protected]> * fix: remove deadcode Signed-off-by: bkioshn <[email protected]> * fix: hash wrapper example Signed-off-by: bkioshn <[email protected]> * fix: git url Signed-off-by: bkioshn <[email protected]> * fix: add version for pallas Signed-off-by: bkioshn <[email protected]> * fix: doctest Signed-off-by: bkioshn <[email protected]> * chore: typo Signed-off-by: bkioshn <[email protected]> * fix: update dep Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]> Co-authored-by: Steven Johnson <[email protected]>
1 parent 5dee672 commit 0e16500

File tree

15 files changed

+498
-116
lines changed

15 files changed

+498
-116
lines changed

rust/cardano-blockchain-types/Cargo.toml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "cardano-blockchain-types"
33
description = "Common Cardano Blockchain data types for use in both applications and crates"
44
keywords = ["cardano", "catalyst", ]
5-
version = "0.0.5"
5+
version = "0.0.6"
66
authors = [
77
"Steven Johnson <[email protected]>"
88
]
@@ -18,9 +18,7 @@ crate-type = ["cdylib", "rlib"]
1818
workspace = true
1919

2020
[dependencies]
21-
pallas = { version = "0.33.0" }
22-
# pallas-hardano = { version = "0.33.0" }
23-
catalyst-types = { version = "0.0.5", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "catalyst-types-v0.0.5" }
21+
catalyst-types = { version = "0.0.6", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "catalyst-types/v0.0.6" }
2422
cbork-utils = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "cbork-utils-v0.0.2" }
2523

2624
ouroboros = "0.18.4"
@@ -38,3 +36,16 @@ ed25519-dalek = "2.1.1"
3836
serde = "1.0.210"
3937
num-bigint = "0.4.6"
4038
serde_json = "1.0.134"
39+
displaydoc = "0.2.5"
40+
thiserror = "2.0.11"
41+
42+
# TODO: Revert ALL to Upstream Pallas after <https://github.com/txpipe/pallas/pull/685> merges.
43+
pallas-traverse = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
44+
pallas-addresses = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
45+
pallas-primitives = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
46+
pallas-crypto = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
47+
pallas-codec = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
48+
# Only include these for non-wasm32 targets
49+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
50+
pallas-network = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }
51+
pallas-hardano = { version = "=1.0.0-alpha.2", git = "https://github.com/input-output-hk/catalyst-pallas.git", branch = "feat/pallas-network-types" }

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44

55
use anyhow::bail;
66
use dashmap::DashMap;
7-
use pallas::ledger::traverse::MultiEraBlock;
7+
use pallas_traverse::MultiEraBlock;
88

99
use super::aux_data::TransactionAuxData;
1010
use crate::txn_index::TxnIndex;
@@ -44,19 +44,19 @@ impl TryFrom<&MultiEraBlock<'_>> for BlockAuxData {
4444
if let Some(_metadata) = block.as_byron() {
4545
// Nothing to do here.
4646
} else if let Some(alonzo_block) = block.as_alonzo() {
47-
for (txn_idx, metadata) in alonzo_block.auxiliary_data_set.iter() {
47+
for (txn_idx, metadata) in &alonzo_block.auxiliary_data_set {
4848
let mut d = minicbor::Decoder::new(metadata.raw_cbor());
4949
let txn_aux_data = d.decode::<TransactionAuxData>()?;
5050
aux_data.insert((*txn_idx).into(), txn_aux_data);
5151
}
5252
} else if let Some(babbage_block) = block.as_babbage() {
53-
for (txn_idx, metadata) in babbage_block.auxiliary_data_set.iter() {
53+
for (txn_idx, metadata) in &babbage_block.auxiliary_data_set {
5454
let mut d = minicbor::Decoder::new(metadata.raw_cbor());
5555
let txn_aux_data = d.decode::<TransactionAuxData>()?;
5656
aux_data.insert((*txn_idx).into(), txn_aux_data);
5757
}
5858
} else if let Some(conway_block) = block.as_conway() {
59-
for (txn_idx, metadata) in conway_block.auxiliary_data_set.iter() {
59+
for (txn_idx, metadata) in &conway_block.auxiliary_data_set {
6060
let mut d = minicbor::Decoder::new(metadata.raw_cbor());
6161
let txn_aux_data = d.decode::<TransactionAuxData>()?;
6262
aux_data.insert((*txn_idx).into(), txn_aux_data);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::fmt::{Display, Formatter};
77

88
use anyhow::{anyhow, Context, Error, Result};
9-
use pallas::ledger::addresses::Address;
9+
use pallas_addresses::Address;
1010

1111
/// A URI in the CIP-0134 format.
1212
///
@@ -70,7 +70,7 @@ impl Cip0134Uri {
7070
///
7171
/// ```
7272
/// use cardano_blockchain_types::Cip0134Uri;
73-
/// use pallas::ledger::addresses::{Address, Network};
73+
/// use pallas_addresses::{Address, Network};
7474
///
7575
/// let uri = "web+cardano://addr/stake1uyehkck0lajq8gr28t9uxnuvgcqrc6070x3k9r8048z8y5gh6ffgw";
7676
/// let cip0134_uri = Cip0134Uri::parse(uri).unwrap();
@@ -106,7 +106,7 @@ impl TryFrom<&[u8]> for Cip0134Uri {
106106

107107
#[cfg(test)]
108108
mod tests {
109-
use pallas::ledger::addresses::{Address, Network};
109+
use pallas_addresses::{Address, Network};
110110

111111
use super::*;
112112

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

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//! A macro for defining a new type wrappers for the given hash types.
2+
3+
/// Defines a new type wrapper for the given hash types.
4+
///
5+
/// # Examples
6+
///
7+
/// ```
8+
/// use cardano_blockchain_types::define_hashes;
9+
/// use cardano_blockchain_types::hashes::Blake2b128Hash;
10+
///
11+
/// define_hashes!(
12+
/// /// You can document the declared types...
13+
/// (SomeHash, Blake2b128Hash),
14+
/// // ...or not.
15+
/// (AnotherHash, Blake2b128Hash),
16+
/// );
17+
///
18+
/// let hash = SomeHash::new(&[1, 2, 3]);
19+
/// println!("{hash:?}");
20+
/// ```
21+
#[macro_export]
22+
macro_rules! define_hashes {
23+
($($(#[$docs:meta])* ($name:ident, $inner:ty)),+ $(,)?) => {
24+
$(
25+
$(#[$docs])*
26+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
27+
pub struct $name($inner);
28+
29+
impl $name {
30+
/// Creates a new instance from the given bytes by hashing them.
31+
#[must_use]
32+
pub fn new(input_bytes: &[u8]) -> Self {
33+
Self(<$inner>::new(input_bytes))
34+
}
35+
}
36+
37+
impl std::fmt::Display for $name {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
f.write_str(&format!("0x{}", self.0))
40+
}
41+
}
42+
43+
impl From<$name> for Vec<u8> {
44+
fn from(value: $name) -> Self {
45+
value.0.into()
46+
}
47+
}
48+
49+
impl From<$inner> for $name {
50+
fn from(value: $inner) -> Self {
51+
Self(value)
52+
}
53+
}
54+
55+
impl TryFrom<&[u8]> for $name {
56+
type Error = $crate::hashes::Blake2bHashError;
57+
58+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
59+
Ok(Self(<$inner>::try_from(value)?))
60+
}
61+
}
62+
63+
impl TryFrom<Vec<u8>> for $name {
64+
type Error = $crate::hashes::Blake2bHashError;
65+
66+
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
67+
value.as_slice().try_into()
68+
}
69+
}
70+
71+
impl std::str::FromStr for $name {
72+
type Err = $crate::hashes::Blake2bHashError;
73+
74+
fn from_str(s: &str) -> Result<Self, Self::Err> {
75+
let hash: $inner = s.parse().map_err($crate::hashes::Blake2bHashError::from)?;
76+
Ok(Self(hash))
77+
}
78+
}
79+
80+
impl<C> minicbor::Encode<C> for $name {
81+
fn encode<W: minicbor::encode::Write>(
82+
&self, e: &mut minicbor::Encoder<W>, ctx: &mut C,
83+
) -> Result<(), minicbor::encode::Error<W::Error>> {
84+
self.0.encode(e, ctx)
85+
}
86+
}
87+
88+
impl<'a, C> minicbor::Decode<'a, C> for $name {
89+
fn decode(
90+
d: &mut minicbor::Decoder<'a>, ctx: &mut C,
91+
) -> Result<Self, minicbor::decode::Error> {
92+
let hash = <$inner>::decode(d, ctx)?;
93+
Ok(Self(hash))
94+
}
95+
}
96+
)+
97+
};
98+
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use crate::hashes::Blake2b128Hash;
103+
104+
// Define one type without a trailing comma.
105+
define_hashes!((H1, Blake2b128Hash));
106+
// Define one type with a trailing comma and a doc-comment.
107+
define_hashes!(
108+
/// Some documentation.
109+
(H2, Blake2b128Hash),
110+
);
111+
// Define multiple types at once.
112+
define_hashes!(
113+
/// Documentation.
114+
(H3, Blake2b128Hash),
115+
// No documentation.
116+
(H4, Blake2b128Hash),
117+
/// More documentation.
118+
(H5, Blake2b128Hash),
119+
);
120+
121+
// There is little reason to check the conversion itself, it is mostly a demonstration
122+
// that the methods defined by the macro are working.
123+
#[test]
124+
fn hash_wrapper() {
125+
let hash = H1::new(&[1, 2, 3, 4, 5]);
126+
127+
let v = Vec::from(hash);
128+
let from_slice = H1::try_from(v.as_slice()).unwrap();
129+
assert_eq!(hash, from_slice);
130+
131+
let from_vec = H1::try_from(v).unwrap();
132+
assert_eq!(hash, from_vec);
133+
}
134+
135+
// The display implementation is used to get user-friendly representation and must be
136+
// equal to `hex::encode(<underlying bytes>)`.
137+
#[test]
138+
fn display() {
139+
let hash = H1::new(&[1, 2, 3, 4, 5]);
140+
let display = format!("{hash}");
141+
let expected = "0x2a6ad53c3c6986406e1d6c7cfd06b69a";
142+
assert_eq!(expected, display);
143+
}
144+
}

0 commit comments

Comments
 (0)