Skip to content

Commit

Permalink
test(wallet): Add test change index for create tx fail
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal committed Aug 27, 2024
1 parent dae62e1 commit 699a6bb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/wallet/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4203,3 +4203,33 @@ fn single_descriptor_wallet_can_create_tx_and_receive_change() {
"tx change should go to external keychain"
);
}

#[test]
fn create_tx_fail_should_not_increment_change_index() {
// check we only mark a change address used if tx creation
// succeeds. in other words failure to create a tx for example due
// to insufficient funds should not affect the unused status of
// a change address.
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 change_index = wallet.next_unused_address(KeychainKind::Internal).index;
assert_eq!(change_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.next_unused_address(KeychainKind::Internal).index,
change_index,
"failed tx should not increase change address index"
);
}

0 comments on commit 699a6bb

Please sign in to comment.