2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2017-2022 The Bitcoin Core developers
|
2017-12-08 12:00:13 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2023-08-08 10:57:31 -04:00
|
|
|
#include <addresstype.h>
|
2018-08-27 12:28:35 -07:00
|
|
|
#include <chainparams.h>
|
2017-12-08 12:00:13 -08:00
|
|
|
#include <index/txindex.h>
|
2022-01-13 07:57:54 -05:00
|
|
|
#include <interfaces/chain.h>
|
2023-06-28 12:20:05 +02:00
|
|
|
#include <test/util/index.h>
|
2019-11-05 15:18:59 -05:00
|
|
|
#include <test/util/setup_common.h>
|
2020-10-14 16:48:17 -04:00
|
|
|
#include <validation.h>
|
2017-12-08 12:00:13 -08:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(txindex_tests)
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
|
|
|
|
{
|
2022-01-13 07:57:54 -05:00
|
|
|
TxIndex txindex(interfaces::MakeChain(m_node), 1 << 20, true);
|
2023-05-17 00:55:09 -03:00
|
|
|
BOOST_REQUIRE(txindex.Init());
|
2017-12-08 12:00:13 -08: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());
|
|
|
|
|
2023-05-17 00:55:09 -03:00
|
|
|
BOOST_REQUIRE(txindex.StartBackgroundSync());
|
2017-12-08 12:00:13 -08:00
|
|
|
|
|
|
|
// Allow tx index to catch up with the block index.
|
2023-06-28 12:20:05 +02:00
|
|
|
IndexWaitSynced(txindex);
|
2017-12-08 12:00:13 -08:00
|
|
|
|
2018-08-27 12:28:35 -07: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 12:00:13 -08: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 17:00:45 -05:00
|
|
|
CScript coinbase_script_pub_key = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
|
2017-12-08 12:00:13 -08: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 10:18:39 -04:00
|
|
|
|
2022-10-04 13:21:56 +02:00
|
|
|
// It is not safe to stop and destroy the index until it finishes handling
|
|
|
|
// the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
|
|
|
|
// call above is sufficient to ensure this, but the
|
|
|
|
// SyncWithValidationInterfaceQueue() call below is also needed to ensure
|
|
|
|
// TSAN always sees the test thread waiting for the notification thread, and
|
|
|
|
// avoid potential false positive reports.
|
|
|
|
SyncWithValidationInterfaceQueue();
|
|
|
|
|
2019-02-14 14:47:55 -05:00
|
|
|
// shutdown sequence (c.f. Shutdown() in init.cpp)
|
|
|
|
txindex.Stop();
|
2017-12-08 12:00:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|