Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 213 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | ||||
CWalletTx wtx(&wallet, m_coinbase_txns.back()); | CWalletTx wtx(&wallet, m_coinbase_txns.back()); | ||||
auto locked_chain = chain->lock(); | auto locked_chain = chain->lock(); | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wtx.hashBlock = chainActive.Tip()->GetBlockHash(); | wtx.hashBlock = chainActive.Tip()->GetBlockHash(); | ||||
wtx.nIndex = 0; | wtx.nIndex = 0; | ||||
// Call GetImmatureCredit() once before adding the key to the wallet to | // Call GetImmatureCredit() once before adding the key to the wallet to | ||||
// cache the current immature credit amount, which is 0. | // cache the current immature credit amount, which is 0. | ||||
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), Amount::zero()); | BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(*locked_chain), Amount::zero()); | ||||
// Invalidate the cached value, add the key, and make sure a new immature | // Invalidate the cached value, add the key, and make sure a new immature | ||||
// credit amount is calculated. | // credit amount is calculated. | ||||
wtx.MarkDirty(); | wtx.MarkDirty(); | ||||
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | ||||
BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(), 50 * COIN); | BOOST_CHECK_EQUAL(wtx.GetImmatureCredit(*locked_chain), 50 * COIN); | ||||
} | } | ||||
static int64_t AddTx(CWallet &wallet, uint32_t lockTime, int64_t mockTime, | static int64_t AddTx(CWallet &wallet, uint32_t lockTime, int64_t mockTime, | ||||
int64_t blockTime) { | int64_t blockTime) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.nLockTime = lockTime; | tx.nLockTime = lockTime; | ||||
SetMockTime(mockTime); | SetMockTime(mockTime); | ||||
CBlockIndex *block = nullptr; | CBlockIndex *block = nullptr; | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | public: | ||||
CWalletTx &AddTx(CRecipient recipient) { | CWalletTx &AddTx(CRecipient recipient) { | ||||
CTransactionRef tx; | CTransactionRef tx; | ||||
CReserveKey reservekey(wallet.get()); | CReserveKey reservekey(wallet.get()); | ||||
Amount fee; | Amount fee; | ||||
int changePos = -1; | int changePos = -1; | ||||
std::string error; | std::string error; | ||||
CCoinControl dummy; | CCoinControl dummy; | ||||
BOOST_CHECK(wallet->CreateTransaction({recipient}, tx, reservekey, fee, | BOOST_CHECK(wallet->CreateTransaction(*m_locked_chain, {recipient}, tx, | ||||
changePos, error, dummy)); | reservekey, fee, changePos, error, | ||||
dummy)); | |||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, {}, reservekey, | BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, {}, reservekey, | ||||
nullptr, state)); | nullptr, state)); | ||||
CMutableTransaction blocktx; | CMutableTransaction blocktx; | ||||
{ | { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
blocktx = | blocktx = | ||||
CMutableTransaction(*wallet->mapWallet.at(tx->GetId()).tx); | CMutableTransaction(*wallet->mapWallet.at(tx->GetId()).tx); | ||||
Show All 17 Lines | |||||
BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup) { | BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup) { | ||||
std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString(); | std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString(); | ||||
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey | // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey | ||||
// address. | // address. | ||||
std::map<CTxDestination, std::vector<COutput>> list; | std::map<CTxDestination, std::vector<COutput>> list; | ||||
{ | { | ||||
LOCK2(cs_main, wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
list = wallet->ListCoins(); | list = wallet->ListCoins(*m_locked_chain); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(list.size(), 1U); | BOOST_CHECK_EQUAL(list.size(), 1U); | ||||
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | ||||
coinbaseAddress); | coinbaseAddress); | ||||
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U); | ||||
// Check initial balance from one mature coinbase transaction. | // Check initial balance from one mature coinbase transaction. | ||||
BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance()); | BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance()); | ||||
// Add a transaction creating a change address, and confirm ListCoins still | // Add a transaction creating a change address, and confirm ListCoins still | ||||
// returns the coin associated with the change address underneath the | // returns the coin associated with the change address underneath the | ||||
// coinbaseKey pubkey, even though the change address has a different | // coinbaseKey pubkey, even though the change address has a different | ||||
// pubkey. | // pubkey. | ||||
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, | AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, | ||||
false /* subtract fee */}); | false /* subtract fee */}); | ||||
{ | { | ||||
LOCK2(cs_main, wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
list = wallet->ListCoins(); | list = wallet->ListCoins(*m_locked_chain); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(list.size(), 1U); | BOOST_CHECK_EQUAL(list.size(), 1U); | ||||
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | ||||
coinbaseAddress); | coinbaseAddress); | ||||
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); | ||||
// Lock both coins. Confirm number of available coins drops to 0. | // Lock both coins. Confirm number of available coins drops to 0. | ||||
{ | { | ||||
LOCK2(cs_main, wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
std::vector<COutput> available; | std::vector<COutput> available; | ||||
wallet->AvailableCoins(available); | wallet->AvailableCoins(*m_locked_chain, available); | ||||
BOOST_CHECK_EQUAL(available.size(), 2U); | BOOST_CHECK_EQUAL(available.size(), 2U); | ||||
} | } | ||||
for (const auto &group : list) { | for (const auto &group : list) { | ||||
for (const auto &coin : group.second) { | for (const auto &coin : group.second) { | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
wallet->LockCoin(COutPoint(coin.tx->GetId(), coin.i)); | wallet->LockCoin(COutPoint(coin.tx->GetId(), coin.i)); | ||||
} | } | ||||
} | } | ||||
{ | { | ||||
LOCK2(cs_main, wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
std::vector<COutput> available; | std::vector<COutput> available; | ||||
wallet->AvailableCoins(available); | wallet->AvailableCoins(*m_locked_chain, available); | ||||
BOOST_CHECK_EQUAL(available.size(), 0U); | BOOST_CHECK_EQUAL(available.size(), 0U); | ||||
} | } | ||||
// Confirm ListCoins still returns same result as before, despite coins | // Confirm ListCoins still returns same result as before, despite coins | ||||
// being locked. | // being locked. | ||||
{ | { | ||||
LOCK2(cs_main, wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
list = wallet->ListCoins(); | list = wallet->ListCoins(*m_locked_chain); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(list.size(), 1U); | BOOST_CHECK_EQUAL(list.size(), 1U); | ||||
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), | ||||
coinbaseAddress); | coinbaseAddress); | ||||
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U); | ||||
} | } | ||||
BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(wallet_disableprivkeys, TestChain100Setup) { | ||||
Show All 11 Lines |