|
| 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