3

I have a wallet that has several hundred small amounts residing at multiple addresses. I noticed yesterday that, when sending a relatively small amount to a third party that the bitcoind "swept" about 47 of those small amounts together as vin amounts and generated two vout destinations; 1 is the third party, the second is a new address in the wallet (a change address).

What surprised me is that, rather than assembling only the number of fragments necessary for the transfer (plus fee), it swept many of the fragments together to generate a much larger than necessary change transfer.

To give some specifics but to keep it simple:

  • I initiated a sendtoaddress via the cli. The amount transferred was 0.004 BTC
  • Bitcoind used some 47 existing fragments as vin, generating a total value of 0.08+ BTC

Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force sendtoaddress to assemble only the minimal pieces needed?

2

Bitcoin Core's coin selection algorithm has had multiple improvements in the past years. More recent versions of Bitcoin Core (0.17.0+) will attempt to select an input set that does not require a change output. If you are on a sufficiently recent version of Bitcoin Core, it should find and prefer such a solution if one is possible.

This means that either the wallet's UTXO pool did not yield such a solution or your version of Bitcoind is older than 0.17.0. In the former case, you may want to use coin control or createrawtransaction to fall back to manual selection to enforce your selection requirements, in the latter case, you may want to update and try again.

Note that the current difficulty period has had exceptionally low blockspace demand, and it would be an excellent time to consolidate some of your "several hundred small" UTXOs to bigger UTXOs at the minimum feerate of 1 sat/vB. If you were attempting to send at a low feerate, a transaction with 47 inputs might actually save you money in the long run.

1
  • I'm on 0.19. I'll look into coin control. – David Hoelzer Jun 19 at 7:29
1

Coin selection algorithm in Bitcoin Core is weird and lot of other users had similar issues in past. Example: https://github.com/bitcoin/bitcoin/issues/20598

There are few open PRs and few PRs that recently got merged which might change few things although I am not sure as I did not review them.

Apart from fees related issues, there can be some privacy related issues as well. Example: https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2021-May/019027.html

Is there any way, aside from manually generating a raw transaction (which I can do, but I prefer not to when I can avoid it since a simple error can result in inadvertently generating a large fee amount... :) ) to force sendtoaddress to assemble only the minimal pieces needed?

  1. You should use latest Bitcoin Core
  2. Avoid using coin selection algorithm if possible.
  3. Manually select inputs if using GUI for each transaction
  4. If using CLI and don't want to manually add inputs, at least check transaction details before broadcasting :
    bitcoin-cli createrawtransaction "[]" "{\"myaddress\":0.01}"
    bitcoin-cli fundrawtransaction "rawtransactionhex"
    bitcoin-cli signrawtransactionwithwallet "fundedtransactionhex"
    
    bitcoin-cli decoderawtransaction "hexstring"
    
    If inputs look okay:
    bitcoin-cli sendrawtransaction "signedtransactionhex"
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.