Skip to content

Commit e8add40

Browse files
committed
txindex: enable parallel sync
1 parent bd16f59 commit e8add40

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/index/txindex.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ class TxIndex final : public BaseIndex
2929

3030
BaseIndex::DB& GetDB() const override;
3131

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

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

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

src/test/txindex_tests.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <index/txindex.h>
88
#include <interfaces/chain.h>
99
#include <test/util/setup_common.h>
10+
#include <util/threadpool.h>
1011
#include <validation.h>
1112

1213
#include <boost/test/unit_test.hpp>
@@ -73,4 +74,45 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
7374
txindex.Stop();
7475
}
7576

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

0 commit comments

Comments
 (0)