LCOV - code coverage report
Current view: top level - src/test - txospenderindex_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 41 41
Test Date: 2026-02-25 05:45:00 Functions: 100.0 % 3 3
Branches: 52.1 % 194 101

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 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 <index/txospenderindex.h>
       6                 :             : #include <test/util/setup_common.h>
       7                 :             : #include <validation.h>
       8                 :             : 
       9                 :             : #include <boost/test/unit_test.hpp>
      10                 :             : 
      11                 :             : BOOST_AUTO_TEST_SUITE(txospenderindex_tests)
      12                 :             : 
      13   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(txospenderindex_initial_sync, TestChain100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      14                 :             : {
      15                 :             :     // Setup phase:
      16                 :             :     // Mine blocks for coinbase maturity, so we can spend some coinbase outputs in the test.
      17                 :           1 :     const CScript& coinbase_script = m_coinbase_txns[0]->vout[0].scriptPubKey;
      18   [ +  -  +  + ]:          11 :     for (int i = 0; i < 10; i++) CreateAndProcessBlock({}, coinbase_script);
      19                 :             : 
      20                 :             :     // Spend 10 outputs
      21                 :           1 :     std::vector<COutPoint> spent(10);
      22   [ -  +  +  - ]:           1 :     std::vector<CMutableTransaction> spender(spent.size());
      23   [ -  +  +  + ]:          11 :     for (size_t i = 0; i < spent.size(); i++) {
      24                 :             :         // Outpoint
      25         [ +  - ]:          10 :         auto coinbase_tx = m_coinbase_txns[i];
      26         [ +  - ]:          10 :         spent[i] = COutPoint(coinbase_tx->GetHash(), 0);
      27                 :             : 
      28                 :             :         // Spending tx
      29                 :          10 :         spender[i].version = 1;
      30         [ +  - ]:          10 :         spender[i].vin.resize(1);
      31         [ +  - ]:          10 :         spender[i].vin[0].prevout.hash = spent[i].hash;
      32                 :          10 :         spender[i].vin[0].prevout.n = spent[i].n;
      33         [ +  - ]:          10 :         spender[i].vout.resize(1);
      34         [ +  - ]:          10 :         spender[i].vout[0].nValue = coinbase_tx->GetValueOut();
      35                 :          10 :         spender[i].vout[0].scriptPubKey = coinbase_script;
      36                 :             : 
      37                 :             :         // Sign
      38                 :          10 :         std::vector<unsigned char> vchSig;
      39         [ +  - ]:          10 :         const uint256 hash = SignatureHash(coinbase_script, spender[i], 0, SIGHASH_ALL, 0, SigVersion::BASE);
      40   [ +  -  +  -  :          20 :         BOOST_REQUIRE(coinbaseKey.Sign(hash, vchSig));
             +  -  +  - ]
      41         [ +  - ]:          10 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
      42         [ -  + ]:          10 :         spender[i].vin[0].scriptSig << vchSig;
      43         [ +  - ]:          20 :     }
      44                 :             : 
      45                 :             :     // Generate and ensure block has been fully processed
      46   [ +  -  +  - ]:           1 :     const uint256 tip_hash = CreateAndProcessBlock(spender, coinbase_script).GetHash();
      47         [ +  - ]:           1 :     m_node.validation_signals->SyncWithValidationInterfaceQueue();
      48   [ +  -  +  -  :           3 :     BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return m_node.chainman->ActiveTip()->GetBlockHash()), tip_hash);
                   +  - ]
      49                 :             : 
      50                 :             :     // Now we concluded the setup phase, run index
      51   [ +  -  +  - ]:           1 :     TxoSpenderIndex txospenderindex(interfaces::MakeChain(m_node), 1 << 20, true);
      52   [ +  -  +  -  :           2 :     BOOST_REQUIRE(txospenderindex.Init());
             +  -  +  - ]
      53   [ +  -  +  -  :           2 :     BOOST_CHECK(!txospenderindex.BlockUntilSyncedToCurrentChain()); // false when not synced
             +  -  +  - ]
      54   [ +  -  +  -  :           1 :     BOOST_CHECK_NE(txospenderindex.GetSummary().best_block_hash, tip_hash);
                   +  - ]
      55                 :             : 
      56                 :             :     // Transaction should not be found in the index before it is synced.
      57         [ +  + ]:          11 :     for (const auto& outpoint : spent) {
      58   [ +  -  +  -  :          30 :         BOOST_CHECK(!txospenderindex.FindSpender(outpoint).value());
                   +  - ]
      59                 :             :     }
      60                 :             : 
      61         [ +  - ]:           1 :     txospenderindex.Sync();
      62   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(txospenderindex.GetSummary().best_block_hash, tip_hash);
                   +  - ]
      63                 :             : 
      64   [ -  +  +  + ]:          11 :     for (size_t i = 0; i < spent.size(); i++) {
      65         [ +  - ]:          10 :         const auto tx_spender{txospenderindex.FindSpender(spent[i])};
      66   [ +  -  +  -  :          20 :         BOOST_REQUIRE(tx_spender.has_value());
                   +  - ]
      67   [ +  -  +  -  :          20 :         BOOST_REQUIRE(tx_spender->has_value());
                   +  - ]
      68   [ +  -  +  -  :          20 :         BOOST_CHECK_EQUAL((*tx_spender)->tx->GetHash(), spender[i].GetHash());
                   +  - ]
      69   [ +  -  +  - ]:          10 :         BOOST_CHECK_EQUAL((*tx_spender)->block_hash, tip_hash);
      70                 :          10 :     }
      71                 :             : 
      72         [ +  - ]:           1 :     txospenderindex.Stop();
      73                 :           1 : }
      74                 :             : 
      75                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1