aboutsummaryrefslogtreecommitdiff
path: root/src/index
diff options
context:
space:
mode:
authorJames O'Beirne <james.obeirne@gmail.com>2019-09-23 14:44:54 -0400
committerJames O'Beirne <james.obeirne@pm.me>2023-09-30 06:38:47 -0400
commit373cf91531b84bfdd06fdf8abf4dca228029ce6b (patch)
tree0038ed40c747e33d8cca9a905fdb07cb539ee5ac /src/index
parent1fffdd76a1bca908f55d73b64983655b14cf7432 (diff)
downloadbitcoin-373cf91531b84bfdd06fdf8abf4dca228029ce6b.tar.xz
validation: indexing changes for assumeutxo
When using an assumedvalid chainstate, only process validationinterface callbacks from the background chainstate within indexes. This ensures that all indexes are built in-order. Later, we can possibly designate indexes which can be built out of order and continue their operation during snapshot use. Once the background sync has completed, restart the indexes so that they continue to index the now-validated snapshot chainstate.
Diffstat (limited to 'src/index')
-rw-r--r--src/index/base.cpp32
-rw-r--r--src/index/base.h12
2 files changed, 38 insertions, 6 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp
index 98a8bad102..8474d01c41 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -79,9 +79,15 @@ BaseIndex::~BaseIndex()
bool BaseIndex::Init()
{
+ AssertLockNotHeld(cs_main);
+
+ // May need reset if index is being restarted.
+ m_interrupt.reset();
+
// 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();
+ m_chainstate = WITH_LOCK(::cs_main,
+ return &m_chain->context()->chainman->GetChainstateForIndexing());
// Register to validation interface before setting the 'm_synced' flag, so that
// callbacks are not missed once m_synced is true.
RegisterValidationInterface(this);
@@ -92,7 +98,8 @@ bool BaseIndex::Init()
}
LOCK(cs_main);
- CChain& active_chain = m_chainstate->m_chain;
+ CChain& index_chain = m_chainstate->m_chain;
+
if (locator.IsNull()) {
SetBestBlockIndex(nullptr);
} else {
@@ -114,7 +121,7 @@ bool BaseIndex::Init()
// Note: this will latch to true immediately if the user starts up with an empty
// datadir and an index enabled. If this is the case, indexation will happen solely
// via `BlockConnected` signals until, possibly, the next restart.
- m_synced = start_block == active_chain.Tip();
+ m_synced = start_block == index_chain.Tip();
m_init = true;
return true;
}
@@ -143,6 +150,8 @@ void BaseIndex::ThreadSync()
std::chrono::steady_clock::time_point last_locator_write_time{0s};
while (true) {
if (m_interrupt) {
+ LogPrintf("%s: m_interrupt set; exiting ThreadSync\n", GetName());
+
SetBestBlockIndex(pindex);
// No need to handle errors in Commit. If it fails, the error will be already be
// logged. The best way to recover is to continue, as index cannot be corrupted by
@@ -252,6 +261,17 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex)
{
+ // Ignore events from the assumed-valid chain; we will process its blocks
+ // (sequentially) after it is fully verified by the background chainstate. This
+ // is to avoid any out-of-order indexing.
+ //
+ // TODO at some point we could parameterize whether a particular index can be
+ // built out of order, but for now just do the conservative simple thing.
+ if (role == ChainstateRole::ASSUMEDVALID) {
+ return;
+ }
+
+ // Ignore BlockConnected signals until we have fully indexed the chain.
if (!m_synced) {
return;
}
@@ -298,6 +318,12 @@ void BaseIndex::BlockConnected(ChainstateRole role, const std::shared_ptr<const
void BaseIndex::ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator)
{
+ // Ignore events from the assumed-valid chain; we will process its blocks
+ // (sequentially) after it is fully verified by the background chainstate.
+ if (role == ChainstateRole::ASSUMEDVALID) {
+ return;
+ }
+
if (!m_synced) {
return;
}
diff --git a/src/index/base.h b/src/index/base.h
index b93103eb36..154061fb19 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -15,6 +15,7 @@
class CBlock;
class CBlockIndex;
class Chainstate;
+class ChainstateManager;
namespace interfaces {
class Chain;
} // namespace interfaces
@@ -30,6 +31,11 @@ struct IndexSummary {
* Base class for indices of blockchain data. This implements
* CValidationInterface and ensures blocks are indexed sequentially according
* to their position in the active chain.
+ *
+ * In the presence of multiple chainstates (i.e. if a UTXO snapshot is loaded),
+ * only the background "IBD" chainstate will be indexed to avoid building the
+ * index out of order. When the background chainstate completes validation, the
+ * index will be reinitialized and indexing will continue.
*/
class BaseIndex : public CValidationInterface
{
@@ -122,9 +128,6 @@ protected:
virtual DB& GetDB() const = 0;
- /// Get the name of the index for display in logs.
- const std::string& GetName() const LIFETIMEBOUND { return m_name; }
-
/// Update the internal best block index as well as the prune lock.
void SetBestBlockIndex(const CBlockIndex* block);
@@ -133,6 +136,9 @@ public:
/// Destructor interrupts sync thread if running and blocks until it exits.
virtual ~BaseIndex();
+ /// Get the name of the index for display in logs.
+ const std::string& GetName() const LIFETIMEBOUND { return m_name; }
+
/// Blocks the current thread until the index is caught up to the current
/// state of the block chain. This only blocks if the index has gotten in
/// sync once and only needs to process blocks in the ValidationInterface