Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13115461
D17705.id52803.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
15 KB
Subscribers
None
D17705.id52803.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
@@ -6,6 +6,7 @@
#define BITCOIN_INDEX_BASE_H
#include <dbwrapper.h>
+#include <interfaces/chain.h>
#include <threadinterrupt.h>
#include <validationinterface.h>
@@ -83,6 +84,7 @@
virtual bool AllowPrune() const = 0;
protected:
+ std::unique_ptr<interfaces::Chain> m_chain;
Chainstate *m_chainstate{nullptr};
void BlockConnected(const std::shared_ptr<const CBlock> &block,
@@ -118,6 +120,7 @@
void SetBestBlockIndex(const CBlockIndex *block);
public:
+ BaseIndex(std::unique_ptr<interfaces::Chain> chain);
/// Destructor interrupts sync thread if running and blocks until it exits.
virtual ~BaseIndex();
@@ -132,7 +135,7 @@
/// Start initializes the sync state and registers the instance as a
/// ValidationInterface so that it stays in sync with blockchain updates.
- [[nodiscard]] bool Start(Chainstate &active_chainstate);
+ [[nodiscard]] bool Start();
/// 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
@@ -7,8 +7,10 @@
#include <common/args.h>
#include <config.h>
#include <index/base.h>
+#include <interfaces/chain.h>
#include <logging.h>
#include <node/blockstorage.h>
+#include <node/context.h>
#include <node/database_args.h>
#include <node/ui_interface.h>
#include <shutdown.h>
@@ -60,6 +62,9 @@
batch.Write(DB_BEST_BLOCK, locator);
}
+BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain)
+ : m_chain{std::move(chain)} {}
+
BaseIndex::~BaseIndex() {
Interrupt();
Stop();
@@ -380,8 +385,10 @@
m_interrupt();
}
-bool BaseIndex::Start(Chainstate &active_chainstate) {
- m_chainstate = &active_chainstate;
+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);
diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h
--- a/src/index/blockfilterindex.h
+++ b/src/index/blockfilterindex.h
@@ -62,7 +62,8 @@
public:
/** Constructs the index, which becomes available to be queried. */
- explicit BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
+ explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain,
+ BlockFilterType filter_type, size_t n_cache_size,
bool f_memory = false, bool f_wipe = false);
BlockFilterType GetFilterType() const { return m_filter_type; }
@@ -98,8 +99,10 @@
* exist. Returns true if a new index is created and false if one has already
* been initialized.
*/
-bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
- bool f_memory = false, bool f_wipe = false);
+bool InitBlockFilterIndex(
+ std::function<std::unique_ptr<interfaces::Chain>()> make_chain,
+ BlockFilterType filter_type, size_t n_cache_size, bool f_memory = false,
+ bool f_wipe = false);
/**
* Destroy the block filter index with the given type. Returns false if no such
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -102,10 +102,11 @@
static std::map<BlockFilterType, BlockFilterIndex> g_filter_indexes;
-BlockFilterIndex::BlockFilterIndex(BlockFilterType filter_type,
+BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain,
+ BlockFilterType filter_type,
size_t n_cache_size, bool f_memory,
bool f_wipe)
- : m_filter_type(filter_type) {
+ : BaseIndex(std::move(chain)), m_filter_type(filter_type) {
const std::string &filter_name = BlockFilterTypeName(filter_type);
if (filter_name.empty()) {
throw std::invalid_argument("unknown filter_type");
@@ -495,11 +496,14 @@
}
}
-bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
- bool f_memory, bool f_wipe) {
+bool InitBlockFilterIndex(
+ std::function<std::unique_ptr<interfaces::Chain>()> make_chain,
+ BlockFilterType filter_type, size_t n_cache_size, bool f_memory,
+ bool f_wipe) {
auto result = g_filter_indexes.emplace(
std::piecewise_construct, std::forward_as_tuple(filter_type),
- std::forward_as_tuple(filter_type, n_cache_size, f_memory, f_wipe));
+ std::forward_as_tuple(make_chain(), filter_type, n_cache_size, f_memory,
+ f_wipe));
return result.second;
}
diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h
--- a/src/index/coinstatsindex.h
+++ b/src/index/coinstatsindex.h
@@ -57,7 +57,8 @@
public:
// Constructs the index, which becomes available to be queried.
- explicit CoinStatsIndex(size_t n_cache_size, bool f_memory = false,
+ explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain,
+ size_t n_cache_size, bool f_memory = false,
bool f_wipe = false);
// Look up stats for a specific block using CBlockIndex
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -101,8 +101,9 @@
std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
-CoinStatsIndex::CoinStatsIndex(size_t n_cache_size, bool f_memory,
- bool f_wipe) {
+CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain,
+ size_t n_cache_size, bool f_memory, bool f_wipe)
+ : BaseIndex(std::move(chain)) {
fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"};
fs::create_directories(path);
diff --git a/src/index/txindex.h b/src/index/txindex.h
--- a/src/index/txindex.h
+++ b/src/index/txindex.h
@@ -37,7 +37,8 @@
public:
/// Constructs the index, which becomes available to be queried.
- explicit TxIndex(size_t n_cache_size, bool f_memory = false,
+ explicit TxIndex(std::unique_ptr<interfaces::Chain> chain,
+ size_t n_cache_size, bool f_memory = false,
bool f_wipe = false);
// Destructor is declared because this class contains a unique_ptr to an
diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp
--- a/src/index/txindex.cpp
+++ b/src/index/txindex.cpp
@@ -46,8 +46,10 @@
return WriteBatch(batch);
}
-TxIndex::TxIndex(size_t n_cache_size, bool f_memory, bool f_wipe)
- : m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe)) {}
+TxIndex::TxIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size,
+ bool f_memory, bool f_wipe)
+ : BaseIndex(std::move(chain)),
+ m_db(std::make_unique<TxIndex::DB>(n_cache_size, f_memory, f_wipe)) {}
TxIndex::~TxIndex() {}
diff --git a/src/init.cpp b/src/init.cpp
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2642,25 +2642,27 @@
}
g_txindex =
- std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
- if (!g_txindex->Start(chainman.ActiveChainstate())) {
+ std::make_unique<TxIndex>(interfaces::MakeChain(node, Params()),
+ cache_sizes.tx_index, false, fReindex);
+ if (!g_txindex->Start()) {
return false;
}
}
for (const auto &filter_type : g_enabled_filter_types) {
- InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false,
- fReindex);
- if (!GetBlockFilterIndex(filter_type)
- ->Start(chainman.ActiveChainstate())) {
+ InitBlockFilterIndex(
+ [&] { return interfaces::MakeChain(node, Params()); }, filter_type,
+ cache_sizes.filter_index, false, fReindex);
+ if (!GetBlockFilterIndex(filter_type)->Start()) {
return false;
}
}
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
g_coin_stats_index = std::make_unique<CoinStatsIndex>(
- /* cache size */ 0, false, fReindex);
- if (!g_coin_stats_index->Start(chainman.ActiveChainstate())) {
+ interfaces::MakeChain(node, Params()), /* cache size */ 0, false,
+ fReindex);
+ if (!g_coin_stats_index->Start()) {
return false;
}
}
diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h
--- a/src/interfaces/chain.h
+++ b/src/interfaces/chain.h
@@ -304,6 +304,10 @@
//! Return true if an assumed-valid chain is in use.
virtual bool hasAssumedValidChain() = 0;
+ //! Get internal node context. Useful for testing, but not
+ //! accessible across processes.
+ virtual node::NodeContext *context() { return nullptr; }
+
//! This Chain's parameters
virtual const CChainParams ¶ms() const = 0;
};
diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp
--- a/src/node/interfaces.cpp
+++ b/src/node/interfaces.cpp
@@ -785,6 +785,7 @@
return Assert(m_node.chainman)->IsSnapshotActive();
}
const CChainParams ¶ms() const override { return m_params; }
+ NodeContext *context() override { return &m_node; }
NodeContext &m_node;
const CChainParams &m_params;
};
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
@@ -7,6 +7,7 @@
#include <config.h>
#include <consensus/validation.h>
#include <index/blockfilterindex.h>
+#include <interfaces/chain.h>
#include <node/miner.h>
#include <pow/pow.h>
#include <script/standard.h>
@@ -120,7 +121,8 @@
BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync,
BuildChainTestingSetup) {
- BlockFilterIndex filter_index(BlockFilterType::BASIC, 1 << 20, true);
+ BlockFilterIndex filter_index(interfaces::MakeChain(m_node, Params()),
+ BlockFilterType::BASIC, 1 << 20, true);
uint256 last_header;
@@ -151,7 +153,7 @@
// started.
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(filter_index.Start(m_node.chainman->ActiveChainstate()));
+ BOOST_REQUIRE(filter_index.Start());
// Allow filter index to catch up with the block index.
constexpr int64_t timeout_ms = 30 * 1000;
@@ -313,16 +315,18 @@
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index == nullptr);
- BOOST_CHECK(
- InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
+ BOOST_CHECK(InitBlockFilterIndex(
+ [&] { return interfaces::MakeChain(m_node, Params()); },
+ BlockFilterType::BASIC, 1 << 20, true, false));
filter_index = GetBlockFilterIndex(BlockFilterType::BASIC);
BOOST_CHECK(filter_index != nullptr);
BOOST_CHECK(filter_index->GetFilterType() == BlockFilterType::BASIC);
// Initialize returns false if index already exists.
- BOOST_CHECK(
- !InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
+ BOOST_CHECK(!InitBlockFilterIndex(
+ [&] { return interfaces::MakeChain(m_node, Params()); },
+ BlockFilterType::BASIC, 1 << 20, true, false));
int iter_count = 0;
ForEachBlockFilterIndex(
@@ -338,8 +342,9 @@
BOOST_CHECK(filter_index == nullptr);
// Reinitialize index.
- BOOST_CHECK(
- InitBlockFilterIndex(BlockFilterType::BASIC, 1 << 20, true, false));
+ BOOST_CHECK(InitBlockFilterIndex(
+ [&] { return interfaces::MakeChain(m_node, Params()); },
+ BlockFilterType::BASIC, 1 << 20, true, false));
DestroyAllBlockFilterIndexes();
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
@@ -5,6 +5,7 @@
#include <chainparams.h>
#include <config.h>
#include <index/coinstatsindex.h>
+#include <interfaces/chain.h>
#include <test/util/setup_common.h>
#include <test/util/validation.h>
#include <util/time.h>
@@ -30,7 +31,8 @@
}
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup) {
- CoinStatsIndex coin_stats_index{1 << 20, true};
+ CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node, Params()),
+ 1 << 20, true};
const CBlockIndex *block_index;
{
@@ -45,7 +47,7 @@
// is started.
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(coin_stats_index.Start(m_node.chainman->ActiveChainstate()));
+ BOOST_REQUIRE(coin_stats_index.Start());
IndexWaitSynced(coin_stats_index);
@@ -89,8 +91,8 @@
Chainstate &chainstate = Assert(m_node.chainman)->ActiveChainstate();
const Config &config = m_node.chainman->GetConfig();
{
- CoinStatsIndex index{1 << 20};
- BOOST_REQUIRE(index.Start(chainstate));
+ CoinStatsIndex index{interfaces::MakeChain(m_node, Params()), 1 << 20};
+ BOOST_REQUIRE(index.Start());
IndexWaitSynced(index);
std::shared_ptr<const CBlock> new_block;
CBlockIndex *new_block_index = nullptr;
@@ -131,9 +133,9 @@
}
{
- CoinStatsIndex index{1 << 20};
+ CoinStatsIndex index{interfaces::MakeChain(m_node, Params()), 1 << 20};
// Make sure the index can be loaded.
- BOOST_REQUIRE(index.Start(chainstate));
+ BOOST_REQUIRE(index.Start());
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
@@ -5,6 +5,7 @@
#include <index/txindex.h>
#include <chainparams.h>
+#include <interfaces/chain.h>
#include <script/standard.h>
#include <util/time.h>
#include <validation.h>
@@ -16,7 +17,7 @@
BOOST_AUTO_TEST_SUITE(txindex_tests)
BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) {
- TxIndex txindex(1 << 20, true);
+ TxIndex txindex(interfaces::MakeChain(m_node, Params()), 1 << 20, true);
CTransactionRef tx_disk;
BlockHash block_hash;
@@ -30,7 +31,7 @@
// started.
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
- BOOST_REQUIRE(txindex.Start(m_node.chainman->ActiveChainstate()));
+ BOOST_REQUIRE(txindex.Start());
// Allow tx index to catch up with the block index.
constexpr int64_t timeout_ms = 10 * 1000;
diff --git a/test/lint/lint-circular-dependencies.sh b/test/lint/lint-circular-dependencies.sh
--- a/test/lint/lint-circular-dependencies.sh
+++ b/test/lint/lint-circular-dependencies.sh
@@ -33,6 +33,9 @@
"kernel/coinstats -> validation -> kernel/coinstats"
"kernel/mempool_persist -> validation -> kernel/mempool_persist"
"kernel/disconnected_transactions -> validation -> kernel/disconnected_transactions"
+
+ # Temporary, removed in a followup
+ "index/base -> node/context -> net_processing -> index/blockfilterindex -> index/base"
)
EXIT_CODE=0
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 1, 11:08 (15 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5183135
Default Alt Text
D17705.id52803.diff (15 KB)
Attached To
D17705: indexes, refactor: Pass Chain interface instead of CChainState class to indexes
Event Timeline
Log In to Comment