+
+ #[test]
+ fn test_tx_value_overrun() {
+ // If we have a bogus input amount or outputs valued more than inputs, we should fail
+ let mut tx = Transaction { version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: vec![TxOut {
+ script_pubkey: Script::new(), value: 1000
+ }] };
+ assert!(maybe_add_change_output(&mut tx, 21_000_000_0000_0001, 0, 253, Script::new()).is_err());
+ assert!(maybe_add_change_output(&mut tx, 400, 0, 253, Script::new()).is_err());
+ assert!(maybe_add_change_output(&mut tx, 4000, 0, 253, Script::new()).is_ok());
+ }
+
+ #[test]
+ fn test_tx_change_edge() {
+ // Check that we never add dust outputs
+ let mut tx = Transaction { version: 2, lock_time: PackedLockTime::ZERO, input: Vec::new(), output: Vec::new() };
+ let orig_wtxid = tx.wtxid();
+ let output_spk = Script::new_p2pkh(&PubkeyHash::hash(&[0; 0]));
+ assert_eq!(output_spk.dust_value().to_sat(), 546);
+ // 9 sats isn't enough to pay fee on a dummy transaction...
+ assert_eq!(tx.weight() as u64, 40); // ie 10 vbytes
+ assert!(maybe_add_change_output(&mut tx, 9, 0, 250, output_spk.clone()).is_err());
+ assert_eq!(tx.wtxid(), orig_wtxid); // Failure doesn't change the transaction
+ // but 10-564 is, just not enough to add a change output...
+ assert!(maybe_add_change_output(&mut tx, 10, 0, 250, output_spk.clone()).is_ok());
+ assert_eq!(tx.output.len(), 0);
+ assert_eq!(tx.wtxid(), orig_wtxid); // If we don't add an output, we don't change the transaction
+ assert!(maybe_add_change_output(&mut tx, 549, 0, 250, output_spk.clone()).is_ok());
+ assert_eq!(tx.output.len(), 0);
+ assert_eq!(tx.wtxid(), orig_wtxid); // If we don't add an output, we don't change the transaction
+ // 590 is also not enough, if we anticipate 2 more weight units pushing us up to the next vbyte
+ // (considering the two bytes for segwit flags)
+ assert!(maybe_add_change_output(&mut tx, 590, 2, 250, output_spk.clone()).is_ok());
+ assert_eq!(tx.output.len(), 0);
+ assert_eq!(tx.wtxid(), orig_wtxid); // If we don't add an output, we don't change the transaction
+ // at 590 we can afford the change output at the dust limit (546)
+ assert!(maybe_add_change_output(&mut tx, 590, 0, 250, output_spk.clone()).is_ok());
+ assert_eq!(tx.output.len(), 1);
+ assert_eq!(tx.output[0].value, 546);
+ assert_eq!(tx.output[0].script_pubkey, output_spk);
+ assert_eq!(tx.weight() / 4, 590-546); // New weight is exactly the fee we wanted.
+
+ tx.output.pop();
+ assert_eq!(tx.wtxid(), orig_wtxid); // The only change is the addition of one output.
+ }
+
+ #[test]
+ fn test_tx_extra_outputs() {
+ // Check that we correctly handle existing outputs
+ let mut tx = Transaction { version: 2, lock_time: PackedLockTime::ZERO, input: vec![TxIn {
+ previous_output: OutPoint::new(Txid::all_zeros(), 0), script_sig: Script::new(), witness: Witness::new(), sequence: Sequence::ZERO,
+ }], output: vec![TxOut {
+ script_pubkey: Builder::new().push_int(1).into_script(), value: 1000
+ }] };
+ let orig_wtxid = tx.wtxid();
+ let orig_weight = tx.weight();
+ assert_eq!(orig_weight / 4, 61);
+
+ assert_eq!(Builder::new().push_int(2).into_script().dust_value().to_sat(), 474);
+
+ // Input value of the output value + fee - 1 should fail:
+ assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100 - 1, 400, 250, Builder::new().push_int(2).into_script()).is_err());
+ assert_eq!(tx.wtxid(), orig_wtxid); // Failure doesn't change the transaction
+ // but one more input sat should succeed, without changing the transaction
+ assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100, 400, 250, Builder::new().push_int(2).into_script()).is_ok());
+ assert_eq!(tx.wtxid(), orig_wtxid); // If we don't add an output, we don't change the transaction
+ // In order to get a change output, we need to add 474 plus the output's weight / 4 (10)...
+ assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100 + 474 + 9, 400, 250, Builder::new().push_int(2).into_script()).is_ok());
+ assert_eq!(tx.wtxid(), orig_wtxid); // If we don't add an output, we don't change the transaction
+
+ assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100 + 474 + 10, 400, 250, Builder::new().push_int(2).into_script()).is_ok());
+ assert_eq!(tx.output.len(), 2);
+ assert_eq!(tx.output[1].value, 474);
+ assert_eq!(tx.output[1].script_pubkey, Builder::new().push_int(2).into_script());
+ assert_eq!(tx.weight() - orig_weight, 40); // Weight difference matches what we had to add above
+ tx.output.pop();
+ assert_eq!(tx.wtxid(), orig_wtxid); // The only change is the addition of one output.
+ }