Skip to content

Commit 349b099

Browse files
committed
txindex: enable parallel sync
1 parent 9639bf7 commit 349b099

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/index/txindex.h

+6
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ class TxIndex final : public BaseIndex
3131

3232
BaseIndex::DB& GetDB() const override;
3333

34+
std::any CustomProcessBlock(const interfaces::BlockInfo& block) override {
35+
return CustomAppend(block);
36+
}
37+
3438
public:
3539
/// Constructs the index, which becomes available to be queried.
3640
explicit TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
3741

3842
// Destructor is declared because this class contains a unique_ptr to an incomplete type.
3943
virtual ~TxIndex() override;
4044

45+
bool AllowParallelSync() override { return true; }
46+
4147
/// Look up a transaction by hash.
4248
///
4349
/// @param[in] tx_hash The hash of the transaction to be returned.

src/test/txindex_tests.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <interfaces/chain.h>
99
#include <test/util/index.h>
1010
#include <test/util/setup_common.h>
11+
#include <util/threadpool.h>
1112
#include <validation.h>
1213

1314
#include <boost/test/unit_test.hpp>
@@ -77,4 +78,45 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
7778
txindex.Stop();
7879
}
7980

81+
BOOST_FIXTURE_TEST_CASE(txindex_parallel_initial_sync, TestChain100Setup)
82+
{
83+
int tip_height = 100; // pre-mined blocks
84+
const uint16_t MINE_BLOCKS = 650;
85+
for (int round = 0; round < 2; round++) { // two rounds to test sync from genesis and from a higher block
86+
// Generate blocks
87+
mineBlocks(MINE_BLOCKS);
88+
const CBlockIndex* tip = WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip());
89+
BOOST_REQUIRE(tip->nHeight == MINE_BLOCKS + tip_height);
90+
tip_height = tip->nHeight;
91+
92+
// Init and start index
93+
TxIndex txindex(interfaces::MakeChain(m_node), 1 << 20, /*f_memory=*/false);
94+
BOOST_REQUIRE(txindex.Init());
95+
std::shared_ptr<ThreadPool> thread_pool = std::make_shared<ThreadPool>();
96+
thread_pool->Start(2);
97+
txindex.SetThreadPool(thread_pool);
98+
txindex.SetTasksPerWorker(200);
99+
100+
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
101+
BOOST_REQUIRE(txindex.StartBackgroundSync());
102+
103+
// Allow tx index to catch up with the block index.
104+
IndexWaitSynced(txindex, *Assert(m_node.shutdown_signal));
105+
106+
// Check that txindex has all txs that were in the chain before it started.
107+
CTransactionRef tx_disk;
108+
uint256 block_hash;
109+
for (const auto& txn : m_coinbase_txns) {
110+
if (!txindex.FindTx(txn->GetHash(), block_hash, tx_disk)) {
111+
BOOST_ERROR("FindTx failed");
112+
} else if (tx_disk->GetHash() != txn->GetHash()) {
113+
BOOST_ERROR("Read incorrect tx");
114+
}
115+
}
116+
117+
txindex.Interrupt();
118+
txindex.Stop();
119+
}
120+
}
121+
80122
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)