From 03fbeb4d9f4836ccb1aa5aedfc778f23e8d1bf84 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Fri, 28 Jan 2022 09:10:21 -0300 Subject: [PATCH] Quick and dirty test of wasm compilation --- pallas-alonzo/Cargo.toml | 6 ++-- pallas-alonzo/src/crypto.rs | 50 ---------------------------------- pallas-alonzo/src/framework.rs | 13 +++++++-- pallas-alonzo/src/lib.rs | 4 +-- pallas-alonzo/src/model.rs | 26 +++++++++--------- 5 files changed, 28 insertions(+), 71 deletions(-) delete mode 100644 pallas-alonzo/src/crypto.rs diff --git a/pallas-alonzo/Cargo.toml b/pallas-alonzo/Cargo.toml index 5d4ef139..4aabe2f1 100644 --- a/pallas-alonzo/Cargo.toml +++ b/pallas-alonzo/Cargo.toml @@ -13,8 +13,8 @@ authors = [ ] [dependencies] -minicbor = { version = "0.12", features = ["std"] } +minicbor = { version = "0.12", features = ["derive"] } minicbor-derive = "0.8.0" + +[dev-dependencies] hex = "0.4.3" -log = "0.4.14" -pallas-crypto = { version = "0.3", path = "../pallas-crypto" } \ No newline at end of file diff --git a/pallas-alonzo/src/crypto.rs b/pallas-alonzo/src/crypto.rs deleted file mode 100644 index 0ee66b03..00000000 --- a/pallas-alonzo/src/crypto.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::{AuxiliaryData, Header, PlutusData, TransactionBody}; -use pallas_crypto::hash::{Hash, Hasher}; - -pub fn hash_block_header(data: &Header) -> Hash<32> { - Hasher::<256>::hash_cbor(data) -} - -pub fn hash_auxiliary_data(data: &AuxiliaryData) -> Hash<32> { - Hasher::<256>::hash_cbor(data) -} - -pub fn hash_transaction(data: &TransactionBody) -> Hash<32> { - Hasher::<256>::hash_cbor(data) -} - -pub fn hash_plutus_data(data: &PlutusData) -> Hash<32> { - Hasher::<256>::hash_cbor(data) -} - -#[cfg(test)] -mod tests { - use crate::{BlockWrapper, Fragment}; - - use super::hash_transaction; - - #[test] - fn transaction_hash_works() { - // TODO: expand this test to include more test blocks - let block_idx = 1; - let block_str = include_str!("test_data/test1.block"); - - let block_bytes = hex::decode(block_str).expect(&format!("bad block file {}", block_idx)); - let block_model = BlockWrapper::decode_fragment(&block_bytes[..]) - .expect(&format!("error decoding cbor for file {}", block_idx)); - - let valid_hashes = vec![ - "8ae0cd531635579a9b52b954a840782d12235251fb1451e5c699e864c677514a", - "bb5bb4e1c09c02aa199c60e9f330102912e3ef977bb73ecfd8f790945c6091d4", - "8cdd88042ddb6c800714fb1469fb1a1a93152aae3c87a81f2a3016f2ee5c664a", - "10add6bdaa7ade06466bdd768456e756709090846b58bf473f240c484db517fa", - "8838f5ab27894a6543255aeaec086f7b3405a6db6e7457a541409cdbbf0cd474", - ]; - - for (tx_idx, tx) in block_model.1.transaction_bodies.iter().enumerate() { - let computed_hash = hash_transaction(tx); - let known_hash = valid_hashes[tx_idx]; - assert_eq!(hex::encode(computed_hash), known_hash) - } - } -} diff --git a/pallas-alonzo/src/framework.rs b/pallas-alonzo/src/framework.rs index 497501b0..08330a53 100644 --- a/pallas-alonzo/src/framework.rs +++ b/pallas-alonzo/src/framework.rs @@ -13,10 +13,19 @@ where T: minicbor::Encode + minicbor::Decode<'a> + Sized, { fn encode_fragment(&self) -> Result, Error> { - minicbor::to_vec(self).map_err(|e| e.into()) + let mut buf = Vec::new(); + { + let mut encoder = minicbor::Encoder::new(&mut buf); + encoder.encode(self).expect("error encoding"); + } + + Ok(buf) } fn decode_fragment(bytes: &'a [u8]) -> Result { - minicbor::decode(bytes).map_err(|e| e.into()) + let mut decoder = minicbor::Decoder::new(bytes); + let out = decoder.decode().expect("error decoding"); + + Ok(out) } } diff --git a/pallas-alonzo/src/lib.rs b/pallas-alonzo/src/lib.rs index e26af565..da059a9b 100644 --- a/pallas-alonzo/src/lib.rs +++ b/pallas-alonzo/src/lib.rs @@ -4,7 +4,5 @@ mod framework; mod model; mod utils; -pub use framework::*; pub use model::*; - -pub mod crypto; +pub use framework::*; \ No newline at end of file diff --git a/pallas-alonzo/src/model.rs b/pallas-alonzo/src/model.rs index 58821d2a..e3966fa2 100644 --- a/pallas-alonzo/src/model.rs +++ b/pallas-alonzo/src/model.rs @@ -2,14 +2,15 @@ //! //! Handcrafted, idiomatic rust artifacts based on based on the [Alonzo CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl) file in IOHK repo. -use log::warn; use minicbor::{bytes::ByteVec, data::Tag}; use minicbor_derive::{Decode, Encode}; -use pallas_crypto::hash::Hash; use std::{collections::BTreeMap, ops::Deref}; use crate::utils::{KeyValuePairs, MaybeIndefArray}; +pub type Hash32 = ByteVec; +pub type Hash28 = ByteVec; + #[derive(Debug, PartialEq, PartialOrd, Eq, Ord)] pub struct SkipCbor {} @@ -17,7 +18,6 @@ impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor { fn decode(d: &mut minicbor::Decoder<'b>) -> Result { { let probe = d.probe(); - warn!("skipped cbor value {}: {:?}", N, probe.datatype()?); println!("skipped cbor value {}: {:?}", N, probe.datatype()?); } @@ -47,7 +47,7 @@ pub struct HeaderBody { pub slot: u64, #[n(2)] - pub prev_hash: Hash<32>, + pub prev_hash: Hash32, #[n(3)] pub issuer_vkey: ByteVec, @@ -65,7 +65,7 @@ pub struct HeaderBody { pub block_body_size: u64, #[n(8)] - pub block_body_hash: Hash<32>, + pub block_body_hash: Hash32, #[n(9)] pub operational_cert: ByteVec, @@ -101,7 +101,7 @@ pub struct Header { #[derive(Encode, Decode, Debug, PartialEq)] pub struct TransactionInput { #[n(0)] - pub transaction_id: Hash<32>, + pub transaction_id: Hash32, #[n(1)] pub index: u64, @@ -125,7 +125,7 @@ pub struct Nonce { pub variant: NonceVariant, #[n(1)] - pub hash: Hash<32>, + pub hash: Hash32, } pub type ScriptHash = ByteVec; @@ -197,11 +197,11 @@ pub struct TransactionOutput { pub datum_hash: Option, } -pub type PoolKeyhash = Hash<28>; +pub type PoolKeyhash = Hash28; pub type Epoch = u64; pub type Genesishash = ByteVec; pub type GenesisDelegateHash = ByteVec; -pub type VrfKeyhash = Hash<32>; +pub type VrfKeyhash = Hash32; /* move_instantaneous_reward = [ 0 / 1, { * stake_credential => delta_coin } / coin ] ; The first field determines where the funds are drawn from. @@ -359,7 +359,7 @@ impl minicbor::encode::Encode for Relay { } } -pub type PoolMetadataHash = Hash<32>; +pub type PoolMetadataHash = Hash32; #[derive(Encode, Decode, Debug, PartialEq)] pub struct PoolMetadata { @@ -370,8 +370,8 @@ pub struct PoolMetadata { pub hash: PoolMetadataHash, } -pub type AddrKeyhash = Hash<28>; -pub type Scripthash = Hash<28>; +pub type AddrKeyhash = Hash28; +pub type Scripthash = Hash28; #[derive(Debug, PartialEq)] pub struct RationalNumber { @@ -718,7 +718,7 @@ pub enum TransactionBodyComponent { AuxiliaryDataHash(ByteVec), ValidityIntervalStart(u64), Mint(Multiasset), - ScriptDataHash(Hash<32>), + ScriptDataHash(Hash32), Collateral(MaybeIndefArray), RequiredSigners(MaybeIndefArray), NetworkId(NetworkId),