Branch data Line data Source code
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 : :
17 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
18 : : {
19 [ + - ]: 1 : TxIndex txindex(interfaces::MakeChain(m_node), 1 << 20, true);
20 [ + - + - : 2 : BOOST_REQUIRE(txindex.Init());
+ - ]
21 : :
22 : 1 : CTransactionRef tx_disk;
23 : 1 : uint256 block_hash;
24 : :
25 : : // Transaction should not be found in the index before it is started.
26 [ + + ]: 101 : for (const auto& txn : m_coinbase_txns) {
27 [ + - + - : 200 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
+ - ]
28 : : }
29 : :
30 : : // BlockUntilSyncedToCurrentChain should return false before txindex is started.
31 [ + - + - : 2 : BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
+ - + - ]
32 : :
33 [ + - + - : 2 : BOOST_REQUIRE(txindex.StartBackgroundSync());
+ - + - ]
34 : :
35 : : // Allow tx index to catch up with the block index.
36 [ + - + - ]: 1 : IndexWaitSynced(txindex, *Assert(m_node.shutdown_signal));
37 : :
38 : : // Check that txindex excludes genesis block transactions.
39 [ + - ]: 1 : const CBlock& genesis_block = Params().GenesisBlock();
40 [ + + ]: 2 : for (const auto& txn : genesis_block.vtx) {
41 [ + - + - : 2 : 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 [ + + ]: 101 : for (const auto& txn : m_coinbase_txns) {
46 [ + - - + ]: 100 : if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
47 [ # # # # ]: 0 : BOOST_ERROR("FindTx failed");
48 [ - + ]: 100 : } else if (tx_disk->GetHash() != txn->GetHash()) {
49 [ # # # # ]: 0 : BOOST_ERROR("Read incorrect tx");
50 : : }
51 : : }
52 : :
53 : : // Check that new transactions in new blocks make it into the index.
54 [ + + ]: 11 : for (int i = 0; i < 10; i++) {
55 [ + - + - : 10 : CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
+ - ]
56 : 10 : std::vector<CMutableTransaction> no_txns;
57 [ + - ]: 10 : const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
58 [ + - ]: 10 : const CTransaction& txn = *block.vtx[0];
59 : :
60 [ + - + - : 20 : BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
+ - + - ]
61 [ + - - + ]: 10 : if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
62 [ # # # # ]: 0 : BOOST_ERROR("FindTx failed");
63 [ - + ]: 10 : } else if (tx_disk->GetHash() != txn.GetHash()) {
64 [ # # # # ]: 0 : BOOST_ERROR("Read incorrect tx");
65 : : }
66 : 10 : }
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 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
75 : :
76 : : // shutdown sequence (c.f. Shutdown() in init.cpp)
77 [ + - ]: 1 : txindex.Stop();
78 : 1 : }
79 : :
80 : : BOOST_AUTO_TEST_SUITE_END()
|