Bitcoin Forum
June 14, 2024, 01:52:59 PM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Story Problems: How does Bitcoin-Core determine which private key to spend?  (Read 1029 times)
BusyBeaverHP (OP)
Full Member
***
Offline Offline

Activity: 209
Merit: 100


View Profile
September 13, 2014, 10:54:16 PM
 #1

Suppose that I have one wallet containing two private keys with one bitcoin deposited into each:

PK1 = 1 BTC (1st deposit, oldest)
PK2 = 1 BTC (2nd deposit, youngest)

1. I'm going to send 1 BTC, does the private key with the oldest coin get spent first?

--- --- ---

In another hypothetical scenario, I have one wallet containing three private keys with one bitcoin deposited into each:

PK1 = 1 BTC (1st deposit, oldest)
PK2 = 1 BTC (2nd deposit)
PK3 = 1 BTC (3rd deposit, youngest)

2. I'm going to spend 1.5 BTC, which private key will get drained completely and which will get half drained?

3. After the above transaction, I'm going to spend 0.75 BTC. Will Bitcoin-Core's behavior try to minimize input to save fees, or will it only consider coin age? How would the input change if instead of 0.75 BTC, I spend 1 BTC?
DannyHamilton
Legendary
*
Offline Offline

Activity: 3416
Merit: 4658



View Profile
September 13, 2014, 11:28:30 PM
 #2

Suppose that I have one wallet containing two private keys with one bitcoin deposited into each:

PK1 = 1 BTC (1st deposit, oldest)
PK2 = 1 BTC (2nd deposit, youngest)

1. I'm going to send 1 BTC, does the private key with the oldest coin get spent first?

To start with, private keys don't get spent.  Work on using the correct words when you ask a question, and you'll be more likely to get the right answer.  It is clear that you have a very limited and somewhat mistaken understanding about how bitcoin works and this is leading you to ask some questions that don't even make sense.

As for the question above, it looks like you are asking:

Given a wallet that has exactly two unspent outputs both of which are valued at exactly 1 BTC, if I send exactly 1 BTC (with no transaction fee) will the wallet choose to spend the older unspent output or the newer one (or is come other criteria used)?

Note that the answer to this question depends a LOT on what wallet you are using.  These implementation details are not part of the protocol, and are left for each wallet developer to decide for themselves how they want to handle it.  Therefore, it would be best to start out by telling us what wallet you are asking about.  I assume you are asking about version 0.9.2.1 of Bitcoin Core?



In another hypothetical scenario, I have one wallet containing three private keys with one bitcoin deposited into each:

PK1 = 1 BTC (1st deposit, oldest)
PK2 = 1 BTC (2nd deposit)
PK3 = 1 BTC (3rd deposit, youngest)

2. I'm going to spend 1.5 BTC, which private key will get drained completely and which will get half drained?

Again, I assume you are talking about a wallet that has exactly 3 unspent outputs each valued at exactly 1 BTC?

Please note that it is not possible to "half-drain" an unspent output.  The wallet will have to COMPLETELY spend at least 2 of the unspent outputs.  It will then create 2 new outputs.  One will be for 1.5 BTC to wherever you are sending it.  The other will be 0.5 BTC in "change" that will be sent back into the wallet.  Which address the wallet uses for the change will depend on what wallet you are using.  In the case of Bitcoin Core, the wallet will create a brand new address (which it will keep hidden from you) with a brand new private key, and will send the 0.5 BTC change to this new address.

3. After the above transaction, I'm going to spend 0.75 BTC. Will Bitcoin-Core's behavior try to minimize input to save fees, or will it only consider coin age?

It is my understanding that Bitcoin Core generally gives priority to lower fee transactions.  I'm not sure how it handles situations where two different transactions will result in the same current fee. I'd have to look at the code and get back to you on that.

How would the input change if instead of 0.75 BTC, I spend 1 BTC?

Again, I'd have to get back to you once I get a chance to review the exact code.  I don't recall the exact conditions for building a transaction.
DannyHamilton
Legendary
*
Offline Offline

Activity: 3416
Merit: 4658



View Profile
September 14, 2014, 12:02:09 AM
 #3

Looking at the code of Bitcoin Core, it makes efforts for the current transaction to:
  • Avoid fees
  • Reduce fees
  • Avoid dust outputs

It doesn't seem to put much effort into considering the possibility that the current transaction could cause future transactions to have larger fees or dust outputs.  Most of this can be found in wallet.cpp.

I don't have time to analyze it all right now, but if nobody else has responded in the next few days, I'll try and take a look.

If anyone else wants to look into it, you want to look at CreateTransaction(), SelectCoins(), and SelectCoinsMinConf().
BusyBeaverHP (OP)
Full Member
***
Offline Offline

Activity: 209
Merit: 100


View Profile
September 14, 2014, 12:15:04 AM
 #4

I'm beginning to understand it better now.

EDIT: Confused problem solved.

Thanks for your explanation so far.
RedDiamond
Sr. Member
****
Offline Offline

Activity: 294
Merit: 250


View Profile
September 14, 2014, 10:23:52 AM
Last edit: September 14, 2014, 10:42:51 AM by RedDiamond
 #5

The latest Bitcoin Core has also a Coin Control feature which allows you to manually choose which inputs are used for transcation.



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!