|
| 1 | +// Copyright (c) 2017-2018 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 <index/txindex.h> |
| 6 | +#include <script/standard.h> |
| 7 | +#include <test/test_bitcoin.h> |
| 8 | +#include <util.h> |
| 9 | +#include <utiltime.h> |
| 10 | +#include <validation.h> |
| 11 | + |
| 12 | +#include <boost/test/unit_test.hpp> |
| 13 | + |
| 14 | +BOOST_AUTO_TEST_SUITE(txindex_tests) |
| 15 | + |
| 16 | +BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) |
| 17 | +{ |
| 18 | + TxIndex txindex(MakeUnique<TxIndexDB>(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 | + txindex.Start(); |
| 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 | + MilliSleep(100); |
| 39 | + } |
| 40 | + |
| 41 | + // Check that txindex has all txs that were in the chain before it started. |
| 42 | + for (const auto& txn : m_coinbase_txns) { |
| 43 | + if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) { |
| 44 | + BOOST_ERROR("FindTx failed"); |
| 45 | + } else if (tx_disk->GetHash() != txn->GetHash()) { |
| 46 | + BOOST_ERROR("Read incorrect tx"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Check that new transactions in new blocks make it into the index. |
| 51 | + for (int i = 0; i < 10; i++) { |
| 52 | + CScript coinbase_script_pub_key = GetScriptForDestination(coinbaseKey.GetPubKey().GetID()); |
| 53 | + std::vector<CMutableTransaction> no_txns; |
| 54 | + const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key); |
| 55 | + const CTransaction& txn = *block.vtx[0]; |
| 56 | + |
| 57 | + BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain()); |
| 58 | + if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) { |
| 59 | + BOOST_ERROR("FindTx failed"); |
| 60 | + } else if (tx_disk->GetHash() != txn.GetHash()) { |
| 61 | + BOOST_ERROR("Read incorrect tx"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments