Bitcoin Core  24.99.0
P2P Digital Currency
txindex_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 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 <interfaces/chain.h>
8 #include <script/standard.h>
10 #include <util/time.h>
11 #include <validation.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
15 BOOST_AUTO_TEST_SUITE(txindex_tests)
16 
18 {
19  TxIndex txindex(interfaces::MakeChain(m_node), 1 << 20, true);
20 
21  CTransactionRef tx_disk;
22  uint256 block_hash;
23 
24  // Transaction should not be found in the index before it is started.
25  for (const auto& txn : m_coinbase_txns) {
26  BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
27  }
28 
29  // BlockUntilSyncedToCurrentChain should return false before txindex is started.
30  BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
31 
32  BOOST_REQUIRE(txindex.Start());
33 
34  // Allow tx index to catch up with the block index.
35  constexpr int64_t timeout_ms = 10 * 1000;
36  int64_t time_start = GetTimeMillis();
37  while (!txindex.BlockUntilSyncedToCurrentChain()) {
38  BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
39  UninterruptibleSleep(std::chrono::milliseconds{100});
40  }
41 
42  // Check that txindex excludes genesis block transactions.
43  const CBlock& genesis_block = Params().GenesisBlock();
44  for (const auto& txn : genesis_block.vtx) {
45  BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
46  }
47 
48  // Check that txindex has all txs that were in the chain before it started.
49  for (const auto& txn : m_coinbase_txns) {
50  if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
51  BOOST_ERROR("FindTx failed");
52  } else if (tx_disk->GetHash() != txn->GetHash()) {
53  BOOST_ERROR("Read incorrect tx");
54  }
55  }
56 
57  // Check that new transactions in new blocks make it into the index.
58  for (int i = 0; i < 10; i++) {
59  CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
60  std::vector<CMutableTransaction> no_txns;
61  const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
62  const CTransaction& txn = *block.vtx[0];
63 
64  BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
65  if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
66  BOOST_ERROR("FindTx failed");
67  } else if (tx_disk->GetHash() != txn.GetHash()) {
68  BOOST_ERROR("Read incorrect tx");
69  }
70  }
71 
72  // It is not safe to stop and destroy the index until it finishes handling
73  // the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
74  // call above is sufficient to ensure this, but the
75  // SyncWithValidationInterfaceQueue() call below is also needed to ensure
76  // TSAN always sees the test thread waiting for the notification thread, and
77  // avoid potential false positive reports.
79 
80  // shutdown sequence (c.f. Shutdown() in init.cpp)
81  txindex.Stop();
82 }
83 
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
const CChainParams & Params()
Return the currently selected parameters.
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:400
bool Start()
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:381
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
const CBlock & GenesisBlock() const
Definition: chainparams.h:95
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
const uint256 & GetHash() const
Definition: transaction.h:337
TxIndex is used to look up transactions included in the blockchain by hash.
Definition: txindex.h:18
bool FindTx(const uint256 &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Definition: txindex.cpp:75
256-bit opaque blob.
Definition: uint256.h:119
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:797
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:334
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:134
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:105
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...