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>
8 #include <test/util/setup_common.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
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync,TestChain100Setup)16 BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
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
75 SyncWithValidationInterfaceQueue();
76 }
77
78 BOOST_AUTO_TEST_SUITE_END()
79