Skip to content

Commit 429aa9e

Browse files
committed
[test] Move some tests from qt -> wallet
After previous refactoring, the tests make more sense here.
1 parent d944bd7 commit 429aa9e

File tree

2 files changed

+101
-117
lines changed

2 files changed

+101
-117
lines changed

src/qt/test/wallettests.cpp

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "wallettests.h"
22

3-
#include "consensus/validation.h"
43
#include "qt/bitcoinamountfield.h"
54
#include "qt/callback.h"
65
#include "qt/optionsmodel.h"
@@ -12,8 +11,6 @@
1211
#include "qt/walletmodel.h"
1312
#include "test/test_bitcoin.h"
1413
#include "validation.h"
15-
#include "wallet/test/wallet_test_fixture.h"
16-
#include "wallet/coincontrol.h"
1714
#include "wallet/wallet.h"
1815

1916
#include <QAbstractButton>
@@ -23,118 +20,6 @@
2320

2421
namespace
2522
{
26-
27-
void TestLoadReceiveRequests()
28-
{
29-
WalletTestingSetup test;
30-
OptionsModel optionsModel;
31-
WalletModel walletModel(nullptr, pwalletMain, &optionsModel);
32-
33-
CTxDestination dest = CKeyID();
34-
pwalletMain->AddDestData(dest, "misc", "val_misc");
35-
pwalletMain->AddDestData(dest, "rr0", "val_rr0");
36-
pwalletMain->AddDestData(dest, "rr1", "val_rr1");
37-
38-
std::vector<std::string> values;
39-
walletModel.loadReceiveRequests(values);
40-
QCOMPARE((int)values.size(), 2);
41-
QCOMPARE(QString::fromStdString(values[0]), QString("val_rr0"));
42-
QCOMPARE(QString::fromStdString(values[1]), QString("val_rr1"));
43-
}
44-
45-
class ListCoinsTestingSetup : public TestChain100Setup
46-
{
47-
public:
48-
ListCoinsTestingSetup()
49-
{
50-
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
51-
::bitdb.MakeMock();
52-
wallet.reset(new CWallet(std::unique_ptr<CWalletDBWrapper>(new CWalletDBWrapper(&bitdb, "wallet_test.dat"))));
53-
bool firstRun;
54-
wallet->LoadWallet(firstRun);
55-
LOCK(wallet->cs_wallet);
56-
wallet->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
57-
wallet->ScanForWalletTransactions(chainActive.Genesis());
58-
}
59-
60-
~ListCoinsTestingSetup()
61-
{
62-
::bitdb.Flush(true);
63-
::bitdb.Reset();
64-
}
65-
66-
CWalletTx& AddTx(CRecipient recipient)
67-
{
68-
CWalletTx wtx;
69-
CReserveKey reservekey(wallet.get());
70-
CAmount fee;
71-
int changePos = -1;
72-
std::string error;
73-
wallet->CreateTransaction({recipient}, wtx, reservekey, fee, changePos, error);
74-
CValidationState state;
75-
wallet->CommitTransaction(wtx, reservekey, nullptr, state);
76-
auto it = wallet->mapWallet.find(wtx.GetHash());
77-
CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
78-
it->second.SetMerkleBranch(chainActive.Tip(), 1);
79-
return it->second;
80-
}
81-
82-
std::unique_ptr<CWallet> wallet;
83-
};
84-
85-
void TestListCoins()
86-
{
87-
ListCoinsTestingSetup test;
88-
OptionsModel optionsModel;
89-
WalletModel walletModel(nullptr, test.wallet.get(), &optionsModel);
90-
QString coinbaseAddress = QString::fromStdString(CBitcoinAddress(test.coinbaseKey.GetPubKey().GetID()).ToString());
91-
92-
LOCK(test.wallet->cs_wallet);
93-
94-
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
95-
// address.
96-
std::map<QString, std::vector<COutput>> list;
97-
walletModel.listCoins(list);
98-
QCOMPARE((int)list.size(), 1);
99-
QCOMPARE(list.begin()->first, coinbaseAddress);
100-
QCOMPARE((int)list.begin()->second.size(), 1);
101-
102-
// Check initial balance from one mature coinbase transaction.
103-
CCoinControl coinControl;
104-
QCOMPARE(50 * COIN, walletModel.getBalance(&coinControl));
105-
106-
// Add a transaction creating a change address, and confirm ListCoins still
107-
// returns the coin associated with the change address underneath the
108-
// coinbaseKey pubkey, even though the change address has a different
109-
// pubkey.
110-
test.AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
111-
list.clear();
112-
walletModel.listCoins(list);
113-
QCOMPARE((int)list.size(), 1);
114-
QCOMPARE(list.begin()->first, coinbaseAddress);
115-
QCOMPARE((int)list.begin()->second.size(), 2);
116-
117-
// Lock both coins. Confirm number of available coins drops to 0.
118-
std::vector<COutput> available;
119-
test.wallet->AvailableCoins(available);
120-
QCOMPARE((int)available.size(), 2);
121-
for (const auto& group : list) {
122-
for (const auto& coin : group.second) {
123-
test.wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
124-
}
125-
}
126-
test.wallet->AvailableCoins(available);
127-
QCOMPARE((int)available.size(), 0);
128-
129-
// Confirm ListCoins still returns same result as before, despite coins
130-
// being locked.
131-
list.clear();
132-
walletModel.listCoins(list);
133-
QCOMPARE((int)list.size(), 1);
134-
QCOMPARE(list.begin()->first, coinbaseAddress);
135-
QCOMPARE((int)list.begin()->second.size(), 2);
136-
}
137-
13823
//! Press "Yes" button in modal send confirmation dialog.
13924
void ConfirmSend()
14025
{
@@ -236,7 +121,5 @@ void TestSendCoins()
236121

237122
void WalletTests::walletTests()
238123
{
239-
TestLoadReceiveRequests();
240-
TestListCoins();
241124
TestSendCoins();
242125
}

src/wallet/test/wallet_tests.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <utility>
1010
#include <vector>
1111

12+
#include "consensus/validation.h"
1213
#include "rpc/server.h"
1314
#include "test/test_bitcoin.h"
1415
#include "validation.h"
@@ -515,4 +516,104 @@ BOOST_AUTO_TEST_CASE(ComputeTimeSmart)
515516
SetMockTime(0);
516517
}
517518

519+
BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
520+
{
521+
CTxDestination dest = CKeyID();
522+
pwalletMain->AddDestData(dest, "misc", "val_misc");
523+
pwalletMain->AddDestData(dest, "rr0", "val_rr0");
524+
pwalletMain->AddDestData(dest, "rr1", "val_rr1");
525+
526+
auto values = pwalletMain->GetDestValues("rr");
527+
BOOST_CHECK_EQUAL(values.size(), 2);
528+
BOOST_CHECK_EQUAL(values[0], "val_rr0");
529+
BOOST_CHECK_EQUAL(values[1], "val_rr1");
530+
}
531+
532+
class ListCoinsTestingSetup : public TestChain100Setup
533+
{
534+
public:
535+
ListCoinsTestingSetup()
536+
{
537+
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
538+
::bitdb.MakeMock();
539+
wallet.reset(new CWallet(std::unique_ptr<CWalletDBWrapper>(new CWalletDBWrapper(&bitdb, "wallet_test.dat"))));
540+
bool firstRun;
541+
wallet->LoadWallet(firstRun);
542+
LOCK(wallet->cs_wallet);
543+
wallet->AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());
544+
wallet->ScanForWalletTransactions(chainActive.Genesis());
545+
}
546+
547+
~ListCoinsTestingSetup()
548+
{
549+
wallet.reset();
550+
::bitdb.Flush(true);
551+
::bitdb.Reset();
552+
}
553+
554+
CWalletTx& AddTx(CRecipient recipient)
555+
{
556+
CWalletTx wtx;
557+
CReserveKey reservekey(wallet.get());
558+
CAmount fee;
559+
int changePos = -1;
560+
std::string error;
561+
BOOST_CHECK(wallet->CreateTransaction({recipient}, wtx, reservekey, fee, changePos, error));
562+
CValidationState state;
563+
BOOST_CHECK(wallet->CommitTransaction(wtx, reservekey, nullptr, state));
564+
auto it = wallet->mapWallet.find(wtx.GetHash());
565+
BOOST_CHECK(it != wallet->mapWallet.end());
566+
CreateAndProcessBlock({CMutableTransaction(*it->second.tx)}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
567+
it->second.SetMerkleBranch(chainActive.Tip(), 1);
568+
return it->second;
569+
}
570+
571+
std::unique_ptr<CWallet> wallet;
572+
};
573+
574+
BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
575+
{
576+
std::string coinbaseAddress = coinbaseKey.GetPubKey().GetID().ToString();
577+
LOCK(wallet->cs_wallet);
578+
579+
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
580+
// address.
581+
auto list = wallet->ListCoins();
582+
BOOST_CHECK_EQUAL(list.size(), 1);
583+
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
584+
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1);
585+
586+
// Check initial balance from one mature coinbase transaction.
587+
BOOST_CHECK_EQUAL(50 * COIN, wallet->GetAvailableBalance());
588+
589+
// Add a transaction creating a change address, and confirm ListCoins still
590+
// returns the coin associated with the change address underneath the
591+
// coinbaseKey pubkey, even though the change address has a different
592+
// pubkey.
593+
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
594+
list = wallet->ListCoins();
595+
BOOST_CHECK_EQUAL(list.size(), 1);
596+
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
597+
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);
598+
599+
// Lock both coins. Confirm number of available coins drops to 0.
600+
std::vector<COutput> available;
601+
wallet->AvailableCoins(available);
602+
BOOST_CHECK_EQUAL(available.size(), 2);
603+
for (const auto& group : list) {
604+
for (const auto& coin : group.second) {
605+
wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
606+
}
607+
}
608+
wallet->AvailableCoins(available);
609+
BOOST_CHECK_EQUAL(available.size(), 0);
610+
611+
// Confirm ListCoins still returns same result as before, despite coins
612+
// being locked.
613+
list = wallet->ListCoins();
614+
BOOST_CHECK_EQUAL(list.size(), 1);
615+
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
616+
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);
617+
}
618+
518619
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)