Skip to content

Commit 79e8247

Browse files
committed
Merge bitcoin/bitcoin#28039: wallet: don't include bdb files from our headers
8b5397c wallet: bdb: include bdb header from our implementation files only (Cory Fields) 6e01062 wallet: bdb: don't use bdb define in header (Cory Fields) 004b184 wallet: bdb: move BerkeleyDatabase constructor to cpp file (Cory Fields) b3582ba wallet: bdb: move SafeDbt to cpp file (Cory Fields) e5e5aa1 wallet: bdb: move SpanFromDbt to below SafeDbt's implementation (Cory Fields) 4216f69 wallet: bdb: move TxnBegin to cpp file since it uses a bdb function (Cory Fields) 43369f3 wallet: bdb: drop default parameter (Cory Fields) Pull request description: Only `#include` upstream bdb headers from our cpp files. It's generally good practice to avoid including 3rd party deps in headers as otherwise they tend to sneak into new compilation units. IMO this makes for a nice cleanup. There's a good bit of code movement here, but each commit is small and _should_ be obviously correct. Note: in the future, the buildsystem can add the bdb include path for `bdb.cpp` and `salvage.cpp` only, rather than all wallet sources. ACKs for top commit: achow101: reACK 8b5397c hebasto: ACK 8b5397c Tree-SHA512: 0ef6e8a9c4c6e2d1e5d6a3534495f91900e4175143911a5848258c56da54535b85fad67b6d573da5f7b96e7881299b5a8ca2327e708f305b317b9a3e85038d66
2 parents 87e19b0 + 8b5397c commit 79e8247

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

src/wallet/bdb.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <stdint.h>
2020

21+
#include <db_cxx.h>
2122
#include <sys/stat.h>
2223

2324
// Windows may not define S_IRUSR or S_IWUSR. We define both
@@ -29,12 +30,10 @@
2930
#endif
3031
#endif
3132

33+
static_assert(BDB_DB_FILE_ID_LEN == DB_FILE_ID_LEN, "DB_FILE_ID_LEN should be 20.");
34+
3235
namespace wallet {
3336
namespace {
34-
Span<const std::byte> SpanFromDbt(const SafeDbt& dbt)
35-
{
36-
return {reinterpret_cast<const std::byte*>(dbt.get_data()), dbt.get_size()};
37-
}
3837

3938
//! Make sure database has a unique fileid within the environment. If it
4039
//! doesn't, throw an error. BDB caches do not work properly when more than one
@@ -236,6 +235,26 @@ BerkeleyEnvironment::BerkeleyEnvironment() : m_use_shared_memory(false)
236235
fMockDb = true;
237236
}
238237

238+
/** RAII class that automatically cleanses its data on destruction */
239+
class SafeDbt final
240+
{
241+
Dbt m_dbt;
242+
243+
public:
244+
// construct Dbt with internally-managed data
245+
SafeDbt();
246+
// construct Dbt with provided data
247+
SafeDbt(void* data, size_t size);
248+
~SafeDbt();
249+
250+
// delegate to Dbt
251+
const void* get_data() const;
252+
uint32_t get_size() const;
253+
254+
// conversion operator to access the underlying Dbt
255+
operator Dbt*();
256+
};
257+
239258
SafeDbt::SafeDbt()
240259
{
241260
m_dbt.set_flags(DB_DBT_MALLOC);
@@ -275,6 +294,18 @@ SafeDbt::operator Dbt*()
275294
return &m_dbt;
276295
}
277296

297+
static Span<const std::byte> SpanFromDbt(const SafeDbt& dbt)
298+
{
299+
return {reinterpret_cast<const std::byte*>(dbt.get_data()), dbt.get_size()};
300+
}
301+
302+
BerkeleyDatabase::BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options) :
303+
WalletDatabase(), env(std::move(env)), m_filename(std::move(filename)), m_max_log_mb(options.max_log_mb)
304+
{
305+
auto inserted = this->env->m_databases.emplace(m_filename, std::ref(*this));
306+
assert(inserted.second);
307+
}
308+
278309
bool BerkeleyDatabase::Verify(bilingual_str& errorStr)
279310
{
280311
fs::path walletDir = env->Directory();
@@ -462,6 +493,15 @@ void BerkeleyEnvironment::ReloadDbEnv()
462493
Open(open_err);
463494
}
464495

496+
DbTxn* BerkeleyEnvironment::TxnBegin(int flags)
497+
{
498+
DbTxn* ptxn = nullptr;
499+
int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
500+
if (!ptxn || ret != 0)
501+
return nullptr;
502+
return ptxn;
503+
}
504+
465505
bool BerkeleyDatabase::Rewrite(const char* pszSkip)
466506
{
467507
while (true) {
@@ -742,7 +782,7 @@ bool BerkeleyBatch::TxnBegin()
742782
{
743783
if (!pdb || activeTxn)
744784
return false;
745-
DbTxn* ptxn = env->TxnBegin();
785+
DbTxn* ptxn = env->TxnBegin(DB_TXN_WRITE_NOSYNC);
746786
if (!ptxn)
747787
return false;
748788
activeTxn = ptxn;

src/wallet/bdb.h

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
#include <unordered_map>
2222
#include <vector>
2323

24-
#include <db_cxx.h>
25-
2624
struct bilingual_str;
2725

26+
class DbEnv;
27+
class DbTxn;
28+
class Db;
29+
class Dbc;
30+
31+
// This constant was introduced in BDB 4.0.14 and has never changed, but there
32+
// is a belt-and-suspenders check in the cpp file just in case.
33+
#define BDB_DB_FILE_ID_LEN 20 /* Unique file ID length. */
34+
2835
namespace wallet {
2936

3037
struct WalletDatabaseFileId {
31-
uint8_t value[DB_FILE_ID_LEN];
38+
uint8_t value[BDB_DB_FILE_ID_LEN];
3239
bool operator==(const WalletDatabaseFileId& rhs) const;
3340
};
3441

@@ -67,14 +74,7 @@ class BerkeleyEnvironment
6774
void CloseDb(const fs::path& filename);
6875
void ReloadDbEnv();
6976

70-
DbTxn* TxnBegin(int flags = DB_TXN_WRITE_NOSYNC)
71-
{
72-
DbTxn* ptxn = nullptr;
73-
int ret = dbenv->txn_begin(nullptr, &ptxn, flags);
74-
if (!ptxn || ret != 0)
75-
return nullptr;
76-
return ptxn;
77-
}
77+
DbTxn* TxnBegin(int flags);
7878
};
7979

8080
/** Get BerkeleyEnvironment given a directory path. */
@@ -91,12 +91,7 @@ class BerkeleyDatabase : public WalletDatabase
9191
BerkeleyDatabase() = delete;
9292

9393
/** Create DB handle to real database */
94-
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options) :
95-
WalletDatabase(), env(std::move(env)), m_filename(std::move(filename)), m_max_log_mb(options.max_log_mb)
96-
{
97-
auto inserted = this->env->m_databases.emplace(m_filename, std::ref(*this));
98-
assert(inserted.second);
99-
}
94+
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, fs::path filename, const DatabaseOptions& options);
10095

10196
~BerkeleyDatabase() override;
10297

@@ -159,26 +154,6 @@ class BerkeleyDatabase : public WalletDatabase
159154
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
160155
};
161156

162-
/** RAII class that automatically cleanses its data on destruction */
163-
class SafeDbt final
164-
{
165-
Dbt m_dbt;
166-
167-
public:
168-
// construct Dbt with internally-managed data
169-
SafeDbt();
170-
// construct Dbt with provided data
171-
SafeDbt(void* data, size_t size);
172-
~SafeDbt();
173-
174-
// delegate to Dbt
175-
const void* get_data() const;
176-
uint32_t get_size() const;
177-
178-
// conversion operator to access the underlying Dbt
179-
operator Dbt*();
180-
};
181-
182157
class BerkeleyCursor : public DatabaseCursor
183158
{
184159
private:

src/wallet/salvage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <wallet/wallet.h>
1212
#include <wallet/walletdb.h>
1313

14+
#include <db_cxx.h>
15+
1416
namespace wallet {
1517
/* End of headers, beginning of key/value data */
1618
static const char *HEADER_END = "HEADER=END";
@@ -175,7 +177,7 @@ bool RecoverDatabaseFile(const ArgsManager& args, const fs::path& file_path, bil
175177
return false;
176178
}
177179

178-
DbTxn* ptxn = env->TxnBegin();
180+
DbTxn* ptxn = env->TxnBegin(DB_TXN_WRITE_NOSYNC);
179181
CWallet dummyWallet(nullptr, "", std::make_unique<DummyDatabase>());
180182
for (KeyValPair& row : salvagedData)
181183
{

0 commit comments

Comments
 (0)