diff --git a/src/Makefile.am b/src/Makefile.am index 11b8f6e4c32..0db9812f938 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -347,6 +347,7 @@ BITCOIN_CORE_H = \ wallet/feebumper.h \ wallet/fees.h \ wallet/load.h \ + wallet/migrate.h \ wallet/receive.h \ wallet/rpc/util.h \ wallet/rpc/wallet.h \ @@ -507,6 +508,7 @@ libbitcoin_wallet_a_SOURCES = \ wallet/fees.cpp \ wallet/interfaces.cpp \ wallet/load.cpp \ + wallet/migrate.cpp \ wallet/receive.cpp \ wallet/rpc/addresses.cpp \ wallet/rpc/backup.cpp \ diff --git a/src/wallet/migrate.cpp b/src/wallet/migrate.cpp new file mode 100644 index 00000000000..e567cb0f518 --- /dev/null +++ b/src/wallet/migrate.cpp @@ -0,0 +1,37 @@ +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +namespace wallet { + +void BerkeleyRODatabase::Open() +{ +} + +std::unique_ptr BerkeleyRODatabase::MakeBatch(bool flush_on_close) +{ + return std::make_unique(*this); +} + +bool BerkeleyRODatabase::Backup(const std::string& dest) const +{ + return false; +} + +bool BerkeleyROBatch::ReadKey(DataStream&& key, DataStream& value) +{ + return false; +} + +bool BerkeleyROBatch::HasKey(DataStream&& key) +{ + return false; +} + +DatabaseCursor::Status BerkeleyROCursor::Next(DataStream& ssKey, DataStream& ssValue) +{ + return DatabaseCursor::Status::FAIL; +} + +} // namespace wallet diff --git a/src/wallet/migrate.h b/src/wallet/migrate.h new file mode 100644 index 00000000000..de7082e8a04 --- /dev/null +++ b/src/wallet/migrate.h @@ -0,0 +1,108 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_WALLET_MIGRATE_H +#define BITCOIN_WALLET_MIGRATE_H + +#include + +#include + +namespace wallet { +/** + * A class representing a BerkeleyDB file from which we can only read records. + * This is used only for migration of legacy to descriptor wallets + */ +class BerkeleyRODatabase : public WalletDatabase +{ +private: + const fs::path m_filepath; + +public: + /** Create DB handle */ + BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath) + { + if (open) Open(); + } + ~BerkeleyRODatabase(){}; + + /** Open the database if it is not already opened. */ + void Open() override; + + /** Indicate the a new database user has began using the database. Increments m_refcount */ + void AddRef() override {} + /** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */ + void RemoveRef() override {} + + /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero + */ + bool Rewrite(const char* pszSkip = nullptr) override { return false; } + + /** Back up the entire database to a file. + */ + bool Backup(const std::string& strDest) const override; + + /** Make sure all changes are flushed to database file. + */ + void Flush() override {} + /** Flush to the database file and close the database. + * Also close the environment if no other databases are open in it. + */ + void Close() override {} + /* flush the wallet passively (TRY_LOCK) + ideal to be called periodically */ + bool PeriodicFlush() override { return false; } + + void IncrementUpdateCounter() override {} + + void ReloadDbEnv() override {} + + /** Return path to main database file for logs and error messages. */ + std::string Filename() override { return fs::PathToString(m_filepath); } + + std::string Format() override { return "bdb_ro"; } + + /** Make a DatabaseBatch connected to this database */ + std::unique_ptr MakeBatch(bool flush_on_close = true) override; +}; + +class BerkeleyROCursor : public DatabaseCursor +{ +public: + Status Next(DataStream& key, DataStream& value) override; +}; + +/** RAII class that provides access to a BerkeleyRODatabase */ +class BerkeleyROBatch : public DatabaseBatch +{ +private: + const BerkeleyRODatabase& m_database; + + bool ReadKey(DataStream&& key, DataStream& value) override; + // WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail. + // It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration. + bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; } + bool EraseKey(DataStream&& key) override { return false; } + bool HasKey(DataStream&& key) override; + bool ErasePrefix(Span prefix) override { return false; } + +public: + explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {} + ~BerkeleyROBatch() {} + + BerkeleyROBatch(const BerkeleyROBatch&) = delete; + BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete; + + void Flush() override {} + void Close() override {} + + std::unique_ptr GetNewCursor() override { return std::make_unique(); } + std::unique_ptr GetNewPrefixCursor(Span prefix) override { return std::make_unique(); } + bool TxnBegin() override { return false; } + bool TxnCommit() override { return false; } + bool TxnAbort() override { return false; } +}; +} // namespace wallet + +#endif // BITCOIN_WALLET_MIGRATE_H