Bitcoin ABC  0.26.3
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) {
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 
const CChainParams & Params()
Return the currently selected parameters.
Access to the wallet database.
Definition: walletdb.h:175
bool WriteName(const CTxDestination &address, const std::string &strName)
Definition: walletdb.cpp:60
bool WritePurpose(const CTxDestination &address, const std::string &purpose)
Definition: walletdb.cpp:81
bool ErasePurpose(const CTxDestination &address)
Definition: walletdb.cpp:91
bool EraseDestData(const CTxDestination &address, const std::string &key)
Erase destination data tuple from wallet database.
Definition: walletdb.cpp:1090
bool EraseName(const CTxDestination &address)
Definition: walletdb.cpp:70
DBErrors LoadWallet(CWallet *pwallet)
Definition: walletdb.cpp:774
bool WriteDestData(const CTxDestination &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
Definition: walletdb.cpp:1077
std::unique_ptr< Chain > MakeChain(node::NodeContext &node, const CChainParams &params)
Return implementation of Chain interface.
Definition: interfaces.cpp:789
Definition: init.h:28
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
Testing setup and teardown for wallet.
NodeContext struct containing references to chain state and connection state.
Definition: context.h:38
#define LOCK(cs)
Definition: sync.h:306
uint160 uint160S(const char *str)
Definition: uint256.h:161
std::shared_ptr< CWallet > m_wallet
Definition: interfaces.cpp:475
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
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1170
DBErrors
Error statuses for the wallet database.
Definition: walletdb.h:45
BOOST_AUTO_TEST_CASE(write_erase_name)