@@ -219,10 +219,8 @@ mod tests {
219
219
// If we have a bogus input amount or outputs valued more than inputs, we should fail
220
220
let version = Version :: TWO ;
221
221
let lock_time = LockTime :: ZERO ;
222
- let input = Vec :: new ( ) ;
223
222
let tx_out = TxOut { script_pubkey : ScriptBuf :: new ( ) , value : Amount :: from_sat ( 1000 ) } ;
224
- let output = vec ! [ tx_out] ;
225
- let mut tx = Transaction { version, lock_time, input, output } ;
223
+ let mut tx = Transaction { version, lock_time, input : Vec :: new ( ) , output : vec ! [ tx_out] } ;
226
224
let amount = Amount :: from_sat ( 21_000_000_0000_0001 ) ;
227
225
assert ! ( maybe_add_change_output( & mut tx, amount, 0 , 253 , ScriptBuf :: new( ) ) . is_err( ) ) ;
228
226
let amount = Amount :: from_sat ( 400 ) ;
@@ -294,36 +292,32 @@ mod tests {
294
292
let tx_in = TxIn { previous_output, script_sig, witness, sequence } ;
295
293
let version = Version :: TWO ;
296
294
let lock_time = LockTime :: ZERO ;
297
- let input = vec ! [ tx_in] ;
298
- let output = vec ! [ tx_out] ;
299
- let mut tx = Transaction { version, lock_time, input, output } ;
295
+ let mut tx = Transaction { version, lock_time, input : vec ! [ tx_in] , output : vec ! [ tx_out] } ;
300
296
let orig_wtxid = tx. compute_wtxid ( ) ;
301
297
let orig_weight = tx. weight ( ) . to_wu ( ) ;
302
298
assert_eq ! ( orig_weight / 4 , 61 ) ;
303
299
304
300
assert_eq ! ( Builder :: new( ) . push_int( 2 ) . into_script( ) . minimal_non_dust( ) . to_sat( ) , 474 ) ;
305
301
302
+ let script = Builder :: new ( ) . push_int ( 2 ) . into_script ( ) ;
303
+
306
304
// Input value of the output value + fee - 1 should fail:
307
305
let amount = Amount :: from_sat ( 1000 + 61 + 100 - 1 ) ;
308
- let script = Builder :: new ( ) . push_int ( 2 ) . into_script ( ) ;
309
- assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script) . is_err( ) ) ;
306
+ assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script. clone( ) ) . is_err( ) ) ;
310
307
// Failure doesn't change the transaction
311
308
assert_eq ! ( tx. compute_wtxid( ) , orig_wtxid) ;
312
309
// but one more input sat should succeed, without changing the transaction
313
310
let amount = Amount :: from_sat ( 1000 + 61 + 100 ) ;
314
- let script = Builder :: new ( ) . push_int ( 2 ) . into_script ( ) ;
315
- assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script) . is_ok( ) ) ;
311
+ assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script. clone( ) ) . is_ok( ) ) ;
316
312
// If we don't add an output, we don't change the transaction
317
313
assert_eq ! ( tx. compute_wtxid( ) , orig_wtxid) ;
318
314
// In order to get a change output, we need to add 474 plus the output's weight / 4 (10)...
319
315
let amount = Amount :: from_sat ( 1000 + 61 + 100 + 474 + 9 ) ;
320
- let script = Builder :: new ( ) . push_int ( 2 ) . into_script ( ) ;
321
- assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script) . is_ok( ) ) ;
316
+ assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script. clone( ) ) . is_ok( ) ) ;
322
317
// If we don't add an output, we don't change the transaction
323
318
assert_eq ! ( tx. compute_wtxid( ) , orig_wtxid) ;
324
319
325
320
let amount = Amount :: from_sat ( 1000 + 61 + 100 + 474 + 10 ) ;
326
- let script = Builder :: new ( ) . push_int ( 2 ) . into_script ( ) ;
327
321
assert ! ( maybe_add_change_output( & mut tx, amount, 400 , 250 , script) . is_ok( ) ) ;
328
322
assert_eq ! ( tx. output. len( ) , 2 ) ;
329
323
assert_eq ! ( tx. output[ 1 ] . value. to_sat( ) , 474 ) ;
0 commit comments