Bitcoin Forum
May 11, 2024, 12:49:12 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Internal algorithm how Electrum auto-selects addresses to spend money from?  (Read 114 times)
pxstein (OP)
Newbie
*
Offline Offline

Activity: 24
Merit: 22


View Profile
December 16, 2021, 10:10:20 AM
Merited by o_e_l_e_o (4), Pmalek (2), ABCbits (1), hosseinimr93 (1)
 #1

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?
1715388552
Hero Member
*
Offline Offline

Posts: 1715388552

View Profile Personal Message (Offline)

Ignore
1715388552
Reply with quote  #2

1715388552
Report to moderator
1715388552
Hero Member
*
Offline Offline

Posts: 1715388552

View Profile Personal Message (Offline)

Ignore
1715388552
Reply with quote  #2

1715388552
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
ranochigo
Legendary
*
Offline Offline

Activity: 2968
Merit: 4177



View Profile
December 16, 2021, 10:28:42 AM
Merited by o_e_l_e_o (4), ABCbits (3), Pmalek (2), hugeblack (2), hosseinimr93 (2), NotATether (2), nc50lc (1)
 #2

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

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
o_e_l_e_o
In memoriam
Legendary
*
Offline Offline

Activity: 2268
Merit: 18512


View Profile
December 16, 2021, 11:26:32 AM
Last edit: December 16, 2021, 11:51:55 AM by o_e_l_e_o
 #3

-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.
PawGo
Legendary
*
Offline Offline

Activity: 952
Merit: 1367


View Profile
December 16, 2021, 12:20:53 PM
Merited by Abdussamad (2), ABCbits (2), Pmalek (1)
 #4

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.
nc50lc
Legendary
*
Offline Offline

Activity: 2408
Merit: 5594


Self-proclaimed Genius


View Profile
December 16, 2021, 12:26:59 PM
 #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
-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.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!