Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/coinselector_tests.cpp
Show First 20 Lines • Show All 660 Lines • ▼ Show 20 Lines | |||||
// Tests that with the ideal conditions, the coin selector will always be able | // Tests that with the ideal conditions, the coin selector will always be able | ||||
// to find a solution that can pay the target value | // to find a solution that can pay the target value | ||||
BOOST_AUTO_TEST_CASE(SelectCoins_test) { | BOOST_AUTO_TEST_CASE(SelectCoins_test) { | ||||
// Random generator stuff | // Random generator stuff | ||||
std::default_random_engine generator; | std::default_random_engine generator; | ||||
std::exponential_distribution<double> distribution(100); | std::exponential_distribution<double> distribution(100); | ||||
FastRandomContext rand; | FastRandomContext rand; | ||||
// Output stuff | |||||
Amount out_value = Amount::zero(); | |||||
CoinSet out_set; | |||||
Amount target = Amount::zero(); | |||||
bool bnb_used; | |||||
// Run this test 100 times | // Run this test 100 times | ||||
for (int i = 0; i < 100; ++i) { | for (int i = 0; i < 100; ++i) { | ||||
// Reset | |||||
out_value = Amount::zero(); | |||||
target = Amount::zero(); | |||||
out_set.clear(); | |||||
empty_wallet(); | empty_wallet(); | ||||
// Make a wallet with 1000 exponentially distributed random inputs | // Make a wallet with 1000 exponentially distributed random inputs | ||||
for (int j = 0; j < 1000; ++j) { | for (int j = 0; j < 1000; ++j) { | ||||
add_coin(m_wallet, | add_coin(m_wallet, | ||||
int64_t(10000000 * distribution(generator)) * SATOSHI); | int64_t(10000000 * distribution(generator)) * SATOSHI); | ||||
} | } | ||||
// Generate a random fee rate in the range of 100 - 400 | // Generate a random fee rate in the range of 100 - 400 | ||||
CFeeRate rate(int64_t(rand.randrange(300) + 100) * SATOSHI); | CFeeRate rate(int64_t(rand.randrange(300) + 100) * SATOSHI); | ||||
// Generate a random target value between 1000 and wallet balance | // Generate a random target value between 1000 and wallet balance | ||||
target = | Amount target = | ||||
int64_t(rand.randrange(balance / SATOSHI - 1000) + 1000) * SATOSHI; | int64_t(rand.randrange(balance / SATOSHI - 1000) + 1000) * SATOSHI; | ||||
// Perform selection | // Perform selection | ||||
CoinSelectionParams coin_selection_params_knapsack( | CoinSelectionParams coin_selection_params_knapsack( | ||||
false, 34, 148, CFeeRate(Amount::zero()), 0); | false, 34, 148, CFeeRate(Amount::zero()), 0); | ||||
CoinSelectionParams coin_selection_params_bnb( | CoinSelectionParams coin_selection_params_bnb( | ||||
true, 34, 148, CFeeRate(Amount::zero()), 0); | true, 34, 148, CFeeRate(Amount::zero()), 0); | ||||
CoinSet out_set; | |||||
Amount out_value = Amount::zero(); | |||||
bool bnb_used; | |||||
BOOST_CHECK(m_wallet.SelectCoinsMinConf( | BOOST_CHECK(m_wallet.SelectCoinsMinConf( | ||||
target, filter_standard, vCoins, out_set, out_value, | target, filter_standard, vCoins, out_set, out_value, | ||||
coin_selection_params_bnb, bnb_used) || | coin_selection_params_bnb, bnb_used) || | ||||
m_wallet.SelectCoinsMinConf( | m_wallet.SelectCoinsMinConf( | ||||
target, filter_standard, vCoins, out_set, out_value, | target, filter_standard, vCoins, out_set, out_value, | ||||
coin_selection_params_knapsack, bnb_used)); | coin_selection_params_knapsack, bnb_used)); | ||||
BOOST_CHECK_GE(out_value, target); | BOOST_CHECK_GE(out_value, target); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |