Skip to content

Commit fa29d0b

Browse files
author
MacroFake
committed
Move ::hashAssumeValid into ChainstateManager
This changes the assumed valid block for the bitcoin-chainstate executable. Previously it was uint256{}, now it is defaultAssumeValid.
1 parent faf4487 commit fa29d0b

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

src/init.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,6 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
937937
fCheckBlockIndex = args.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
938938
fCheckpointsEnabled = args.GetBoolArg("-checkpoints", DEFAULT_CHECKPOINTS_ENABLED);
939939

940-
hashAssumeValid = uint256S(args.GetArg("-assumevalid", chainparams.GetConsensus().defaultAssumeValid.GetHex()));
941-
942940
if (args.IsArgSet("-minimumchainwork")) {
943941
const std::string minChainWorkStr = args.GetArg("-minimumchainwork", "");
944942
if (!IsHexNumber(minChainWorkStr)) {

src/kernel/chainstatemanager_opts.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#ifndef BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_CHAINSTATEMANAGER_OPTS_H
77

8+
#include <uint256.h>
89
#include <util/time.h>
910

1011
#include <cstdint>
1112
#include <functional>
13+
#include <optional>
1214

1315
class CChainParams;
1416

@@ -24,6 +26,8 @@ namespace kernel {
2426
struct ChainstateManagerOpts {
2527
const CChainParams& chainparams;
2628
const std::function<NodeClock::time_point()> adjusted_time_callback{nullptr};
29+
//! If set, it will override the block hash whose ancestors we will assume to have valid scripts without checking them.
30+
std::optional<uint256> assumed_valid_block;
2731
//! If the tip is older than this, the node is considered to be in initial block download.
2832
std::chrono::seconds max_tip_age{DEFAULT_MAX_TIP_AGE};
2933
};

src/node/chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
3232
return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
3333
};
3434

35-
if (!hashAssumeValid.IsNull()) {
36-
LogPrintf("Assuming ancestors of block %s have valid signatures.\n", hashAssumeValid.GetHex());
35+
if (!chainman.AssumedValidBlock().IsNull()) {
36+
LogPrintf("Assuming ancestors of block %s have valid signatures.\n", chainman.AssumedValidBlock().GetHex());
3737
} else {
3838
LogPrintf("Validating signatures for all blocks.\n");
3939
}

src/node/chainstatemanager_args.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace node {
1313
void ApplyArgsManOptions(const ArgsManager& args, ChainstateManager::Options& opts)
1414
{
15+
if (auto value{args.GetArg("-assumevalid")}) opts.assumed_valid_block = uint256S(*value);
16+
1517
if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value};
1618
}
1719
} // namespace node

src/validation.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ bool g_parallel_script_checks{false};
124124
bool fCheckBlockIndex = false;
125125
bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
126126

127-
uint256 hashAssumeValid;
128127
arith_uint256 nMinimumChainWork;
129128

130129
const CBlockIndex* Chainstate::FindForkInGlobalIndex(const CBlockLocator& locator) const
@@ -2036,13 +2035,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
20362035
}
20372036

20382037
bool fScriptChecks = true;
2039-
if (!hashAssumeValid.IsNull()) {
2038+
if (!m_chainman.AssumedValidBlock().IsNull()) {
20402039
// We've been configured with the hash of a block which has been externally verified to have a valid history.
20412040
// A suitable default value is included with the software and updated from time to time. Because validity
20422041
// relative to a piece of software is an objective fact these defaults can be easily reviewed.
20432042
// This setting doesn't force the selection of any particular chain but makes validating some faster by
20442043
// effectively caching the result of part of the verification.
2045-
BlockMap::const_iterator it = m_blockman.m_block_index.find(hashAssumeValid);
2044+
BlockMap::const_iterator it{m_blockman.m_block_index.find(m_chainman.AssumedValidBlock())};
20462045
if (it != m_blockman.m_block_index.end()) {
20472046
if (it->second.GetAncestor(pindex->nHeight) == pindex &&
20482047
m_chainman.m_best_header->GetAncestor(pindex->nHeight) == pindex &&
@@ -5244,6 +5243,20 @@ void ChainstateManager::ResetChainstates()
52445243
m_active_chainstate = nullptr;
52455244
}
52465245

5246+
/**
5247+
* Apply default chain params to nullopt members.
5248+
* This helps to avoid coding errors around the accidental use of the compare
5249+
* operators that accept nullopt, thus ignoring the intended default value.
5250+
*/
5251+
static ChainstateManager::Options&& Flatten(ChainstateManager::Options&& opts)
5252+
{
5253+
if (!opts.assumed_valid_block.has_value()) opts.assumed_valid_block = opts.chainparams.GetConsensus().defaultAssumeValid;
5254+
Assert(opts.adjusted_time_callback);
5255+
return std::move(opts);
5256+
}
5257+
5258+
ChainstateManager::ChainstateManager(Options options) : m_options{Flatten(std::move(options))} {}
5259+
52475260
ChainstateManager::~ChainstateManager()
52485261
{
52495262
LOCK(::cs_main);

src/validation.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ extern bool g_parallel_script_checks;
9999
extern bool fCheckBlockIndex;
100100
extern bool fCheckpointsEnabled;
101101

102-
/** Block hash whose ancestors we will assume to have valid scripts without checking them. */
103-
extern uint256 hashAssumeValid;
104-
105102
/** Minimum work we will assume exists on some valid chain. */
106103
extern arith_uint256 nMinimumChainWork;
107104

@@ -864,13 +861,11 @@ class ChainstateManager
864861
public:
865862
using Options = kernel::ChainstateManagerOpts;
866863

867-
explicit ChainstateManager(Options options) : m_options{std::move(options)}
868-
{
869-
Assert(m_options.adjusted_time_callback);
870-
}
864+
explicit ChainstateManager(Options options);
871865

872866
const CChainParams& GetParams() const { return m_options.chainparams; }
873867
const Consensus::Params& GetConsensus() const { return m_options.chainparams.GetConsensus(); }
868+
const uint256& AssumedValidBlock() const { return *Assert(m_options.assumed_valid_block); }
874869

875870
/**
876871
* Alias for ::cs_main.

0 commit comments

Comments
 (0)