Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 449 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(ApproximateBestSubset) { | ||||
BOOST_CHECK(wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, | BOOST_CHECK(wallet.SelectCoinsMinConf(1003 * COIN, 1, 6, 0, vCoins, | ||||
setCoinsRet, nValueRet)); | setCoinsRet, nValueRet)); | ||||
BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN); | BOOST_CHECK_EQUAL(nValueRet, 1003 * COIN); | ||||
BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); | BOOST_CHECK_EQUAL(setCoinsRet.size(), 2U); | ||||
empty_wallet(); | empty_wallet(); | ||||
} | } | ||||
static void AddKey(CWallet &wallet, const CKey &key) { | |||||
LOCK(wallet.cs_wallet); | |||||
wallet.AddKeyPubKey(key, key.GetPubKey()); | |||||
} | |||||
BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
// Cap last block file size, and mine new block in a new block file. | // Cap last block file size, and mine new block in a new block file. | ||||
CBlockIndex *const nullBlock = nullptr; | CBlockIndex *const nullBlock = nullptr; | ||||
CBlockIndex *oldTip = chainActive.Tip(); | CBlockIndex *oldTip = chainActive.Tip(); | ||||
GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; | GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; | ||||
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
CBlockIndex *newTip = chainActive.Tip(); | CBlockIndex *newTip = chainActive.Tip(); | ||||
// Verify ScanForWalletTransactions picks up transactions in both the old | // Verify ScanForWalletTransactions picks up transactions in both the old | ||||
// and new block files. | // and new block files. | ||||
{ | { | ||||
CWallet wallet(Params()); | CWallet wallet(Params()); | ||||
LOCK(wallet.cs_wallet); | AddKey(wallet, coinbaseKey); | ||||
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | |||||
BOOST_CHECK_EQUAL(nullBlock, | BOOST_CHECK_EQUAL(nullBlock, | ||||
wallet.ScanForWalletTransactions(oldTip, nullptr)); | wallet.ScanForWalletTransactions(oldTip, nullptr)); | ||||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 100 * COIN); | BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 100 * COIN); | ||||
} | } | ||||
// Prune the older block file. | // Prune the older block file. | ||||
PruneOneBlockFile(oldTip->GetBlockPos().nFile); | PruneOneBlockFile(oldTip->GetBlockPos().nFile); | ||||
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); | UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); | ||||
// Verify ScanForWalletTransactions only picks transactions in the new block | // Verify ScanForWalletTransactions only picks transactions in the new block | ||||
// file. | // file. | ||||
{ | { | ||||
CWallet wallet(Params()); | CWallet wallet(Params()); | ||||
LOCK(wallet.cs_wallet); | AddKey(wallet, coinbaseKey); | ||||
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | |||||
BOOST_CHECK_EQUAL(oldTip, | BOOST_CHECK_EQUAL(oldTip, | ||||
wallet.ScanForWalletTransactions(oldTip, nullptr)); | wallet.ScanForWalletTransactions(oldTip, nullptr)); | ||||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN); | BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN); | ||||
} | } | ||||
// Verify importmulti RPC returns failure for a key whose creation time is | // Verify importmulti RPC returns failure for a key whose creation time is | ||||
// before the missing block, and success for a key whose creation time is | // before the missing block, and success for a key whose creation time is | ||||
// after. | // after. | ||||
▲ Show 20 Lines • Show All 208 Lines • ▼ Show 20 Lines | ListCoinsTestingSetup() { | ||||
CreateAndProcessBlock({}, | CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
::bitdb.MakeMock(); | ::bitdb.MakeMock(); | ||||
wallet.reset(new CWallet( | wallet.reset(new CWallet( | ||||
Params(), std::unique_ptr<CWalletDBWrapper>( | Params(), std::unique_ptr<CWalletDBWrapper>( | ||||
new CWalletDBWrapper(&bitdb, "wallet_test.dat")))); | new CWalletDBWrapper(&bitdb, "wallet_test.dat")))); | ||||
bool firstRun; | bool firstRun; | ||||
wallet->LoadWallet(firstRun); | wallet->LoadWallet(firstRun); | ||||
LOCK(wallet->cs_wallet); | AddKey(*wallet, coinbaseKey); | ||||
wallet->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | |||||
wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr); | wallet->ScanForWalletTransactions(chainActive.Genesis(), nullptr); | ||||
} | } | ||||
~ListCoinsTestingSetup() { | ~ListCoinsTestingSetup() { | ||||
wallet.reset(); | wallet.reset(); | ||||
::bitdb.Flush(true); | ::bitdb.Flush(true); | ||||
::bitdb.Reset(); | ::bitdb.Reset(); | ||||
} | } | ||||
Show All 17 Lines | CWalletTx &AddTx(CRecipient recipient) { | ||||
return it->second; | return it->second; | ||||
} | } | ||||
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(); | ||||
LOCK(wallet->cs_wallet); | LOCK2(cs_main, wallet->cs_wallet); | ||||
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey | // Confirm ListCoins initially returns 1 coin grouped under coinbaseKey | ||||
// address. | // address. | ||||
auto list = wallet->ListCoins(); | auto list = wallet->ListCoins(); | ||||
BOOST_CHECK_EQUAL(list.size(), 1); | BOOST_CHECK_EQUAL(list.size(), 1); | ||||
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(), 1); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 1); | ||||
Show All 38 Lines |