Bitcoin Core  27.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 <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 
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.
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 
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
node::NodeContext m_node
Definition: bitcoin-gui.cpp:37
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:77
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:398
bool Init()
Initializes the sync state and registers the instance to the validation interface so that it stays in...
Definition: base.cpp:79
bool StartBackgroundSync()
Starts the initial sync process on a background thread.
Definition: base.cpp:390
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
const CBlock & GenesisBlock() const
Definition: chainparams.h:97
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:296
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
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:106
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
void IndexWaitSynced(const BaseIndex &index, const util::SignalInterrupt &interrupt)
Block until the index is synced to the current chain.
Definition: index.cpp:12
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
Definition: interfaces.cpp:835
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:104
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:77
util::SignalInterrupt * shutdown
Interrupt object used to track whether node shutdown was requested.
Definition: context.h:55
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)