Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show All 29 Lines | 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(scan_for_wallet_transactions, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) { | ||||
auto chain = interfaces::MakeChain(); | auto chain = interfaces::MakeChain(); | ||||
// 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. | ||||
const CBlockIndex *const null_block = 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(); | ||||
LockAnnotation lock(::cs_main); | LockAnnotation lock(::cs_main); | ||||
auto locked_chain = chain->lock(); | auto locked_chain = chain->lock(); | ||||
// Verify ScanForWalletTransactions accommodates a null start block. | // Verify ScanForWalletTransactions accommodates a null start block. | ||||
{ | { | ||||
CWallet wallet(Params(), *chain, WalletLocation(), | CWallet wallet(Params(), *chain, WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(&wallet); | WalletRescanReserver reserver(&wallet); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
const CBlockIndex *stop_block = null_block + 1, | CWallet::ScanResult result = wallet.ScanForWalletTransactions( | ||||
*failed_block = null_block + 1; | BlockHash(), BlockHash(), reserver, false /* update */); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); | ||||
wallet.ScanForWalletTransactions(nullptr, nullptr, reserver, | BOOST_CHECK(result.failed_block.IsNull()); | ||||
failed_block, stop_block), | BOOST_CHECK(result.stop_block.IsNull()); | ||||
CWallet::ScanResult::SUCCESS); | BOOST_CHECK(!result.stop_height); | ||||
BOOST_CHECK_EQUAL(failed_block, null_block); | |||||
BOOST_CHECK_EQUAL(stop_block, null_block); | |||||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), Amount::zero()); | BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), Amount::zero()); | ||||
} | } | ||||
// 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(), *chain, WalletLocation(), | CWallet wallet(Params(), *chain, WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(&wallet); | WalletRescanReserver reserver(&wallet); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
const CBlockIndex *stop_block = null_block + 1, | CWallet::ScanResult result = wallet.ScanForWalletTransactions( | ||||
*failed_block = null_block + 1; | oldTip->GetBlockHash(), BlockHash(), reserver, false /* update */); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); | ||||
wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, | BOOST_CHECK(result.failed_block.IsNull()); | ||||
failed_block, stop_block), | BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash()); | ||||
CWallet::ScanResult::SUCCESS); | BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight); | ||||
BOOST_CHECK_EQUAL(failed_block, null_block); | |||||
BOOST_CHECK_EQUAL(stop_block, newTip); | |||||
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(), *chain, WalletLocation(), | CWallet wallet(Params(), *chain, WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(&wallet); | WalletRescanReserver reserver(&wallet); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
const CBlockIndex *stop_block = null_block + 1, | CWallet::ScanResult result = wallet.ScanForWalletTransactions( | ||||
*failed_block = null_block + 1; | oldTip->GetBlockHash(), BlockHash(), reserver, false /* update */); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); | ||||
wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, | BOOST_CHECK_EQUAL(result.failed_block, oldTip->GetBlockHash()); | ||||
failed_block, stop_block), | BOOST_CHECK_EQUAL(result.stop_block, newTip->GetBlockHash()); | ||||
CWallet::ScanResult::FAILURE); | BOOST_CHECK_EQUAL(*result.stop_height, newTip->nHeight); | ||||
BOOST_CHECK_EQUAL(failed_block, oldTip); | |||||
BOOST_CHECK_EQUAL(stop_block, newTip); | |||||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN); | BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 50 * COIN); | ||||
} | } | ||||
// Prune the remaining block file. | // Prune the remaining block file. | ||||
PruneOneBlockFile(newTip->GetBlockPos().nFile); | PruneOneBlockFile(newTip->GetBlockPos().nFile); | ||||
UnlinkPrunedFiles({newTip->GetBlockPos().nFile}); | UnlinkPrunedFiles({newTip->GetBlockPos().nFile}); | ||||
// Verify ScanForWalletTransactions scans no blocks. | // Verify ScanForWalletTransactions scans no blocks. | ||||
{ | { | ||||
CWallet wallet(Params(), *chain, WalletLocation(), | CWallet wallet(Params(), *chain, WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(&wallet); | WalletRescanReserver reserver(&wallet); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
const CBlockIndex *stop_block = null_block + 1, | CWallet::ScanResult result = wallet.ScanForWalletTransactions( | ||||
*failed_block = null_block + 1; | oldTip->GetBlockHash(), BlockHash(), reserver, false /* update */); | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); | ||||
wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, | BOOST_CHECK_EQUAL(result.failed_block, newTip->GetBlockHash()); | ||||
failed_block, stop_block), | BOOST_CHECK(result.stop_block.IsNull()); | ||||
CWallet::ScanResult::FAILURE); | BOOST_CHECK(!result.stop_height); | ||||
BOOST_CHECK_EQUAL(failed_block, newTip); | |||||
BOOST_CHECK_EQUAL(stop_block, null_block); | |||||
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), Amount::zero()); | BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), Amount::zero()); | ||||
} | } | ||||
} | } | ||||
BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) { | ||||
auto chain = interfaces::MakeChain(); | auto chain = interfaces::MakeChain(); | ||||
// 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. | ||||
▲ Show 20 Lines • Show All 176 Lines • ▼ Show 20 Lines | if (blockTime > 0) { | ||||
const BlockHash &hash = inserted.first->first; | const BlockHash &hash = inserted.first->first; | ||||
block = inserted.first->second; | block = inserted.first->second; | ||||
block->nTime = blockTime; | block->nTime = blockTime; | ||||
block->phashBlock = &hash; | block->phashBlock = &hash; | ||||
} | } | ||||
CWalletTx wtx(&wallet, MakeTransactionRef(tx)); | CWalletTx wtx(&wallet, MakeTransactionRef(tx)); | ||||
if (block) { | if (block) { | ||||
wtx.SetMerkleBranch(block, 0); | wtx.SetMerkleBranch(block->GetBlockHash(), 0); | ||||
} | } | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
wallet.AddToWallet(wtx); | wallet.AddToWallet(wtx); | ||||
} | } | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
return wallet.mapWallet.at(wtx.GetId()).nTimeSmart; | return wallet.mapWallet.at(wtx.GetId()).nTimeSmart; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | ListCoinsTestingSetup() { | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
wallet = std::make_unique<CWallet>(Params(), *m_chain, WalletLocation(), | wallet = std::make_unique<CWallet>(Params(), *m_chain, WalletLocation(), | ||||
WalletDatabase::CreateMock()); | WalletDatabase::CreateMock()); | ||||
bool firstRun; | bool firstRun; | ||||
wallet->LoadWallet(firstRun); | wallet->LoadWallet(firstRun); | ||||
AddKey(*wallet, coinbaseKey); | AddKey(*wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(wallet.get()); | WalletRescanReserver reserver(wallet.get()); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
const CBlockIndex *const null_block = nullptr; | CWallet::ScanResult result = wallet->ScanForWalletTransactions( | ||||
const CBlockIndex *stop_block = null_block + 1, | ::ChainActive().Genesis()->GetBlockHash(), BlockHash(), reserver, | ||||
*failed_block = null_block + 1; | false /* update */); | ||||
BOOST_CHECK_EQUAL(wallet->ScanForWalletTransactions( | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::SUCCESS); | ||||
ChainActive().Genesis(), nullptr, reserver, | BOOST_CHECK_EQUAL(result.stop_block, | ||||
failed_block, stop_block), | ::ChainActive().Tip()->GetBlockHash()); | ||||
CWallet::ScanResult::SUCCESS); | BOOST_CHECK_EQUAL(*result.stop_height, ::ChainActive().Height()); | ||||
BOOST_CHECK_EQUAL(stop_block, ChainActive().Tip()); | BOOST_CHECK(result.failed_block.IsNull()); | ||||
BOOST_CHECK_EQUAL(failed_block, null_block); | |||||
} | } | ||||
~ListCoinsTestingSetup() { wallet.reset(); } | ~ListCoinsTestingSetup() { wallet.reset(); } | ||||
CWalletTx &AddTx(CRecipient recipient) { | CWalletTx &AddTx(CRecipient recipient) { | ||||
CTransactionRef tx; | CTransactionRef tx; | ||||
CReserveKey reservekey(wallet.get()); | CReserveKey reservekey(wallet.get()); | ||||
Amount fee; | Amount fee; | ||||
Show All 12 Lines | CWalletTx &AddTx(CRecipient recipient) { | ||||
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(), 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 | // Temporary. Removed in upcoming lock cleanup | ||||
std::unique_ptr<interfaces::Chain::Lock> m_locked_chain = | std::unique_ptr<interfaces::Chain::Lock> m_locked_chain = | ||||
m_chain->assumeLocked(); | m_chain->assumeLocked(); | ||||
std::unique_ptr<CWallet> wallet; | std::unique_ptr<CWallet> wallet; | ||||
▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines |