+
+ #[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: 0, 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: 0, input: Vec::new(), output: Vec::new() };
+ let orig_wtxid = tx.wtxid();
+ // 9 sats isn't enough to pay fee on a dummy transaction...
+ assert_eq!(tx.get_weight() as u64, 40); // ie 10 vbytes
+ assert!(maybe_add_change_output(&mut tx, 9, 0, 253, Script::new()).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, 253, Script::new()).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, 564, 0, 253, Script::new()).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
+ // 565 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, 565, 2, 253, Script::new()).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 565 we can afford the change output at the dust limit (546)
+ assert!(maybe_add_change_output(&mut tx, 565, 0, 253, Script::new()).is_ok());
+ assert_eq!(tx.output.len(), 1);
+ assert_eq!(tx.output[0].value, 546);
+ assert_eq!(tx.output[0].script_pubkey, Script::new());
+ assert_eq!(tx.get_weight() / 4, 565-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: 0, input: vec![TxIn {
+ previous_output: OutPoint::new(Txid::from_hash(Sha256dHash::default()), 0), script_sig: Script::new(), witness: Vec::new(), sequence: 0,
+ }], output: vec![TxOut {
+ script_pubkey: Builder::new().push_int(1).into_script(), value: 1000
+ }] };
+ let orig_wtxid = tx.wtxid();
+ let orig_weight = tx.get_weight();
+ assert_eq!(orig_weight / 4, 61);
+
+ // 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 546 plus the output's weight / 4 (10)...
+ assert!(maybe_add_change_output(&mut tx, 1000 + 61 + 100 + 546 + 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 + 546 + 10, 400, 250, Builder::new().push_int(2).into_script()).is_ok());
+ assert_eq!(tx.output.len(), 2);
+ assert_eq!(tx.output[1].value, 546);
+ assert_eq!(tx.output[1].script_pubkey, Builder::new().push_int(2).into_script());
+ assert_eq!(tx.get_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.
+ }