Bitcoin Core  27.99.0
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2022 The Bitcoin Core 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 <config/bitcoin-config.h> // IWYU pragma: keep
6 
7 #include <boost/test/unit_test.hpp>
8 
10 #include <util/check.h>
11 #include <util/fs.h>
12 #include <util/translation.h>
13 #ifdef USE_BDB
14 #include <wallet/bdb.h>
15 #endif
16 #ifdef USE_SQLITE
17 #include <wallet/sqlite.h>
18 #endif
19 #include <wallet/migrate.h>
20 #include <wallet/test/util.h>
21 #include <wallet/walletutil.h> // for WALLET_FLAG_DESCRIPTORS
22 
23 #include <fstream>
24 #include <memory>
25 #include <string>
26 
27 inline std::ostream& operator<<(std::ostream& os, const std::pair<const SerializeData, SerializeData>& kv)
28 {
29  Span key{kv.first}, value{kv.second};
30  os << "(\"" << std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\", \""
31  << std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\")";
32  return os;
33 }
34 
35 namespace wallet {
36 
37 static Span<const std::byte> StringBytes(std::string_view str)
38 {
39  return AsBytes<const char>({str.data(), str.size()});
40 }
41 
42 static SerializeData StringData(std::string_view str)
43 {
44  auto bytes = StringBytes(str);
45  return SerializeData{bytes.begin(), bytes.end()};
46 }
47 
49 {
50  std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
51  MockableData actual;
52  while (true) {
53  DataStream key, value;
54  DatabaseCursor::Status status = cursor->Next(key, value);
55  if (status == DatabaseCursor::Status::DONE) break;
58  actual.emplace(SerializeData(key.begin(), key.end()), SerializeData(value.begin(), value.end())).second);
59  }
60  BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
61 }
62 
63 BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
64 
65 static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, fs::path& database_filename)
66 {
67  fs::path data_file = BDBDataFile(path);
68  database_filename = data_file.filename();
69  return GetBerkeleyEnv(data_file.parent_path(), false);
70 }
71 
72 BOOST_AUTO_TEST_CASE(getwalletenv_file)
73 {
74  fs::path test_name = "test_name.dat";
75  const fs::path datadir = m_args.GetDataDirNet();
76  fs::path file_path = datadir / test_name;
77  std::ofstream f{file_path};
78  f.close();
79 
80  fs::path filename;
81  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
82  BOOST_CHECK_EQUAL(filename, test_name);
83  BOOST_CHECK_EQUAL(env->Directory(), datadir);
84 }
85 
86 BOOST_AUTO_TEST_CASE(getwalletenv_directory)
87 {
88  fs::path expected_name = "wallet.dat";
89  const fs::path datadir = m_args.GetDataDirNet();
90 
91  fs::path filename;
92  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
93  BOOST_CHECK_EQUAL(filename, expected_name);
94  BOOST_CHECK_EQUAL(env->Directory(), datadir);
95 }
96 
97 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)
98 {
99  fs::path datadir = m_args.GetDataDirNet() / "1";
100  fs::path datadir_2 = m_args.GetDataDirNet() / "2";
101  fs::path filename;
102 
103  std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename);
104  std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename);
105  std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename);
106 
107  BOOST_CHECK(env_1 == env_2);
108  BOOST_CHECK(env_2 != env_3);
109 }
110 
111 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
112 {
113  fs::path datadir = gArgs.GetDataDirNet() / "1";
114  fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
115  fs::path filename;
116 
117  std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename);
118  std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename);
119  env_1_a.reset();
120 
121  std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename);
122  std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename);
123 
124  BOOST_CHECK(env_1_a != env_1_b);
125  BOOST_CHECK(env_2_a == env_2_b);
126 }
127 
128 static std::vector<std::unique_ptr<WalletDatabase>> TestDatabases(const fs::path& path_root)
129 {
130  std::vector<std::unique_ptr<WalletDatabase>> dbs;
131  DatabaseOptions options;
132  DatabaseStatus status;
133  bilingual_str error;
134 #ifdef USE_BDB
135  dbs.emplace_back(MakeBerkeleyDatabase(path_root / "bdb", options, status, error));
136  // Needs BDB to make the DB to read
137  dbs.emplace_back(std::make_unique<BerkeleyRODatabase>(BDBDataFile(path_root / "bdb"), /*open=*/false));
138 #endif
139 #ifdef USE_SQLITE
140  dbs.emplace_back(MakeSQLiteDatabase(path_root / "sqlite", options, status, error));
141 #endif
142  dbs.emplace_back(CreateMockableWalletDatabase());
143  return dbs;
144 }
145 
146 BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test)
147 {
148  // Test each supported db
149  for (const auto& database : TestDatabases(m_path_root)) {
150  std::vector<std::string> prefixes = {"", "FIRST", "SECOND", "P\xfe\xff", "P\xff\x01", "\xff\xff"};
151 
152  std::unique_ptr<DatabaseBatch> handler = Assert(database)->MakeBatch();
153  if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
154  // For BerkeleyRO, open the file now. This must happen after BDB has written to the file
155  database->Open();
156  } else {
157  // Write elements to it if not berkeleyro
158  for (unsigned int i = 0; i < 10; i++) {
159  for (const auto& prefix : prefixes) {
160  BOOST_CHECK(handler->Write(std::make_pair(prefix, i), i));
161  }
162  }
163  }
164 
165  // Now read all the items by prefix and verify that each element gets parsed correctly
166  for (const auto& prefix : prefixes) {
167  DataStream s_prefix;
168  s_prefix << prefix;
169  std::unique_ptr<DatabaseCursor> cursor = handler->GetNewPrefixCursor(s_prefix);
170  DataStream key;
171  DataStream value;
172  for (int i = 0; i < 10; i++) {
173  DatabaseCursor::Status status = cursor->Next(key, value);
175 
176  std::string key_back;
177  unsigned int i_back;
178  key >> key_back >> i_back;
179  BOOST_CHECK_EQUAL(key_back, prefix);
180 
181  unsigned int value_back;
182  value >> value_back;
183  BOOST_CHECK_EQUAL(value_back, i_back);
184  }
185 
186  // Let's now read it once more, it should return DONE
187  BOOST_CHECK(cursor->Next(key, value) == DatabaseCursor::Status::DONE);
188  }
189  handler.reset();
190  database->Close();
191  }
192 }
193 
194 // Lower level DatabaseBase::GetNewPrefixCursor test, to cover cases that aren't
195 // covered in the higher level test above. The higher level test uses
196 // serialized strings which are prefixed with string length, so it doesn't test
197 // truly empty prefixes or prefixes that begin with \xff
198 BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test)
199 {
200  const MockableData::value_type
201  e{StringData(""), StringData("e")},
202  p{StringData("prefix"), StringData("p")},
203  ps{StringData("prefixsuffix"), StringData("ps")},
204  f{StringData("\xff"), StringData("f")},
205  fs{StringData("\xffsuffix"), StringData("fs")},
206  ff{StringData("\xff\xff"), StringData("ff")},
207  ffs{StringData("\xff\xffsuffix"), StringData("ffs")};
208  for (const auto& database : TestDatabases(m_path_root)) {
209  std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
210 
211  if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
212  // For BerkeleyRO, open the file now. This must happen after BDB has written to the file
213  database->Open();
214  } else {
215  // Write elements to it if not berkeleyro
216  for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) {
217  batch->Write(Span{k}, Span{v});
218  }
219  }
220 
221  CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs});
222  CheckPrefix(*batch, StringBytes("prefix"), {p, ps});
223  CheckPrefix(*batch, StringBytes("\xff"), {f, fs, ff, ffs});
224  CheckPrefix(*batch, StringBytes("\xff\xff"), {ff, ffs});
225  batch.reset();
226  database->Close();
227  }
228 }
229 
230 BOOST_AUTO_TEST_CASE(db_availability_after_write_error)
231 {
232  // Ensures the database remains accessible without deadlocking after a write error.
233  // To simulate the behavior, record overwrites are disallowed, and the test verifies
234  // that the database remains active after failing to store an existing record.
235  for (const auto& database : TestDatabases(m_path_root)) {
236  if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
237  // Skip this test if BerkeleyRO
238  continue;
239  }
240  // Write original record
241  std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
242  std::string key = "key";
243  std::string value = "value";
244  std::string value2 = "value_2";
245  BOOST_CHECK(batch->Write(key, value));
246  // Attempt to overwrite the record (expect failure)
247  BOOST_CHECK(!batch->Write(key, value2, /*fOverwrite=*/false));
248  // Successfully overwrite the record
249  BOOST_CHECK(batch->Write(key, value2, /*fOverwrite=*/true));
250  // Sanity-check; read and verify the overwritten value
251  std::string read_value;
252  BOOST_CHECK(batch->Read(key, read_value));
253  BOOST_CHECK_EQUAL(read_value, value2);
254  }
255 }
256 
257 // Verify 'ErasePrefix' functionality using db keys similar to the ones used by the wallet.
258 // Keys are in the form of std::pair<TYPE, ENTRY_ID>
259 BOOST_AUTO_TEST_CASE(erase_prefix)
260 {
261  const std::string key = "key";
262  const std::string key2 = "key2";
263  const std::string value = "value";
264  const std::string value2 = "value_2";
265  auto make_key = [](std::string type, std::string id) { return std::make_pair(type, id); };
266 
267  for (const auto& database : TestDatabases(m_path_root)) {
268  if (dynamic_cast<BerkeleyRODatabase*>(database.get())) {
269  // Skip this test if BerkeleyRO
270  continue;
271  }
272  std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
273 
274  // Write two entries with the same key type prefix, a third one with a different prefix
275  // and a fourth one with the type-id values inverted
276  BOOST_CHECK(batch->Write(make_key(key, value), value));
277  BOOST_CHECK(batch->Write(make_key(key, value2), value2));
278  BOOST_CHECK(batch->Write(make_key(key2, value), value));
279  BOOST_CHECK(batch->Write(make_key(value, key), value));
280 
281  // Erase the ones with the same prefix and verify result
282  BOOST_CHECK(batch->TxnBegin());
283  BOOST_CHECK(batch->ErasePrefix(DataStream() << key));
284  BOOST_CHECK(batch->TxnCommit());
285 
286  BOOST_CHECK(!batch->Exists(make_key(key, value)));
287  BOOST_CHECK(!batch->Exists(make_key(key, value2)));
288  // Also verify that entries with a different prefix were not erased
289  BOOST_CHECK(batch->Exists(make_key(key2, value)));
290  BOOST_CHECK(batch->Exists(make_key(value, key)));
291  }
292 }
293 
294 #ifdef USE_SQLITE
295 
296 // Test-only statement execution error
297 constexpr int TEST_SQLITE_ERROR = -999;
298 
299 class DbExecBlocker : public SQliteExecHandler
300 {
301 private:
302  SQliteExecHandler m_base_exec;
303  std::set<std::string> m_blocked_statements;
304 public:
305  DbExecBlocker(std::set<std::string> blocked_statements) : m_blocked_statements(blocked_statements) {}
306  int Exec(SQLiteDatabase& database, const std::string& statement) override {
307  if (m_blocked_statements.contains(statement)) return TEST_SQLITE_ERROR;
308  return m_base_exec.Exec(database, statement);
309  }
310 };
311 
312 BOOST_AUTO_TEST_CASE(txn_close_failure_dangling_txn)
313 {
314  // Verifies that there is no active dangling, to-be-reversed db txn
315  // after the batch object that initiated it is destroyed.
316  DatabaseOptions options;
317  DatabaseStatus status;
318  bilingual_str error;
319  std::unique_ptr<SQLiteDatabase> database = MakeSQLiteDatabase(m_path_root / "sqlite", options, status, error);
320 
321  std::string key = "key";
322  std::string value = "value";
323 
324  std::unique_ptr<SQLiteBatch> batch = std::make_unique<SQLiteBatch>(*database);
325  BOOST_CHECK(batch->TxnBegin());
326  BOOST_CHECK(batch->Write(key, value));
327  // Set a handler to prevent txn abortion during destruction.
328  // Mimicking a db statement execution failure.
329  batch->SetExecHandler(std::make_unique<DbExecBlocker>(std::set<std::string>{"ROLLBACK TRANSACTION"}));
330  // Destroy batch
331  batch.reset();
332 
333  // Ensure there is no dangling, to-be-reversed db txn
334  BOOST_CHECK(!database->HasActiveTxn());
335 
336  // And, just as a sanity check; verify that new batchs only write what they suppose to write
337  // and nothing else.
338  std::string key2 = "key2";
339  std::unique_ptr<SQLiteBatch> batch2 = std::make_unique<SQLiteBatch>(*database);
340  BOOST_CHECK(batch2->Write(key2, value));
341  // The first key must not exist
342  BOOST_CHECK(!batch2->Exists(key));
343 }
344 
345 BOOST_AUTO_TEST_CASE(concurrent_txn_dont_interfere)
346 {
347  std::string key = "key";
348  std::string value = "value";
349  std::string value2 = "value_2";
350 
351  DatabaseOptions options;
352  DatabaseStatus status;
353  bilingual_str error;
354  const auto& database = MakeSQLiteDatabase(m_path_root / "sqlite", options, status, error);
355 
356  std::unique_ptr<DatabaseBatch> handler = Assert(database)->MakeBatch();
357 
358  // Verify concurrent db transactions does not interfere between each other.
359  // Start db txn, write key and check the key does exist within the db txn.
360  BOOST_CHECK(handler->TxnBegin());
361  BOOST_CHECK(handler->Write(key, value));
362  BOOST_CHECK(handler->Exists(key));
363 
364  // But, the same key, does not exist in another handler
365  std::unique_ptr<DatabaseBatch> handler2 = Assert(database)->MakeBatch();
366  BOOST_CHECK(handler2->Exists(key));
367 
368  // Attempt to commit the handler txn calling the handler2 methods.
369  // Which, must not be possible.
370  BOOST_CHECK(!handler2->TxnCommit());
371  BOOST_CHECK(!handler2->TxnAbort());
372 
373  // Only the first handler can commit the changes.
374  BOOST_CHECK(handler->TxnCommit());
375  // And, once commit is completed, handler2 can read the record
376  std::string read_value;
377  BOOST_CHECK(handler2->Read(key, read_value));
378  BOOST_CHECK_EQUAL(read_value, value);
379 
380  // Also, once txn is committed, single write statements are re-enabled.
381  // Which means that handler2 can read the record changes directly.
382  BOOST_CHECK(handler->Write(key, value2, /*fOverwrite=*/true));
383  BOOST_CHECK(handler2->Read(key, read_value));
384  BOOST_CHECK_EQUAL(read_value, value2);
385 }
386 #endif // USE_SQLITE
387 
389 } // namespace wallet
ArgsManager gArgs
Definition: args.cpp:41
#define Assert(val)
Identity function.
Definition: check.h:77
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:232
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
const_iterator begin() const
Definition: streams.h:177
const_iterator end() const
Definition: streams.h:179
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Definition: span.h:205
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
path filename() const
Definition: fs.h:72
A class representing a BerkeleyDB file from which we can only read records.
Definition: migrate.h:21
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
virtual std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix)=0
BOOST_AUTO_TEST_SUITE_END()
std::ostream & operator<<(std::ostream &os, const std::pair< const SerializeData, SerializeData > &kv)
Definition: db_tests.cpp:27
Filesystem operations and types.
std::unique_ptr< BerkeleyDatabase > MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley database at specified path.
Definition: bdb.cpp:948
static void CheckPrefix(DatabaseBatch &batch, Span< const std::byte > prefix, MockableData expected)
Definition: db_tests.cpp:48
static std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &path, fs::path &database_filename)
Definition: db_tests.cpp:65
static SerializeData StringData(std::string_view str)
Definition: db_tests.cpp:42
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:694
static std::vector< std::unique_ptr< WalletDatabase > > TestDatabases(const fs::path &path_root)
Definition: db_tests.cpp:128
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:67
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:185
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory, bool use_shared_memory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:81
BOOST_AUTO_TEST_CASE(bnb_search_test)
static Span< const std::byte > StringBytes(std::string_view str)
Definition: db_tests.cpp:37
std::map< SerializeData, SerializeData, std::less<> > MockableData
Definition: util.h:53
DatabaseStatus
Definition: db.h:204
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
const char * prefix
Definition: rest.cpp:1007
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1008
Basic testing setup.
Definition: setup_common.h:63
Bilingual messages:
Definition: translation.h:18
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:49