Skip to content

Commit 8b0a2b2

Browse files
authored
Merge pull request #504 from Emurgo/ruslan/min-ada-calc-fix
Alonzo compatibility improvement
2 parents 68858db + 061b466 commit 8b0a2b2

File tree

9 files changed

+42
-32
lines changed

9 files changed

+42
-32
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.3",
3+
"version": "11.0.5",
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; cd ..; npm run js:ts-json-gen; cd rust; 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.3"
3+
version = "11.0.5"
44
edition = "2018"
55
authors = ["EMURGO"]
66
license = "MIT"

rust/json-gen/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/src/fakes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#![allow(dead_code)]
2-
use crate::{
3-
to_bignum, Address, BaseAddress, Bip32PrivateKey, DataHash, Ed25519KeyHash, Ed25519Signature,
4-
NetworkInfo, StakeCredential, TransactionHash, TransactionIndex, TransactionInput,
5-
TransactionOutput, Value, Vkey,
6-
};
2+
use crate::{to_bignum, Address, BaseAddress, Bip32PrivateKey, DataHash, Ed25519KeyHash, Ed25519Signature, NetworkInfo, StakeCredential, TransactionHash, TransactionIndex, TransactionInput, TransactionOutput, Value, Vkey, PolicyID};
73

84
pub(crate) fn fake_bytes_32(x: u8) -> Vec<u8> {
95
vec![
@@ -69,3 +65,7 @@ pub(crate) fn fake_vkey() -> Vkey {
6965
pub(crate) fn fake_signature(x: u8) -> Ed25519Signature {
7066
Ed25519Signature::from_bytes([x; 64].to_vec()).unwrap()
7167
}
68+
69+
pub(crate) fn fake_policy_id(x: u8) -> PolicyID {
70+
PolicyID::from([x; 28])
71+
}

rust/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ impl DataCost {
264264
}
265265
}
266266
}
267+
268+
// <TODO:REMOVE_AFTER_BABBAGE>
269+
pub(crate) fn coins_per_word(&self) -> Result<Coin, JsError> {
270+
match &self.0 {
271+
DataCostEnum::CoinsPerByte(coins_per_byte) => {
272+
coins_per_byte
273+
.checked_mul(&BigNum::from_str("8")?)
274+
},
275+
DataCostEnum::CoinsPerWord(coins_per_word) => Ok(coins_per_word.clone()),
276+
}
277+
}
267278
}
268279

269280
#[wasm_bindgen]

rust/src/tx_builder.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ pub struct TransactionBuilderConfig {
210210
key_deposit: Coin, // protocol parameter
211211
max_value_size: u32, // protocol parameter
212212
max_tx_size: u32, // protocol parameter
213-
coins_per_utxo_byte: Coin, // protocol parameter
213+
data_cost: DataCost, // protocol parameter
214214
ex_unit_prices: Option<ExUnitPrices>, // protocol parameter
215215
prefer_pure_change: bool,
216216
}
217217

218218
impl TransactionBuilderConfig {
219219
fn utxo_cost(&self) -> DataCost {
220-
DataCost::new_coins_per_byte(&self.coins_per_utxo_byte)
220+
self.data_cost.clone()
221221
}
222222
}
223223

@@ -229,7 +229,7 @@ pub struct TransactionBuilderConfigBuilder {
229229
key_deposit: Option<Coin>, // protocol parameter
230230
max_value_size: Option<u32>, // protocol parameter
231231
max_tx_size: Option<u32>, // protocol parameter
232-
coins_per_utxo_byte: Option<Coin>, // protocol parameter
232+
data_cost: Option<DataCost>, // protocol parameter
233233
ex_unit_prices: Option<ExUnitPrices>, // protocol parameter
234234
prefer_pure_change: bool,
235235
}
@@ -243,7 +243,7 @@ impl TransactionBuilderConfigBuilder {
243243
key_deposit: None,
244244
max_value_size: None,
245245
max_tx_size: None,
246-
coins_per_utxo_byte: None,
246+
data_cost: None,
247247
ex_unit_prices: None,
248248
prefer_pure_change: false,
249249
}
@@ -262,14 +262,14 @@ impl TransactionBuilderConfigBuilder {
262262
note = "Since babbage era cardano nodes use coins per byte. Use '.coins_per_utxo_byte' instead."
263263
)]
264264
pub fn coins_per_utxo_word(&self, coins_per_utxo_word: &Coin) -> Self {
265-
self.coins_per_utxo_byte(
266-
&DataCost::new_coins_per_word(coins_per_utxo_word).coins_per_byte(),
267-
)
265+
let mut cfg = self.clone();
266+
cfg.data_cost = Some(DataCost::new_coins_per_word(coins_per_utxo_word));
267+
cfg
268268
}
269269

