Skip to content

Commit

Permalink
Merge pull request #504 from Emurgo/ruslan/min-ada-calc-fix
Browse files Browse the repository at this point in the history
Alonzo compatibility improvement
  • Loading branch information
vsubhuman committed Aug 24, 2022
2 parents 68858db + 061b466 commit 8b0a2b2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 32 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.3",
"version": "11.0.5",
"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; cd ..; npm run js:ts-json-gen; cd rust; 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.3"
version = "11.0.5"
edition = "2018"
authors = ["EMURGO"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion rust/json-gen/Cargo.lock

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

10 changes: 5 additions & 5 deletions rust/src/fakes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#![allow(dead_code)]
use crate::{
to_bignum, Address, BaseAddress, Bip32PrivateKey, DataHash, Ed25519KeyHash, Ed25519Signature,
NetworkInfo, StakeCredential, TransactionHash, TransactionIndex, TransactionInput,
TransactionOutput, Value, Vkey,
};
use crate::{to_bignum, Address, BaseAddress, Bip32PrivateKey, DataHash, Ed25519KeyHash, Ed25519Signature, NetworkInfo, StakeCredential, TransactionHash, TransactionIndex, TransactionInput, TransactionOutput, Value, Vkey, PolicyID};

pub(crate) fn fake_bytes_32(x: u8) -> Vec<u8> {
vec![
Expand Down Expand Up @@ -69,3 +65,7 @@ pub(crate) fn fake_vkey() -> Vkey {
pub(crate) fn fake_signature(x: u8) -> Ed25519Signature {
Ed25519Signature::from_bytes([x; 64].to_vec()).unwrap()
}

pub(crate) fn fake_policy_id(x: u8) -> PolicyID {
PolicyID::from([x; 28])
}
11 changes: 11 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ impl DataCost {
}
}
}

// <TODO:REMOVE_AFTER_BABBAGE>
pub(crate) fn coins_per_word(&self) -> Result<Coin, JsError> {
match &self.0 {
DataCostEnum::CoinsPerByte(coins_per_byte) => {
coins_per_byte
.checked_mul(&BigNum::from_str("8")?)
},
DataCostEnum::CoinsPerWord(coins_per_word) => Ok(coins_per_word.clone()),
}
}
}

#[wasm_bindgen]
Expand Down
31 changes: 14 additions & 17 deletions rust/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ pub struct TransactionBuilderConfig {
key_deposit: Coin, // protocol parameter
max_value_size: u32, // protocol parameter
max_tx_size: u32, // protocol parameter
coins_per_utxo_byte: Coin, // protocol parameter
data_cost: DataCost, // protocol parameter
ex_unit_prices: Option<ExUnitPrices>, // protocol parameter
prefer_pure_change: bool,
}

impl TransactionBuilderConfig {
fn utxo_cost(&self) -> DataCost {
DataCost::new_coins_per_byte(&self.coins_per_utxo_byte)
self.data_cost.clone()
}
}

Expand All @@ -229,7 +229,7 @@ pub struct TransactionBuilderConfigBuilder {
key_deposit: Option<Coin>, // protocol parameter
max_value_size: Option<u32>, // protocol parameter
max_tx_size: Option<u32>, // protocol parameter
coins_per_utxo_byte: Option<Coin>, // protocol parameter
data_cost: Option<DataCost>, // protocol parameter
ex_unit_prices: Option<ExUnitPrices>, // protocol parameter
prefer_pure_change: bool,
}
Expand All @@ -243,7 +243,7 @@ impl TransactionBuilderConfigBuilder {
key_deposit: None,
max_value_size: None,
max_tx_size: None,
coins_per_utxo_byte: None,
data_cost: None,
ex_unit_prices: None,
prefer_pure_change: false,
}
Expand All @@ -262,14 +262,14 @@ impl TransactionBuilderConfigBuilder {
note = "Since babbage era cardano nodes use coins per byte. Use '.coins_per_utxo_byte' instead."
)]
pub fn coins_per_utxo_word(&self, coins_per_utxo_word: &Coin) -> Self {
self.coins_per_utxo_byte(
&DataCost::new_coins_per_word(coins_per_utxo_word).coins_per_byte(),
)
let mut cfg = self.clone();
cfg.data_cost = Some(DataCost::new_coins_per_word(coins_per_utxo_word));
cfg
}

