Skip to content

Commit

Permalink
wallet: improve outpoint and UTXO accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 20, 2024
1 parent 7e6e933 commit 65588b5
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,24 +341,20 @@ impl<L2C: Layer2Cache> WalletCache<L2C> {
})
}

pub fn txos(&self) -> impl Iterator<Item = WalletUtxo> + '_ {
self.tx.iter().flat_map(|(txid, tx)| {
tx.outputs.iter().enumerate().filter_map(|(vout, out)| {
if let Party::Wallet(w) = out.beneficiary {
Some(WalletUtxo {
outpoint: Outpoint::new(*txid, vout as u32),
value: out.value,
terminal: w.terminal,
status: tx.status,
})
} else {
None
}
})
})
pub fn has_outpoint(&self, outpoint: Outpoint) -> bool {
let Some(tx) = self.tx.get(&outpoint.txid) else {
return false;

Check warning on line 346 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L344-L346

Added lines #L344 - L346 were not covered by tests
};
let Some(out) = tx.outputs.get(outpoint.vout.to_usize()) else {
return false;

Check warning on line 349 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L348-L349

Added lines #L348 - L349 were not covered by tests
};
matches!(out.beneficiary, Party::Wallet(_))
}

Check warning on line 352 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L351-L352

Added lines #L351 - L352 were not covered by tests

pub fn utxo_by(&self, outpoint: Outpoint) -> Result<WalletUtxo, NonWalletItem> {
#[inline]
pub fn is_unspent(&self, outpoint: Outpoint) -> bool { self.utxo.contains(&outpoint) }

Check warning on line 355 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L355

Added line #L355 was not covered by tests

pub fn outpoint_by(&self, outpoint: Outpoint) -> Result<WalletUtxo, NonWalletItem> {

Check warning on line 357 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L357

Added line #L357 was not covered by tests
let tx = self.tx.get(&outpoint.txid).ok_or(NonWalletItem::NonWalletTx(outpoint.txid))?;
let debit = tx
.outputs
Expand All @@ -374,6 +370,23 @@ impl<L2C: Layer2Cache> WalletCache<L2C> {
})
}

pub fn txos(&self) -> impl Iterator<Item = WalletUtxo> + '_ {
self.tx.iter().flat_map(|(txid, tx)| {
tx.outputs.iter().enumerate().filter_map(|(vout, out)| {
if let Party::Wallet(w) = out.beneficiary {
Some(WalletUtxo {
outpoint: Outpoint::new(*txid, vout as u32),
value: out.value,
terminal: w.terminal,
status: tx.status,
})

Check warning on line 382 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L373-L382

Added lines #L373 - L382 were not covered by tests
} else {
None

Check warning on line 384 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L384

Added line #L384 was not covered by tests
}
})
})
}

Check warning on line 388 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L386-L388

Added lines #L386 - L388 were not covered by tests

pub fn utxos(&self) -> impl Iterator<Item = WalletUtxo> + '_ {

Check warning on line 390 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L390

Added line #L390 was not covered by tests
self.utxo.iter().map(|outpoint| {
let tx = self.tx.get(&outpoint.txid).expect("cache data inconsistency");
Expand Down Expand Up @@ -461,7 +474,7 @@ impl<K, D: Descriptor<K>, L2: Layer2> PsbtConstructor for Wallet<K, D, L2> {
fn descriptor(&self) -> &D { &self.descr.generator }

fn utxo(&self, outpoint: Outpoint) -> Option<Utxo> {
self.cache.utxo_by(outpoint).ok().map(WalletUtxo::into_utxo)
self.cache.outpoint_by(outpoint).ok().map(WalletUtxo::into_utxo)

Check warning on line 477 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L477

Added line #L477 was not covered by tests
}

fn network(&self) -> Network { self.descr.network }
Expand Down Expand Up @@ -582,10 +595,14 @@ impl<K, D: Descriptor<K>, L2: Layer2> Wallet<K, D, L2> {
self.cache.history()
}

pub fn txos(&self) -> impl Iterator<Item = WalletUtxo> + '_ { self.cache.txos() }
pub fn utxo_by(&self, outpoint: Outpoint) -> Result<WalletUtxo, NonWalletItem> {
self.cache.utxo_by(outpoint)
pub fn has_outpoint(&self, outpoint: Outpoint) -> bool { self.cache.has_outpoint(outpoint) }
pub fn is_unspent(&self, outpoint: Outpoint) -> bool { self.cache.is_unspent(outpoint) }

Check warning on line 599 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L598-L599

Added lines #L598 - L599 were not covered by tests

pub fn outpoint_by(&self, outpoint: Outpoint) -> Result<WalletUtxo, NonWalletItem> {
self.cache.outpoint_by(outpoint)
}

Check warning on line 603 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L601-L603

Added lines #L601 - L603 were not covered by tests

pub fn txos(&self) -> impl Iterator<Item = WalletUtxo> + '_ { self.cache.txos() }
pub fn utxos(&self) -> impl Iterator<Item = WalletUtxo> + '_ { self.cache.utxos() }

Check warning on line 606 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L605-L606

Added lines #L605 - L606 were not covered by tests

pub fn coinselect<'a>(
Expand Down

0 comments on commit 65588b5

Please sign in to comment.