fix(tx_graph)!: Change tx_last_seen to Option<u64>

Also fixup `test_list_owned_txouts` to check that the right
outputs, utxos, and balance are returned at different local
chain heights.

This fixes an issue where unbroadcast and otherwise non-canonical
transactions were returned from methods `list_chain_txs` and
`Wallet::transactions` because every tx inserted had a last_seen
of 0 making it appear unconfirmed.

Note this commit changes the way `Balance` is represented due to
new logic in `try_get_chain_position` that no longer considers
txs with non-canonical anchors. Before this change, a tx anchored
to a block that is reorged out had a permanent effect on the
pending balance, and now only txs with a last_seen time or an
anchor confirmed in the best chain will return a `ChainPosition`.
This commit is contained in:
valued mammal
2024-05-23 17:33:45 -04:00
parent 324eeb3eb4
commit bbc19c3536
7 changed files with 100 additions and 71 deletions

View File

@@ -1187,7 +1187,16 @@ fn test_create_tx_add_utxo() {
version: transaction::Version::non_standard(0),
lock_time: absolute::LockTime::ZERO,
};
wallet.insert_tx(small_output_tx.clone());
let txid = small_output_tx.compute_txid();
wallet.insert_tx(small_output_tx);
insert_anchor_from_conf(
&mut wallet,
txid,
ConfirmationTime::Confirmed {
height: 2000,
time: 200,
},
);
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
@@ -1195,10 +1204,7 @@ fn test_create_tx_add_utxo() {
let mut builder = wallet.build_tx();
builder
.add_recipient(addr.script_pubkey(), Amount::from_sat(30_000))
.add_utxo(OutPoint {
txid: small_output_tx.compute_txid(),
vout: 0,
})
.add_utxo(OutPoint { txid, vout: 0 })
.unwrap();
let psbt = builder.finish().unwrap();
let sent_received =
@@ -1231,8 +1237,16 @@ fn test_create_tx_manually_selected_insufficient() {
version: transaction::Version::non_standard(0),
lock_time: absolute::LockTime::ZERO,
};
let txid = small_output_tx.compute_txid();
wallet.insert_tx(small_output_tx.clone());
insert_anchor_from_conf(
&mut wallet,
txid,
ConfirmationTime::Confirmed {
height: 2000,
time: 200,
},
);
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX")
.unwrap()
@@ -1240,10 +1254,7 @@ fn test_create_tx_manually_selected_insufficient() {
let mut builder = wallet.build_tx();
builder
.add_recipient(addr.script_pubkey(), Amount::from_sat(30_000))
.add_utxo(OutPoint {
txid: small_output_tx.compute_txid(),
vout: 0,
})
.add_utxo(OutPoint { txid, vout: 0 })
.unwrap()
.manually_selected_only();
builder.finish().unwrap();
@@ -1278,7 +1289,9 @@ fn test_create_tx_policy_path_no_csv() {
value: Amount::from_sat(50_000),
}],
};
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let external_policy = wallet.policies(KeychainKind::External).unwrap().unwrap();
let root_id = external_policy.id;
@@ -1670,6 +1683,7 @@ fn test_bump_fee_irreplaceable_tx() {
let tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
wallet.build_fee_bump(txid).unwrap().finish().unwrap();
}
@@ -1713,6 +1727,7 @@ fn test_bump_fee_low_fee_rate() {
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::BROADCAST_MIN);
@@ -1744,6 +1759,7 @@ fn test_bump_fee_low_abs() {
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::from_sat(10));
@@ -1764,6 +1780,7 @@ fn test_bump_fee_zero_abs() {
let tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::ZERO);
@@ -1788,6 +1805,7 @@ fn test_bump_fee_reduce_change() {
let tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap();
@@ -1884,6 +1902,7 @@ fn test_bump_fee_reduce_single_recipient() {
let original_fee = check_fee!(wallet, psbt);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let feerate = FeeRate::from_sat_per_kwu(625); // 2.5 sat/vb
let mut builder = wallet.build_fee_bump(txid).unwrap();
@@ -1930,6 +1949,7 @@ fn test_bump_fee_absolute_reduce_single_recipient() {
let original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder
@@ -2004,6 +2024,7 @@ fn test_bump_fee_drain_wallet() {
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
assert_eq!(original_sent_received.0, Amount::from_sat(25_000));
// for the new feerate, it should be enough to reduce the output, but since we specify
@@ -2068,6 +2089,7 @@ fn test_bump_fee_remove_output_manually_selected_only() {
let original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
assert_eq!(original_sent_received.0, Amount::from_sat(25_000));
let mut builder = wallet.build_fee_bump(txid).unwrap();
@@ -2114,6 +2136,7 @@ fn test_bump_fee_add_input() {
let original_details = wallet.sent_and_received(&tx);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(50));
@@ -2169,6 +2192,7 @@ fn test_bump_fee_absolute_add_input() {
let original_sent_received = wallet.sent_and_received(&tx);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_absolute(Amount::from_sat(6_000));
@@ -2233,6 +2257,7 @@ fn test_bump_fee_no_change_add_input_and_change() {
let tx = psbt.extract_tx().expect("failed to extract tx");
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
// Now bump the fees, the wallet should add an extra input and a change output, and leave
// the original output untouched.
@@ -2301,6 +2326,7 @@ fn test_bump_fee_add_input_change_dust() {
assert_eq!(tx.output.len(), 2);
let txid = tx.compute_txid();
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
// We set a fee high enough that during rbf we are forced to add
@@ -2370,6 +2396,7 @@ fn test_bump_fee_force_add_input() {
txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
}
wallet.insert_tx(tx.clone());
wallet.insert_seen_at(txid, 0);
// the new fee_rate is low enough that just reducing the change would be fine, but we force
// the addition of an extra input with `add_utxo()`
let mut builder = wallet.build_fee_bump(txid).unwrap();
@@ -2435,6 +2462,7 @@ fn test_bump_fee_absolute_force_add_input() {
txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
}
wallet.insert_tx(tx.clone());
wallet.insert_seen_at(txid, 0);
// the new fee_rate is low enough that just reducing the change would be fine, but we force
// the addition of an extra input with `add_utxo()`
@@ -2512,6 +2540,7 @@ fn test_bump_fee_unconfirmed_inputs_only() {
txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
}
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder.fee_rate(FeeRate::from_sat_per_vb_unchecked(25));
builder.finish().unwrap();
@@ -2543,6 +2572,7 @@ fn test_bump_fee_unconfirmed_input() {
txin.witness.push([0x00; P2WPKH_FAKE_WITNESS_SIZE]); // fake signature
}
wallet.insert_tx(tx);
wallet.insert_seen_at(txid, 0);
let mut builder = wallet.build_fee_bump(txid).unwrap();
builder