@@ -226,30 +226,29 @@ mod tests {
226226 #[ test]
227227 fn test_tx_change_edge ( ) {
228228 // Check that we never add dust outputs
229- let mut tx = Transaction { version : 2 , lock_time : LockTime :: ZERO , input : Vec :: new ( ) , output : Vec :: new ( ) } ;
229+ let mut tx = Transaction { version : Version :: TWO , lock_time : LockTime :: ZERO , input : Vec :: new ( ) , output : Vec :: new ( ) } ;
230230 let orig_wtxid = tx. wtxid ( ) ;
231231 let output_spk = ScriptBuf :: new_p2pkh ( & PubkeyHash :: hash ( & [ 0 ; 0 ] ) ) ;
232232 assert_eq ! ( output_spk. dust_value( ) . to_sat( ) , 546 ) ;
233- // 9 sats isn't enough to pay fee on a dummy transaction...
234- assert_eq ! ( tx . weight ( ) . to_wu ( ) , 40 ) ; // ie 10 vbytes
235- assert ! ( maybe_add_change_output( & mut tx, 9 , 0 , 250 , output_spk. clone( ) ) . is_err( ) ) ;
233+ assert_eq ! ( tx . weight ( ) . to_wu ( ) , 42 ) ; // 3 * (base size = 4 + 1 + 1 + 4 = 10) + (total size = 4 + 2 + 1 + 1 + 4 = 12)
234+ // 10 sats isn't enough to pay fee on a dummy transaction...
235+ assert ! ( maybe_add_change_output( & mut tx, Amount :: from_sat ( 10 ) , 0 , 250 , output_spk. clone( ) ) . is_err( ) ) ;
236236 assert_eq ! ( tx. wtxid( ) , orig_wtxid) ; // Failure doesn't change the transaction
237- // but 10-564 is, just not enough to add a change output...
238- assert ! ( maybe_add_change_output( & mut tx, 10 , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
237+ // but 11 (= ceil(42 * 250 / 1000)) is, just not enough to add a change output...
238+ assert ! ( maybe_add_change_output( & mut tx, Amount :: from_sat ( 11 ) , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
239239 assert_eq ! ( tx. output. len( ) , 0 ) ;
240240 assert_eq ! ( tx. wtxid( ) , orig_wtxid) ; // If we don't add an output, we don't change the transaction
241- assert ! ( maybe_add_change_output( & mut tx, 549 , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
241+ assert ! ( maybe_add_change_output( & mut tx, Amount :: from_sat ( 549 ) , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
242242 assert_eq ! ( tx. output. len( ) , 0 ) ;
243243 assert_eq ! ( tx. wtxid( ) , orig_wtxid) ; // If we don't add an output, we don't change the transaction
244- // 590 is also not enough, if we anticipate 2 more weight units pushing us up to the next vbyte
245- // (considering the two bytes for segwit flags)
246- assert ! ( maybe_add_change_output( & mut tx, 590 , 2 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
244+ // 590 is also not enough
245+ assert ! ( maybe_add_change_output( & mut tx, Amount :: from_sat( 590 ) , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
247246 assert_eq ! ( tx. output. len( ) , 0 ) ;
248247 assert_eq ! ( tx. wtxid( ) , orig_wtxid) ; // If we don't add an output, we don't change the transaction
249- // at 590 we can afford the change output at the dust limit (546)
250- assert ! ( maybe_add_change_output( & mut tx, 590 , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
248+ // at 591 we can afford the change output at the dust limit (546)
249+ assert ! ( maybe_add_change_output( & mut tx, Amount :: from_sat ( 591 ) , 0 , 250 , output_spk. clone( ) ) . is_ok( ) ) ;
251250 assert_eq ! ( tx. output. len( ) , 1 ) ;
252- assert_eq ! ( tx. output[ 0 ] . value, 546 ) ;
251+ assert_eq ! ( tx. output[ 0 ] . value. to_sat ( ) , 546 ) ;
253252 assert_eq ! ( tx. output[ 0 ] . script_pubkey, output_spk) ;
254253 assert_eq ! ( tx. weight( ) . to_wu( ) / 4 , 590 -546 ) ; // New weight is exactly the fee we wanted.
255254
0 commit comments