LCOV - code coverage report
Current view: top level - src/wallet/test - util.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 10.1 % 129 13
Test Date: 2024-09-01 05:20:30 Functions: 12.5 % 16 2
Branches: 5.4 % 167 9

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 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 <streams.h>
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : #include <validationinterface.h>
      13                 :             : #include <wallet/context.h>
      14                 :             : #include <wallet/wallet.h>
      15                 :             : #include <wallet/walletdb.h>
      16                 :             : 
      17                 :             : #include <memory>
      18                 :             : 
      19                 :             : namespace wallet {
      20                 :           0 : std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key)
      21                 :             : {
      22   [ #  #  #  # ]:           0 :     auto wallet = std::make_unique<CWallet>(&chain, "", CreateMockableWalletDatabase());
      23                 :             :     {
      24   [ #  #  #  #  :           0 :         LOCK2(wallet->cs_wallet, ::cs_main);
             #  #  #  # ]
      25   [ #  #  #  #  :           0 :         wallet->SetLastBlockProcessed(cchain.Height(), cchain.Tip()->GetBlockHash());
             #  #  #  # ]
      26                 :           0 :     }
      27                 :             :     {
      28   [ #  #  #  # ]:           0 :         LOCK(wallet->cs_wallet);
      29         [ #  # ]:           0 :         wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
      30         [ #  # ]:           0 :         wallet->SetupDescriptorScriptPubKeyMans();
      31                 :             : 
      32                 :           0 :         FlatSigningProvider provider;
      33                 :           0 :         std::string error;
      34   [ #  #  #  #  :           0 :         auto descs = Parse("combo(" + EncodeSecret(key) + ")", provider, error, /* require_checksum=*/ false);
             #  #  #  # ]
      35         [ #  # ]:           0 :         assert(descs.size() == 1);
      36         [ #  # ]:           0 :         auto& desc = descs.at(0);
      37   [ #  #  #  # ]:           0 :         WalletDescriptor w_desc(std::move(desc), 0, 0, 1, 1);
      38   [ #  #  #  #  :           0 :         if (!wallet->AddWalletDescriptor(w_desc, provider, "", false)) assert(false);
                   #  # ]
      39                 :           0 :     }
      40         [ #  # ]:           0 :     WalletRescanReserver reserver(*wallet);
      41         [ #  # ]:           0 :     reserver.reserve();
      42   [ #  #  #  #  :           0 :     CWallet::ScanResult result = wallet->ScanForWalletTransactions(cchain.Genesis()->GetBlockHash(), /*start_height=*/0, /*max_height=*/{}, reserver, /*fUpdate=*/false, /*save_progress=*/false);
                   #  # ]
      43         [ #  # ]:           0 :     assert(result.status == CWallet::ScanResult::SUCCESS);
      44   [ #  #  #  #  :           0 :     assert(result.last_scanned_block == cchain.Tip()->GetBlockHash());
             #  #  #  # ]
      45   [ #  #  #  # ]:           0 :     assert(*result.last_scanned_height == cchain.Height());
      46   [ #  #  #  # ]:           0 :     assert(result.last_failed_block.IsNull());
      47                 :           0 :     return wallet;
      48         [ #  # ]:           0 : }
      49                 :             : 
      50                 :           0 : std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags)
      51                 :             : {
      52                 :           0 :     bilingual_str error;
      53                 :           0 :     std::vector<bilingual_str> warnings;
      54   [ #  #  #  # ]:           0 :     auto wallet = CWallet::Create(context, "", std::move(database), create_flags, error, warnings);
      55         [ #  # ]:           0 :     NotifyWalletLoaded(context, wallet);
      56         [ #  # ]:           0 :     if (context.chain) {
      57         [ #  # ]:           0 :         wallet->postInitProcess();
      58                 :           0 :     }
      59                 :           0 :     return wallet;
      60         [ #  # ]:           0 : }
      61                 :             : 
      62                 :           0 : std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context)
      63                 :             : {
      64                 :           0 :     DatabaseOptions options;
      65                 :           0 :     options.create_flags = WALLET_FLAG_DESCRIPTORS;
      66                 :           0 :     DatabaseStatus status;
      67                 :           0 :     bilingual_str error;
      68                 :           0 :     std::vector<bilingual_str> warnings;
      69   [ #  #  #  # ]:           0 :     auto database = MakeWalletDatabase("", options, status, error);
      70         [ #  # ]:           0 :     return TestLoadWallet(std::move(database), context, options.create_flags);
      71                 :           0 : }
      72                 :             : 
      73                 :           0 : void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet)
      74                 :             : {
      75                 :             :     // Calls SyncWithValidationInterfaceQueue
      76                 :           0 :     wallet->chain().waitForNotificationsIfTipChanged({});
      77                 :           0 :     wallet->m_chain_notifications_handler.reset();
      78                 :           0 :     WaitForDeleteWallet(std::move(wallet));
      79                 :           0 : }
      80                 :             : 
      81                 :           0 : std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database)
      82                 :             : {
      83         [ #  # ]:           0 :     return std::make_unique<MockableDatabase>(dynamic_cast<MockableDatabase&>(database).m_records);
      84                 :             : }
      85                 :             : 
      86                 :           0 : std::string getnewaddress(CWallet& w)
      87                 :             : {
      88                 :           0 :     constexpr auto output_type = OutputType::BECH32;
      89         [ #  # ]:           0 :     return EncodeDestination(getNewDestination(w, output_type));
      90                 :           0 : }
      91                 :             : 
      92                 :           0 : CTxDestination getNewDestination(CWallet& w, OutputType output_type)
      93                 :             : {
      94   [ #  #  #  #  :           0 :     return *Assert(w.GetNewDestination(output_type, ""));
          #  #  #  #  #  
                      # ]
      95                 :           0 : }
      96                 :             : 
      97                 :           0 : MockableCursor::MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix)
      98                 :           0 : {
      99                 :           0 :     m_pass = pass;
     100         [ #  # ]:           0 :     std::tie(m_cursor, m_cursor_end) = records.equal_range(BytePrefix{prefix});
     101                 :           0 : }
     102                 :             : 
     103                 :           0 : DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
     104                 :             : {
     105         [ #  # ]:           0 :     if (!m_pass) {
     106                 :           0 :         return Status::FAIL;
     107                 :             :     }
     108         [ #  # ]:           0 :     if (m_cursor == m_cursor_end) {
     109                 :           0 :         return Status::DONE;
     110                 :             :     }
     111                 :           0 :     key.clear();
     112                 :           0 :     value.clear();
     113                 :           0 :     const auto& [key_data, value_data] = *m_cursor;
     114                 :           0 :     key.write(key_data);
     115                 :           0 :     value.write(value_data);
     116                 :           0 :     m_cursor++;
     117                 :           0 :     return Status::MORE;
     118                 :           0 : }
     119                 :             : 
     120                 :           0 : bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
     121                 :             : {
     122         [ #  # ]:           0 :     if (!m_pass) {
     123                 :           0 :         return false;
     124                 :             :     }
     125                 :           0 :     SerializeData key_data{key.begin(), key.end()};
     126         [ #  # ]:           0 :     const auto& it = m_records.find(key_data);
     127         [ #  # ]:           0 :     if (it == m_records.end()) {
     128                 :           0 :         return false;
     129                 :             :     }
     130         [ #  # ]:           0 :     value.clear();
     131   [ #  #  #  # ]:           0 :     value.write(it->second);
     132                 :           0 :     return true;
     133                 :           0 : }
     134                 :             : 
     135                 :     3677997 : bool MockableBatch::WriteKey(DataStream&& key, DataStream&& value, bool overwrite)
     136                 :             : {
     137         [ -  + ]:     3677997 :     if (!m_pass) {
     138                 :           0 :         return false;
     139                 :             :     }
     140                 :     3677997 :     SerializeData key_data{key.begin(), key.end()};
     141   [ +  -  +  -  :     3677997 :     SerializeData value_data{value.begin(), value.end()};
                   +  - ]
     142         [ +  - ]:     6037656 :     auto [it, inserted] = m_records.emplace(key_data, value_data);
     143   [ +  +  -  + ]:     3677997 :     if (!inserted && overwrite) { // Overwrite if requested
     144         [ +  - ]:     2359659 :         it->second = value_data;
     145                 :     2359659 :         inserted = true;
     146                 :     2359659 :     }
     147                 :     3677997 :     return inserted;
     148                 :     3677997 : }
     149                 :             : 
     150                 :           0 : bool MockableBatch::EraseKey(DataStream&& key)
     151                 :             : {
     152         [ #  # ]:           0 :     if (!m_pass) {
     153                 :           0 :         return false;
     154                 :             :     }
     155                 :           0 :     SerializeData key_data{key.begin(), key.end()};
     156         [ #  # ]:           0 :     m_records.erase(key_data);
     157                 :           0 :     return true;
     158                 :           0 : }
     159                 :             : 
     160                 :           0 : bool MockableBatch::HasKey(DataStream&& key)
     161                 :             : {
     162         [ #  # ]:           0 :     if (!m_pass) {
     163                 :           0 :         return false;
     164                 :             :     }
     165                 :           0 :     SerializeData key_data{key.begin(), key.end()};
     166         [ #  # ]:           0 :     return m_records.count(key_data) > 0;
     167                 :           0 : }
     168                 :             : 
     169                 :           0 : bool MockableBatch::ErasePrefix(Span<const std::byte> prefix)
     170                 :             : {
     171         [ #  # ]:           0 :     if (!m_pass) {
     172                 :           0 :         return false;
     173                 :             :     }
     174                 :           0 :     auto it = m_records.begin();
     175         [ #  # ]:           0 :     while (it != m_records.end()) {
     176                 :           0 :         auto& key = it->first;
     177   [ #  #  #  # ]:           0 :         if (key.size() < prefix.size() || std::search(key.begin(), key.end(), prefix.begin(), prefix.end()) != key.begin()) {
     178                 :           0 :             it++;
     179                 :           0 :             continue;
     180                 :             :         }
     181                 :           0 :         it = m_records.erase(it);
     182      [ #  #  # ]:           0 :     }
     183                 :           0 :     return true;
     184                 :           0 : }
     185                 :             : 
     186                 :       15145 : std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records)
     187                 :             : {
     188                 :       15145 :     return std::make_unique<MockableDatabase>(records);
     189                 :             : }
     190                 :             : 
     191                 :           0 : MockableDatabase& GetMockableDatabase(CWallet& wallet)
     192                 :             : {
     193         [ #  # ]:           0 :     return dynamic_cast<MockableDatabase&>(wallet.GetDatabase());
     194                 :             : }
     195                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1