2024-06-28 09:07:36 -04:00
|
|
|
use bdk_chain::{Indexer, SpkTxOutIndex};
|
2024-04-26 11:17:23 -03:00
|
|
|
use bitcoin::{
|
|
|
|
absolute, transaction, Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxIn, TxOut,
|
|
|
|
};
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn spk_txout_sent_and_received() {
|
2023-06-21 17:59:34 +02:00
|
|
|
let spk1 = ScriptBuf::from_hex("001404f1e52ce2bab3423c6a8c63b7cd730d8f12542c").unwrap();
|
|
|
|
let spk2 = ScriptBuf::from_hex("00142b57404ae14f08c3a0c903feb2af7830605eb00f").unwrap();
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
let mut index = SpkTxOutIndex::default();
|
|
|
|
index.insert_spk(0, spk1.clone());
|
|
|
|
index.insert_spk(1, spk2.clone());
|
|
|
|
|
|
|
|
let tx1 = Transaction {
|
2023-10-16 19:51:53 +11:00
|
|
|
version: transaction::Version::TWO,
|
2023-06-21 17:59:34 +02:00
|
|
|
lock_time: absolute::LockTime::ZERO,
|
2023-03-01 11:09:08 +01:00
|
|
|
input: vec![],
|
|
|
|
output: vec![TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(42_000),
|
2023-03-01 11:09:08 +01:00
|
|
|
script_pubkey: spk1.clone(),
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
2024-04-26 11:17:23 -03:00
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx1, ..),
|
|
|
|
(Amount::from_sat(0), Amount::from_sat(42_000))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx1, ..1),
|
|
|
|
(Amount::from_sat(0), Amount::from_sat(42_000))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx1, 1..),
|
|
|
|
(Amount::from_sat(0), Amount::from_sat(0))
|
|
|
|
);
|
|
|
|
assert_eq!(index.net_value(&tx1, ..), SignedAmount::from_sat(42_000));
|
2023-08-25 12:52:09 +03:00
|
|
|
index.index_tx(&tx1);
|
2023-03-01 11:09:08 +01:00
|
|
|
assert_eq!(
|
2024-02-06 17:31:22 +11:00
|
|
|
index.sent_and_received(&tx1, ..),
|
2024-04-26 11:17:23 -03:00
|
|
|
(Amount::from_sat(0), Amount::from_sat(42_000)),
|
2023-03-01 11:09:08 +01:00
|
|
|
"shouldn't change after scanning"
|
|
|
|
);
|
|
|
|
|
|
|
|
let tx2 = Transaction {
|
2023-10-16 19:51:53 +11:00
|
|
|
version: transaction::Version::ONE,
|
2023-06-21 17:59:34 +02:00
|
|
|
lock_time: absolute::LockTime::ZERO,
|
2023-03-01 11:09:08 +01:00
|
|
|
input: vec![TxIn {
|
|
|
|
previous_output: OutPoint {
|
deps(bdk): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
deps(chain): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(chain): use `minimal_non_dust()` instead of `dust_value()`
fix(chain): use `compute_txid()` instead of `txid`
deps(testenv): bump `electrsd` to `0.28.0`
deps(electrum): bump `electrum-client` to `0.20.0`
fix(electrum): use `compute_txid()` instead of `txid`
deps(esplora): bump `esplora-client` to `0.8.0`
deps(bitcoind_rpc): bump `bitcoin` to `0.32.0`, `bitcoincore-rpc` to
`0.19.0`
fix(bitcoind_rpc): use `compute_txid()` instead of `txid`
fix(nursery/tmp_plan): use proper `sighash` errors, and fix the expected
`Signature` fields
fix(sqlite): use `compute_txid()` instead of `txid`
deps(hwi): bump `hwi` to `0.9.0`
deps(wallet): bump `bitcoin` to `0.32.0`, miniscript to `12.0.0`
fix(wallet): use `compute_txid()` and `minimal_non_dust()`
- update to use `compute_txid()` instead of deprecated `txid()`
- update to use `minimal_non_dust()` instead of `dust_value()`
- remove unused `bitcoin::hex::FromHex`.
fix(wallet): uses `.into` conversion on `Network` for `NetworkKind`
- uses `.into()` when appropriate, otherwise use the explicit
`NetworkKind`, and it's `.is_mainnet()` method.
fix(wallet): add P2wpkh, Taproot, InputsIndex errors to `SignerError`
fix(wallet): fields on taproot, and ecdsa `Signature` structure
fix(wallet/wallet): convert `Weight` to `usize` for now
- converts the `bitcoin-units::Weight` type to `usize` with help of
`to_wu()` method.
- it should be updated/refactored in the future to handle the `Weight`
type throughout the code instead of current `usize`, only converting
it for now.
- allows the usage of deprecated `is_provably_unspendable()`, needs
further discussion if suggested `is_op_return` is suitable.
- update the expect field to `signature`, as it was renamed from `sig`.
fix(wallet/wallet): use `is_op_return` instead of
`is_provably_unspendable`
fix(wallet/wallet): use `relative::Locktime` instead of `Sequence`
fix(wallet/descriptor): use `ParsePublicKeyError`
fix(wallet/descriptor): use `.into()` to convert from `AbsLockTime` and
`RelLockTime` to `absolute::LockTime` and `relative::LockTime`
fix(wallet/wallet): use `Message::from_digest()` instead of relying on
deprecated `ThirtyTwoByteHash` trait.
fix(wallet/descriptor+wallet): expect `Threshold` type, and handle it
internally
fix(wallet/wallet): remove `0x` prefix from expected `TxId` display
fix(examples): use `compute_txid()` instead of `txid`
fix(ci): remove usage of `bitcoin/no-std` feature
- remove comment: `# The `no-std` feature it's implied when the `std` feature is disabled.`
2024-05-22 18:34:30 -03:00
|
|
|
txid: tx1.compute_txid(),
|
2023-03-01 11:09:08 +01:00
|
|
|
vout: 0,
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
}],
|
|
|
|
output: vec![
|
|
|
|
TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(20_000),
|
2023-03-02 19:08:33 +01:00
|
|
|
script_pubkey: spk2,
|
2023-03-01 11:09:08 +01:00
|
|
|
},
|
|
|
|
TxOut {
|
2023-03-02 19:08:33 +01:00
|
|
|
script_pubkey: spk1,
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(30_000),
|
2023-03-01 11:09:08 +01:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2024-04-26 11:17:23 -03:00
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx2, ..),
|
|
|
|
(Amount::from_sat(42_000), Amount::from_sat(50_000))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx2, ..1),
|
|
|
|
(Amount::from_sat(42_000), Amount::from_sat(30_000))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
index.sent_and_received(&tx2, 1..),
|
|
|
|
(Amount::from_sat(0), Amount::from_sat(20_000))
|
|
|
|
);
|
|
|
|
assert_eq!(index.net_value(&tx2, ..), SignedAmount::from_sat(8_000));
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mark_used() {
|
2023-06-21 17:59:34 +02:00
|
|
|
let spk1 = ScriptBuf::from_hex("001404f1e52ce2bab3423c6a8c63b7cd730d8f12542c").unwrap();
|
|
|
|
let spk2 = ScriptBuf::from_hex("00142b57404ae14f08c3a0c903feb2af7830605eb00f").unwrap();
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
let mut spk_index = SpkTxOutIndex::default();
|
|
|
|
spk_index.insert_spk(1, spk1.clone());
|
2023-03-02 19:08:33 +01:00
|
|
|
spk_index.insert_spk(2, spk2);
|
2023-03-01 11:09:08 +01:00
|
|
|
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(!spk_index.is_used(&1));
|
2023-03-01 11:09:08 +01:00
|
|
|
spk_index.mark_used(&1);
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(spk_index.is_used(&1));
|
2023-03-01 11:09:08 +01:00
|
|
|
spk_index.unmark_used(&1);
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(!spk_index.is_used(&1));
|
2023-03-01 11:09:08 +01:00
|
|
|
spk_index.mark_used(&1);
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(spk_index.is_used(&1));
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
let tx1 = Transaction {
|
2023-10-16 19:51:53 +11:00
|
|
|
version: transaction::Version::TWO,
|
2023-06-21 17:59:34 +02:00
|
|
|
lock_time: absolute::LockTime::ZERO,
|
2023-03-01 11:09:08 +01:00
|
|
|
input: vec![],
|
|
|
|
output: vec![TxOut {
|
2023-10-16 19:51:53 +11:00
|
|
|
value: Amount::from_sat(42_000),
|
2023-03-02 19:08:33 +01:00
|
|
|
script_pubkey: spk1,
|
2023-03-01 11:09:08 +01:00
|
|
|
}],
|
|
|
|
};
|
|
|
|
|
2023-08-25 12:52:09 +03:00
|
|
|
spk_index.index_tx(&tx1);
|
2023-03-01 11:09:08 +01:00
|
|
|
spk_index.unmark_used(&1);
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(
|
2023-03-01 11:09:08 +01:00
|
|
|
spk_index.is_used(&1),
|
|
|
|
"even though we unmark_used it doesn't matter because there was a tx scanned that used it"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unmark_used_does_not_result_in_invalid_representation() {
|
|
|
|
let mut spk_index = SpkTxOutIndex::default();
|
2023-03-02 19:08:33 +01:00
|
|
|
assert!(!spk_index.unmark_used(&0));
|
|
|
|
assert!(!spk_index.unmark_used(&1));
|
|
|
|
assert!(!spk_index.unmark_used(&2));
|
2023-03-01 11:09:08 +01:00
|
|
|
assert!(spk_index.unused_spks(..).collect::<Vec<_>>().is_empty());
|
|
|
|
}
|