2019-04-11 09:53:04 -04:00
|
|
|
// Copyright (c) 2017-2019 The Bitcoin Core developers
|
2017-12-08 17:00:13 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-08-27 16:28:35 -03:00
|
|
|
#include <chainparams.h>
|
2017-12-08 17:00:13 -03:00
|
|
|
#include <index/txindex.h>
|
|
|
|
#include <script/standard.h>
|
2019-04-11 09:44:10 -04:00
|
|
|
#include <test/setup_common.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/time.h>
|
2017-12-08 17:00:13 -03:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(txindex_tests)
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
|
|
|
|
{
|
2018-05-15 20:26:49 -04:00
|
|
|
TxIndex txindex(1 << 20, true);
|
2017-12-08 17:00:13 -03:00
|
|
|
|
|
|
|
CTransactionRef tx_disk;
|
|
|
|
uint256 block_hash;
|
|
|
|
|
|
|
|
// Transaction should not be found in the index before it is started.
|
|
|
|
for (const auto& txn : m_coinbase_txns) {
|
|
|
|
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
|
|
|
|
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
|
|
|
|
|
|
|
|
txindex.Start();
|
|
|
|
|
|
|
|
// Allow tx index to catch up with the block index.
|
|
|
|
constexpr int64_t timeout_ms = 10 * 1000;
|
|
|
|
int64_t time_start = GetTimeMillis();
|
|
|
|
while (!txindex.BlockUntilSyncedToCurrentChain()) {
|
|
|
|
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
|
|
|
|
MilliSleep(100);
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:28:35 -03:00
|
|
|
// Check that txindex excludes genesis block transactions.
|
|
|
|
const CBlock& genesis_block = Params().GenesisBlock();
|
|
|
|
for (const auto& txn : genesis_block.vtx) {
|
|
|
|
BOOST_CHECK(!txindex.FindTx(txn->GetHash(), block_hash, tx_disk));
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:00:13 -03:00
|
|
|
// Check that txindex has all txs that were in the chain before it started.
|
|
|
|
for (const auto& txn : m_coinbase_txns) {
|
|
|
|
if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
|
|
|
|
BOOST_ERROR("FindTx failed");
|
|
|
|
} else if (tx_disk->GetHash() != txn->GetHash()) {
|
|
|
|
BOOST_ERROR("Read incorrect tx");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that new transactions in new blocks make it into the index.
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2019-02-19 19:00:45 -03:00
|
|
|
CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
|
2017-12-08 17:00:13 -03:00
|
|
|
std::vector<CMutableTransaction> no_txns;
|
|
|
|
const CBlock& block = CreateAndProcessBlock(no_txns, coinbase_script_pub_key);
|
|
|
|
const CTransaction& txn = *block.vtx[0];
|
|
|
|
|
|
|
|
BOOST_CHECK(txindex.BlockUntilSyncedToCurrentChain());
|
|
|
|
if (!txindex.FindTx(txn.GetHash(), block_hash, tx_disk)) {
|
|
|
|
BOOST_ERROR("FindTx failed");
|
|
|
|
} else if (tx_disk->GetHash() != txn.GetHash()) {
|
|
|
|
BOOST_ERROR("Read incorrect tx");
|
|
|
|
}
|
|
|
|
}
|
2018-08-26 11:18:39 -03:00
|
|
|
|
2019-02-14 16:47:55 -03:00
|
|
|
// shutdown sequence (c.f. Shutdown() in init.cpp)
|
|
|
|
txindex.Stop();
|
|
|
|
|
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
|
|
|
|
|
|
|
// Rest of shutdown sequence and destructors happen in ~TestingSetup()
|
2017-12-08 17:00:13 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|