Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/coinselector_tests.cpp
Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | |||||
static void add_coin(const Amount nValue, int nInput, CoinSet &set) { | static void add_coin(const Amount nValue, int nInput, CoinSet &set) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vout.resize(nInput + 1); | tx.vout.resize(nInput + 1); | ||||
tx.vout[nInput].nValue = nValue; | tx.vout[nInput].nValue = nValue; | ||||
set.emplace(MakeTransactionRef(tx), nInput); | set.emplace(MakeTransactionRef(tx), nInput); | ||||
} | } | ||||
static void add_coin(CWallet &wallet, const Amount nValue, int nAge = 6 * 24, | static void add_coin(CWallet &wallet, const Amount nValue, int nAge = 6 * 24, | ||||
bool fIsFromMe = false, int nInput = 0, | bool fIsFromMe = false, int nInput = 0) { | ||||
bool spendable = false) { | |||||
balance += nValue; | balance += nValue; | ||||
static int nextLockTime = 0; | static int nextLockTime = 0; | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
// so all transactions get different hashes | // so all transactions get different hashes | ||||
tx.nLockTime = nextLockTime++; | tx.nLockTime = nextLockTime++; | ||||
tx.vout.resize(nInput + 1); | tx.vout.resize(nInput + 1); | ||||
tx.vout[nInput].nValue = nValue; | tx.vout[nInput].nValue = nValue; | ||||
if (spendable) { | |||||
CTxDestination dest; | |||||
std::string error; | |||||
assert(wallet.GetNewDestination(OutputType::LEGACY, "", dest, error)); | |||||
tx.vout[nInput].scriptPubKey = GetScriptForDestination(dest); | |||||
} | |||||
if (fIsFromMe) { | if (fIsFromMe) { | ||||
// IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if | // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if | ||||
// vin.empty(), so stop vin being empty, and cache a non-zero Debit to | // vin.empty(), so stop vin being empty, and cache a non-zero Debit to | ||||
// fake out IsFromMe() | // fake out IsFromMe() | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
} | } | ||||
auto wtx = | auto wtx = | ||||
std::make_unique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))); | std::make_unique<CWalletTx>(&wallet, MakeTransactionRef(std::move(tx))); | ||||
▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(bnb_search_test) { | ||||
add_coin(m_wallet, SATOSHI); | add_coin(m_wallet, SATOSHI); | ||||
// Make sure that it has a negative effective value. The next check should | // Make sure that it has a negative effective value. The next check should | ||||
// assert if this somehow got through. Otherwise it will fail | // assert if this somehow got through. Otherwise it will fail | ||||
vCoins.at(0).nInputBytes = 40; | vCoins.at(0).nInputBytes = 40; | ||||
BOOST_CHECK(!m_wallet.SelectCoinsMinConf( | BOOST_CHECK(!m_wallet.SelectCoinsMinConf( | ||||
1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, | 1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, | ||||
coin_selection_params_bnb, bnb_used)); | coin_selection_params_bnb, bnb_used)); | ||||
// Test fees subtracted from output: | // Make sure that we aren't using BnB when there are preset inputs | ||||
empty_wallet(); | |||||
add_coin(m_wallet, 1 * CENT); | |||||
vCoins.at(0).nInputBytes = 40; | |||||
BOOST_CHECK(!m_wallet.SelectCoinsMinConf( | |||||
1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, | |||||
coin_selection_params_bnb, bnb_used)); | |||||
coin_selection_params_bnb.m_subtract_fee_outputs = true; | |||||
BOOST_CHECK(m_wallet.SelectCoinsMinConf( | |||||
1 * CENT, filter_standard, GroupCoins(vCoins), setCoinsRet, nValueRet, | |||||
coin_selection_params_bnb, bnb_used)); | |||||
BOOST_CHECK_EQUAL(nValueRet, 1 * CENT); | |||||
// Make sure that can use BnB when there are preset inputs | |||||
empty_wallet(); | empty_wallet(); | ||||
{ | add_coin(m_wallet, 5 * CENT); | ||||
auto wallet = | add_coin(m_wallet, 3 * CENT); | ||||
std::make_unique<CWallet>(Params(), m_chain.get(), WalletLocation(), | add_coin(m_wallet, 2 * CENT); | ||||
WalletDatabase::CreateMock()); | |||||
bool firstRun; | |||||
wallet->LoadWallet(firstRun); | |||||
LOCK(wallet->cs_wallet); | |||||
add_coin(*wallet, 5 * CENT, 6 * 24, false, 0, true); | |||||
add_coin(*wallet, 3 * CENT, 6 * 24, false, 0, true); | |||||
add_coin(*wallet, 2 * CENT, 6 * 24, false, 0, true); | |||||
CCoinControl coin_control; | CCoinControl coin_control; | ||||
coin_control.fAllowOtherInputs = true; | coin_control.fAllowOtherInputs = true; | ||||
coin_control.Select( | coin_control.Select(COutPoint(vCoins.at(0).tx->GetId(), vCoins.at(0).i)); | ||||
COutPoint(vCoins.at(0).tx->GetId(), vCoins.at(0).i)); | BOOST_CHECK(m_wallet.SelectCoins(vCoins, 10 * CENT, setCoinsRet, nValueRet, | ||||
coin_selection_params_bnb.effective_fee = CFeeRate(Amount::zero()); | coin_control, coin_selection_params_bnb, | ||||
BOOST_CHECK(wallet->SelectCoins(vCoins, 10 * CENT, setCoinsRet, | bnb_used)); | ||||
nValueRet, coin_control, | BOOST_CHECK(!bnb_used); | ||||
coin_selection_params_bnb, bnb_used)); | BOOST_CHECK(!coin_selection_params_bnb.use_bnb); | ||||
BOOST_CHECK(bnb_used); | |||||
BOOST_CHECK(coin_selection_params_bnb.use_bnb); | |||||
} | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(knapsack_solver_test) { | BOOST_AUTO_TEST_CASE(knapsack_solver_test) { | ||||
auto testChain = interfaces::MakeChain(testNode, Params()); | auto testChain = interfaces::MakeChain(testNode, Params()); | ||||
CWallet testWallet(Params(), testChain.get(), WalletLocation(), | CWallet testWallet(Params(), testChain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
CoinSet setCoinsRet, setCoinsRet2; | CoinSet setCoinsRet, setCoinsRet2; | ||||
▲ Show 20 Lines • Show All 469 Lines • Show Last 20 Lines |