Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Jul 12, 2022
1 parent 58ea93f commit 70fd854
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cardano-serialization-lib",
"version": "11.0.0-rc.5",
"version": "11.0.0-rc.6",
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
"scripts": {
"rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; wasm-pack pack) && npm run js:flowgen",
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardano-serialization-lib"
version = "11.0.0-rc.5"
version = "11.0.0-rc.6"
edition = "2018"
authors = ["EMURGO"]
license = "MIT"
Expand Down
13 changes: 3 additions & 10 deletions rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -5953,6 +5953,9 @@ declare export class TransactionBuilder {
* in the builder to be used when building the tx body.
* In case there are no plutus input witnesses present - nothing will change
* You can set specific hash value using `.set_script_data_hash`
* NOTE: this function will check which language versions are used in the present scripts
* and will assert and require for a corresponding cost-model to be present in the passed map.
* Only the cost-models for the present language versions will be used in the hash calculation.
* @param {Costmdls} cost_models
*/
calc_script_data_hash(cost_models: Costmdls): void;
Expand Down Expand Up @@ -6727,16 +6730,6 @@ declare export class TxBuilderConstants {
* @returns {Costmdls}
*/
static plutus_vasil_cost_models(): Costmdls;

/**
* @returns {Costmdls}
*/
static plutus_vasil_cost_models_v1_only(): Costmdls;

/**
* @returns {Costmdls}
*/
static plutus_vasil_cost_models_v2_only(): Costmdls;
}
/**
*/
Expand Down
75 changes: 75 additions & 0 deletions rust/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,9 @@ impl TransactionBuilder {
/// in the builder to be used when building the tx body.
/// In case there are no plutus input witnesses present - nothing will change
/// You can set specific hash value using `.set_script_data_hash`
/// NOTE: this function will check which language versions are used in the present scripts
/// and will assert and require for a corresponding cost-model to be present in the passed map.
/// Only the cost-models for the present language versions will be used in the hash calculation.
pub fn calc_script_data_hash(&mut self, cost_models: &Costmdls) -> Result<(), JsError> {
let mut retained_cost_models = Costmdls::new();
if let Some(pw) = self.inputs.get_plutus_input_scripts() {
Expand Down Expand Up @@ -6010,5 +6013,77 @@ mod tests {
assert!(tx_builder.total_collateral.is_none());
assert!(tx_builder.collateral_return.is_none());
}

#[test]
fn test_costmodel_retaining_for_v1() {
let mut tx_builder = create_reallistic_tx_builder();
tx_builder.set_fee(&to_bignum(42));
tx_builder.set_collateral(&create_collateral());

let (script1, _) = plutus_script_and_hash(0);
let datum = PlutusData::new_integer(&BigInt::from_str("42").unwrap());
let redeemer = Redeemer::new(
&RedeemerTag::new_spend(),
&to_bignum(0),
&datum,
&ExUnits::new(&to_bignum(1700), &to_bignum(368100)),
);
tx_builder.add_plutus_script_input(
&PlutusWitness::new(&script1, &datum, &redeemer),
&TransactionInput::new(&genesis_id(), 0),
&Value::new(&to_bignum(1_000_000)),
);

// Setting script data hash removes the error
tx_builder.calc_script_data_hash(
&TxBuilderConstants::plutus_vasil_cost_models(),
).unwrap();

// Using SAFE `.build_tx`
let res2 = tx_builder.build_tx();
assert!(res2.is_ok());

let v1 = Language::new_plutus_v1();
let v1_costmodel = TxBuilderConstants::plutus_vasil_cost_models().get(&v1).unwrap();
let mut retained_cost_models = Costmdls::new();
retained_cost_models.insert(&v1, &v1_costmodel);

let data_hash = hash_script_data(
&Redeemers::from(vec![redeemer.clone()]),
&retained_cost_models,
Some(PlutusList::from(vec![datum])),
);
assert_eq!(tx_builder.script_data_hash.unwrap(), data_hash);
}

#[test]
fn test_costmodel_retaining_fails_on_missing_costmodel() {
let mut tx_builder = create_reallistic_tx_builder();
tx_builder.set_fee(&to_bignum(42));
tx_builder.set_collateral(&create_collateral());

let (script1, _) = plutus_script_and_hash(0);
let datum = PlutusData::new_integer(&BigInt::from_str("42").unwrap());
let redeemer = Redeemer::new(
&RedeemerTag::new_spend(),
&to_bignum(0),
&datum,
&ExUnits::new(&to_bignum(1700), &to_bignum(368100)),
);
tx_builder.add_plutus_script_input(
&PlutusWitness::new(&script1, &datum, &redeemer),
&TransactionInput::new(&genesis_id(), 0),
&Value::new(&to_bignum(1_000_000)),
);

let v2 = Language::new_plutus_v2();
let v2_costmodel = TxBuilderConstants::plutus_vasil_cost_models().get(&v2).unwrap();
let mut retained_cost_models = Costmdls::new();
retained_cost_models.insert(&v2, &v2_costmodel);

// Setting script data hash removes the error
let calc_result = tx_builder.calc_script_data_hash(&retained_cost_models);
assert!(calc_result.is_err());
}
}

2 changes: 1 addition & 1 deletion rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,7 @@ mod tests {
}

#[test]
fn ttt() {
fn test_vasil_v1_costmodel_hashing() {
let v1 = Language::new_plutus_v1();
let v1_cost_model = TxBuilderConstants::plutus_vasil_cost_models()
.get(&v1).unwrap();
Expand Down

0 comments on commit 70fd854

Please sign in to comment.