Skip to content

Commit 70fd854

Browse files
committed
tests
1 parent 58ea93f commit 70fd854

File tree

7 files changed

+83
-15
lines changed

7 files changed

+83
-15
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cardano-serialization-lib",
3-
"version": "11.0.0-rc.5",
3+
"version": "11.0.0-rc.6",
44
"description": "(De)serialization functions for the Cardano blockchain along with related utility functions",
55
"scripts": {
66
"rust:build-nodejs": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=nodejs; wasm-pack pack) && npm run js:flowgen",

rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cardano-serialization-lib"
3-
version = "11.0.0-rc.5"
3+
version = "11.0.0-rc.6"
44
edition = "2018"
55
authors = ["EMURGO"]
66
license = "MIT"

rust/pkg/cardano_serialization_lib.js.flow

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5953,6 +5953,9 @@ declare export class TransactionBuilder {
59535953
* in the builder to be used when building the tx body.
59545954
* In case there are no plutus input witnesses present - nothing will change
59555955
* You can set specific hash value using `.set_script_data_hash`
5956+
* NOTE: this function will check which language versions are used in the present scripts
5957+
* and will assert and require for a corresponding cost-model to be present in the passed map.
5958+
* Only the cost-models for the present language versions will be used in the hash calculation.
59565959
* @param {Costmdls} cost_models
59575960
*/
59585961
calc_script_data_hash(cost_models: Costmdls): void;
@@ -6727,16 +6730,6 @@ declare export class TxBuilderConstants {
67276730
* @returns {Costmdls}
67286731
*/
67296732
static plutus_vasil_cost_models(): Costmdls;
6730-
6731-
/**
6732-
* @returns {Costmdls}
6733-
*/
6734-
static plutus_vasil_cost_models_v1_only(): Costmdls;
6735-
6736-
/**
6737-
* @returns {Costmdls}
6738-
*/
6739-
static plutus_vasil_cost_models_v2_only(): Costmdls;
67406733
}
67416734
/**
67426735
*/

rust/src/tx_builder.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,9 @@ impl TransactionBuilder {
14201420
/// in the builder to be used when building the tx body.
14211421
/// In case there are no plutus input witnesses present - nothing will change
14221422
/// You can set specific hash value using `.set_script_data_hash`
1423+
/// NOTE: this function will check which language versions are used in the present scripts
1424+
/// and will assert and require for a corresponding cost-model to be present in the passed map.
1425+
/// Only the cost-models for the present language versions will be used in the hash calculation.
14231426
pub fn calc_script_data_hash(&mut self, cost_models: &Costmdls) -> Result<(), JsError> {
14241427
let mut retained_cost_models = Costmdls::new();
14251428
if let Some(pw) = self.inputs.get_plutus_input_scripts() {
@@ -6010,5 +6013,77 @@ mod tests {
60106013
assert!(tx_builder.total_collateral.is_none());
60116014
assert!(tx_builder.collateral_return.is_none());
60126015
}
6016+
6017+
#[test]
6018+
fn test_costmodel_retaining_for_v1() {
6019+
let mut tx_builder = create_reallistic_tx_builder();
6020+
tx_builder.set_fee(&to_bignum(42));
6021+
tx_builder.set_collateral(&create_collateral());
6022+
6023+
let (script1, _) = plutus_script_and_hash(0);
6024+
let datum = PlutusData::new_integer(&BigInt::from_str("42").unwrap());
6025+
let redeemer = Redeemer::new(
6026+
&RedeemerTag::new_spend(),
6027+
&to_bignum(0),
6028+
&datum,
6029+
&ExUnits::new(&to_bignum(1700), &to_bignum(368100)),
6030+
);
6031+
tx_builder.add_plutus_script_input(
6032+
&PlutusWitness::new(&script1, &datum, &redeemer),
6033+
&TransactionInput::new(&genesis_id(), 0),
6034+
&Value::new(&to_bignum(1_000_000)),
6035+
);
6036+
6037+
// Setting script data hash removes the error
6038+
tx_builder.calc_script_data_hash(
6039+
&TxBuilderConstants::plutus_vasil_cost_models(),
6040+
).unwrap();
6041+
6042+
// Using SAFE `.build_tx`
6043+
let res2 = tx_builder.build_tx();
6044+
assert!(res2.is_ok());
6045+
6046+
let v1 = Language::new_plutus_v1();
6047+
let v1_costmodel = TxBuilderConstants::plutus_vasil_cost_models().get(&v1).unwrap();
6048+
let mut retained_cost_models = Costmdls::new();
6049+
retained_cost_models.insert(&v1, &v1_costmodel);
6050+
6051+
let data_hash = hash_script_data(
6052+
&Redeemers::from(vec![redeemer.clone()]),
6053+
&retained_cost_models,
6054+
Some(PlutusList::from(vec![datum])),
6055+
);
6056+
assert_eq!(tx_builder.script_data_hash.unwrap(), data_hash);
6057+
}
6058+
6059+
#[test]
6060+
fn test_costmodel_retaining_fails_on_missing_costmodel() {
6061+
let mut tx_builder = create_reallistic_tx_builder();
6062+
tx_builder.set_fee(&to_bignum(42));
6063+
tx_builder.set_collateral(&create_collateral());
6064+
6065+
let (script1, _) = plutus_script_and_hash(0);
6066+
let datum = PlutusData::new_integer(&BigInt::from_str("42").unwrap());
6067+
let redeemer = Redeemer::new(
6068+
&RedeemerTag::new_spend(),
6069+
&to_bignum(0),
6070+
&datum,
6071+
&ExUnits::new(&to_bignum(1700), &to_bignum(368100)),
6072+
);
6073+
tx_builder.add_plutus_script_input(
6074+
&PlutusWitness::new(&script1, &datum, &redeemer),
6075+
&TransactionInput::new(&genesis_id(), 0),
6076+
&Value::new(&to_bignum(1_000_000)),
6077+
);
6078+
6079+
let v2 = Language::new_plutus_v2();
6080+
let v2_costmodel = TxBuilderConstants::plutus_vasil_cost_models().get(&v2).unwrap();
6081+
let mut retained_cost_models = Costmdls::new();
6082+
retained_cost_models.insert(&v2, &v2_costmodel);
6083+
6084+
// Setting script data hash removes the error
6085+
let calc_result = tx_builder.calc_script_data_hash(&retained_cost_models);
6086+
assert!(calc_result.is_err());
6087+
}
60136088
}
60146089

rust/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ mod tests {
24492449
}
24502450

24512451
#[test]
2452-
fn ttt() {
2452+
fn test_vasil_v1_costmodel_hashing() {
24532453
let v1 = Language::new_plutus_v1();
24542454
let v1_cost_model = TxBuilderConstants::plutus_vasil_cost_models()
24552455
.get(&v1).unwrap();

0 commit comments

Comments
 (0)