Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 380 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(*m_locked_chain, {recipient}, tx, | { | ||||
reservekey, fee, changePos, error, | auto locked_chain = m_chain->lock(); | ||||
dummy)); | BOOST_CHECK(wallet->CreateTransaction(*locked_chain, {recipient}, | ||||
tx, reservekey, fee, | |||||
changePos, error, dummy)); | |||||
} | |||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, reservekey, state)); | BOOST_CHECK(wallet->CommitTransaction(tx, {}, {}, reservekey, 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); | ||||
} | } | ||||
CreateAndProcessBlock({CMutableTransaction(blocktx)}, | CreateAndProcessBlock({CMutableTransaction(blocktx)}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
auto it = wallet->mapWallet.find(tx->GetId()); | auto it = wallet->mapWallet.find(tx->GetId()); | ||||
BOOST_CHECK(it != wallet->mapWallet.end()); | BOOST_CHECK(it != wallet->mapWallet.end()); | ||||
it->second.SetMerkleBranch(::ChainActive().Tip()->GetBlockHash(), 1); | it->second.SetMerkleBranch(::ChainActive().Tip()->GetBlockHash(), 1); | ||||
return it->second; | return it->second; | ||||
} | } | ||||
std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain(); | std::unique_ptr<interfaces::Chain> m_chain = interfaces::MakeChain(); | ||||
// Temporary. Removed in upcoming lock cleanup | |||||
std::unique_ptr<interfaces::Chain::Lock> m_locked_chain = | |||||
m_chain->assumeLocked(); | |||||
std::unique_ptr<CWallet> wallet; | std::unique_ptr<CWallet> wallet; | ||||
}; | }; | ||||
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); | auto locked_chain = m_chain->lock(); | ||||
list = wallet->ListCoins(*m_locked_chain); | LOCK(wallet->cs_wallet); | ||||
list = wallet->ListCoins(*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); | auto locked_chain = m_chain->lock(); | ||||
list = wallet->ListCoins(*m_locked_chain); | LOCK(wallet->cs_wallet); | ||||
list = wallet->ListCoins(*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); | auto locked_chain = m_chain->lock(); | ||||
LOCK(wallet->cs_wallet); | |||||
std::vector<COutput> available; | std::vector<COutput> available; | ||||
wallet->AvailableCoins(*m_locked_chain, available); | wallet->AvailableCoins(*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); | auto locked_chain = m_chain->lock(); | ||||
LOCK(wallet->cs_wallet); | |||||
std::vector<COutput> available; | std::vector<COutput> available; | ||||
wallet->AvailableCoins(*m_locked_chain, available); | wallet->AvailableCoins(*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); | auto locked_chain = m_chain->lock(); | ||||
list = wallet->ListCoins(*m_locked_chain); | LOCK(wallet->cs_wallet); | ||||
list = wallet->ListCoins(*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 |