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/index.h> 9 : #include <test/util/setup_common.h> 10 : #include <validation.h> 11 : 12 : #include <boost/test/unit_test.hpp> 13 : 14 146 : BOOST_AUTO_TEST_SUITE(txindex_tests) 15 : 16 149 : BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) 17 : { 18 1 : TxIndex txindex(1 << 20, true); 19 : 20 1 : CTransactionRef tx_disk; 21 1 : uint256 block_hash; 22 : 23 : // Transaction should not be found in the index before it is started. 24 101 : for (const auto& txn : m_coinbase_txns) { 25 100 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)); 26 : } 27 : 28 : // BlockUntilSyncedToCurrentChain should return false before txindex is started. 29 1 : BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain()); 30 : 31 1 : BOOST_REQUIRE(txindex.Start(m_node.chainman->ActiveChainstate())); 32 : 33 : // Allow tx index to catch up with the block index. 34 1 : IndexWaitSynced(txindex); 35 : 36 : // Check that txindex excludes genesis block transactions. 37 1 : const CBlock& genesis_block = Params().GenesisBlock(); 38 2 : for (const auto& txn : genesis_block.vtx) { 39 1 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)); 40 : } 41 : 42 : // Check that txindex has all txs that were in the chain before it started. 43 101 : for (const auto& txn : m_coinbase_txns) { 44 100 : if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) { 45 0 : BOOST_ERROR("FindTx failed"); 46 100 : } else if (tx_disk->GetHash() != txn->GetHash()) { 47 0 : BOOST_ERROR("Read incorrect tx"); 48 0 : } 49 : } 50 : 51 : // Check that new transactions in new blocks make it into the index. 52 11 : for (int i = 0; i < 10; i++) { 53 10 : CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey())); 54 10 : std::vector<CMutableTransaction> no_txns; 55 10 : const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key); 56 10 : const CTransaction& txn = *block.vtx[0]; 57 : 58 10 : BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain()); 59 10 : if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) { 60 0 : BOOST_ERROR("FindTx failed"); 61 10 : } else if (tx_disk->GetHash() != txn.GetHash()) { 62 0 : BOOST_ERROR("Read incorrect tx"); 63 0 : } 64 10 : } 65 : 66 : // It is not safe to stop and destroy the index until it finishes handling 67 : // the last BlockConnected notification. The BlockUntilSyncedToCurrentChain() 68 : // call above is sufficient to ensure this, but the 69 : // SyncWithValidationInterfaceQueue() call below is also needed to ensure 70 : // TSAN always sees the test thread waiting for the notification thread, and 71 : // avoid potential false positive reports. 72 1 : SyncWithValidationInterfaceQueue(); 73 : 74 : // shutdown sequence (c.f. Shutdown() in init.cpp) 75 1 : txindex.Stop(); 76 1 : } 77 : 78 146 : BOOST_AUTO_TEST_SUITE_END()