]> Untitled Git - bdk/commit
Fix the early InsufficientFunds error in the branch and bound
authorAlekos Filini <alekos.filini@gmail.com>
Tue, 26 Jul 2022 15:27:40 +0000 (17:27 +0200)
committerAlekos Filini <alekos.filini@gmail.com>
Wed, 3 Aug 2022 17:15:06 +0000 (19:15 +0200)
commit9d85c9667f7d12902afef3ba08ea7231f6868a78
tree018581703eacfb39a808d2ba2a28a54a88a3c2e8
parent9c0a7696759cecdf86165c38be914018d7bd016c
Fix the early InsufficientFunds error in the branch and bound

We were wrongly considering the sum of "effective value" (i.e. value -
fee cost) when reporting an early "insufficient funds" error in the
branch and bound coin selection.

This commit fixes essentially two issues:
- Very high fee rates could cause a panic during the i64 -> u64
  conversion because we assumed the sum of effective values would never
  be negative
- Since we were comparing the sum of effective values of *all* the UTXOs
  (even the optional UTXOs with negative effective value) with the target
  we'd like to reach, we could in some cases error and tell the user we
  don't have enough funds, while in fact we do! Since we are not required
  to spend any of the optional UTXOs, so we could just ignore the ones
  that *cost us* money to spend and excluding them could potentially
  allow us to reach the target.

There's a third issue that was present before and remains even with this
fix: when we report the "available" funds in the error, we are ignoring
UTXOs with negative effective value, so it may look like there are less
funds in the wallet than there actually are.

I don't know how to convey the right message the user: if we actually
consider them we just make the "needed" value larger and larger (which
may be confusing, because if the user asks BDK to send 10k satoshis, why
do we say that we actually need 100k?), while if we don't we could report
an incorrect "available" value.
src/wallet/coin_selection.rs