pub fn coins_per_utxo_byte(&self, coins_per_utxo_byte: &Coin) -> Self {
let mut cfg = self.clone();
cfg.coins_per_utxo_byte = Some(coins_per_utxo_byte.clone());
cfg.data_cost = Some(DataCost::new_coins_per_byte(coins_per_utxo_byte));
cfg
}

Expand Down Expand Up @@ -327,8 +327,8 @@ impl TransactionBuilderConfigBuilder {
max_tx_size: cfg
.max_tx_size
.ok_or(JsError::from_str("uninitialized field: max_tx_size"))?,
coins_per_utxo_byte: cfg.coins_per_utxo_byte.ok_or(JsError::from_str(
"uninitialized field: coins_per_utxo_byte",
data_cost: cfg.data_cost.ok_or(JsError::from_str(
"uninitialized field: coins_per_utxo_byte or coins_per_utxo_word",
))?,
ex_unit_prices: cfg.ex_unit_prices,
prefer_pure_change: cfg.prefer_pure_change,
Expand Down Expand Up @@ -1844,10 +1844,7 @@ impl TransactionBuilder {
mod tests {
use super::output_builder::TransactionOutputBuilder;
use super::*;
use crate::fakes::{
fake_base_address, fake_bytes_32, fake_key_hash, fake_tx_hash, fake_tx_input,
fake_tx_input2, fake_value, fake_value2,
};
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};
use crate::tx_builder_constants::TxBuilderConstants;
use fees::*;

Expand Down Expand Up @@ -3561,9 +3558,9 @@ mod tests {

fn create_multiasset() -> (MultiAsset, [ScriptHash; 3], [AssetName; 3]) {
let policy_ids = [
PolicyID::from([0u8; 28]),
PolicyID::from([1u8; 28]),
PolicyID::from([2u8; 28]),
fake_policy_id(0),
fake_policy_id(1),
fake_policy_id(2),
];
let names = [
AssetName::new(vec![99u8; 32]).unwrap(),
Expand Down
12 changes: 7 additions & 5 deletions rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,17 +1463,19 @@ impl MinOutputAdaCalculator {

pub fn calculate_ada(&self) -> Result<BigNum, JsError> {
let coins_per_byte = self.data_cost.coins_per_byte();
// <TODO:REMOVE_AFTER_BABBAGE>
let coins_per_word = self.data_cost.coins_per_word()?;
let mut output: TransactionOutput = self.output.clone();
fn calc_required_coin(
output: &TransactionOutput,
coins_per_byte: &Coin,
coins_per_word: &Coin,
) -> Result<Coin, JsError> {
// <TODO:REMOVE_AFTER_BABBAGE>
let legacy_coin = _min_ada_required_legacy(
&output.amount(),
output.has_data_hash(),
&coins_per_byte
.checked_add(&BigNum::one())?
.checked_mul(&BigNum::from_str("8")?)?
coins_per_word,
)?;
//according to https://hydra.iohk.io/build/15339994/download/1/babbage-changes.pdf
//See on the page 9 getValue txout
Expand All @@ -1483,15 +1485,15 @@ impl MinOutputAdaCalculator {
Ok(BigNum::max(&result, &legacy_coin))
}
for _ in 0..3 {
let required_coin = calc_required_coin(&output, &coins_per_byte)?;
let required_coin = calc_required_coin(&output, &coins_per_byte, &coins_per_word)?;
if output.amount.coin.less_than(&required_coin) {
output.amount.coin = required_coin.clone();
} else {
return Ok(required_coin);
}
}
output.amount.coin = to_bignum(u64::MAX);
calc_required_coin(&output, &coins_per_byte)
calc_required_coin(&output, &coins_per_byte, &coins_per_word)
}

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

0 comments on commit 8b0a2b2

Please sign in to comment.