Skip to content

Commit acc2d35

Browse files
authored
Merge pull request #206 from Emurgo/ruslan/value-not-output
Replaced max_output_size with max_value_size
2 parents 1bc6a81 + 40a3804 commit acc2d35

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

rust/src/tx_builder.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub struct TransactionBuilder {
126126
minimum_utxo_val: BigNum,
127127
pool_deposit: BigNum,
128128
key_deposit: BigNum,
129-
max_output_size: u32,
129+
max_value_size: u32,
130130
max_tx_size: u32,
131131
fee_algo: fees::LinearFee,
132132
inputs: Vec<TxBuilderInput>,
@@ -233,12 +233,12 @@ impl TransactionBuilder {
233233
}
234234

235235
pub fn add_output(&mut self, output: &TransactionOutput) -> Result<(), JsError> {
236-
let output_size = output.to_bytes().len();
237-
if output_size > self.max_output_size as usize {
236+
let value_size = output.amount.to_bytes().len();
237+
if value_size > self.max_value_size as usize {
238238
return Err(JsError::from_str(&format!(
239-
"Maximum output size of {} exceeded. Found: {}",
240-
self.max_output_size,
241-
output_size
239+
"Maximum value size of {} exceeded. Found: {}",
240+
self.max_value_size,
241+
value_size
242242
)));
243243
}
244244
let min_ada = min_ada_required(&output.amount(), &self.minimum_utxo_val);
@@ -306,14 +306,14 @@ impl TransactionBuilder {
306306
minimum_utxo_val: &Coin,
307307
pool_deposit: &BigNum, // protocol parameter
308308
key_deposit: &BigNum, // protocol parameter
309-
max_output_size: u32, // protocol parameter
309+
max_value_size: u32, // protocol parameter
310310
max_tx_size: u32, // protocol parameter
311311
) -> Self {
312312
Self {
313313
minimum_utxo_val: minimum_utxo_val.clone(),
314314
key_deposit: key_deposit.clone(),
315315
pool_deposit: pool_deposit.clone(),
316-
max_output_size,
316+
max_value_size,
317317
max_tx_size,
318318
fee_algo: linear_fee.clone(),
319319
inputs: Vec::new(),
@@ -407,7 +407,7 @@ impl TransactionBuilder {
407407
}
408408
let change_estimator = input_total.checked_sub(&output_total)?;
409409
if has_assets(change_estimator.multiasset()) {
410-
fn pack_nfts_for_change(max_output_size: u32, change_address: &Address, change_estimator: &Value) -> Result<MultiAsset, JsError> {
410+
fn pack_nfts_for_change(max_value_size: u32, change_address: &Address, change_estimator: &Value) -> Result<MultiAsset, JsError> {
411411
// we insert the entire available ADA temporarily here since that could potentially impact the size
412412
// as it could be 1, 2 3 or 4 bytes for Coin.
413413
let mut base_coin = Value::new(&change_estimator.coin());
@@ -436,14 +436,14 @@ impl TransactionBuilder {
436436
// so for simplicity we will just do it the safe, naive way unless
437437
// performance becomes an issue.
438438
//let extra_bytes = policy.to_bytes().len() + assets.to_bytes().len() + 2 + cbor_len_diff;
439-
//if bytes_used + extra_bytes <= max_output_size as usize {
439+
//if bytes_used + extra_bytes <= max_value_size as usize {
440440
let old_amount = output.amount.clone();
441441
let mut val = Value::new(&Coin::zero());
442442
let mut next_nft = MultiAsset::new();
443443
next_nft.insert(policy, assets);
444444
val.set_multiasset(&next_nft);
445445
output.amount = output.amount.checked_add(&val)?;
446-
if output.to_bytes().len() > max_output_size as usize {
446+
if output.amount.to_bytes().len() > max_value_size as usize {
447447
output.amount = old_amount;
448448
break;
449449
}
@@ -455,7 +455,7 @@ impl TransactionBuilder {
455455
// we might need multiple change outputs for cases where the change has many asset types
456456
// which surpass the max UTXO size limit
457457
while let Some(Ordering::Greater) = change_left.multiasset.as_ref().map_or_else(|| None, |ma| ma.partial_cmp(&MultiAsset::new())) {
458-
let nft_change = pack_nfts_for_change(self.max_output_size, address, &change_left)?;
458+
let nft_change = pack_nfts_for_change(self.max_value_size, address, &change_left)?;
459459
if nft_change.len() == 0 {
460460
// this likely should never happen
461461
return Err(JsError::from_str("NFTs too large for change output"));
@@ -587,7 +587,7 @@ mod tests {
587587
use super::*;
588588
use fees::*;
589589

590-
const MAX_OUTPUT_SIZE: u32 = 4000;
590+
const MAX_VALUE_SIZE: u32 = 4000;
591591
const MAX_TX_SIZE: u32 = 8000; // might be out of date but suffices for our tests
592592

593593
fn genesis_id() -> TransactionHash {
@@ -612,7 +612,7 @@ mod tests {
612612
&to_bignum(1),
613613
&to_bignum(1),
614614
&to_bignum(1),
615-
MAX_OUTPUT_SIZE,
615+
MAX_VALUE_SIZE,
616616
MAX_TX_SIZE
617617
);
618618
let spend = root_key_15()
@@ -675,7 +675,7 @@ mod tests {
675675
&to_bignum(1),
676676
&to_bignum(1),
677677
&to_bignum(1),
678-
MAX_OUTPUT_SIZE,
678+
MAX_VALUE_SIZE,
679679
MAX_TX_SIZE
680680
);
681681
let spend = root_key_15()
@@ -736,7 +736,7 @@ mod tests {
736736
&to_bignum(1),
737737
&to_bignum(1),
738738
&to_bignum(1_000_000),
739-
MAX_OUTPUT_SIZE,
739+
MAX_VALUE_SIZE,
740740
MAX_TX_SIZE
741741
);
742742
let spend = root_key_15()
@@ -805,7 +805,7 @@ mod tests {
805805
&to_bignum(1),
806806
&to_bignum(0),
807807
&to_bignum(0),
808-
MAX_OUTPUT_SIZE,
808+
MAX_VALUE_SIZE,
809809
MAX_TX_SIZE
810810
);
811811
let spend = root_key_15()
@@ -862,7 +862,7 @@ mod tests {
862862
&to_bignum(1),
863863
&to_bignum(0),
864864
&to_bignum(0),
865-
MAX_OUTPUT_SIZE,
865+
MAX_VALUE_SIZE,
866866
MAX_TX_SIZE
867867
);
868868
let spend = root_key_15()
@@ -928,7 +928,7 @@ mod tests {
928928
&to_bignum(1),
929929
&to_bignum(0),
930930
&to_bignum(5),
931-
MAX_OUTPUT_SIZE,
931+
MAX_VALUE_SIZE,
932932
MAX_TX_SIZE
933933
);
934934
let spend = root_key_15()
@@ -999,7 +999,7 @@ mod tests {
999999
&to_bignum(1),
10001000
&to_bignum(1),
10011001
&to_bignum(1),
1002-
MAX_OUTPUT_SIZE,
1002+
MAX_VALUE_SIZE,
10031003
MAX_TX_SIZE
10041004
);
10051005
let spend = root_key_15()
@@ -1080,7 +1080,7 @@ mod tests {
10801080
&minimum_utxo_value,
10811081
&to_bignum(0),
10821082
&to_bignum(0),
1083-
MAX_OUTPUT_SIZE,
1083+
MAX_VALUE_SIZE,
10841084
MAX_TX_SIZE
10851085
);
10861086
let spend = root_key_15()
@@ -1196,7 +1196,7 @@ mod tests {
11961196
&to_bignum(1),
11971197
&to_bignum(1),
11981198
&to_bignum(1),
1199-
MAX_OUTPUT_SIZE,
1199+
MAX_VALUE_SIZE,
12001200
MAX_TX_SIZE
12011201
);
12021202
let spend = root_key_15()
@@ -1270,7 +1270,7 @@ mod tests {
12701270
&to_bignum(1000000),
12711271
&to_bignum(500000000),
12721272
&to_bignum(2000000),
1273-
MAX_OUTPUT_SIZE,
1273+
MAX_VALUE_SIZE,
12741274
MAX_TX_SIZE
12751275
);
12761276

@@ -1312,7 +1312,7 @@ mod tests {
13121312
&to_bignum(1000000),
13131313
&to_bignum(500000000),
13141314
&to_bignum(2000000),
1315-
MAX_OUTPUT_SIZE,
1315+
MAX_VALUE_SIZE,
13161316
MAX_TX_SIZE
13171317
);
13181318

@@ -1357,7 +1357,7 @@ mod tests {
13571357
&to_bignum(1000000),
13581358
&to_bignum(500000000),
13591359
&to_bignum(2000000),
1360-
MAX_OUTPUT_SIZE,
1360+
MAX_VALUE_SIZE,
13611361
MAX_TX_SIZE,
13621362
);
13631363

@@ -1416,20 +1416,7 @@ mod tests {
14161416
assert!(change_assets.is_none());
14171417
}
14181418

1419-
#[test]
1420-
fn build_tx_add_change_split_nfts() {
1421-
let linear_fee = LinearFee::new(&to_bignum(0), &to_bignum(1));
1422-
let minimum_utxo_value = to_bignum(1);
1423-
let max_output_size = 150; // super low max output size to test with fewer assets
1424-
let mut tx_builder = TransactionBuilder::new(
1425-
&linear_fee,
1426-
&minimum_utxo_value,
1427-
&to_bignum(0),
1428-
&to_bignum(0),
1429-
max_output_size,
1430-
MAX_TX_SIZE
1431-
);
1432-
1419+
fn create_multiasset() -> (MultiAsset, [ScriptHash; 3], [AssetName; 3]) {
14331420
let policy_ids = [
14341421
PolicyID::from([0u8; 28]),
14351422
PolicyID::from([1u8; 28]),
@@ -1440,7 +1427,6 @@ mod tests {
14401427
AssetName::new(vec![0u8, 1, 2, 3]).unwrap(),
14411428
AssetName::new(vec![4u8, 5, 6, 7, 8, 9]).unwrap(),
14421429
];
1443-
14441430
let multiasset = policy_ids
14451431
.iter()
14461432
.zip(names.iter())
@@ -1452,6 +1438,24 @@ mod tests {
14521438
});
14531439
acc
14541440
});
1441+
return (multiasset, policy_ids, names);
1442+
}
1443+
1444+
#[test]
1445+
fn build_tx_add_change_split_nfts() {
1446+
let linear_fee = LinearFee::new(&to_bignum(0), &to_bignum(1));
1447+
let minimum_utxo_value = to_bignum(1);
1448+
let max_value_size = 100; // super low max output size to test with fewer assets
1449+
let mut tx_builder = TransactionBuilder::new(
1450+
&linear_fee,
1451+
&minimum_utxo_value,
1452+
&to_bignum(0),
1453+
&to_bignum(0),
1454+
max_value_size,
1455+
MAX_TX_SIZE
1456+
);
1457+
1458+
let (multiasset, policy_ids, names) = create_multiasset();
14551459

14561460
let mut input_value = Value::new(&to_bignum(10));
14571461
input_value.set_multiasset(&multiasset);
@@ -1491,7 +1495,7 @@ mod tests {
14911495
);
14921496
}
14931497
for output in final_tx.outputs.0.iter() {
1494-
assert!(output.to_bytes().len() <= max_output_size as usize);
1498+
assert!(output.amount.to_bytes().len() <= max_value_size as usize);
14951499
}
14961500
}
14971501

@@ -1518,7 +1522,8 @@ mod tests {
15181522
);
15191523

15201524
let output_addr = ByronAddress::from_base58("Ae2tdPwUPEZD9QQf2ZrcYV34pYJwxK4vqXaF8EXkup1eYH73zUScHReM42b").unwrap().to_address();
1521-
let output_amount = Value::new(&to_bignum(1));
1525+
let mut output_amount = Value::new(&to_bignum(1));
1526+
output_amount.set_multiasset(&create_multiasset().0);
15221527

15231528
assert!(tx_builder.add_output(&TransactionOutput::new(&output_addr, &output_amount)).is_err());
15241529
}
@@ -1527,13 +1532,13 @@ mod tests {
15271532
fn build_tx_add_change_nfts_not_enough_ada() {
15281533
let linear_fee = LinearFee::new(&to_bignum(0), &to_bignum(1));
15291534
let minimum_utxo_value = to_bignum(1);
1530-
let max_output_size = 150; // super low max output size to test with fewer assets
1535+
let max_value_size = 150; // super low max output size to test with fewer assets
15311536
let mut tx_builder = TransactionBuilder::new(
15321537
&linear_fee,
15331538
&minimum_utxo_value,
15341539
&to_bignum(0),
15351540
&to_bignum(0),
1536-
max_output_size,
1541+
max_value_size,
15371542
MAX_TX_SIZE
15381543
);
15391544

0 commit comments

Comments
 (0)