Author

Topic: Internal algorithm how Electrum auto-selects addresses to spend money from? (Read 133 times)

legendary
Activity: 2618
Merit: 6452
Self-proclaimed Genius
Sample addresses:

11....11   0.06 BTC
22....22   0.05 BTC
33....33   0.03 BTC
44....44   0.02 BTC
55....55   0.01.BTC

BTCs to transfer: 0.09 BTC
-snip-
Which algorithm is implemented?
How about an actual test? (Amounts down by 2 decimal points 'cause I don't have enough testnet coins)

Done in "testnet":
Here's the coins (UTXO), I've also added a 0.0004 amount.


When sending 0.0009 BTC @1sat/vB, it will select "0.0006" and "0.0004" since those are the best candidates to fulfill the will-be-sent amount and the fee.
Pretty much your number 2 algorithm.


But as you can see in the replies above and the overwhelming "coinchooser.py" code of Electrum, it's much more complicated that your list.
legendary
Activity: 952
Merit: 1386
They say the algorithm is based on:
Code:
Attempts to better preserve user privacy.
First, if any coin is spent from a user address, all coins are.
Compared to spending from other addresses to make up an amount, this reduces
information leakage about sender holdings.  It also helps to
reduce blockchain UTXO bloat, and reduce future privacy loss that
would come from reusing that address' remaining UTXOs.
Second, it penalizes change that is quite different to the sent amount.
Third, it penalizes change that is too big.

As far as I know, in the past there were other approaches - based on order of incoming transactions, or combining addresses to merge the change under new address.
Now it is selecting single input if it is possible, probably to save on tx size.
legendary
Activity: 2268
Merit: 18771
-snip-
I was under the impression that if you had multiple possible buckets which could cover a specific transaction, Electrum would select the bucket with the fewest inputs, but actually reading from line 453 onwards, it seems Electrum actually tries to choose the bucket which will produce the most private amount of change.

Furthermore I don't use the right click context menu "Spend from" to manually select the addresses
which should be used for the next transaction.
On a slight tangent from your question, you can also click on "View -> Show Coins" to display an additional "Coins" tab across the top of Electrum. This tab shows every unspent UTXO in your wallet, and which address they are stored on. If you have multiple inputs on a single address, then this makes it possible to not spend them all at once if you don't want to, unlike the "Spend from" option.

If you have any interest/concern in Electrum spending specific inputs for your transactions, then really you should be manually selecting those inputs for each transaction.
legendary
Activity: 3038
Merit: 4418
Crypto Swap Exchange
Yellow highlights are used for change addresses, which are categorized differently from your receiving addresses.

Electrum doesn't allow their users to select the "algorithm" or the methodology of doing so. Electrum groups the UTXO into buckets, or bundles of them. Each bucket corresponds to the UTXO that is associated with the same address. Electrum prioritizes spending from a singular bucket and only mixes them if a single bucket of UTXOs cannot fulfill the requirements of the user. It also spends the entire bucket when one of the UTXO within it is spent. So in a sense, Electrum doesn't strictly minimize change.

The code includes additional penalization based on the combination that is given. You might have a better understanding by looking at the code and the comments[1].

[1] https://github.com/spesmilo/electrum/blob/6650e6bbae12a79e12667857ee039f1b1f30c7e3/electrum/coinchooser.py
newbie
Activity: 25
Merit: 22
Assume I want to send BTCs to someone else.

On the "Adresses" tab I have multiple yellow addresses containing BTCs.
Furthermore I don't use the right click context menu "Spend from" to manually select the addresses
which should be used for the next transaction.

So Electrum will automatically choose the addresses.

How does this auto-selection algorithm work?

I can think of

1.) Select addresses with the biggest BTC amount first
     =minimize the number of used addresses

2.) Same as 1. but more smarter:
     Select addresses with the biggest BTC amount first
     but try to minimize change too

3.) Try to consilidate as much addresses
     = minimize the number of addresses in wallet which contain BTCs afterwards

4.) Minimize fee by selecting addresses with minimum number of previous Inputs

5.) .....?

Sample addresses:

11....11   0.06 BTC
22....22   0.05 BTC
33....33   0.03 BTC
44....44   0.02 BTC
55....55   0.01.BTC

BTCs to transfer: 0.09 BTC

Resulting used addresses for algorithms:

1.) 11.....11 + 22...22
2.) 11.....11 + 33...33
3.) 22.....22 + 33....33 + 44....44 + 55....55

Which algorithm is implemented?

Can I select algorithm?

Is the algorithm Electrum specific or defined for all BTC wallet software?
Jump to: