Skip to content

Refactor SelectCoinsMinConf() and add unit tests.#905

Closed
dooglus wants to merge 4 commits intobitcoin:masterfrom
dooglus:refactor_coinselect
Closed

Refactor SelectCoinsMinConf() and add unit tests.#905
dooglus wants to merge 4 commits intobitcoin:masterfrom
dooglus:refactor_coinselect

Conversation

@dooglus
Copy link
Contributor

@dooglus dooglus commented Feb 27, 2012

AvailableCoins() makes a vector of available outputs which is then passed to SelectCoinsMinConf(). This allows unit tests to test the coin selection algorithm without having the whole blockchain available.

This change was suggested in the comments of pull #898.

AvailableCoins() makes a vector of available outputs which is then passed to SelectCoinsMinConf().  This allows unit tests to test the coin selection algorithm without having the whole blockchain available.
@sipa
Copy link
Member

sipa commented Feb 28, 2012

ACK; I really like having unit tests for the coin selection algorithm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The random_shuffle was deleted here, but I don't see any calls to randomize the coins selected. A random_shuffle of the result of AvailableCoins should probably be done so coin selection is unpredictable.

A unit test to test randomness that calls SelectCoins two times to get (say) 5 out of 100 coins, all of the same value/age, and fails if exactly the same coins are selected would also be nifty.

@dooglus
Copy link
Contributor Author

dooglus commented Apr 7, 2012

@gavinandresen I noticed that the shuffle was missing some time ago. It has been added back in in #1017 which is my current branch for these changes, and includes the coincontrol and lesschange changes too.

I'll also add a new unit test for randomness too.

Do you need this pull request with just the refactoring updated too? I merged these related changes all together to make them easier to keep up to date.

@dooglus
Copy link
Contributor Author

dooglus commented Apr 7, 2012

Notice that the coins are sorted into order before running the stochastic approximation:

// Solve subset sum by stochastic approximation
sort(vValue.rbegin(), vValue.rend());

and so the shuffle's work will be undone in the case you propose. The only times the shuffle has any effect is if:

  1. there are multiple coins with the exact value requested (e.g. have 1,1,1 and request 1), or
  2. there is no coin with the exact value requested, and the sum of coins smaller than the sum requested is less than the sum requested, and there are a multiple coins of the same value all representing the smallest value that is larger than the sum requested (e.g. have 1,1,1,5,5,6 and request 4; sum of smaller coins = 3 < requested 4; smallest value larger than requested 4 is 5; there are multiple 5 coins). I'll add tests for both these cases to make sure the shuffle is happening.

I added your suggested "4-from-100 identical" test to check that different random coins were being selected each time. It ran the test 100 times. And every time it failed on iterations 49 and 51. It turns out that this is because rand() is being used in the stochastic approximation code:

if (nPass == 0 ? rand() % 2 : !vfIncluded[i])

and rand() isn't being seeded, so returns the same sequence each time bitcoin is run. See new bug #1057.

If I use GetRandInt(2) instead of rand()%2 then the tests fail occasionally, but randomly. It turns out that selecting 4 coins from a sorted list of 100 often picks the same 4. So I'll pick 50 from 100 instead to minimise the chance of a random identical selection.

@dooglus
Copy link
Contributor Author

dooglus commented Apr 7, 2012

I've merged my latest changes to the unit tests from lesschange-v0.6.0 (#1017) back onto this branch. There are also tests for sub-cent change suggested by Luke here: #898 (comment) which didn't get into this branch before.

@sipa
Copy link
Member

sipa commented Jun 12, 2012

Is this superceded or not?

@gavinandresen
Copy link
Contributor

Closing; I pulled 1416 which was an updated version.

suprnurd pushed a commit to chaincoin-legacy/chaincoin that referenced this pull request Dec 5, 2017
ptschip pushed a commit to ptschip/bitcoin that referenced this pull request Jan 25, 2018
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants