Skip to content

Commit c6af23c

Browse files
committed
validation: add ChainstateRole
1 parent 9f2318c commit c6af23c

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/kernel/chain.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <chain.h>
66
#include <interfaces/chain.h>
7+
#include <kernel/chain.h>
78
#include <sync.h>
89
#include <uint256.h>
910

@@ -25,3 +26,13 @@ interfaces::BlockInfo MakeBlockInfo(const CBlockIndex* index, const CBlock* data
2526
return info;
2627
}
2728
} // namespace kernel
29+
30+
std::ostream& operator<<(std::ostream& os, const ChainstateRole& role) {
31+
switch(role) {
32+
case ChainstateRole::NORMAL: os << "normal"; break;
33+
case ChainstateRole::ASSUMEDVALID: os << "assumedvalid"; break;
34+
case ChainstateRole::BACKGROUND: os << "background"; break;
35+
default: os.setstate(std::ios_base::failbit);
36+
}
37+
return os;
38+
}

src/kernel/chain.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_KERNEL_CHAIN_H
66
#define BITCOIN_KERNEL_CHAIN_H
77

8+
#include<iostream>
9+
810
class CBlock;
911
class CBlockIndex;
1012
namespace interfaces {
@@ -14,6 +16,24 @@ struct BlockInfo;
1416
namespace kernel {
1517
//! Return data from block index.
1618
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex* block_index, const CBlock* data = nullptr);
19+
1720
} // namespace kernel
1821

22+
//! This enum describes the various roles a specific Chainstate instance can take.
23+
//! Other parts of the system sometimes need to vary in behavior depending on the
24+
//! existence of a background validation chainstate, e.g. when building indexes.
25+
enum class ChainstateRole {
26+
// Single chainstate in use, "normal" IBD mode.
27+
NORMAL,
28+
29+
// Doing IBD-style validation in the background. Implies use of an assumed-valid
30+
// chainstate.
31+
BACKGROUND,
32+
33+
// Active assumed-valid chainstate. Implies use of a background IBD chainstate.
34+
ASSUMEDVALID,
35+
};
36+
37+
std::ostream& operator<<(std::ostream& os, const ChainstateRole& role);
38+
1939
#endif // BITCOIN_KERNEL_CHAIN_H

src/validation.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5813,6 +5813,16 @@ bool ChainstateManager::DeleteSnapshotChainstate()
58135813
return true;
58145814
}
58155815

5816+
ChainstateRole Chainstate::GetRole() const
5817+
{
5818+
if (m_chainman.GetAll().size() <= 1) {
5819+
return ChainstateRole::NORMAL;
5820+
}
5821+
return (this != &m_chainman.ActiveChainstate()) ?
5822+
ChainstateRole::BACKGROUND :
5823+
ChainstateRole::ASSUMEDVALID;
5824+
}
5825+
58165826
const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock() const
58175827
{
58185828
return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr;

src/validation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <arith_uint256.h>
1414
#include <attributes.h>
1515
#include <chain.h>
16+
#include <kernel/chain.h>
1617
#include <consensus/amount.h>
1718
#include <deploymentstatus.h>
1819
#include <kernel/chainparams.h>
@@ -511,6 +512,12 @@ class Chainstate
511512
ChainstateManager& chainman,
512513
std::optional<uint256> from_snapshot_blockhash = std::nullopt);
513514

515+
//! Return the current role of the chainstate. See `ChainstateManager`
516+
//! documentation for a description of the different types of chainstates.
517+
//!
518+
//! @sa ChainstateRole
519+
ChainstateRole GetRole() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
520+
514521
/**
515522
* Initialize the CoinsViews UTXO set database management data structures. The in-memory
516523
* cache is initialized separately.

0 commit comments

Comments
 (0)