Post
Topic
Board Development & Technical Discussion
Topic OP
SelectCoins algorithm
by
bitcoinandroid
on 18/10/2011, 06:23:08 UTC
Hi,

I am trying to understand the SelectCoins function (at least at a high level):
https://github.com/bitcoin/bitcoin/blob/master/src/wallet.cpp#L746

It looks like it has a bunch of optimizations, but I'm wondering if something simple like this would work as a naive implementation:

1. find all tx outputs that meet the following conditions
 - sent to me (have a hash160 belonging to a key in my wallet)
 - have not been spent yet
 - are in a confirmed block
2. sort the outputs smallest to largest amount
3. for each output
  - create an input that references it and add to current_amount
  - break if current_amount > amount i want to send
4. create one output (change) sending the difference back to me at a new address

Does something like this make sense or am I missing any pieces?  Also, does it make sense to use the smallest coins first to prevent fragmentation or is there a better way?

Thanks!  Haven't seen a good writeup on this elsewhere, but if I missed it feel free to link me to it.