Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/coinselector_tests.cpp
Show First 20 Lines • Show All 583 Lines • ▼ Show 20 Lines | for (int i = 0; i < RUN_TESTS; i++) { | ||||
// but if we try to make 99.9, we should take the bigger of the two | // but if we try to make 99.9, we should take the bigger of the two | ||||
// small coins to avoid small change | // small coins to avoid small change | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
9990 * MIN_CHANGE / 100, filter_confirmed, GroupCoins(vCoins), | 9990 * MIN_CHANGE / 100, filter_confirmed, GroupCoins(vCoins), | ||||
setCoinsRet, nValueRet, coin_selection_params, bnb_used)); | setCoinsRet, nValueRet, coin_selection_params, bnb_used)); | ||||
BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE); | BOOST_CHECK_EQUAL(nValueRet, 101 * MIN_CHANGE); | ||||
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); | BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); | ||||
} | |||||
// test with many inputs | // test with many inputs | ||||
for (Amount amt = 1500 * SATOSHI; amt < COIN; amt = 10 * amt) { | for (Amount amt = 1500 * SATOSHI; amt < COIN; amt = 10 * amt) { | ||||
empty_wallet(); | empty_wallet(); | ||||
// Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 | // Create 676 inputs (= (old MAX_STANDARD_TX_SIZE == 100000) / 148 | ||||
// bytes per input) | // bytes per input) | ||||
for (uint16_t j = 0; j < 676; j++) { | for (uint16_t j = 0; j < 676; j++) { | ||||
add_coin(testWallet, amt); | add_coin(testWallet, amt); | ||||
} | } | ||||
// We only create the wallet once to save time, but we still run the | |||||
// coin selection RUN_TESTS times. | |||||
for (int i = 0; i < RUN_TESTS; i++) { | |||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
2000 * SATOSHI, filter_confirmed, GroupCoins(vCoins), | 2000 * SATOSHI, filter_confirmed, GroupCoins(vCoins), | ||||
setCoinsRet, nValueRet, coin_selection_params, bnb_used)); | setCoinsRet, nValueRet, coin_selection_params, bnb_used)); | ||||
if (amt - 2000 * SATOSHI < MIN_CHANGE) { | if (amt - 2000 * SATOSHI < MIN_CHANGE) { | ||||
// needs more than one input: | // needs more than one input: | ||||
uint16_t returnSize = std::ceil( | uint16_t returnSize = std::ceil( | ||||
(2000.0 + (MIN_CHANGE / SATOSHI)) / (amt / SATOSHI)); | (2000.0 + (MIN_CHANGE / SATOSHI)) / (amt / SATOSHI)); | ||||
Amount returnValue = returnSize * amt; | Amount returnValue = returnSize * amt; | ||||
BOOST_CHECK_EQUAL(nValueRet, returnValue); | BOOST_CHECK_EQUAL(nValueRet, returnValue); | ||||
BOOST_CHECK_EQUAL(setCoinsRet.size(), returnSize); | BOOST_CHECK_EQUAL(setCoinsRet.size(), returnSize); | ||||
} else { | } else { | ||||
// one input is sufficient: | // one input is sufficient: | ||||
BOOST_CHECK_EQUAL(nValueRet, amt); | BOOST_CHECK_EQUAL(nValueRet, amt); | ||||
BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); | BOOST_CHECK_EQUAL(setCoinsRet.size(), 1U); | ||||
} | } | ||||
} | } | ||||
} | |||||
// test randomness | // test randomness | ||||
{ | { | ||||
empty_wallet(); | empty_wallet(); | ||||
for (int i2 = 0; i2 < 100; i2++) { | for (int i2 = 0; i2 < 100; i2++) { | ||||
add_coin(testWallet, COIN); | add_coin(testWallet, COIN); | ||||
} | } | ||||
// Again, we only create the wallet once to save time, but we still run | |||||
// the coin selection RUN_TESTS times. | |||||
for (int i = 0; i < RUN_TESTS; i++) { | |||||
// picking 50 from 100 coins doesn't depend on the shuffle, but does | // picking 50 from 100 coins doesn't depend on the shuffle, but does | ||||
// depend on randomness in the stochastic approximation code | // depend on randomness in the stochastic approximation code | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet, | 50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet, | ||||
nValueRet, coin_selection_params, bnb_used)); | nValueRet, coin_selection_params, bnb_used)); | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, | 50 * COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, | ||||
nValueRet, coin_selection_params, bnb_used)); | nValueRet, coin_selection_params, bnb_used)); | ||||
Show All 10 Lines | // test randomness | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, | COIN, filter_standard, GroupCoins(vCoins), setCoinsRet2, | ||||
nValueRet, coin_selection_params, bnb_used)); | nValueRet, coin_selection_params, bnb_used)); | ||||
if (equal_sets(setCoinsRet, setCoinsRet2)) { | if (equal_sets(setCoinsRet, setCoinsRet2)) { | ||||
fails++; | fails++; | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK_NE(fails, RANDOM_REPEATS); | BOOST_CHECK_NE(fails, RANDOM_REPEATS); | ||||
} | |||||
// add 75 cents in small change. not enough to make 90 cents, then | // add 75 cents in small change. not enough to make 90 cents, then | ||||
// try making 90 cents. there are multiple competing "smallest | // try making 90 cents. there are multiple competing "smallest | ||||
// bigger" coins, one of which should be picked at random | // bigger" coins, one of which should be picked at random | ||||
add_coin(testWallet, 5 * CENT); | add_coin(testWallet, 5 * CENT); | ||||
add_coin(testWallet, 10 * CENT); | add_coin(testWallet, 10 * CENT); | ||||
add_coin(testWallet, 15 * CENT); | add_coin(testWallet, 15 * CENT); | ||||
add_coin(testWallet, 20 * CENT); | add_coin(testWallet, 20 * CENT); | ||||
add_coin(testWallet, 25 * CENT); | add_coin(testWallet, 25 * CENT); | ||||
fails = 0; | for (int i = 0; i < RUN_TESTS; i++) { | ||||
int fails = 0; | |||||
for (int j = 0; j < RANDOM_REPEATS; j++) { | for (int j = 0; j < RANDOM_REPEATS; j++) { | ||||
// selecting 1 from 100 identical coins depends on the shuffle; | // selecting 1 from 100 identical coins depends on the shuffle; | ||||
// this test will fail 1% of the time run the test | // this test will fail 1% of the time run the test | ||||
// RANDOM_REPEATS times and only complain if all of them fail | // RANDOM_REPEATS times and only complain if all of them fail | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
90 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, | 90 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, | ||||
nValueRet, coin_selection_params, bnb_used)); | nValueRet, coin_selection_params, bnb_used)); | ||||
BOOST_CHECK(testWallet.SelectCoinsMinConf( | BOOST_CHECK(testWallet.SelectCoinsMinConf( | ||||
90 * CENT, filter_standard, GroupCoins(vCoins), | 90 * CENT, filter_standard, GroupCoins(vCoins), | ||||
setCoinsRet2, nValueRet, coin_selection_params, bnb_used)); | setCoinsRet2, nValueRet, coin_selection_params, bnb_used)); | ||||
if (equal_sets(setCoinsRet, setCoinsRet2)) { | if (equal_sets(setCoinsRet, setCoinsRet2)) { | ||||
fails++; | fails++; | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK_NE(fails, RANDOM_REPEATS); | BOOST_CHECK_NE(fails, RANDOM_REPEATS); | ||||
} | } | ||||
} | } | ||||
empty_wallet(); | empty_wallet(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(ApproximateBestSubset) { | BOOST_AUTO_TEST_CASE(ApproximateBestSubset) { | ||||
CoinSet setCoinsRet; | CoinSet setCoinsRet; | ||||
Amount nValueRet; | Amount nValueRet; | ||||
bool bnb_used; | bool bnb_used; | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |