|
| 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 | +{ |
| 19 | + CoinStatsIndex coin_stats_index{1 << 20, true}; |
| 20 | + |
| 21 | + CCoinsStats coin_stats{CoinStatsHashType::MUHASH}; |
| 22 | + const CBlockIndex* block_index; |
| 23 | + { |
| 24 | + LOCK(cs_main); |
| 25 | + block_index = ChainActive().Tip(); |
| 26 | + } |
| 27 | + |
| 28 | + // CoinStatsIndex should not be found before it is started. |
| 29 | + BOOST_CHECK(!coin_stats_index.LookUpStats(block_index, coin_stats)); |
| 30 | + |
| 31 | + // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex |
| 32 | + // is started. |
| 33 | + BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain()); |
| 34 | + |
| 35 | + 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 | + const auto timeout = GetTime<std::chrono::seconds>() + 120s; |
| 40 | + while (!coin_stats_index.BlockUntilSyncedToCurrentChain()) { |
| 41 | + BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>()); |
| 42 | + UninterruptibleSleep(100ms); |
| 43 | + } |
| 44 | + |
| 45 | + // Check that CoinStatsIndex works for genesis block. |
| 46 | + const CBlockIndex* genesis_block_index; |
| 47 | + { |
| 48 | + LOCK(cs_main); |
| 49 | + genesis_block_index = ChainActive().Genesis(); |
| 50 | + } |
| 51 | + BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index, coin_stats)); |
| 52 | + |
| 53 | + // Check that CoinStatsIndex updates with new blocks. |
| 54 | + coin_stats_index.LookUpStats(block_index, coin_stats); |
| 55 | + |
| 56 | + const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG}; |
| 57 | + std::vector<CMutableTransaction> noTxns; |
| 58 | + CreateAndProcessBlock(noTxns, script_pub_key); |
| 59 | + |
| 60 | + // Let the CoinStatsIndex to catch up again. |
| 61 | + BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain()); |
| 62 | + |
| 63 | + CCoinsStats new_coin_stats{CoinStatsHashType::MUHASH}; |
| 64 | + const CBlockIndex* new_block_index; |
| 65 | + { |
| 66 | + LOCK(cs_main); |
| 67 | + new_block_index = ChainActive().Tip(); |
| 68 | + } |
| 69 | + coin_stats_index.LookUpStats(new_block_index, new_coin_stats); |
| 70 | + |
| 71 | + BOOST_CHECK(block_index != new_block_index); |
| 72 | + |
| 73 | + // Shutdown sequence (c.f. Shutdown() in init.cpp) |
| 74 | + coin_stats_index.Stop(); |
| 75 | + |
| 76 | + // Rest of shutdown sequence and destructors happen in ~TestingSetup() |
| 77 | +} |
| 78 | + |
| 79 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments