Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/test/wallet_tests.cpp
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) { | ||||
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
CBlockIndex *newTip = ::ChainActive().Tip(); | CBlockIndex *newTip = ::ChainActive().Tip(); | ||||
NodeContext node; | NodeContext node; | ||||
auto chain = interfaces::MakeChain(node, Params()); | auto chain = interfaces::MakeChain(node, Params()); | ||||
// Verify ScanForWalletTransactions fails to read an unknown start block. | // Verify ScanForWalletTransactions fails to read an unknown start block. | ||||
{ | { | ||||
CWallet wallet(Params(), chain.get(), WalletLocation(), | CWallet wallet(chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
{ | { | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), | wallet.SetLastBlockProcessed(::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(wallet); | WalletRescanReserver reserver(wallet); | ||||
reserver.reserve(); | reserver.reserve(); | ||||
CWallet::ScanResult result = wallet.ScanForWalletTransactions( | CWallet::ScanResult result = wallet.ScanForWalletTransactions( | ||||
BlockHash() /* start_block */, 0 /* start_height */, | BlockHash() /* start_block */, 0 /* start_height */, | ||||
{} /* max_height */, reserver, false /* update */); | {} /* max_height */, reserver, false /* update */); | ||||
BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); | BOOST_CHECK_EQUAL(result.status, CWallet::ScanResult::FAILURE); | ||||
BOOST_CHECK(result.last_failed_block.IsNull()); | BOOST_CHECK(result.last_failed_block.IsNull()); | ||||
BOOST_CHECK(result.last_scanned_block.IsNull()); | BOOST_CHECK(result.last_scanned_block.IsNull()); | ||||
BOOST_CHECK(!result.last_scanned_height); | BOOST_CHECK(!result.last_scanned_height); | ||||
BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, Amount::zero()); | BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 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.get(), WalletLocation(), | CWallet wallet(chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
{ | { | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), | wallet.SetLastBlockProcessed(::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(wallet); | WalletRescanReserver reserver(wallet); | ||||
Show All 13 Lines | // Prune the older block file. | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile); | Assert(m_node.chainman)->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.get(), WalletLocation(), | CWallet wallet(chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
{ | { | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), | wallet.SetLastBlockProcessed(::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(wallet); | WalletRescanReserver reserver(wallet); | ||||
Show All 12 Lines | BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) { | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
Assert(m_node.chainman)->PruneOneBlockFile(newTip->GetBlockPos().nFile); | Assert(m_node.chainman)->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.get(), WalletLocation(), | CWallet wallet(chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
{ | { | ||||
LOCK(wallet.cs_wallet); | LOCK(wallet.cs_wallet); | ||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), | wallet.SetLastBlockProcessed(::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
AddKey(wallet, coinbaseKey); | AddKey(wallet, coinbaseKey); | ||||
WalletRescanReserver reserver(wallet); | WalletRescanReserver reserver(wallet); | ||||
Show All 25 Lines | // Prune the older block file. | ||||
Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile); | Assert(m_node.chainman)->PruneOneBlockFile(oldTip->GetBlockPos().nFile); | ||||
} | } | ||||
UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); | UnlinkPrunedFiles({oldTip->GetBlockPos().nFile}); | ||||
// 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. | ||||
{ | { | ||||
std::shared_ptr<CWallet> wallet = | std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>( | ||||
std::make_shared<CWallet>(Params(), chain.get(), WalletLocation(), | chain.get(), WalletLocation(), WalletDatabase::CreateDummy()); | ||||
WalletDatabase::CreateDummy()); | |||||
wallet->SetupLegacyScriptPubKeyMan(); | wallet->SetupLegacyScriptPubKeyMan(); | ||||
WITH_LOCK(wallet->cs_wallet, | WITH_LOCK(wallet->cs_wallet, | ||||
wallet->SetLastBlockProcessed(newTip->nHeight, | wallet->SetLastBlockProcessed(newTip->nHeight, | ||||
newTip->GetBlockHash())); | newTip->GetBlockHash())); | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
UniValue keys; | UniValue keys; | ||||
keys.setArray(); | keys.setArray(); | ||||
UniValue key; | UniValue key; | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup) { | ||||
NodeContext node; | NodeContext node; | ||||
auto chain = interfaces::MakeChain(node, Params()); | auto chain = interfaces::MakeChain(node, Params()); | ||||
std::string backup_file = (GetDataDir() / "wallet.backup").string(); | std::string backup_file = (GetDataDir() / "wallet.backup").string(); | ||||
// Import key into wallet and call dumpwallet to create backup file. | // Import key into wallet and call dumpwallet to create backup file. | ||||
{ | { | ||||
std::shared_ptr<CWallet> wallet = | std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>( | ||||
std::make_shared<CWallet>(Params(), chain.get(), WalletLocation(), | chain.get(), WalletLocation(), WalletDatabase::CreateDummy()); | ||||
WalletDatabase::CreateDummy()); | |||||
{ | { | ||||
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan(); | auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan(); | ||||
LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore); | LOCK2(wallet->cs_wallet, spk_man->cs_KeyStore); | ||||
spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()] | spk_man->mapKeyMetadata[coinbaseKey.GetPubKey().GetID()] | ||||
.nCreateTime = KEY_TIME; | .nCreateTime = KEY_TIME; | ||||
spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | spk_man->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey()); | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
wallet->SetLastBlockProcessed( | wallet->SetLastBlockProcessed( | ||||
::ChainActive().Height(), | ::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
util::Ref context; | util::Ref context; | ||||
JSONRPCRequest request(context); | JSONRPCRequest request(context); | ||||
request.params.setArray(); | request.params.setArray(); | ||||
request.params.push_back(backup_file); | request.params.push_back(backup_file); | ||||
::dumpwallet(GetConfig(), request); | ::dumpwallet(GetConfig(), request); | ||||
RemoveWallet(wallet); | RemoveWallet(wallet); | ||||
} | } | ||||
// Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME | // Call importwallet RPC and verify all blocks with timestamps >= BLOCK_TIME | ||||
// were scanned, and no prior blocks were scanned. | // were scanned, and no prior blocks were scanned. | ||||
{ | { | ||||
std::shared_ptr<CWallet> wallet = | std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>( | ||||
std::make_shared<CWallet>(Params(), chain.get(), WalletLocation(), | chain.get(), WalletLocation(), WalletDatabase::CreateDummy()); | ||||
WalletDatabase::CreateDummy()); | |||||
LOCK(wallet->cs_wallet); | LOCK(wallet->cs_wallet); | ||||
wallet->SetupLegacyScriptPubKeyMan(); | wallet->SetupLegacyScriptPubKeyMan(); | ||||
util::Ref context; | util::Ref context; | ||||
JSONRPCRequest request(context); | JSONRPCRequest request(context); | ||||
request.params.setArray(); | request.params.setArray(); | ||||
request.params.push_back(backup_file); | request.params.push_back(backup_file); | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
Show All 16 Lines | |||||
// the cached value after a MarkDirty() call. | // the cached value after a MarkDirty() call. | ||||
// | // | ||||
// This is a regression test written to verify a bugfix for the immature credit | // This is a regression test written to verify a bugfix for the immature credit | ||||
// function. Similar tests probably should be written for the other credit and | // function. Similar tests probably should be written for the other credit and | ||||
// debit functions. | // debit functions. | ||||
BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | BOOST_FIXTURE_TEST_CASE(coin_mark_dirty_immature_credit, TestChain100Setup) { | ||||
NodeContext node; | NodeContext node; | ||||
auto chain = interfaces::MakeChain(node, Params()); | auto chain = interfaces::MakeChain(node, Params()); | ||||
CWallet wallet(Params(), chain.get(), WalletLocation(), | CWallet wallet(chain.get(), WalletLocation(), | ||||
WalletDatabase::CreateDummy()); | WalletDatabase::CreateDummy()); | ||||
auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan(); | auto spk_man = wallet.GetOrCreateLegacyScriptPubKeyMan(); | ||||
CWalletTx wtx(&wallet, m_coinbase_txns.back()); | CWalletTx wtx(&wallet, m_coinbase_txns.back()); | ||||
LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore); | LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore); | ||||
wallet.SetLastBlockProcessed(::ChainActive().Height(), | wallet.SetLastBlockProcessed(::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
▲ Show 20 Lines • Show All 172 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys) { | ||||
TestWatchOnlyPubKey(spk_man, pubkey); | TestWatchOnlyPubKey(spk_man, pubkey); | ||||
} | } | ||||
class ListCoinsTestingSetup : public TestChain100Setup { | class ListCoinsTestingSetup : public TestChain100Setup { | ||||
public: | public: | ||||
ListCoinsTestingSetup() { | ListCoinsTestingSetup() { | ||||
CreateAndProcessBlock({}, | CreateAndProcessBlock({}, | ||||
GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | GetScriptForRawPubKey(coinbaseKey.GetPubKey())); | ||||
wallet = | wallet = std::make_unique<CWallet>(m_chain.get(), WalletLocation(), | ||||
std::make_unique<CWallet>(Params(), m_chain.get(), WalletLocation(), | |||||
WalletDatabase::CreateMock()); | WalletDatabase::CreateMock()); | ||||
{ | { | ||||
LOCK2(wallet->cs_wallet, ::cs_main); | LOCK2(wallet->cs_wallet, ::cs_main); | ||||
wallet->SetLastBlockProcessed( | wallet->SetLastBlockProcessed( | ||||
::ChainActive().Height(), | ::ChainActive().Height(), | ||||
::ChainActive().Tip()->GetBlockHash()); | ::ChainActive().Tip()->GetBlockHash()); | ||||
} | } | ||||
bool firstRun; | bool firstRun; | ||||
wallet->LoadWallet(firstRun); | wallet->LoadWallet(firstRun); | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | BOOST_CHECK_EQUAL(boost::get<PKHash>(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) { | ||||
NodeContext node; | NodeContext node; | ||||
auto chain = interfaces::MakeChain(node, Params()); | auto chain = interfaces::MakeChain(node, Params()); | ||||
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>( | std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>( | ||||
Params(), chain.get(), WalletLocation(), WalletDatabase::CreateDummy()); | chain.get(), WalletLocation(), WalletDatabase::CreateDummy()); | ||||
wallet->SetupLegacyScriptPubKeyMan(); | wallet->SetupLegacyScriptPubKeyMan(); | ||||
wallet->SetMinVersion(FEATURE_LATEST); | wallet->SetMinVersion(FEATURE_LATEST); | ||||
wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS); | wallet->SetWalletFlag(WALLET_FLAG_DISABLE_PRIVATE_KEYS); | ||||
BOOST_CHECK(!wallet->TopUpKeyPool(1000)); | BOOST_CHECK(!wallet->TopUpKeyPool(1000)); | ||||
CTxDestination dest; | CTxDestination dest; | ||||
std::string error; | std::string error; | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
!wallet->GetNewDestination(OutputType::LEGACY, "", dest, error)); | !wallet->GetNewDestination(OutputType::LEGACY, "", dest, error)); | ||||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |