Skip to content

Commit 756ff9b

Browse files
committed
wallet: add dummy BerkeleyRODatabase and BerkeleyROBatch classes
BerkeleyRODatabase and BerkeleyROBatch will be used to access a BDB file without the use of BDB. For now, these are dummy classes.
1 parent ca18aea commit 756ff9b

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ BITCOIN_CORE_H = \
347347
wallet/feebumper.h \
348348
wallet/fees.h \
349349
wallet/load.h \
350+
wallet/migrate.h \
350351
wallet/receive.h \
351352
wallet/rpc/util.h \
352353
wallet/rpc/wallet.h \
@@ -507,6 +508,7 @@ libbitcoin_wallet_a_SOURCES = \
507508
wallet/fees.cpp \
508509
wallet/interfaces.cpp \
509510
wallet/load.cpp \
511+
wallet/migrate.cpp \
510512
wallet/receive.cpp \
511513
wallet/rpc/addresses.cpp \
512514
wallet/rpc/backup.cpp \

src/wallet/migrate.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Distributed under the MIT software license, see the accompanying
2+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
3+
4+
#include <wallet/migrate.h>
5+
6+
namespace wallet {
7+
8+
void BerkeleyRODatabase::Open()
9+
{
10+
}
11+
12+
std::unique_ptr<DatabaseBatch> BerkeleyRODatabase::MakeBatch(bool flush_on_close)
13+
{
14+
return std::make_unique<BerkeleyROBatch>(*this);
15+
}
16+
17+
bool BerkeleyRODatabase::Backup(const std::string& dest) const
18+
{
19+
return false;
20+
}
21+
22+
bool BerkeleyROBatch::ReadKey(DataStream&& key, DataStream& value)
23+
{
24+
return false;
25+
}
26+
27+
bool BerkeleyROBatch::HasKey(DataStream&& key)
28+
{
29+
return false;
30+
}
31+
32+
DatabaseCursor::Status BerkeleyROCursor::Next(DataStream& ssKey, DataStream& ssValue)
33+
{
34+
return DatabaseCursor::Status::FAIL;
35+
}
36+
37+
} // namespace wallet

src/wallet/migrate.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) 2021 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+
#ifndef BITCOIN_WALLET_MIGRATE_H
6+
#define BITCOIN_WALLET_MIGRATE_H
7+
8+
#include <wallet/db.h>
9+
10+
#include <optional>
11+
12+
namespace wallet {
13+
/**
14+
* A class representing a BerkeleyDB file from which we can only read records.
15+
* This is used only for migration of legacy to descriptor wallets
16+
*/
17+
class BerkeleyRODatabase : public WalletDatabase
18+
{
19+
private:
20+
const fs::path m_filepath;
21+
22+
public:
23+
/** Create DB handle */
24+
BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath)
25+
{
26+
if (open) Open();
27+
}
28+
~BerkeleyRODatabase(){};
29+
30+
/** Open the database if it is not already opened. */
31+
void Open() override;
32+
33+
/** Indicate the a new database user has began using the database. Increments m_refcount */
34+
void AddRef() override {}
35+
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
36+
void RemoveRef() override {}
37+
38+
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
39+
*/
40+
bool Rewrite(const char* pszSkip = nullptr) override { return false; }
41+
42+
/** Back up the entire database to a file.
43+
*/
44+
bool Backup(const std::string& strDest) const override;
45+
46+
/** Make sure all changes are flushed to database file.
47+
*/
48+
void Flush() override {}
49+
/** Flush to the database file and close the database.
50+
* Also close the environment if no other databases are open in it.
51+
*/
52+
void Close() override {}
53+
/* flush the wallet passively (TRY_LOCK)
54+
ideal to be called periodically */
55+
bool PeriodicFlush() override { return false; }
56+
57+
void IncrementUpdateCounter() override {}
58+
59+
void ReloadDbEnv() override {}
60+
61+
/** Return path to main database file for logs and error messages. */
62+
std::string Filename() override { return fs::PathToString(m_filepath); }
63+
64+
std::string Format() override { return "bdb_ro"; }
65+
66+
/** Make a DatabaseBatch connected to this database */
67+
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
68+
};
69+
70+
class BerkeleyROCursor : public DatabaseCursor
71+
{
72+
public:
73+
Status Next(DataStream& key, DataStream& value) override;
74+
};
75+
76+
/** RAII class that provides access to a BerkeleyRODatabase */
77+
class BerkeleyROBatch : public DatabaseBatch
78+
{
79+
private:
80+
const BerkeleyRODatabase& m_database;
81+
82+
bool ReadKey(DataStream&& key, DataStream& value) override;
83+
// WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail.
84+
// It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration.
85+
bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; }
86+
bool EraseKey(DataStream&& key) override { return false; }
87+
bool HasKey(DataStream&& key) override;
88+
bool ErasePrefix(Span<const std::byte> prefix) override { return false; }
89+
90+
public:
91+
explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {}
92+
~BerkeleyROBatch() {}
93+
94+
BerkeleyROBatch(const BerkeleyROBatch&) = delete;
95+
BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete;
96+
97+
void Flush() override {}
98+
void Close() override {}
99+
100+
std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(); }
101+
std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override { return std::make_unique<BerkeleyROCursor>(); }
102+
bool TxnBegin() override { return false; }
103+
bool TxnCommit() override { return false; }
104+
bool TxnAbort() override { return false; }
105+
};
106+
} // namespace wallet
107+
108+
#endif // BITCOIN_WALLET_MIGRATE_H

0 commit comments

Comments
 (0)