txindex_tests.cpp raw

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