util.h raw

   1  // Copyright (c) 2021-2022 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_TEST_UTIL_H
   6  #define LIMENKA_WALLET_TEST_UTIL_H
   7  
   8  #include <limenka-build-config.h> // IWYU pragma: keep
   9  
  10  #include <addresstype.h>
  11  #include <wallet/db.h>
  12  #include <wallet/scriptpubkeyman.h>
  13  
  14  #include <memory>
  15  
  16  class ArgsManager;
  17  class CChain;
  18  class CKey;
  19  enum class OutputType;
  20  namespace interfaces {
  21  class Chain;
  22  } // namespace interfaces
  23  
  24  namespace wallet {
  25  class CWallet;
  26  class WalletDatabase;
  27  struct WalletContext;
  28  
  29  static const DatabaseFormat DATABASE_FORMATS[] = {
  30  #ifdef USE_SQLITE
  31         DatabaseFormat::SQLITE,
  32  #endif
  33  #ifdef USE_BDB
  34         DatabaseFormat::BERKELEY,
  35  #endif
  36  };
  37  
  38  const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
  39  
  40  std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
  41  
  42  std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
  43  std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
  44  void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
  45  
  46  // Creates a copy of the provided database
  47  std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
  48  
  49  /** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
  50  std::string getnewaddress(CWallet& w);
  51  /** Returns a new destination, of an specific type, from the wallet */
  52  CTxDestination getNewDestination(CWallet& w, OutputType output_type);
  53  
  54  using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
  55  
  56  class MockableCursor: public DatabaseCursor
  57  {
  58  public:
  59      MockableData::const_iterator m_cursor;
  60      MockableData::const_iterator m_cursor_end;
  61      bool m_pass;
  62  
  63      explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
  64      MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
  65      ~MockableCursor() = default;
  66  
  67      Status Next(DataStream& key, DataStream& value) override;
  68  };
  69  
  70  class MockableBatch : public DatabaseBatch
  71  {
  72  private:
  73      MockableData& m_records;
  74      bool m_pass;
  75  
  76      bool ReadKey(DataStream&& key, DataStream& value) override;
  77      bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
  78      bool EraseKey(DataStream&& key) override;
  79      bool HasKey(DataStream&& key) override;
  80      bool ErasePrefix(Span<const std::byte> prefix) override;
  81  
  82  public:
  83      explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
  84      ~MockableBatch() = default;
  85  
  86      void Flush() override {}
  87      void Close() override {}
  88  
  89      std::unique_ptr<DatabaseCursor> GetNewCursor() override
  90      {
  91          return std::make_unique<MockableCursor>(m_records, m_pass);
  92      }
  93      std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
  94          return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
  95      }
  96      bool TxnBegin() override { return m_pass; }
  97      bool TxnCommit() override { return m_pass; }
  98      bool TxnAbort() override { return m_pass; }
  99      bool HasActiveTxn() override { return false; }
 100  };
 101  
 102  /** A WalletDatabase whose contents and return values can be modified as needed for testing
 103   **/
 104  class MockableDatabase : public WalletDatabase
 105  {
 106  public:
 107      MockableData m_records;
 108      bool m_pass{true};
 109  
 110      MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
 111      ~MockableDatabase() = default;
 112  
 113      void Open() override {}
 114      void AddRef() override {}
 115      void RemoveRef() override {}
 116  
 117      bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
 118      bool Backup(const std::string& strDest) const override { return m_pass; }
 119      void Flush() override {}
 120      void Close() override {}
 121      bool PeriodicFlush() override { return m_pass; }
 122      void IncrementUpdateCounter() override {}
 123      void ReloadDbEnv() override {}
 124  
 125      std::string Filename() override { return "mockable"; }
 126      std::vector<fs::path> Files() override { return {}; }
 127      std::string Format() override { return "mock"; }
 128      std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
 129  };
 130  
 131  std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
 132  MockableDatabase& GetMockableDatabase(CWallet& wallet);
 133  
 134  ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success);
 135  } // namespace wallet
 136  
 137  #endif // LIMENKA_WALLET_TEST_UTIL_H
 138