Skip to content

Commit

Permalink
add tests and update plutus_data version
Browse files Browse the repository at this point in the history
  • Loading branch information
OlofBlomqvist committed Aug 3, 2023
1 parent 6d20355 commit 65ff687
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 8 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "marlowe_lang"
description = "experimental parser lib for Cardano Marlowe DSL"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Olof Blomqvist <olof@twnet.se>"]
repository = "https://github.com/OlofBlomqvist/marlowe_rust"
Expand All @@ -13,7 +13,7 @@ pest = { version = "2.7.1", features= ["pretty-print"] }
serde = { version = "1.0.171", features = ["derive"] }
serde_json = { version = "1.0.103" , features = ["unbounded_depth"] }
clap = { version = "4.0.19", features = ["derive"] , optional = true}
plutus_data = { version = "0.0.6", optional = true }
plutus_data = { version = "0.0.7", optional = true }
#plutus_data = { path = "../plutus_data/plutus_data", optional = true }
console_error_panic_hook = { version = "0.1.7", optional = true }
serde_derive = { version = "1.0.171", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/lib/extras/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn try_decode_cborhex_marlowe_plutus_contract(cbor_hex:&str) -> Result<Contr
match plutus_data::from_bytes(&cbor) {
Ok(x) => Contract::from_plutus_data(x,&[]),
Err(e) =>
Err(format!("Failed to decode plutus datum from input! Exception: {:?}",e))
Err(format!("Failed to decode plutus contract from cbor_hex! Exception: {:?}",e))

}
}
Expand Down
176 changes: 176 additions & 0 deletions src/lib/tests/plutus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use std::collections::HashMap;
use std::fs::read_to_string;
use crate::deserialization::json::deserialize;
use crate::extras::utils::*;
use crate::parsing::marlowe::ContractParseResult;
use crate::types::marlowe::Address;
use crate::types::marlowe::Contract;
use crate::types::marlowe::MarloweDatum;
use crate::types::marlowe::MarloweParams;
use crate::types::marlowe::State;
use crate::types::marlowe::Token;
use hex::ToHex;
use pallas_primitives::Fragment;
use pallas_primitives::ToCanonicalJson;
use plutus_data::ToPlutusData;
use plutus_data::FromPlutusData;

Expand Down Expand Up @@ -306,5 +317,170 @@ fn mainnet_addresses_07() {





#[test]
fn datum_decoding_matches_prior_versions() {
let jcon = r#"{
"when": [
{
"then": {
"when": [
{
"then": "close",
"case": {
"notify_if": true
}
}
],
"timeout_continuation": "close",
"timeout": 1691053939415
},
"case": {
"party": {
"role_token": "Depositor"
},
"of_token": {
"token_name": "",
"currency_symbol": ""
},
"into_account": {
"role_token": "Depositor"
},
"deposits": 1000000
}
}
],
"timeout_continuation": "close",
"timeout": 1691053939415
}
"#;
let contract : crate::types::marlowe::Contract = deserialize(jcon.into()).unwrap();
let original = "d8799fd8799f581c2393a2c7dcdd0e2d07ccd9b69ed1c4f90179e50863eecb139876cf09ffd8799fa1d8799fd8799fd87980d8799fd8799f581cd5b085a4aa0d42df98337bb9ab4ae77d46e579109c9f990c53762cfcffd87a80ffffd8799f4040ffff1a001e8480a0a001ffd87c9f9fd8799fd8799fd87a9f494465706f7369746f72ffd87a9f494465706f7369746f72ffd8799f4040ffd87a9f1a000f4240ffffd87c9f9fd8799fd87b9fd9050280ffd87980ffff1b00000189baab5ad7d87980ffffff1b00000189baab5ad7d87980ffff";
let mut accmap = HashMap::new();
accmap.insert((crate::types::marlowe::Party::Address(Address::from_bech32("addr_test1vr2mppdy4gx59hucxdamn262ua75detezzwflxgv2dmzelq9578t3").unwrap()),Token::ada()), 2000000);

let my_datum = MarloweDatum {
marlowe_params: MarloweParams("2393a2c7dcdd0e2d07ccd9b69ed1c4f90179e50863eecb139876cf09".into()),
state: State {
accounts: accmap,
choices: HashMap::new(),
bound_values: HashMap::new(),
min_time: 1,
},
contract: contract,
};
//println!("{my_datum:?}");
let cb = my_datum.to_plutus_data(&vec![]).unwrap();
let hex = plutus_data::to_hex(&cb).unwrap();
let reda = MarloweDatum::from_plutus_data(cb, &vec![]).unwrap();
//println!("hex: {}",hex);
let jsondatum = crate::serialization::json::serialize(reda).unwrap();
//println!("reda: {}",&jsondatum);
let fromj : MarloweDatum = crate::deserialization::json::deserialize(jsondatum).unwrap();
//println!("datum from json: {:?}",fromj);
assert!(hex == original);


let original_bytes = hex::decode(original).unwrap();
let original_decoded_as_pd = plutus_data::from_bytes(&original_bytes).unwrap();
let original_from_pd = MarloweDatum::from_plutus_data(original_decoded_as_pd, &vec![]).unwrap();
let original_to_hex = plutus_data::to_hex(&original_from_pd.to_plutus_data(&vec![]).unwrap()).unwrap();
assert!(&original_to_hex == original);

}


