-
Notifications
You must be signed in to change notification settings - Fork 389
test,refactor(wallet): Move bump fee + foreign utxo tests #1907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
ValuedMammal
wants to merge
2
commits into
bitcoindevkit:master
from
ValuedMammal:refactor/wallet-tests
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,292 @@ | ||
use bdk_wallet::psbt::PsbtUtils; | ||
use bdk_wallet::signer::SignOptions; | ||
use bdk_wallet::test_utils::*; | ||
use bdk_wallet::tx_builder::AddForeignUtxoError; | ||
use bdk_wallet::KeychainKind; | ||
use bitcoin::psbt; | ||
use bitcoin::{Address, Amount}; | ||
use std::str::FromStr; | ||
|
||
mod common; | ||
|
||
#[test] | ||
fn test_add_foreign_utxo() { | ||
let (mut wallet1, _) = get_funded_wallet_wpkh(); | ||
let (wallet2, _) = | ||
get_funded_wallet_single("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)"); | ||
|
||
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") | ||
.unwrap() | ||
.assume_checked(); | ||
let utxo = wallet2.list_unspent().next().expect("must take!"); | ||
let foreign_utxo_satisfaction = wallet2 | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
let psbt_input = psbt::Input { | ||
witness_utxo: Some(utxo.txout.clone()), | ||
..Default::default() | ||
}; | ||
|
||
let mut builder = wallet1.build_tx(); | ||
builder | ||
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) | ||
.only_witness_utxo() | ||
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) | ||
.unwrap(); | ||
let mut psbt = builder.finish().unwrap(); | ||
wallet1.insert_txout(utxo.outpoint, utxo.txout); | ||
let fee = check_fee!(wallet1, psbt); | ||
let sent_received = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it will be easier to read if this tuple is named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||
wallet1.sent_and_received(&psbt.clone().extract_tx().expect("failed to extract tx")); | ||
|
||
assert_eq!( | ||
(sent_received.0 - sent_received.1), | ||
Amount::from_sat(10_000) + fee.unwrap_or(Amount::ZERO), | ||
"we should have only net spent ~10_000" | ||
); | ||
|
||
assert!( | ||
psbt.unsigned_tx | ||
.input | ||
.iter() | ||
.any(|input| input.previous_output == utxo.outpoint), | ||
"foreign_utxo should be in there" | ||
); | ||
|
||
ValuedMammal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let finished = wallet1 | ||
.sign( | ||
&mut psbt, | ||
SignOptions { | ||
trust_witness_utxo: true, | ||
..Default::default() | ||
}, | ||
) | ||
.unwrap(); | ||
|
||
assert!( | ||
!finished, | ||
"only one of the inputs should have been signed so far" | ||
); | ||
|
||
let finished = wallet2 | ||
.sign( | ||
&mut psbt, | ||
SignOptions { | ||
trust_witness_utxo: true, | ||
..Default::default() | ||
}, | ||
) | ||
.unwrap(); | ||
assert!(finished, "all the inputs should have been signed now"); | ||
} | ||
|
||
#[test] | ||
fn test_calculate_fee_with_missing_foreign_utxo() { | ||
use bdk_chain::tx_graph::CalculateFeeError; | ||
let (mut wallet1, _) = get_funded_wallet_wpkh(); | ||
let (wallet2, _) = | ||
get_funded_wallet_single("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)"); | ||
|
||
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") | ||
.unwrap() | ||
.assume_checked(); | ||
let utxo = wallet2.list_unspent().next().expect("must take!"); | ||
let foreign_utxo_satisfaction = wallet2 | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
let psbt_input = psbt::Input { | ||
witness_utxo: Some(utxo.txout.clone()), | ||
..Default::default() | ||
}; | ||
|
||
let mut builder = wallet1.build_tx(); | ||
builder | ||
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) | ||
.only_witness_utxo() | ||
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) | ||
.unwrap(); | ||
let psbt = builder.finish().unwrap(); | ||
let tx = psbt.extract_tx().expect("failed to extract tx"); | ||
let res = wallet1.calculate_fee(&tx); | ||
assert!( | ||
matches!(res, Err(CalculateFeeError::MissingTxOut(outpoints)) if outpoints[0] == utxo.outpoint) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_add_foreign_utxo_invalid_psbt_input() { | ||
let (mut wallet, _) = get_funded_wallet_wpkh(); | ||
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint; | ||
let foreign_utxo_satisfaction = wallet | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
let mut builder = wallet.build_tx(); | ||
let result = | ||
builder.add_foreign_utxo(outpoint, psbt::Input::default(), foreign_utxo_satisfaction); | ||
assert!(matches!(result, Err(AddForeignUtxoError::MissingUtxo))); | ||
} | ||
|
||
#[test] | ||
fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() { | ||
let (mut wallet1, txid1) = get_funded_wallet_wpkh(); | ||
let (wallet2, txid2) = | ||
get_funded_wallet_single("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)"); | ||
|
||
let utxo2 = wallet2.list_unspent().next().unwrap(); | ||
let tx1 = wallet1.get_tx(txid1).unwrap().tx_node.tx.clone(); | ||
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone(); | ||
|
||
let satisfaction_weight = wallet2 | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
let mut builder = wallet1.build_tx(); | ||
assert!( | ||
builder | ||
.add_foreign_utxo( | ||
utxo2.outpoint, | ||
psbt::Input { | ||
non_witness_utxo: Some(tx1.as_ref().clone()), | ||
..Default::default() | ||
}, | ||
satisfaction_weight | ||
) | ||
.is_err(), | ||
"should fail when outpoint doesn't match psbt_input" | ||
); | ||
assert!( | ||
builder | ||
.add_foreign_utxo( | ||
utxo2.outpoint, | ||
psbt::Input { | ||
non_witness_utxo: Some(tx2.as_ref().clone()), | ||
..Default::default() | ||
}, | ||
satisfaction_weight | ||
) | ||
.is_ok(), | ||
"should be ok when outpoint does match psbt_input" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_add_foreign_utxo_only_witness_utxo() { | ||
let (mut wallet1, _) = get_funded_wallet_wpkh(); | ||
let (wallet2, txid2) = | ||
get_funded_wallet_single("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)"); | ||
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") | ||
.unwrap() | ||
.assume_checked(); | ||
let utxo2 = wallet2.list_unspent().next().unwrap(); | ||
|
||
let satisfaction_weight = wallet2 | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
{ | ||
let mut builder = wallet1.build_tx(); | ||
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)); | ||
|
||
let psbt_input = psbt::Input { | ||
witness_utxo: Some(utxo2.txout.clone()), | ||
..Default::default() | ||
}; | ||
builder | ||
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight) | ||
.unwrap(); | ||
assert!( | ||
builder.finish().is_err(), | ||
"psbt_input with witness_utxo should fail with only witness_utxo" | ||
); | ||
} | ||
|
||
{ | ||
let mut builder = wallet1.build_tx(); | ||
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)); | ||
|
||
let psbt_input = psbt::Input { | ||
witness_utxo: Some(utxo2.txout.clone()), | ||
..Default::default() | ||
}; | ||
builder | ||
.only_witness_utxo() | ||
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight) | ||
.unwrap(); | ||
assert!( | ||
builder.finish().is_ok(), | ||
"psbt_input with just witness_utxo should succeed when `only_witness_utxo` is enabled" | ||
); | ||
} | ||
|
||
{ | ||
let mut builder = wallet1.build_tx(); | ||
builder.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)); | ||
|
||
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx; | ||
let psbt_input = psbt::Input { | ||
non_witness_utxo: Some(tx2.as_ref().clone()), | ||
..Default::default() | ||
}; | ||
builder | ||
.add_foreign_utxo(utxo2.outpoint, psbt_input, satisfaction_weight) | ||
.unwrap(); | ||
assert!( | ||
builder.finish().is_ok(), | ||
"psbt_input with non_witness_utxo should succeed by default" | ||
); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_taproot_foreign_utxo() { | ||
let (mut wallet1, _) = get_funded_wallet_wpkh(); | ||
let (wallet2, _) = get_funded_wallet_single(get_test_tr_single_sig()); | ||
|
||
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX") | ||
.unwrap() | ||
.assume_checked(); | ||
let utxo = wallet2.list_unspent().next().unwrap(); | ||
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap(); | ||
let foreign_utxo_satisfaction = wallet2 | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
assert!( | ||
psbt_input.non_witness_utxo.is_none(), | ||
"`non_witness_utxo` should never be populated for taproot" | ||
); | ||
|
||
let mut builder = wallet1.build_tx(); | ||
builder | ||
.add_recipient(addr.script_pubkey(), Amount::from_sat(60_000)) | ||
.add_foreign_utxo(utxo.outpoint, psbt_input, foreign_utxo_satisfaction) | ||
.unwrap(); | ||
let psbt = builder.finish().unwrap(); | ||
let sent_received = | ||
wallet1.sent_and_received(&psbt.clone().extract_tx().expect("failed to extract tx")); | ||
wallet1.insert_txout(utxo.outpoint, utxo.txout); | ||
let fee = check_fee!(wallet1, psbt); | ||
|
||
assert_eq!( | ||
sent_received.0 - sent_received.1, | ||
Amount::from_sat(10_000) + fee.unwrap_or(Amount::ZERO), | ||
"we should have only net spent ~10_000" | ||
); | ||
|
||
assert!( | ||
psbt.unsigned_tx | ||
.input | ||
.iter() | ||
.any(|input| input.previous_output == utxo.outpoint), | ||
"foreign_utxo should be in there" | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious about how the
change output
was handled. I can't see a test case or the method responsible for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no change address is given, then the wallet looks for the next unused address from the change keychain
bdk/crates/wallet/src/wallet/mod.rs
Lines 1434 to 1438 in 5817ed0
And the result of coin selection determines whether the change output is finally added.
bdk/crates/wallet/src/wallet/mod.rs
Lines 1511 to 1517 in 5817ed0