util.h raw

   1  // Copyright (c) 2021-present 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_TEST_UTIL_H
   6  #define BITCOIN_WALLET_TEST_UTIL_H
   7  
   8  #include <addresstype.h>
   9  #include <wallet/db.h>
  10  #include <wallet/scriptpubkeyman.h>
  11  #include <wallet/sqlite.h>
  12  
  13  #include <memory>
  14  
  15  class ArgsManager;
  16  class CChain;
  17  class CKey;
  18  enum class OutputType;
  19  namespace interfaces {
  20  class Chain;
  21  } // namespace interfaces
  22  
  23  namespace wallet {
  24  class CWallet;
  25  class WalletDatabase;
  26  struct WalletContext;
  27  
  28  static const DatabaseFormat DATABASE_FORMATS[] = {
  29         DatabaseFormat::SQLITE,
  30  };
  31  
  32  const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
  33  
  34  std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
  35  
  36  std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context);
  37  std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
  38  std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
  39  std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context);
  40  void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
  41  
  42  /** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
  43  std::string getnewaddress(CWallet& w);
  44  /** Returns a new destination, of an specific type, from the wallet */
  45  CTxDestination getNewDestination(CWallet& w, OutputType output_type);
  46  
  47  using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
  48  
  49  
  50  class MockableSQLiteBatch : public SQLiteBatch
  51  {
  52  public:
  53      using SQLiteBatch::SQLiteBatch;
  54      using SQLiteBatch::WriteKey;
  55  };
  56  
  57  /** A WalletDatabase whose contents and return values can be modified as needed for testing
  58   **/
  59  class MockableSQLiteDatabase : public InMemoryWalletDatabase
  60  {
  61  public:
  62      MockableSQLiteDatabase();
  63  
  64      bool Backup(const std::string& strDest) const override { return true; }
  65  
  66      std::string Filename() override { return "mockable"; }
  67      std::string Format() override { return "sqlite-mock"; }
  68      std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableSQLiteBatch>(*this); }
  69  };
  70  
  71  std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase();
  72  MockableSQLiteDatabase& GetMockableDatabase(CWallet& wallet);
  73  
  74  DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, bool success);
  75  } // namespace wallet
  76  
  77  #endif // BITCOIN_WALLET_TEST_UTIL_H
  78