Bitcoin ABC  0.24.7
P2P Digital Currency
walletdb_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <wallet/walletdb.h>
6 
7 #include <chainparams.h>
8 #include <interfaces/chain.h>
9 #include <node/context.h>
10 #include <wallet/wallet.h>
11 
12 #include <test/util/setup_common.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 #include <memory>
18 
19 namespace {
20 static std::unique_ptr<CWallet> LoadWallet(WalletBatch &batch) {
21  NodeContext node;
22  auto chain = interfaces::MakeChain(node, Params());
23  std::unique_ptr<CWallet> wallet =
24  std::make_unique<CWallet>(chain.get(), "", CreateDummyWalletDatabase());
25  DBErrors res = batch.LoadWallet(wallet.get());
27  return wallet;
28 }
29 } // namespace
30 
32 
33 BOOST_AUTO_TEST_CASE(write_erase_name) {
34  WalletBatch batch(m_wallet.GetDBHandle());
35 
36  CTxDestination dst1 = PKHash(uint160S("c0ffee"));
37  CTxDestination dst2 = PKHash(uint160S("f00d"));
38 
39  BOOST_CHECK(batch.WriteName(dst1, "name1"));
40  BOOST_CHECK(batch.WriteName(dst2, "name2"));
41  {
42  auto w = LoadWallet(batch);
43  LOCK(w->cs_wallet);
44  BOOST_CHECK_EQUAL(1, w->m_address_book.count(dst1));
45  BOOST_CHECK_EQUAL("name1", w->m_address_book[dst1].GetLabel());
46  BOOST_CHECK_EQUAL("name2", w->m_address_book[dst2].GetLabel());
47  }
48 
49  batch.EraseName(dst1);
50 
51  {
52  auto w = LoadWallet(batch);
53  LOCK(w->cs_wallet);
54  BOOST_CHECK_EQUAL(0, w->m_address_book.count(dst1));
55  BOOST_CHECK_EQUAL(1, w->m_address_book.count(dst2));
56  }
57 }
58 
59 BOOST_AUTO_TEST_CASE(write_erase_purpose) {
60  WalletBatch batch(m_wallet.GetDBHandle());
61 
62  CTxDestination dst1 = PKHash(uint160S("c0ffee"));
63  CTxDestination dst2 = PKHash(uint160S("f00d"));
64 
65  BOOST_CHECK(batch.WritePurpose(dst1, "purpose1"));
66  BOOST_CHECK(batch.WritePurpose(dst2, "purpose2"));
67  {
68  auto w = LoadWallet(batch);
69  LOCK(w->cs_wallet);
70  BOOST_CHECK_EQUAL(1, w->m_address_book.count(dst1));
71  BOOST_CHECK_EQUAL("purpose1", w->m_address_book[dst1].purpose);
72  BOOST_CHECK_EQUAL("purpose2", w->m_address_book[dst2].purpose);
73  }
74 
75  batch.ErasePurpose(dst1);
76 
77  {
78  auto w = LoadWallet(batch);
79  LOCK(w->cs_wallet);
80  BOOST_CHECK_EQUAL(0, w->m_address_book.count(dst1));
81  BOOST_CHECK_EQUAL(1, w->m_address_book.count(dst2));
82  }
83 }
84 
85 BOOST_AUTO_TEST_CASE(write_erase_destdata) {
86  WalletBatch batch(m_wallet.GetDBHandle());
87 
88  CTxDestination dst1 = PKHash(uint160S("c0ffee"));
89  CTxDestination dst2 = PKHash(uint160S("f00d"));
90 
91  BOOST_CHECK(batch.WriteDestData(dst1, "key1", "value1"));
92  BOOST_CHECK(batch.WriteDestData(dst1, "key2", "value2"));
93  BOOST_CHECK(batch.WriteDestData(dst2, "key1", "value3"));
94  BOOST_CHECK(batch.WriteDestData(dst2, "key2", "value4"));
95  {
96  auto w = LoadWallet(batch);
97  LOCK(w->cs_wallet);
98  std::string val;
99  BOOST_CHECK(w->GetDestData(dst1, "key1", &val));
100  BOOST_CHECK_EQUAL("value1", val);
101  BOOST_CHECK(w->GetDestData(dst1, "key2", &val));
102  BOOST_CHECK_EQUAL("value2", val);
103  BOOST_CHECK(w->GetDestData(dst2, "key1", &val));
104  BOOST_CHECK_EQUAL("value3", val);
105  BOOST_CHECK(w->GetDestData(dst2, "key2", &val));
106  BOOST_CHECK_EQUAL("value4", val);
107  }
108 
109  batch.EraseDestData(dst1, "key2");
110 
111  {
112  auto w = LoadWallet(batch);
113  LOCK(w->cs_wallet);
114  std::string dummy;
115  BOOST_CHECK(w->GetDestData(dst1, "key1", &dummy));
116  BOOST_CHECK(!w->GetDestData(dst1, "key2", &dummy));
117  BOOST_CHECK(w->GetDestData(dst2, "key1", &dummy));
118  BOOST_CHECK(w->GetDestData(dst2, "key2", &dummy));
119  }
120 }
121 
122 BOOST_AUTO_TEST_CASE(no_dest_fails) {
123  WalletBatch batch(m_wallet.GetDBHandle(), "cr+");
124 
126  BOOST_CHECK(!batch.WriteName(dst, "name"));
127  BOOST_CHECK(!batch.WritePurpose(dst, "purpose"));
128  BOOST_CHECK(!batch.WriteDestData(dst, "key", "value"));
129 }
130 
m_wallet
std::shared_ptr< CWallet > m_wallet
Definition: wallet.cpp:462
CreateDummyWalletDatabase
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1164
wallet.h
WalletBatch::LoadWallet
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:768
chainparams.h
chain.h
WalletBatch::WriteDestData
bool WriteDestData(const CTxDestination &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1071
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
context.h
WalletTestingSetup
Testing setup and teardown for wallet.
Definition: wallet_test_fixture.h:21
WalletBatch::EraseDestData
bool EraseDestData(const CTxDestination &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1084
DBErrors::LOAD_OK
@ LOAD_OK
CNoDestination
Definition: standard.h:96
wallet_test_fixture.h
uint160S
uint160 uint160S(const char *str)
Definition: uint256.h:159
PKHash
Definition: standard.h:106
DBErrors
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:46
WalletBatch::ErasePurpose
bool ErasePurpose(const CTxDestination &address)
Definition: walletdb.cpp:91
LOCK
#define LOCK(cs)
Definition: sync.h:241
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:506
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: chain.cpp:491
walletdb.h
LoadWallet
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, std::optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:263
WalletBatch::WriteName
bool WriteName(const CTxDestination &address, const std::string &strName)
Definition: walletdb.cpp:60
WalletBatch::EraseName
bool EraseName(const CTxDestination &address)
Definition: walletdb.cpp:70
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
WalletBatch::WritePurpose
bool WritePurpose(const CTxDestination &address, const std::string &purpose)
Definition: walletdb.cpp:81
WalletBatch
Access to the wallet database.
Definition: walletdb.h:176
CTxDestination
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:132
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(write_erase_name)
Definition: walletdb_tests.cpp:33
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18