Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 455 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static void AddKey(CWallet &wallet, const CKey &key) { | static void AddKey(CWallet &wallet, const CKey &key) { | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.AddKeyPubKey(key, key.GetPubKey()); | wallet.AddKeyPubKey(key, key.GetPubKey()); | ||||
} | } | ||||
BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { | ||||
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(); | ||||
LOCK(cs_main); | |||||
// 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()); | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
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); | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { | ||||
} | } | ||||
} | } | ||||
// Verify importwallet RPC starts rescan at earliest block with timestamp | // Verify importwallet RPC starts rescan at earliest block with timestamp | ||||
// greater or equal than key birthday. Previously there was a bug where | // greater or equal than key birthday. Previously there was a bug where | ||||
// importwallet RPC would start the scan at the latest block with timestamp less | // importwallet RPC would start the scan at the latest block with timestamp less | ||||
// than or equal to key birthday. | // than or equal to key birthday. | ||||
BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { | ||||
LOCK(cs_main); | |||||
// Create two blocks with same timestamp to verify that importwallet rescan | // Create two blocks with same timestamp to verify that importwallet rescan | ||||
// will pick up both blocks, not just the first. | // will pick up both blocks, not just the first. | ||||
const int64_t BLOCK_TIME = chainActive.Tip()->GetBlockTimeMax() + 5; | const int64_t BLOCK_TIME = chainActive.Tip()->GetBlockTimeMax() + 5; | ||||
SetMockTime(BLOCK_TIME); | SetMockTime(BLOCK_TIME); | ||||
coinbaseTxns.emplace_back( | coinbaseTxns.emplace_back( | ||||
*CreateAndProcessBlock({}, | *CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
coinbaseTxns.emplace_back( | coinbaseTxns.emplace_back( | ||||
*CreateAndProcessBlock({}, | *CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
// Set key birthday to block time increased by the timestamp window, so | // Set key birthday to block time increased by the timestamp window, so | ||||
// rescan will start at the block time. | // rescan will start at the block time. | ||||
const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW; | const int64_t KEY_TIME = BLOCK_TIME + TIMESTAMP_WINDOW; | ||||
SetMockTime(KEY_TIME); | SetMockTime(KEY_TIME); | ||||
coinbaseTxns.emplace_back( | coinbaseTxns.emplace_back( | ||||
*CreateAndProcessBlock({}, | *CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | GetScriptForRawPubKey(coinbaseKey.GetPubKey())) | ||||
.vtx[0]); | .vtx[0]); | ||||
LOCK(cs_main); | |||||
// Import key into wallet and call dumpwallet to create backup file. | // Import key into wallet and call dumpwallet to create backup file. | ||||
{ | { | ||||
CWallet wallet(Params()); | CWallet wallet(Params()); | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = | wallet.mapKeyMetadata[coinbaseKey.GetPubKey().GetID()].nCreateTime = | ||||
KEY_TIME; | KEY_TIME; | ||||
wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | wallet.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | CWalletTx &AddTx(CRecipient recipient) { | ||||
CReserveKey reservekey(wallet.get()); | CReserveKey reservekey(wallet.get()); | ||||
Amount fee; | Amount fee; | ||||
int changePos = -1; | int changePos = -1; | ||||
std::string error; | std::string error; | ||||
BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, | BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, | ||||
changePos, error)); | changePos, error)); | ||||
CValidationState state; | CValidationState state; | ||||
BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state)); | BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state)); | ||||
CMutableTransaction blocktx; | |||||
{ | |||||
LOCK(wallet->cs_wallet); | |||||
blocktx = | |||||
CMutableTransaction(*wallet->mapWallet.at(wtx.GetId()).tx); | |||||
} | |||||
CreateAndProcessBlock({CMutableTransaction(blocktx)}, | |||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | |||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
auto it = wallet->mapWallet.find(wtx.GetId()); | auto it = wallet->mapWallet.find(wtx.GetId()); | ||||
BOOST_CHECK(it != wallet->mapWallet.end()); | BOOST_CHECK(it != wallet->mapWallet.end()); | ||||
CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, | |||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | |||||
it->second.SetMerkleBranch(chainActive.Tip(), 1); | it->second.SetMerkleBranch(chainActive.Tip(), 1); | ||||
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(); | ||||
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 14 Lines | BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup) { | ||||
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 2); | ||||
// Lock both coins. Confirm number of available coins drops to 0. | // Lock both coins. Confirm number of available coins drops to 0. | ||||
std::vector<COutput> available; | std::vector<COutput> available; | ||||
wallet->AvailableCoins(available); | wallet->AvailableCoins(available); | ||||
BOOST_CHECK_EQUAL(available.size(), 2); | BOOST_CHECK_EQUAL(available.size(), 2); | ||||
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); | |||||
wallet->LockCoin(COutPoint(coin.tx->GetId(), coin.i)); | wallet->LockCoin(COutPoint(coin.tx->GetId(), coin.i)); | ||||
} | } | ||||
} | } | ||||
wallet->AvailableCoins(available); | wallet->AvailableCoins(available); | ||||
BOOST_CHECK_EQUAL(available.size(), 0); | BOOST_CHECK_EQUAL(available.size(), 0); | ||||
// Confirm ListCoins still returns same result as before, despite coins | // Confirm ListCoins still returns same result as before, despite coins | ||||
// being locked. | // being locked. | ||||
list = wallet->ListCoins(); | 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(), 2); | BOOST_CHECK_EQUAL(list.begin()->second.size(), 2); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |