migrate.h raw

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