Skip to content

Commit

Permalink
test(wallet): Add test_create_tx_increment_change_index
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Sep 5, 2024
1 parent 4fea09a commit 004b0ca
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,60 @@ fn test_create_tx_global_xpubs_with_origin() {
assert_eq!(psbt.xpub.get(&key), Some(&(fingerprint, path)));
}

#[test]
fn test_create_tx_increment_change_index() {
use coin_selection::Error;
let (desc, change_desc) = get_test_tr_single_sig_xprv_with_change_desc();
let (mut wallet, _) = get_funded_wallet_with_change(desc, change_desc);
let addr = wallet.next_unused_address(KeychainKind::External);
let internal_addr0 = wallet.next_unused_address(KeychainKind::Internal);
assert_eq!(internal_addr0.index, 0);

// try to send an amount that results in `InsufficientFunds` error
let amount = wallet.balance().total();
let mut builder = wallet.build_tx();
builder.add_recipient(addr.script_pubkey(), amount);
let err = builder.finish().unwrap_err();
assert!(matches!(
err,
CreateTxError::CoinSelection(Error::InsufficientFunds { .. }),
));
assert_eq!(wallet.derivation_index(KeychainKind::Internal), Some(0));
assert_eq!(
wallet.next_unused_address(KeychainKind::Internal),
internal_addr0,
"create tx fail should not mark change address used",
);

// send all (no change) should not increment index
let fee = Amount::from_sat(150);
let mut builder = wallet.build_tx();
builder.add_recipient(addr.script_pubkey(), amount - fee);
let psbt = builder.finish().unwrap();
assert_eq!(psbt.unsigned_tx.output.len(), 1);
assert_eq!(wallet.derivation_index(KeychainKind::Internal), Some(0));
assert_eq!(
wallet.next_unused_address(KeychainKind::Internal),
internal_addr0,
"no change output should not mark change address used",
);

// create tx with change should increment index
let mut builder = wallet.build_tx();
builder.add_recipient(addr.script_pubkey(), amount / 2);
let psbt = builder.finish().unwrap();
assert_eq!(psbt.unsigned_tx.output.len(), 2);
assert!(
!wallet.mark_used(KeychainKind::Internal, 0),
"creating tx should mark change address used",
);
assert!(wallet
.list_unused_addresses(KeychainKind::Internal)
.next()
.is_none());
assert_eq!(wallet.reveal_next_address(KeychainKind::Internal).index, 1);
}

#[test]
fn test_add_foreign_utxo() {
let (mut wallet1, _) = get_funded_wallet_wpkh();
Expand Down

0 comments on commit 004b0ca

Please sign in to comment.