#[test]
fn datum_encoding_matches_prior_versions() {
let j = r#"
{
"marlowe_params": "2393a2c7dcdd0e2d07ccd9b69ed1c4f90179e50863eecb139876cf09",
"state": {
"accounts": [
[
[
{
"address": "addr_test1vr2mppdy4gx59hucxdamn262ua75detezzwflxgv2dmzelq9578t3"
},
{
"token_name": "",
"currency_symbol": ""
}
],
2000000
]
],
"choices": [],
"boundValues": [],
"minTime": 1
},
"contract": {
"when": [
{
"then": {
"when": [
{
"then": "close",
"case": {
"notify_if": true
}
}
],
"timeout_continuation": "close",
"timeout": 1691053939415
},
"case": {
"party": {
"role_token": "Depositor"
},
"of_token": {
"token_name": "",
"currency_symbol": ""
},
"into_account": {
"role_token": "Depositor"
},
"deposits": 1000000
}
}
],
"timeout_continuation": "close",
"timeout": 1691053939415
}
}
"#;

let datum : MarloweDatum = crate::deserialization::json::deserialize(j.into()).unwrap();
let hexed = plutus_data::to_hex(&datum.to_plutus_data(&vec![]).unwrap()).unwrap();

// println!("{:?}",datum);
//println!("hex: {}",hexed);

let dehex = crate::plutus_data::from_bytes(&hex::decode(&hexed).unwrap()).unwrap();

//println!("DEHEX: {:?}",dehex);

}


#[test]
fn reencode_Datum() {

let original = "d8799fd8799f581c2393a2c7dcdd0e2d07ccd9b69ed1c4f90179e50863eecb139876cf09ffd8799fa1d8799fd8799fd87980d8799fd8799f581cd5b085a4aa0d42df98337bb9ab4ae77d46e579109c9f990c53762cfcffd87a80ffffd8799f4040ffff1a001e8480a0a001ffd87c9f9fd8799fd8799fd87a9f494465706f7369746f72ffd87a9f494465706f7369746f72ffd8799f4040ffd87a9f1a000f4240ffffd87c9f9fd8799fd87b9fd9050280ffd87980ffff1b00000189baab5ad7d87980ffffff1b00000189baab5ad7d87980ffff";

let pd = plutus_data::PlutusData::decode_fragment(&hex::decode(original).unwrap()).unwrap();


let rec1 = pd.encode_fragment().unwrap();
let reh : String = rec1.encode_hex();

assert!(original == reh);

//println!("{}",pd.to_json());

let marlowe_datum : MarloweDatum = MarloweDatum::from_plutus_data(pd,&vec![]).unwrap();

// println!("{:?}",marlowe_datum);

}

0 comments on commit 65ff687

Please sign in to comment.