LCOV - code coverage report
Current view: top level - src/wallet/test - util.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 5.3 % 95 5
Test Date: 2026-11-13 06:27:04 Functions: 16.7 % 12 2
Branches: 0.0 % 150 0

             Branch data     Line data    Source code
       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                 :             : #include <wallet/test/util.h>
       6                 :             : 
       7                 :             : #include <chain.h>
       8                 :             : #include <key.h>
       9                 :             : #include <key_io.h>
      10                 :             : #include <test/util/setup_common.h>
      11                 :             : #include <validationinterface.h>
      12                 :             : #include <wallet/context.h>
      13                 :             : #include <wallet/scan.h>
      14                 :             : #include <wallet/wallet.h>
      15                 :             : #include <wallet/walletdb.h>
      16                 :             : 
      17                 :             : #include <sqlite3.h>
      18                 :             : 
      19                 :             : #include <memory>
      20                 :             : 
      21                 :             : namespace wallet {
      22                 :           0 : std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
      23                 :             : {
      24         [ #  # ]:           0 :     auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
      25                 :           0 :     {
      26   [ #  #  #  # ]:           0 :         LOCK2(wallet->cs_wallet, ::cs_main);
      27   [ #  #  #  # ]:           0 :         wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
      28         [ #  # ]:           0 :     }
      29                 :           0 :     {
      30         [ #  # ]:           0 :         LOCK(wallet->cs_wallet);
      31         [ #  # ]:           0 :         wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
      32         [ #  # ]:           0 :         wallet->SetupDescriptorScriptPubKeyMans();
      33                 :           0 :     }
      34         [ #  # ]:           0 :     AddKey(*wallet, key);
      35         [ #  # ]:           0 :     WalletRescanReserver reserver(*wallet);
      36         [ #  # ]:           0 :     reserver.reserve();
      37   [ #  #  #  #  :           0 :     ScanResult result = wallet->Scanner().Scan(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*save_progress=*/false);
                   #  # ]
      38         [ #  # ]:           0 :     assert(result.status == ScanResult::SUCCESS);
      39   [ #  #  #  # ]:           0 :     assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
      40   [ #  #  #  # ]:           0 :     assert(*result.last_scanned_height == cchain.Height());
      41         [ #  # ]:           0 :     assert(result.last_failed_block.IsNull());
      42                 :           0 :     return wallet;
      43                 :           0 : }
      44                 :             : 
      45                 :           0 : void AddKey(CWallet& wallet, const CKey& key)
      46                 :             : {
      47                 :           0 :     LOCK(wallet.cs_wallet);
      48                 :           0 :     FlatSigningProvider provider;
      49         [ #  # ]:           0 :     std::string error;
      50   [ #  #  #  #  :           0 :     auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
             #  #  #  # ]
      51   [ #  #  #  # ]:           0 :     assert(descs.size() == 1);
      52         [ #  # ]:           0 :     auto& desc = descs.at(0);
      53   [ #  #  #  # ]:           0 :     WalletDescriptor w_desc(std::move(desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/1);
      54   [ #  #  #  #  :           0 :     Assert(wallet.AddWalletDescriptor(w_desc, provider, /*label=*/"", /*internal=*/false));
                   #  # ]
      55         [ #  # ]:           0 : }
      56                 :             : 
      57                 :           0 : std::shared_ptr<CWallet> TestCreateWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
      58                 :             : {
      59         [ #  # ]:           0 :     bilingual_str _error;
      60                 :           0 :     std::vector<bilingual_str> _warnings;
      61   [ #  #  #  # ]:           0 :     auto wallet = CWallet::CreateNew(context, "", std::move(database), create_flags, /*born_encrypted=*/false, _error, _warnings);
      62         [ #  # ]:           0 :     NotifyWalletLoaded(context, wallet);
      63         [ #  # ]:           0 :     if (context.chain) {
      64         [ #  # ]:           0 :         wallet->postInitProcess();
      65                 :             :     }
      66                 :           0 :     return wallet;
      67                 :           0 : }
      68                 :             : 
      69                 :           0 : std::shared_ptr<CWallet> TestCreateWallet(WalletContext& context)
      70                 :             : {
      71         [ #  # ]:           0 :     DatabaseOptions options;
      72                 :           0 :     options.require_create = true;
      73                 :           0 :     options.create_flags = WALLET_FLAG_DESCRIPTORS;
      74                 :           0 :     DatabaseStatus status;
      75         [ #  # ]:           0 :     bilingual_str error;
      76   [ #  #  #  # ]:           0 :     auto database = MakeWalletDatabase("", options, status, error);
      77         [ #  # ]:           0 :     return TestCreateWallet(std::move(database), context, options.create_flags);
      78                 :           0 : }
      79                 :             : 
      80                 :             : 
      81                 :           0 : std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context)
      82                 :             : {
      83         [ #  # ]:           0 :     bilingual_str error;
      84                 :           0 :     std::vector<bilingual_str> warnings;
      85   [ #  #  #  # ]:           0 :     auto wallet = CWallet::LoadExisting(context, "", std::move(database), error, warnings);
      86         [ #  # ]:           0 :     NotifyWalletLoaded(context, wallet);
      87         [ #  # ]:           0 :     if (context.chain) {
      88         [ #  # ]:           0 :         wallet->postInitProcess();
      89                 :             :     }
      90                 :           0 :     return wallet;
      91                 :           0 : }
      92                 :             : 
      93                 :           0 : std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
      94                 :             : {
      95         [ #  # ]:           0 :     DatabaseOptions options;
      96                 :           0 :     options.require_existing = true;
      97                 :           0 :     DatabaseStatus status;
      98         [ #  # ]:           0 :     bilingual_str error;
      99   [ #  #  #  # ]:           0 :     auto database = MakeWalletDatabase("", options, status, error);
     100         [ #  # ]:           0 :     return TestLoadWallet(std::move(database), context);
     101                 :           0 : }
     102                 :             : 
     103                 :           0 : void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
     104                 :             : {
     105                 :             :     // Calls SyncWithValidationInterfaceQueue
     106                 :           0 :     wallet->chain().waitForNotificationsIfTipChanged({});
     107         [ #  # ]:           0 :     wallet->m_chain_notifications_handler.reset();
     108                 :           0 :     WaitForDeleteWallet(std::move(wallet));
     109                 :           0 : }
     110                 :             : 
     111                 :           0 : std::string getnewaddress(CWallet& w)
     112                 :             : {
     113                 :           0 :     constexpr auto output_type = OutputType::BECH32;
     114         [ #  # ]:           0 :     return EncodeDestination(getNewDestination(w, output_type));
     115                 :             : }
     116                 :             : 
     117                 :           0 : CTxDestination getNewDestination(CWallet& w, OutputType output_type)
     118                 :             : {
     119   [ #  #  #  #  :           0 :     return *Assert(w.GetNewDestination(output_type, ""));
                   #  # ]
     120                 :             : }
     121                 :             : 
     122                 :       11691 : MockableSQLiteDatabase::MockableSQLiteDatabase()
     123                 :       11691 :     : InMemoryWalletDatabase()
     124                 :       11691 : {}
     125                 :             : 
     126                 :       11691 : std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase()
     127                 :             : {
     128                 :       11691 :     return std::make_unique<MockableSQLiteDatabase>();
     129                 :             : }
     130                 :             : 
     131                 :           0 : wallet::DescriptorScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success)
     132                 :             : {
     133                 :           0 :     keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
     134                 :             : 
     135                 :           0 :     FlatSigningProvider keys;
     136         [ #  # ]:           0 :     std::string error;
     137   [ #  #  #  # ]:           0 :     auto parsed_descs = Parse(desc_str, keys, error, false);
     138         [ #  # ]:           0 :     Assert(success == (!parsed_descs.empty()));
     139         [ #  # ]:           0 :     if (!success) return nullptr;
     140         [ #  # ]:           0 :     auto& desc = parsed_descs.at(0);
     141                 :             : 
     142                 :           0 :     const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1;
     143                 :             : 
     144   [ #  #  #  # ]:           0 :     WalletDescriptor w_desc(std::move(desc), timestamp, range_start, range_end, next_index);
     145                 :             : 
     146         [ #  # ]:           0 :     LOCK(keystore.cs_wallet);
     147   [ #  #  #  #  :           0 :     auto spkm = Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false));
                   #  # ]
     148                 :           0 :     return &spkm.value().get();
     149         [ #  # ]:           0 : };
     150                 :             : } // namespace wallet
        

Generated by: LCOV version 2.5.0-full