Branch data Line data Source code
# 1 : : // Copyright (c) 2017-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/txindex.h> # 7 : : #include <script/standard.h> # 8 : : #include <test/util/setup_common.h> # 9 : : #include <util/time.h> # 10 : : #include <validation.h> # 11 : : # 12 : : #include <boost/test/unit_test.hpp> # 13 : : # 14 : : BOOST_AUTO_TEST_SUITE(txindex_tests) # 15 : : # 16 : : BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) # 17 : 2 : { # 18 : 2 : TxIndex txindex(1 << 20, true); # 19 : : # 20 : 2 : CTransactionRef tx_disk; # 21 : 2 : uint256 block_hash; # 22 : : # 23 : : // Transaction should not be found in the index before it is started. # 24 [ + + ]: 200 : for (const auto& txn : m_coinbase_txns) { # 25 : 200 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)); # 26 : 200 : } # 27 : : # 28 : : // BlockUntilSyncedToCurrentChain should return false before txindex is started. # 29 : 2 : BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain()); # 30 : : # 31 : 2 : BOOST_REQUIRE(txindex.Start(m_node.chainman->ActiveChainstate())); # 32 : : # 33 : : // Allow tx index to catch up with the block index. # 34 : 2 : constexpr int64_t timeout_ms = 10 * 1000; # 35 : 2 : int64_t time_start = GetTimeMillis(); # 36 [ + + ]: 4 : while (!txindex.BlockUntilSyncedToCurrentChain()) { # 37 : 2 : BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis()); # 38 : 2 : UninterruptibleSleep(std::chrono::milliseconds{100}); # 39 : 2 : } # 40 : : # 41 : : // Check that txindex excludes genesis block transactions. # 42 : 2 : const CBlock& genesis_block = Params().GenesisBlock(); # 43 [ + + ]: 2 : for (const auto& txn : genesis_block.vtx) { # 44 : 2 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)); # 45 : 2 : } # 46 : : # 47 : : // Check that txindex has all txs that were in the chain before it started. # 48 [ + + ]: 200 : for (const auto& txn : m_coinbase_txns) { # 49 [ - + ]: 200 : if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) { # 50 : 0 : BOOST_ERROR("FindTx failed"); # 51 [ - + ]: 200 : } else if (tx_disk->GetHash() != txn->GetHash()) { # 52 : 0 : BOOST_ERROR("Read incorrect tx"); # 53 : 0 : } # 54 : 200 : } # 55 : : # 56 : : // Check that new transactions in new blocks make it into the index. # 57 [ + + ]: 22 : for (int i = 0; i < 10; i++) { # 58 : 20 : CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey())); # 59 : 20 : std::vector<CMutableTransaction> no_txns; # 60 : 20 : const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key); # 61 : 20 : const CTransaction& txn = *block.vtx[0]; # 62 : : # 63 : 20 : BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain()); # 64 [ - + ]: 20 : if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) { # 65 : 0 : BOOST_ERROR("FindTx failed"); # 66 [ - + ]: 20 : } else if (tx_disk->GetHash() != txn.GetHash()) { # 67 : 0 : BOOST_ERROR("Read incorrect tx"); # 68 : 0 : } # 69 : 20 : } # 70 : : # 71 : : // shutdown sequence (c.f. Shutdown() in init.cpp) # 72 : 2 : txindex.Stop(); # 73 : : # 74 : : // Let scheduler events finish running to avoid accessing any memory related to txindex after it is destructed # 75 : 2 : SyncWithValidationInterfaceQueue(); # 76 : 2 : } # 77 : : # 78 : : BOOST_AUTO_TEST_SUITE_END()