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/setup_common.h>
10 : : #include <validation.h>
11 : :
12 : : #include <boost/test/unit_test.hpp>
13 : :
14 : : BOOST_AUTO_TEST_SUITE(txindex_tests)
15 : :
16 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
17 : : {
18 [ + - ]: 1 : TxIndex txindex(interfaces::MakeChain(m_node), 1 << 20, true);
19 [ + - + - : 2 : BOOST_REQUIRE(txindex.Init());
+ - ]
20 : :
21 : 1 : CTransactionRef tx_disk;
22 : 1 : uint256 block_hash;
23 : :
24 : : // Transaction should not be found in the index before it is started.
25 [ + + ]: 101 : for (const auto& txn : m_coinbase_txns) {
26 [ + - + - : 200 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
+ - ]
27 : : }
28 : :
29 : : // BlockUntilSyncedToCurrentChain should return false before txindex is started.
30 [ + - + - : 2 : BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
+ - + - ]
31 : :
32 [ + - ]: 1 : txindex.Sync();
33 : :
34 : : // Check that txindex excludes genesis block transactions.
35 [ + - ]: 1 : const CBlock& genesis_block = Params().GenesisBlock();
36 [ + + ]: 2 : for (const auto& txn : genesis_block.vtx) {
37 [ + - + - : 2 : BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
+ - ]
38 : : }
39 : :
40 : : // Check that txindex has all txs that were in the chain before it started.
41 [ + + ]: 101 : for (const auto& txn : m_coinbase_txns) {
42 [ + - - + ]: 100 : if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
43 [ # # # # ]: 0 : BOOST_ERROR("FindTx failed");
44 [ - + ]: 100 : } else if (tx_disk->GetHash() != txn->GetHash()) {
45 [ # # # # ]: 0 : BOOST_ERROR("Read incorrect tx");
46 : : }
47 : : }
48 : :
49 : : // Check that new transactions in new blocks make it into the index.
50 [ + + ]: 11 : for (int i = 0; i < 10; i++) {
51 [ + - + - : 10 : CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
+ - ]
52 : 10 : std::vector<CMutableTransaction> no_txns;
53 [ + - ]: 10 : const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
54 [ + - ]: 10 : const CTransaction& txn = *block.vtx[0];
55 : :
56 [ + - + - : 20 : BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
+ - + - ]
57 [ + - - + ]: 10 : if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
58 [ # # # # ]: 0 : BOOST_ERROR("FindTx failed");
59 [ - + ]: 10 : } else if (tx_disk->GetHash() != txn.GetHash()) {
60 [ # # # # ]: 0 : BOOST_ERROR("Read incorrect tx");
61 : : }
62 : 10 : }
63 : :
64 : : // It is not safe to stop and destroy the index until it finishes handling
65 : : // the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
66 : : // call above is sufficient to ensure this, but the
67 : : // SyncWithValidationInterfaceQueue() call below is also needed to ensure
68 : : // TSAN always sees the test thread waiting for the notification thread, and
69 : : // avoid potential false positive reports.
70 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
71 : :
72 : : // shutdown sequence (c.f. Shutdown() in init.cpp)
73 [ + - ]: 1 : txindex.Stop();
74 : 1 : }
75 : :
76 : : BOOST_AUTO_TEST_SUITE_END()
|