Bitcoin Core  22.99.0
P2P Digital Currency
txindex_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 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>
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 
17 {
18  TxIndex txindex(1 << 20, true);
19 
20  CTransactionRef tx_disk;
21  uint256 block_hash;
22 
23  // Transaction should not be found in the index before it is started.
24  for (const auto& txn : m_coinbase_txns) {
25  BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
26  }
27 
28  // BlockUntilSyncedToCurrentChain should return false before txindex is started.
29  BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
30 
31  BOOST_REQUIRE(txindex.Start(m_node.chainman->ActiveChainstate()));
32 
33  // Allow tx index to catch up with the block index.
34  constexpr int64_t timeout_ms = 10 * 1000;
35  int64_t time_start = GetTimeMillis();
36  while (!txindex.BlockUntilSyncedToCurrentChain()) {
37  BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
38  UninterruptibleSleep(std::chrono::milliseconds{100});
39  }
40 
41  // Check that txindex excludes genesis block transactions.
42  const CBlock& genesis_block = Params().GenesisBlock();
43  for (const auto& txn : genesis_block.vtx) {
44  BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
45  }
46 
47  // Check that txindex has all txs that were in the chain before it started.
48  for (const auto& txn : m_coinbase_txns) {
49  if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
50  BOOST_ERROR("FindTx failed");
51  } else if (tx_disk->GetHash() != txn->GetHash()) {
52  BOOST_ERROR("Read incorrect tx");
53  }
54  }
55 
56  // Check that new transactions in new blocks make it into the index.
57  for (int i = 0; i < 10; i++) {
58  CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
59  std::vector<CMutableTransaction> no_txns;
60  const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
61  const CTransaction& txn = *block.vtx[0];
62 
63  BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
64  if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
65  BOOST_ERROR("FindTx failed");
66  } else if (tx_disk->GetHash() != txn.GetHash()) {
67  BOOST_ERROR("Read incorrect tx");
68  }
69  }
70 
71  // shutdown sequence (c.f. Shutdown() in init.cpp)
72  txindex.Stop();
73 
74  // Let scheduler events finish running to avoid accessing any memory related to txindex after it is destructed
76 }
77 
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
setup_common.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
m_node
NodeContext & m_node
Definition: bitcoin-node.cpp:38
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
Definition: txindex_tests.cpp:16
chainparams.h
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
txindex.h
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:160
BaseIndex::Stop
void Stop()
Stops the instance from staying in sync with blockchain updates.
Definition: base.cpp:355
time.h
standard.h
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:116
CChainParams::GenesisBlock
const CBlock & GenesisBlock() const
Definition: chainparams.h:95
uint256
256-bit opaque blob.
Definition: uint256.h:124
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
CBlock
Definition: block.h:62
PKHash
Definition: standard.h:79
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition: block.h:66
NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:47
TxIndex::FindTx
bool FindTx(const uint256 &tx_hash, uint256 &block_hash, CTransactionRef &tx) const
Look up a transaction by hash.
Definition: txindex.cpp:72
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:559
TxIndex
TxIndex is used to look up transactions included in the blockchain by hash.
Definition: txindex.h:15
CTransaction::GetHash
const uint256 & GetHash() const
Definition: transaction.h:302
BaseIndex::Start
bool Start(CChainState &active_chainstate)
Start initializes the sync state and registers the instance as a ValidationInterface so that it stays...
Definition: base.cpp:341
GetTimeMillis
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17