@@ -126,7 +126,7 @@ pub struct TransactionBuilder {
126
126
minimum_utxo_val : BigNum ,
127
127
pool_deposit : BigNum ,
128
128
key_deposit : BigNum ,
129
- max_output_size : u32 ,
129
+ max_value_size : u32 ,
130
130
max_tx_size : u32 ,
131
131
fee_algo : fees:: LinearFee ,
132
132
inputs : Vec < TxBuilderInput > ,
@@ -233,12 +233,12 @@ impl TransactionBuilder {
233
233
}
234
234
235
235
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 {
238
238
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
242
242
) ) ) ;
243
243
}
244
244
let min_ada = min_ada_required ( & output. amount ( ) , & self . minimum_utxo_val ) ;
@@ -306,14 +306,14 @@ impl TransactionBuilder {
306
306
minimum_utxo_val : & Coin ,
307
307
pool_deposit : & BigNum , // protocol parameter
308
308
key_deposit : & BigNum , // protocol parameter
309
- max_output_size : u32 , // protocol parameter
309
+ max_value_size : u32 , // protocol parameter
310
310
max_tx_size : u32 , // protocol parameter
311
311
) -> Self {
312
312
Self {
313
313
minimum_utxo_val : minimum_utxo_val. clone ( ) ,
314
314
key_deposit : key_deposit. clone ( ) ,
315
315
pool_deposit : pool_deposit. clone ( ) ,
316
- max_output_size ,
316
+ max_value_size ,
317
317
max_tx_size,
318
318
fee_algo : linear_fee. clone ( ) ,
319
319
inputs : Vec :: new ( ) ,
@@ -407,7 +407,7 @@ impl TransactionBuilder {
407
407
}
408
408
let change_estimator = input_total. checked_sub ( & output_total) ?;
409
409
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 > {
411
411
// we insert the entire available ADA temporarily here since that could potentially impact the size
412
412
// as it could be 1, 2 3 or 4 bytes for Coin.
413
413
let mut base_coin = Value :: new ( & change_estimator. coin ( ) ) ;
@@ -436,14 +436,14 @@ impl TransactionBuilder {
436
436
// so for simplicity we will just do it the safe, naive way unless
437
437
// performance becomes an issue.
438
438
//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 {
440
440
let old_amount = output. amount . clone ( ) ;
441
441
let mut val = Value :: new ( & Coin :: zero ( ) ) ;
442
442
let mut next_nft = MultiAsset :: new ( ) ;
443
443
next_nft. insert ( policy, assets) ;
444
444
val. set_multiasset ( & next_nft) ;
445
445
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 {
447
447
output. amount = old_amount;
448
448
break ;
449
449
}
@@ -455,7 +455,7 @@ impl TransactionBuilder {
455
455
// we might need multiple change outputs for cases where the change has many asset types
456
456
// which surpass the max UTXO size limit
457
457
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) ?;
459
459
if nft_change. len ( ) == 0 {
460
460
// this likely should never happen
461
461
return Err ( JsError :: from_str ( "NFTs too large for change output" ) ) ;
@@ -587,7 +587,7 @@ mod tests {
587
587
use super :: * ;
588
588
use fees:: * ;
589
589
590
- const MAX_OUTPUT_SIZE : u32 = 4000 ;
590
+ const MAX_VALUE_SIZE : u32 = 4000 ;
591
591
const MAX_TX_SIZE : u32 = 8000 ; // might be out of date but suffices for our tests
592
592
593
593
fn genesis_id ( ) -> TransactionHash {
@@ -612,7 +612,7 @@ mod tests {
612
612
& to_bignum ( 1 ) ,
613
613
& to_bignum ( 1 ) ,
614
614
& to_bignum ( 1 ) ,
615
- MAX_OUTPUT_SIZE ,
615
+ MAX_VALUE_SIZE ,
616
616
MAX_TX_SIZE
617
617
) ;
618
618
let spend = root_key_15 ( )
@@ -675,7 +675,7 @@ mod tests {
675
675
& to_bignum ( 1 ) ,
676
676
& to_bignum ( 1 ) ,
677
677
& to_bignum ( 1 ) ,
678
- MAX_OUTPUT_SIZE ,
678
+ MAX_VALUE_SIZE ,
679
679
MAX_TX_SIZE
680
680
) ;
681
681
let spend = root_key_15 ( )
@@ -736,7 +736,7 @@ mod tests {
736
736
& to_bignum ( 1 ) ,
737
737
& to_bignum ( 1 ) ,
738
738
& to_bignum ( 1_000_000 ) ,
739
- MAX_OUTPUT_SIZE ,
739
+ MAX_VALUE_SIZE ,
740
740
MAX_TX_SIZE
741
741
) ;
742
742
let spend = root_key_15 ( )
@@ -805,7 +805,7 @@ mod tests {
805
805
& to_bignum ( 1 ) ,
806
806
& to_bignum ( 0 ) ,
807
807
& to_bignum ( 0 ) ,
808
- MAX_OUTPUT_SIZE ,
808
+ MAX_VALUE_SIZE ,
809
809
MAX_TX_SIZE
810
810
) ;
811
811
let spend = root_key_15 ( )
@@ -862,7 +862,7 @@ mod tests {
862
862
& to_bignum ( 1 ) ,
863
863
& to_bignum ( 0 ) ,
864
864
& to_bignum ( 0 ) ,
865
- MAX_OUTPUT_SIZE ,
865
+ MAX_VALUE_SIZE ,
866
866
MAX_TX_SIZE
867
867
) ;
868
868
let spend = root_key_15 ( )
@@ -928,7 +928,7 @@ mod tests {
928
928
& to_bignum ( 1 ) ,
929
929
& to_bignum ( 0 ) ,
930
930
& to_bignum ( 5 ) ,
931
- MAX_OUTPUT_SIZE ,
931
+ MAX_VALUE_SIZE ,
932
932
MAX_TX_SIZE
933
933
) ;
934
934
let spend = root_key_15 ( )
@@ -999,7 +999,7 @@ mod tests {
999
999
& to_bignum ( 1 ) ,
1000
1000
& to_bignum ( 1 ) ,
1001
1001
& to_bignum ( 1 ) ,
1002
- MAX_OUTPUT_SIZE ,
1002
+ MAX_VALUE_SIZE ,
1003
1003
MAX_TX_SIZE
1004
1004
) ;
1005
1005
let spend = root_key_15 ( )
@@ -1080,7 +1080,7 @@ mod tests {
1080
1080
& minimum_utxo_value,
1081
1081
& to_bignum ( 0 ) ,
1082
1082
& to_bignum ( 0 ) ,
1083
- MAX_OUTPUT_SIZE ,
1083
+ MAX_VALUE_SIZE ,
1084
1084
MAX_TX_SIZE
1085
1085
) ;
1086
1086
let spend = root_key_15 ( )
@@ -1196,7 +1196,7 @@ mod tests {
1196
1196
& to_bignum ( 1 ) ,
1197
1197
& to_bignum ( 1 ) ,
1198
1198
& to_bignum ( 1 ) ,
1199
- MAX_OUTPUT_SIZE ,
1199
+ MAX_VALUE_SIZE ,
1200
1200
MAX_TX_SIZE
1201
1201
) ;
1202
1202
let spend = root_key_15 ( )
@@ -1270,7 +1270,7 @@ mod tests {
1270
1270
& to_bignum ( 1000000 ) ,
1271
1271
& to_bignum ( 500000000 ) ,
1272
1272
& to_bignum ( 2000000 ) ,
1273
- MAX_OUTPUT_SIZE ,
1273
+ MAX_VALUE_SIZE ,
1274
1274
MAX_TX_SIZE
1275
1275
) ;
1276
1276
@@ -1312,7 +1312,7 @@ mod tests {
1312
1312
& to_bignum ( 1000000 ) ,
1313
1313
& to_bignum ( 500000000 ) ,
1314
1314
& to_bignum ( 2000000 ) ,
1315
- MAX_OUTPUT_SIZE ,
1315
+ MAX_VALUE_SIZE ,
1316
1316
MAX_TX_SIZE
1317
1317
) ;
1318
1318
@@ -1357,7 +1357,7 @@ mod tests {
1357
1357
& to_bignum ( 1000000 ) ,
1358
1358
& to_bignum ( 500000000 ) ,
1359
1359
& to_bignum ( 2000000 ) ,
1360
- MAX_OUTPUT_SIZE ,
1360
+ MAX_VALUE_SIZE ,
1361
1361
MAX_TX_SIZE ,
1362
1362
) ;
1363
1363
@@ -1416,20 +1416,7 @@ mod tests {
1416
1416
assert ! ( change_assets. is_none( ) ) ;
1417
1417
}
1418
1418
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 ] ) {
1433
1420
let policy_ids = [
1434
1421
PolicyID :: from ( [ 0u8 ; 28 ] ) ,
1435
1422
PolicyID :: from ( [ 1u8 ; 28 ] ) ,
@@ -1440,7 +1427,6 @@ mod tests {
1440
1427
AssetName :: new ( vec ! [ 0u8 , 1 , 2 , 3 ] ) . unwrap ( ) ,
1441
1428
AssetName :: new ( vec ! [ 4u8 , 5 , 6 , 7 , 8 , 9 ] ) . unwrap ( ) ,
1442
1429
] ;
1443
-
1444
1430
let multiasset = policy_ids
1445
1431
. iter ( )
1446
1432
. zip ( names. iter ( ) )
@@ -1452,6 +1438,24 @@ mod tests {
1452
1438
} ) ;
1453
1439
acc
1454
1440
} ) ;
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 ( ) ;
1455
1459
1456
1460
let mut input_value = Value :: new ( & to_bignum ( 10 ) ) ;
1457
1461
input_value. set_multiasset ( & multiasset) ;
@@ -1491,7 +1495,7 @@ mod tests {
1491
1495
) ;
1492
1496
}
1493
1497
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 ) ;
1495
1499
}
1496
1500
}
1497
1501
@@ -1518,7 +1522,8 @@ mod tests {
1518
1522
) ;
1519
1523
1520
1524
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 ) ;
1522
1527
1523
1528
assert ! ( tx_builder. add_output( & TransactionOutput :: new( & output_addr, & output_amount) ) . is_err( ) ) ;
1524
1529
}
@@ -1527,13 +1532,13 @@ mod tests {
1527
1532
fn build_tx_add_change_nfts_not_enough_ada ( ) {
1528
1533
let linear_fee = LinearFee :: new ( & to_bignum ( 0 ) , & to_bignum ( 1 ) ) ;
1529
1534
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
1531
1536
let mut tx_builder = TransactionBuilder :: new (
1532
1537
& linear_fee,
1533
1538
& minimum_utxo_value,
1534
1539
& to_bignum ( 0 ) ,
1535
1540
& to_bignum ( 0 ) ,
1536
- max_output_size ,
1541
+ max_value_size ,
1537
1542
MAX_TX_SIZE
1538
1543
) ;
1539
1544
0 commit comments