270270
pub fn coins_per_utxo_byte(&self, coins_per_utxo_byte: &Coin) -> Self {
271271
let mut cfg = self.clone();
272-
cfg.coins_per_utxo_byte = Some(coins_per_utxo_byte.clone());
272+
cfg.data_cost = Some(DataCost::new_coins_per_byte(coins_per_utxo_byte));
273273
cfg
274274
}
275275

@@ -327,8 +327,8 @@ impl TransactionBuilderConfigBuilder {
327327
max_tx_size: cfg
328328
.max_tx_size
329329
.ok_or(JsError::from_str("uninitialized field: max_tx_size"))?,
330-
coins_per_utxo_byte: cfg.coins_per_utxo_byte.ok_or(JsError::from_str(
331-
"uninitialized field: coins_per_utxo_byte",
330+
data_cost: cfg.data_cost.ok_or(JsError::from_str(
331+
"uninitialized field: coins_per_utxo_byte or coins_per_utxo_word",
332332
))?,
333333
ex_unit_prices: cfg.ex_unit_prices,
334334
prefer_pure_change: cfg.prefer_pure_change,
@@ -1844,10 +1844,7 @@ impl TransactionBuilder {
18441844
mod tests {
18451845
use super::output_builder::TransactionOutputBuilder;
18461846
use super::*;
1847-
use crate::fakes::{
1848-
fake_base_address, fake_bytes_32, fake_key_hash, fake_tx_hash, fake_tx_input,
1849-
fake_tx_input2, fake_value, fake_value2,
1850-
};
1847+
use crate::fakes::{fake_base_address, fake_bytes_32, fake_key_hash, fake_policy_id, fake_tx_hash, fake_tx_input, fake_tx_input2, fake_value, fake_value2};
18511848
use crate::tx_builder_constants::TxBuilderConstants;
18521849
use fees::*;
18531850

@@ -3561,9 +3558,9 @@ mod tests {
35613558

35623559
fn create_multiasset() -> (MultiAsset, [ScriptHash; 3], [AssetName; 3]) {
35633560
let policy_ids = [
3564-
PolicyID::from([0u8; 28]),
3565-
PolicyID::from([1u8; 28]),
3566-
PolicyID::from([2u8; 28]),
3561+
fake_policy_id(0),
3562+
fake_policy_id(1),
3563+
fake_policy_id(2),
35673564
];
35683565
let names = [
35693566
AssetName::new(vec![99u8; 32]).unwrap(),

rust/src/utils.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,17 +1463,19 @@ impl MinOutputAdaCalculator {
14631463

14641464
pub fn calculate_ada(&self) -> Result<BigNum, JsError> {
14651465
let coins_per_byte = self.data_cost.coins_per_byte();
1466+
// <TODO:REMOVE_AFTER_BABBAGE>
1467+
let coins_per_word = self.data_cost.coins_per_word()?;
14661468
let mut output: TransactionOutput = self.output.clone();
14671469
fn calc_required_coin(
14681470
output: &TransactionOutput,
14691471
coins_per_byte: &Coin,
1472+
coins_per_word: &Coin,
14701473
) -> Result<Coin, JsError> {
1474+
// <TODO:REMOVE_AFTER_BABBAGE>
14711475
let legacy_coin = _min_ada_required_legacy(
14721476
&output.amount(),
14731477
output.has_data_hash(),
1474-
&coins_per_byte
1475-
.checked_add(&BigNum::one())?
1476-
.checked_mul(&BigNum::from_str("8")?)?
1478+
coins_per_word,
14771479
)?;
14781480
//according to https://hydra.iohk.io/build/15339994/download/1/babbage-changes.pdf
14791481
//See on the page 9 getValue txout
@@ -1483,15 +1485,15 @@ impl MinOutputAdaCalculator {
14831485
Ok(BigNum::max(&result, &legacy_coin))
14841486
}
14851487
for _ in 0..3 {
1486-
let required_coin = calc_required_coin(&output, &coins_per_byte)?;
1488+
let required_coin = calc_required_coin(&output, &coins_per_byte, &coins_per_word)?;
14871489
if output.amount.coin.less_than(&required_coin) {
14881490
output.amount.coin = required_coin.clone();
14891491
} else {
14901492
return Ok(required_coin);
14911493
}
14921494
}
14931495
output.amount.coin = to_bignum(u64::MAX);
1494-
calc_required_coin(&output, &coins_per_byte)
1496+
calc_required_coin(&output, &coins_per_byte, &coins_per_word)
14951497
}
14961498

14971499
fn create_fake_output() -> Result<TransactionOutput, JsError> {

0 commit comments

Comments
 (0)