LCOV - code coverage report
Current view: top level - src/test - coinstatsindex_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 69 69 100.0 %
Date: 2022-04-21 14:51:19 Functions: 3 3 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 2 2 100.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2020-2021 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 <chainparams.h>
#       6                 :            : #include <index/coinstatsindex.h>
#       7                 :            : #include <test/util/setup_common.h>
#       8                 :            : #include <test/util/validation.h>
#       9                 :            : #include <util/time.h>
#      10                 :            : #include <validation.h>
#      11                 :            : 
#      12                 :            : #include <boost/test/unit_test.hpp>
#      13                 :            : 
#      14                 :            : #include <chrono>
#      15                 :            : 
#      16                 :            : using node::CCoinsStats;
#      17                 :            : using node::CoinStatsHashType;
#      18                 :            : 
#      19                 :            : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
#      20                 :            : 
#      21                 :            : static void IndexWaitSynced(BaseIndex& index)
#      22                 :          4 : {
#      23                 :            :     // Allow the CoinStatsIndex to catch up with the block index that is syncing
#      24                 :            :     // in a background thread.
#      25                 :          4 :     const auto timeout = GetTime<std::chrono::seconds>() + 120s;
#      26         [ +  + ]:        308 :     while (!index.BlockUntilSyncedToCurrentChain()) {
#      27                 :        304 :         BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
#      28                 :        304 :         UninterruptibleSleep(100ms);
#      29                 :        304 :     }
#      30                 :          4 : }
#      31                 :            : 
#      32                 :            : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
#      33                 :          2 : {
#      34                 :          2 :     CoinStatsIndex coin_stats_index{1 << 20, true};
#      35                 :            : 
#      36                 :          2 :     CCoinsStats coin_stats{CoinStatsHashType::MUHASH};
#      37                 :          2 :     const CBlockIndex* block_index;
#      38                 :          2 :     {
#      39                 :          2 :         LOCK(cs_main);
#      40                 :          2 :         block_index = m_node.chainman->ActiveChain().Tip();
#      41                 :          2 :     }
#      42                 :            : 
#      43                 :            :     // CoinStatsIndex should not be found before it is started.
#      44                 :          2 :     BOOST_CHECK(!coin_stats_index.LookUpStats(block_index, coin_stats));
#      45                 :            : 
#      46                 :            :     // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
#      47                 :            :     // is started.
#      48                 :          2 :     BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
#      49                 :            : 
#      50                 :          2 :     BOOST_REQUIRE(coin_stats_index.Start(m_node.chainman->ActiveChainstate()));
#      51                 :            : 
#      52                 :          2 :     IndexWaitSynced(coin_stats_index);
#      53                 :            : 
#      54                 :            :     // Check that CoinStatsIndex works for genesis block.
#      55                 :          2 :     const CBlockIndex* genesis_block_index;
#      56                 :          2 :     {
#      57                 :          2 :         LOCK(cs_main);
#      58                 :          2 :         genesis_block_index = m_node.chainman->ActiveChain().Genesis();
#      59                 :          2 :     }
#      60                 :          2 :     BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index, coin_stats));
#      61                 :            : 
#      62                 :            :     // Check that CoinStatsIndex updates with new blocks.
#      63                 :          2 :     coin_stats_index.LookUpStats(block_index, coin_stats);
#      64                 :            : 
#      65                 :          2 :     const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
#      66                 :          2 :     std::vector<CMutableTransaction> noTxns;
#      67                 :          2 :     CreateAndProcessBlock(noTxns, script_pub_key);
#      68                 :            : 
#      69                 :            :     // Let the CoinStatsIndex to catch up again.
#      70                 :          2 :     BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
#      71                 :            : 
#      72                 :          2 :     CCoinsStats new_coin_stats{CoinStatsHashType::MUHASH};
#      73                 :          2 :     const CBlockIndex* new_block_index;
#      74                 :          2 :     {
#      75                 :          2 :         LOCK(cs_main);
#      76                 :          2 :         new_block_index = m_node.chainman->ActiveChain().Tip();
#      77                 :          2 :     }
#      78                 :          2 :     coin_stats_index.LookUpStats(new_block_index, new_coin_stats);
#      79                 :            : 
#      80                 :          2 :     BOOST_CHECK(block_index != new_block_index);
#      81                 :            : 
#      82                 :            :     // Shutdown sequence (c.f. Shutdown() in init.cpp)
#      83                 :          2 :     coin_stats_index.Stop();
#      84                 :            : 
#      85                 :            :     // Rest of shutdown sequence and destructors happen in ~TestingSetup()
#      86                 :          2 : }
#      87                 :            : 
#      88                 :            : // Test shutdown between BlockConnected and ChainStateFlushed notifications,
#      89                 :            : // make sure index is not corrupted and is able to reload.
#      90                 :            : BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
#      91                 :          2 : {
#      92                 :          2 :     CChainState& chainstate = Assert(m_node.chainman)->ActiveChainstate();
#      93                 :          2 :     const CChainParams& params = Params();
#      94                 :          2 :     {
#      95                 :          2 :         CoinStatsIndex index{1 << 20};
#      96                 :          2 :         BOOST_REQUIRE(index.Start(chainstate));
#      97                 :          2 :         IndexWaitSynced(index);
#      98                 :          2 :         std::shared_ptr<const CBlock> new_block;
#      99                 :          2 :         CBlockIndex* new_block_index = nullptr;
#     100                 :          2 :         {
#     101                 :          2 :             const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
#     102                 :          2 :             const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
#     103                 :            : 
#     104                 :          2 :             new_block = std::make_shared<CBlock>(block);
#     105                 :            : 
#     106                 :          2 :             LOCK(cs_main);
#     107                 :          2 :             BlockValidationState state;
#     108                 :          2 :             BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
#     109                 :          2 :             BOOST_CHECK(chainstate.AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr));
#     110                 :          2 :             CCoinsViewCache view(&chainstate.CoinsTip());
#     111                 :          2 :             BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
#     112                 :          2 :         }
#     113                 :            :         // Send block connected notification, then stop the index without
#     114                 :            :         // sending a chainstate flushed notification. Prior to #24138, this
#     115                 :            :         // would cause the index to be corrupted and fail to reload.
#     116                 :          2 :         ValidationInterfaceTest::BlockConnected(index, new_block, new_block_index);
#     117                 :          2 :         index.Stop();
#     118                 :          2 :     }
#     119                 :            : 
#     120                 :          2 :     {
#     121                 :          2 :         CoinStatsIndex index{1 << 20};
#     122                 :            :         // Make sure the index can be loaded.
#     123                 :          2 :         BOOST_REQUIRE(index.Start(chainstate));
#     124                 :          2 :         index.Stop();
#     125                 :          2 :     }
#     126                 :          2 : }
#     127                 :            : 
#     128                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a