Branch data Line data Source code
# 1 : : // Copyright (c) 2020 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/coinstatsindex.h> # 6 : : #include <test/util/setup_common.h> # 7 : : #include <util/time.h> # 8 : : #include <validation.h> # 9 : : # 10 : : #include <boost/test/unit_test.hpp> # 11 : : # 12 : : #include <chrono> # 13 : : # 14 : : # 15 : : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests) # 16 : : # 17 : : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup) # 18 : 2 : { # 19 : 2 : CoinStatsIndex coin_stats_index{1 << 20, true}; # 20 : : # 21 : 2 : CCoinsStats coin_stats{CoinStatsHashType::MUHASH}; # 22 : 2 : const CBlockIndex* block_index; # 23 : 2 : { # 24 : 2 : LOCK(cs_main); # 25 : 2 : block_index = ChainActive().Tip(); # 26 : 2 : } # 27 : : # 28 : : // CoinStatsIndex should not be found before it is started. # 29 : 2 : BOOST_CHECK(!coin_stats_index.LookUpStats(block_index, coin_stats)); # 30 : : # 31 : : // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex # 32 : : // is started. # 33 : 2 : BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain()); # 34 : : # 35 : 2 : BOOST_REQUIRE(coin_stats_index.Start()); # 36 : : # 37 : : // Allow the CoinStatsIndex to catch up with the block index that is syncing # 38 : : // in a background thread. # 39 : 2 : const auto timeout = GetTime<std::chrono::seconds>() + 120s; # 40 [ + + ]: 164 : while (!coin_stats_index.BlockUntilSyncedToCurrentChain()) { # 41 : 162 : BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>()); # 42 : 162 : UninterruptibleSleep(100ms); # 43 : 162 : } # 44 : : # 45 : : // Check that CoinStatsIndex works for genesis block. # 46 : 2 : const CBlockIndex* genesis_block_index; # 47 : 2 : { # 48 : 2 : LOCK(cs_main); # 49 : 2 : genesis_block_index = ChainActive().Genesis(); # 50 : 2 : } # 51 : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index, coin_stats)); # 52 : : # 53 : : // Check that CoinStatsIndex updates with new blocks. # 54 : 2 : coin_stats_index.LookUpStats(block_index, coin_stats); # 55 : : # 56 : 2 : const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG}; # 57 : 2 : std::vector<CMutableTransaction> noTxns; # 58 : 2 : CreateAndProcessBlock(noTxns, script_pub_key); # 59 : : # 60 : : // Let the CoinStatsIndex to catch up again. # 61 : 2 : BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain()); # 62 : : # 63 : 2 : CCoinsStats new_coin_stats{CoinStatsHashType::MUHASH}; # 64 : 2 : const CBlockIndex* new_block_index; # 65 : 2 : { # 66 : 2 : LOCK(cs_main); # 67 : 2 : new_block_index = ChainActive().Tip(); # 68 : 2 : } # 69 : 2 : coin_stats_index.LookUpStats(new_block_index, new_coin_stats); # 70 : : # 71 : 2 : BOOST_CHECK(block_index != new_block_index); # 72 : : # 73 : : // Shutdown sequence (c.f. Shutdown() in init.cpp) # 74 : 2 : coin_stats_index.Stop(); # 75 : : # 76 : : // Rest of shutdown sequence and destructors happen in ~TestingSetup() # 77 : 2 : } # 78 : : # 79 : : BOOST_AUTO_TEST_SUITE_END()