Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14864565
D17746.id52921.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Subscribers
None
D17746.id52921.diff
View Options
diff --git a/src/index/base.h b/src/index/base.h
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -49,6 +49,8 @@
};
private:
+ /// Whether the index has been initialized or not.
+ std::atomic<bool> m_init{false};
/// Whether the index is in sync with the main chain. The flag is flipped
/// from false to true once, after which point this starts processing
/// ValidationInterface notifications to stay in sync.
@@ -64,10 +66,6 @@
std::thread m_thread_sync;
CThreadInterrupt m_interrupt;
- /// Read best block locator and check that data needed to sync has not been
- /// pruned.
- bool Init();
-
/// Sync the index with the block index starting from the current best
/// block. Intended to be run in its own thread, m_thread_sync, and can be
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
@@ -141,9 +139,12 @@
void Interrupt();
- /// Start initializes the sync state and registers the instance as a
- /// ValidationInterface so that it stays in sync with blockchain updates.
- [[nodiscard]] bool Start();
+ /// Initializes the sync state and registers the instance to the
+ /// validation interface so that it stays in sync with blockchain updates.
+ [[nodiscard]] bool Init();
+
+ /// Starts the initial sync process.
+ [[nodiscard]] bool StartBackgroundSync();
/// Stops the instance from staying in sync with blockchain updates.
void Stop();
diff --git a/src/index/base.cpp b/src/index/base.cpp
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -75,6 +75,13 @@
}
bool BaseIndex::Init() {
+ // m_chainstate member gives indexing code access to node internals. It is
+ // removed in followup https://github.com/bitcoin/bitcoin/pull/24230
+ m_chainstate = &m_chain->context()->chainman->ActiveChainstate();
+ // Register to validation interface before setting the 'm_synced' flag, so
+ // that callbacks are not missed once m_synced is true.
+ RegisterValidationInterface(this);
+
CBlockLocator locator;
if (!GetDB().ReadBestBlock(locator)) {
locator.SetNull();
@@ -158,6 +165,7 @@
// happen solely via `BlockConnected` signals until, possibly, the next
// restart.
m_synced = synced;
+ m_init = true;
return true;
}
@@ -420,15 +428,9 @@
m_interrupt();
}
-bool BaseIndex::Start() {
- // m_chainstate member gives indexing code access to node internals. It is
- // removed in a followup
- m_chainstate = &m_chain->context()->chainman->ActiveChainstate();
- // Need to register this ValidationInterface before running Init(), so that
- // callbacks are not missed if Init sets m_synced to true.
- RegisterValidationInterface(this);
- if (!Init()) {
- return false;
+bool BaseIndex::StartBackgroundSync() {
+ if (!m_init) {
+ throw std::logic_error("Error: Cannot start a non-initialized index");
}
m_thread_sync =
diff --git a/src/init.h b/src/init.h
--- a/src/init.h
+++ b/src/init.h
@@ -93,4 +93,10 @@
*/
void SetupServerArgs(node::NodeContext &node);
+/**
+ * Validates requirements to run the indexes and spawns each index initial
+ * sync thread
+ */
+bool StartIndexBackgroundSync(node::NodeContext &node);
+
#endif // BITCOIN_INIT_H
diff --git a/src/init.cpp b/src/init.cpp
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2650,29 +2650,35 @@
g_txindex =
std::make_unique<TxIndex>(interfaces::MakeChain(node, Params()),
cache_sizes.tx_index, false, fReindex);
- if (!g_txindex->Start()) {
- return false;
- }
+ node.indexes.emplace_back(g_txindex.get());
}
for (const auto &filter_type : g_enabled_filter_types) {
InitBlockFilterIndex(
[&] { return interfaces::MakeChain(node, Params()); }, filter_type,
cache_sizes.filter_index, false, fReindex);
- if (!GetBlockFilterIndex(filter_type)->Start()) {
- return false;
- }
+ node.indexes.emplace_back(GetBlockFilterIndex(filter_type));
}
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
g_coin_stats_index = std::make_unique<CoinStatsIndex>(
interfaces::MakeChain(node, Params()), /* cache size */ 0, false,
fReindex);
- if (!g_coin_stats_index->Start()) {
+ node.indexes.emplace_back(g_coin_stats_index.get());
+ }
+
+ // Init indexes
+ for (auto index : node.indexes) {
+ if (!index->Init()) {
return false;
}
}
+ // Now that all indexes are loaded, start them
+ if (!StartIndexBackgroundSync(node)) {
+ return false;
+ }
+
#if ENABLE_CHRONIK
if (args.GetBoolArg("-chronik", DEFAULT_CHRONIK)) {
const bool fReindexChronik =
@@ -3031,3 +3037,12 @@
return true;
}
+
+bool StartIndexBackgroundSync(NodeContext &node) {
+ for (auto index : node.indexes) {
+ if (!index->StartBackgroundSync()) {
+ return false;
+ }
+ }
+ return true;
+}
diff --git a/src/node/context.h b/src/node/context.h
--- a/src/node/context.h
+++ b/src/node/context.h
@@ -13,8 +13,9 @@
#include <vector>
class ArgsManager;
-class BanMan;
class AddrMan;
+class BanMan;
+class BaseIndex;
class CConnman;
class CScheduler;
class CTxMemPool;
@@ -53,6 +54,8 @@
std::unique_ptr<BanMan> banman;
// Currently a raw pointer because the memory is not managed by this struct
ArgsManager *args{nullptr};
+ // raw pointers because memory is not managed by this struct
+ std::vector<BaseIndex *> indexes;
std::unique_ptr<interfaces::Chain> chain;
//! List of all chain clients (wallet processes or other client) connected
//! to node.
diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp
--- a/src/test/blockfilter_index_tests.cpp
+++ b/src/test/blockfilter_index_tests.cpp
@@ -123,6 +123,7 @@
BuildChainTestingSetup) {
BlockFilterIndex filter_index(interfaces::MakeChain(m_node, Params()),
BlockFilterType::BASIC, 1 << 20, true);
+ BOOST_REQUIRE(filter_index.Init());
uint256 last_header;
@@ -153,7 +154,7 @@
// started.
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(filter_index.Start());
+ BOOST_REQUIRE(filter_index.StartBackgroundSync());
// Allow filter index to catch up with the block index.
constexpr int64_t timeout_ms = 30 * 1000;
diff --git a/src/test/coinstatsindex_tests.cpp b/src/test/coinstatsindex_tests.cpp
--- a/src/test/coinstatsindex_tests.cpp
+++ b/src/test/coinstatsindex_tests.cpp
@@ -33,6 +33,7 @@
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup) {
CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node, Params()),
1 << 20, true};
+ BOOST_REQUIRE(coin_stats_index.Init());
const CBlockIndex *block_index;
{
@@ -47,7 +48,7 @@
// is started.
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(coin_stats_index.Start());
+ BOOST_REQUIRE(coin_stats_index.StartBackgroundSync());
IndexWaitSynced(coin_stats_index);
@@ -92,7 +93,8 @@
const Config &config = m_node.chainman->GetConfig();
{
CoinStatsIndex index{interfaces::MakeChain(m_node, Params()), 1 << 20};
- BOOST_REQUIRE(index.Start());
+ BOOST_REQUIRE(index.Init());
+ BOOST_REQUIRE(index.StartBackgroundSync());
IndexWaitSynced(index);
std::shared_ptr<const CBlock> new_block;
CBlockIndex *new_block_index = nullptr;
@@ -134,8 +136,9 @@
{
CoinStatsIndex index{interfaces::MakeChain(m_node, Params()), 1 << 20};
+ BOOST_REQUIRE(index.Init());
// Make sure the index can be loaded.
- BOOST_REQUIRE(index.Start());
+ BOOST_REQUIRE(index.StartBackgroundSync());
index.Stop();
}
}
diff --git a/src/test/txindex_tests.cpp b/src/test/txindex_tests.cpp
--- a/src/test/txindex_tests.cpp
+++ b/src/test/txindex_tests.cpp
@@ -18,6 +18,7 @@
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) {
TxIndex txindex(interfaces::MakeChain(m_node, Params()), 1 << 20, true);
+ BOOST_REQUIRE(txindex.Init());
CTransactionRef tx_disk;
BlockHash block_hash;
@@ -31,7 +32,7 @@
// started.
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(txindex.Start());
+ BOOST_REQUIRE(txindex.StartBackgroundSync());
// Allow tx index to catch up with the block index.
constexpr int64_t timeout_ms = 10 * 1000;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, May 20, 20:35 (13 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5865893
Default Alt Text
D17746.id52921.diff (8 KB)
Attached To
D17746: refactor: init indexes, decouple 'Start()' from the creation step
Event Timeline
Log In to Comment