Skip to content

Commit 2862aca

Browse files
committed
Move some additional variables into CChainState private
1 parent fd4d80a commit 2862aca

File tree

2 files changed

+47
-46
lines changed

2 files changed

+47
-46
lines changed

src/validation.cpp

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ class CChainState {
113113
* missing the data for the block.
114114
*/
115115
std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
116+
117+
/**
118+
* Every received block is assigned a unique and increasing identifier, so we
119+
* know which one to give priority in case of a fork.
120+
*/
121+
CCriticalSection cs_nBlockSequenceId;
122+
/** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
123+
int32_t nBlockSequenceId = 1;
124+
/** Decreasing counter (used by subsequent preciousblock calls). */
125+
int32_t nBlockReverseSequenceId = -1;
126+
/** chainwork for the last block that preciousblock has been applied to. */
127+
arith_uint256 nLastPreciousChainwork = 0;
128+
129+
/** In order to efficiently track invalidity of headers, we keep the set of
130+
* blocks which we tried to connect and found to be invalid here (ie which
131+
* were set to BLOCK_FAILED_VALID since the last restart). We can then
132+
* walk this set and check if a new header is a descendant of something in
133+
* this set, preventing us from having to walk mapBlockIndex when we try
134+
* to connect a bad block and fail.
135+
*
136+
* While this is more complicated than marking everything which descends
137+
* from an invalid block as invalid at the time we discover it to be
138+
* invalid, doing so would require walking all of mapBlockIndex to find all
139+
* descendants. Since this case should be very rare, keeping track of all
140+
* BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
141+
* well.
142+
*
143+
* Because we already walk mapBlockIndex in height-order at startup, we go
144+
* ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
145+
* instead of putting things in this set.
146+
*/
147+
std::set<CBlockIndex*> g_failed_blocks;
148+
116149
public:
117150
CChain chainActive;
118151
BlockMap mapBlockIndex;
@@ -220,38 +253,6 @@ namespace {
220253
*/
221254
bool fCheckForPruning = false;
222255

223-
/**
224-
* Every received block is assigned a unique and increasing identifier, so we
225-
* know which one to give priority in case of a fork.
226-
*/
227-
CCriticalSection cs_nBlockSequenceId;
228-
/** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
229-
int32_t nBlockSequenceId = 1;
230-
/** Decreasing counter (used by subsequent preciousblock calls). */
231-
int32_t nBlockReverseSequenceId = -1;
232-
/** chainwork for the last block that preciousblock has been applied to. */
233-
arith_uint256 nLastPreciousChainwork = 0;
234-
235-
/** In order to efficiently track invalidity of headers, we keep the set of
236-
* blocks which we tried to connect and found to be invalid here (ie which
237-
* were set to BLOCK_FAILED_VALID since the last restart). We can then
238-
* walk this set and check if a new header is a descendant of something in
239-
* this set, preventing us from having to walk mapBlockIndex when we try
240-
* to connect a bad block and fail.
241-
*
242-
* While this is more complicated than marking everything which descends
243-
* from an invalid block as invalid at the time we discover it to be
244-
* invalid, doing so would require walking all of mapBlockIndex to find all
245-
* descendants. Since this case should be very rare, keeping track of all
246-
* BLOCK_FAILED_VALID blocks in a set should be just fine and work just as
247-
* well.
248-
*
249-
* Because we already walk mapBlockIndex in height-order at startup, we go
250-
* ahead and mark descendants of invalid blocks as FAILED_CHILD at that time,
251-
* instead of putting things in this set.
252-
*/
253-
std::set<CBlockIndex*> g_failed_blocks;
254-
255256
/** Dirty block index entries. */
256257
std::set<CBlockIndex*> setDirtyBlockIndex;
257258

@@ -2129,10 +2130,8 @@ static void DoWarning(const std::string& strWarning)
21292130
}
21302131
}
21312132

2132-
/** Update chainActive and related internal data structures. */
2133-
void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2134-
chainActive.SetTip(pindexNew);
2135-
2133+
/** Check warning conditions and do some notifications on new chain tip set. */
2134+
void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainParams) {
21362135
// New best block
21372136
mempool.AddTransactionsUpdated(1);
21382137

@@ -2142,7 +2141,7 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
21422141
if (!IsInitialBlockDownload())
21432142
{
21442143
int nUpgraded = 0;
2145-
const CBlockIndex* pindex = chainActive.Tip();
2144+
const CBlockIndex* pindex = pindexNew;
21462145
for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
21472146
WarningBitsConditionChecker checker(bit);
21482147
ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
@@ -2173,10 +2172,10 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
21732172
}
21742173
}
21752174
LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)", __func__,
2176-
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
2177-
log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
2178-
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2179-
GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2175+
pindexNew->GetBlockHash().ToString(), pindexNew->nHeight, pindexNew->nVersion,
2176+
log(pindexNew->nChainWork.getdouble())/log(2.0), (unsigned long)pindexNew->nChainTx,
2177+
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", pindexNew->GetBlockTime()),
2178+
GuessVerificationProgress(chainParams.TxData(), pindexNew), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
21802179
if (!warningMessages.empty())
21812180
LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
21822181
LogPrintf("\n");
@@ -2230,7 +2229,8 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
22302229
}
22312230
}
22322231

2233-
// Update chainActive and related variables.
2232+
chainActive.SetTip(pindexDelete->pprev);
2233+
22342234
UpdateTip(pindexDelete->pprev, chainparams);
22352235
// Let wallets know transactions went from 1-confirmed to
22362236
// 0-confirmed or conflicted:
@@ -2359,6 +2359,7 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
23592359
mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
23602360
disconnectpool.removeForBlock(blockConnecting.vtx);
23612361
// Update chainActive & related variables.
2362+
chainActive.SetTip(pindexNew);
23622363
UpdateTip(pindexNew, chainparams);
23632364

23642365
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
@@ -4087,6 +4088,8 @@ bool RewindBlockIndex(const CChainParams& params) {
40874088
}
40884089

40894090
void CChainState::UnloadBlockIndex() {
4091+
nBlockSequenceId = 1;
4092+
g_failed_blocks.clear();
40904093
setBlockIndexCandidates.clear();
40914094
}
40924095

@@ -4103,9 +4106,7 @@ void UnloadBlockIndex()
41034106
mapBlocksUnlinked.clear();
41044107
vinfoBlockFile.clear();
41054108
nLastBlockFile = 0;
4106-
nBlockSequenceId = 1;
41074109
setDirtyBlockIndex.clear();
4108-
g_failed_blocks.clear();
41094110
setDirtyFileInfo.clear();
41104111
versionbitscache.Clear();
41114112
for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
@@ -4637,7 +4638,7 @@ bool DumpMempool(void)
46374638
}
46384639

46394640
//! Guess how far we are in the verification process at the given block index
4640-
double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
4641+
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pindex) {
46414642
if (pindex == nullptr)
46424643
return 0.0;
46434644

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams,
279279
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
280280

281281
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
282-
double GuessVerificationProgress(const ChainTxData& data, CBlockIndex* pindex);
282+
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex* pindex);
283283

284284
/** Calculate the amount of disk space the block & undo files currently use */
285285
uint64_t CalculateCurrentUsage();

0 commit comments

Comments
 (0)