Skip to content

Commit c93c57b

Browse files
committed
Deprecate list_unspent which has an unnecessary .collect()
Use `iter_unspent` instead, as it returns an iterator directly.
1 parent 4624ef8 commit c93c57b

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/testutils/blockchain_tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ macro_rules! bdk_blockchain_tests {
455455
assert!(wallet.database().deref().get_sync_time().unwrap().is_some(), "sync_time hasn't been updated");
456456

457457
assert_eq!(wallet.get_balance().unwrap().untrusted_pending, 50_000, "incorrect balance");
458-
assert_eq!(wallet.list_unspent().unwrap()[0].keychain, KeychainKind::External, "incorrect keychain kind");
458+
assert_eq!(wallet.iter_unspent().unwrap().next().unwrap().keychain, KeychainKind::External, "incorrect keychain kind");
459459

460460
let list_tx_item = &wallet.list_transactions(false).unwrap()[0];
461461
assert_eq!(list_tx_item.txid, txid, "incorrect txid");
@@ -518,7 +518,7 @@ macro_rules! bdk_blockchain_tests {
518518

519519
assert_eq!(wallet.get_balance().unwrap().untrusted_pending, 105_000, "incorrect balance");
520520
assert_eq!(wallet.list_transactions(false).unwrap().len(), 1, "incorrect number of txs");
521-
assert_eq!(wallet.list_unspent().unwrap().len(), 3, "incorrect number of unspents");
521+
assert_eq!(wallet.iter_unspent().unwrap().count(), 3, "incorrect number of unspents");
522522

523523
let list_tx_item = &wallet.list_transactions(false).unwrap()[0];
524524
assert_eq!(list_tx_item.txid, txid, "incorrect txid");
@@ -542,7 +542,7 @@ macro_rules! bdk_blockchain_tests {
542542

543543
assert_eq!(wallet.get_balance().unwrap().untrusted_pending, 75_000, "incorrect balance");
544544
assert_eq!(wallet.list_transactions(false).unwrap().len(), 2, "incorrect number of txs");
545-
assert_eq!(wallet.list_unspent().unwrap().len(), 2, "incorrect number of unspent");
545+
assert_eq!(wallet.iter_unspent().unwrap().count(), 2, "incorrect number of unspent");
546546
}
547547

548548
#[test]
@@ -576,7 +576,7 @@ macro_rules! bdk_blockchain_tests {
576576

577577
assert_eq!(wallet.get_balance().unwrap().untrusted_pending, 50_000, "incorrect balance");
578578
assert_eq!(wallet.list_transactions(false).unwrap().len(), 1, "incorrect number of txs");
579-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "incorrect unspent");
579+
assert_eq!(wallet.iter_unspent().unwrap().count(), 1, "incorrect unspent");
580580

581581
let list_tx_item = &wallet.list_transactions(false).unwrap()[0];
582582
assert_eq!(list_tx_item.txid, txid, "incorrect txid");
@@ -590,7 +590,7 @@ macro_rules! bdk_blockchain_tests {
590590

591591
assert_eq!(wallet.get_balance().unwrap().untrusted_pending, 50_000, "incorrect balance after bump");
592592
assert_eq!(wallet.list_transactions(false).unwrap().len(), 1, "incorrect number of txs after bump");
593-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "incorrect unspent after bump");
593+
assert_eq!(wallet.iter_unspent().unwrap().count(), 1, "incorrect unspent after bump");
594594

595595
let list_tx_item = &wallet.list_transactions(false).unwrap()[0];
596596
assert_eq!(list_tx_item.txid, new_txid, "incorrect txid after bump");
@@ -613,7 +613,7 @@ macro_rules! bdk_blockchain_tests {
613613
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
614614
assert_eq!(wallet.get_balance().unwrap().get_spendable(), 50_000, "incorrect balance");
615615
assert_eq!(wallet.list_transactions(false).unwrap().len(), 1, "incorrect number of txs");
616-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "incorrect number of unspents");
616+
assert_eq!(wallet.iter_unspent().unwrap().count(), 1, "incorrect number of unspents");
617617

618618
let list_tx_item = &wallet.list_transactions(false).unwrap()[0];
619619
assert_eq!(list_tx_item.txid, txid, "incorrect txid");
@@ -661,7 +661,7 @@ macro_rules! bdk_blockchain_tests {
661661
assert_eq!(wallet.get_balance().unwrap().confirmed, details.received, "incorrect balance after send");
662662

663663
assert_eq!(wallet.list_transactions(false).unwrap().len(), 2, "incorrect number of txs");
664-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "incorrect number of unspents");
664+
assert_eq!(wallet.iter_unspent().unwrap().count(), 1, "incorrect number of unspents");
665665
}
666666

667667
// Syncing wallet should not result in wallet address index to decrement.
@@ -1178,7 +1178,7 @@ macro_rules! bdk_blockchain_tests {
11781178
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
11791179
assert_eq!(wallet.get_balance().unwrap().get_spendable(), details.received, "wallet has incorrect balance after send");
11801180
assert_eq!(wallet.list_transactions(false).unwrap().len(), 2, "wallet has incorrect number of txs");
1181-
assert_eq!(wallet.list_unspent().unwrap().len(), 1, "wallet has incorrect number of unspents");
1181+
assert_eq!(wallet.iter_unspent().unwrap().count(), 1, "wallet has incorrect number of unspents");
11821182
test_client.generate(1, None);
11831183

11841184
// 5. Verify 25_000 sats are received by test bitcoind node taproot wallet
@@ -1249,7 +1249,7 @@ macro_rules! bdk_blockchain_tests {
12491249
let initial_tx = psbt.extract_tx();
12501250
let _sent_txid = blockchain.broadcast(&initial_tx).unwrap();
12511251
wallet.sync(&blockchain, SyncOptions::default()).unwrap();
1252-
for utxo in wallet.list_unspent().unwrap() {
1252+
for utxo in wallet.iter_unspent().unwrap() {
12531253
// Making sure the TXO we just spent is not returned by list_unspent
12541254
assert!(utxo.outpoint != initial_tx.input[0].previous_output, "wallet displays spent txo in unspents");
12551255
}
@@ -1265,7 +1265,7 @@ macro_rules! bdk_blockchain_tests {
12651265
builder
12661266
.add_utxo(initial_tx.input[0].previous_output)
12671267
.expect("Can't manually add an UTXO spent");
1268-
for utxo in wallet.list_unspent().unwrap() {
1268+
for utxo in wallet.iter_unspent().unwrap() {
12691269
// Making sure the TXO we just spent is not returned by list_unspent
12701270
assert!(utxo.outpoint != initial_tx.input[0].previous_output, "wallet displays spent txo in unspents");
12711271
}

src/wallet/mod.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,22 @@ where
411411
///
412412
/// Note that this method only operates on the internal database, which first needs to be
413413
/// [`Wallet::sync`] manually.
414+
#[deprecated = "Use Wallet::iter_unspent"]
414415
pub fn list_unspent(&self) -> Result<Vec<LocalUtxo>, Error> {
416+
self.iter_unspent().map(|iter| iter.collect())
417+
}
418+
419+
/// Returns an iterator of unspent outputs (UTXOs) of this wallet.
420+
///
421+
/// Note that this method only operates on the internal database, which first needs to be
422+
/// [`Wallet::sync`] manually.
423+
pub fn iter_unspent(&self) -> Result<impl Iterator<Item = LocalUtxo> + '_, Error> {
415424
Ok(self
416425
.database
417426
.borrow()
418427
.iter_utxos()?
419428
.into_iter()
420-
.filter(|l| !l.is_spent)
421-
.collect())
429+
.filter(|l| !l.is_spent))
422430
}
423431

424432
/// Returns the `UTXO` owned by this wallet corresponding to `outpoint` if it exists in the
@@ -474,7 +482,7 @@ where
474482
let mut trusted_pending = 0;
475483
let mut untrusted_pending = 0;
476484
let mut confirmed = 0;
477-
let utxos = self.list_unspent()?;
485+
478486
let database = self.database.borrow();
479487
let last_sync_height = match database
480488
.get_sync_time()?
@@ -484,7 +492,7 @@ where
484492
// None means database was never synced
485493
None => return Ok(Balance::default()),
486494
};
487-
for u in utxos {
495+
for u in self.iter_unspent()? {
488496
// Unwrap used since utxo set is created from database
489497
let tx = database
490498
.get_tx(&u.outpoint.txid, true)?
@@ -1427,8 +1435,7 @@ where
14271435

14281436
fn get_available_utxos(&self) -> Result<Vec<(LocalUtxo, usize)>, Error> {
14291437
Ok(self
1430-
.list_unspent()?
1431-
.into_iter()
1438+
.iter_unspent()?
14321439
.map(|utxo| {
14331440
let keychain = utxo.keychain;
14341441
(
@@ -3017,7 +3024,7 @@ pub(crate) mod test {
30173024
get_funded_wallet("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)");
30183025

30193026
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX").unwrap();
3020-
let utxo = wallet2.list_unspent().unwrap().remove(0);
3027+
let utxo = wallet2.iter_unspent().unwrap().next().unwrap();
30213028
let foreign_utxo_satisfaction = wallet2
30223029
.get_descriptor_for_keychain(KeychainKind::External)
30233030
.max_satisfaction_weight()
@@ -3082,7 +3089,7 @@ pub(crate) mod test {
30823089
fn test_add_foreign_utxo_invalid_psbt_input() {
30833090
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
30843091
let mut builder = wallet.build_tx();
3085-
let outpoint = wallet.list_unspent().unwrap()[0].outpoint;
3092+
let outpoint = wallet.iter_unspent().unwrap().next().unwrap().outpoint;
30863093
let foreign_utxo_satisfaction = wallet
30873094
.get_descriptor_for_keychain(KeychainKind::External)
30883095
.max_satisfaction_weight()
@@ -3098,7 +3105,7 @@ pub(crate) mod test {
30983105
let (wallet2, _, txid2) =
30993106
get_funded_wallet("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)");
31003107

3101-
let utxo2 = wallet2.list_unspent().unwrap().remove(0);
3108+
let utxo2 = wallet2.iter_unspent().unwrap().next().unwrap();
31023109
let tx1 = wallet1
31033110
.database
31043111
.borrow()
@@ -3156,7 +3163,7 @@ pub(crate) mod test {
31563163
let (wallet2, _, txid2) =
31573164
get_funded_wallet("wpkh(cVbZ8ovhye9AoAHFsqobCf7LxbXDAECy9Kb8TZdfsDYMZGBUyCnm)");
31583165
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX").unwrap();
3159-
let utxo2 = wallet2.list_unspent().unwrap().remove(0);
3166+
let utxo2 = wallet2.iter_unspent().unwrap().next().unwrap();
31603167

31613168
let satisfaction_weight = wallet2
31623169
.get_descriptor_for_keychain(KeychainKind::External)
@@ -3225,7 +3232,7 @@ pub(crate) mod test {
32253232
fn test_get_psbt_input() {
32263233
// this should grab a known good utxo and set the input
32273234
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
3228-
for utxo in wallet.list_unspent().unwrap() {
3235+
for utxo in wallet.iter_unspent().unwrap() {
32293236
let psbt_input = wallet.get_psbt_input(utxo, None, false).unwrap();
32303237
assert!(psbt_input.witness_utxo.is_some() || psbt_input.non_witness_utxo.is_some());
32313238
}
@@ -5020,7 +5027,7 @@ pub(crate) mod test {
50205027
let (wallet2, _, _) = get_funded_wallet(get_test_tr_single_sig());
50215028

50225029
let addr = Address::from_str("2N1Ffz3WaNzbeLFBb51xyFMHYSEUXcbiSoX").unwrap();
5023-
let utxo = wallet2.list_unspent().unwrap().remove(0);
5030+
let utxo = wallet2.iter_unspent().unwrap().next().unwrap();
50245031
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
50255032
let foreign_utxo_satisfaction = wallet2
50265033
.get_descriptor_for_keychain(KeychainKind::External)

0 commit comments

Comments
 (0)