@@ -113,6 +113,39 @@ class CChainState {
113
113
* missing the data for the block.
114
114
*/
115
115
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
+
116
149
public:
117
150
CChain chainActive;
118
151
BlockMap mapBlockIndex;
@@ -220,38 +253,6 @@ namespace {
220
253
*/
221
254
bool fCheckForPruning = false ;
222
255
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
-
255
256
/* * Dirty block index entries. */
256
257
std::set<CBlockIndex*> setDirtyBlockIndex;
257
258
@@ -2129,10 +2130,8 @@ static void DoWarning(const std::string& strWarning)
2129
2130
}
2130
2131
}
2131
2132
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) {
2136
2135
// New best block
2137
2136
mempool.AddTransactionsUpdated (1 );
2138
2137
@@ -2142,7 +2141,7 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2142
2141
if (!IsInitialBlockDownload ())
2143
2142
{
2144
2143
int nUpgraded = 0 ;
2145
- const CBlockIndex* pindex = chainActive. Tip () ;
2144
+ const CBlockIndex* pindex = pindexNew ;
2146
2145
for (int bit = 0 ; bit < VERSIONBITS_NUM_BITS; bit++) {
2147
2146
WarningBitsConditionChecker checker (bit);
2148
2147
ThresholdState state = checker.GetStateFor (pindex, chainParams.GetConsensus (), warningcache[bit]);
@@ -2173,10 +2172,10 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2173
2172
}
2174
2173
}
2175
2174
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 ());
2180
2179
if (!warningMessages.empty ())
2181
2180
LogPrintf (" warning='%s'" , boost::algorithm::join (warningMessages, " , " ));
2182
2181
LogPrintf (" \n " );
@@ -2230,7 +2229,8 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
2230
2229
}
2231
2230
}
2232
2231
2233
- // Update chainActive and related variables.
2232
+ chainActive.SetTip (pindexDelete->pprev );
2233
+
2234
2234
UpdateTip (pindexDelete->pprev , chainparams);
2235
2235
// Let wallets know transactions went from 1-confirmed to
2236
2236
// 0-confirmed or conflicted:
@@ -2359,6 +2359,7 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
2359
2359
mempool.removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2360
2360
disconnectpool.removeForBlock (blockConnecting.vtx );
2361
2361
// Update chainActive & related variables.
2362
+ chainActive.SetTip (pindexNew);
2362
2363
UpdateTip (pindexNew, chainparams);
2363
2364
2364
2365
int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
@@ -4087,6 +4088,8 @@ bool RewindBlockIndex(const CChainParams& params) {
4087
4088
}
4088
4089
4089
4090
void CChainState::UnloadBlockIndex () {
4091
+ nBlockSequenceId = 1 ;
4092
+ g_failed_blocks.clear ();
4090
4093
setBlockIndexCandidates.clear ();
4091
4094
}
4092
4095
@@ -4103,9 +4106,7 @@ void UnloadBlockIndex()
4103
4106
mapBlocksUnlinked.clear ();
4104
4107
vinfoBlockFile.clear ();
4105
4108
nLastBlockFile = 0 ;
4106
- nBlockSequenceId = 1 ;
4107
4109
setDirtyBlockIndex.clear ();
4108
- g_failed_blocks.clear ();
4109
4110
setDirtyFileInfo.clear ();
4110
4111
versionbitscache.Clear ();
4111
4112
for (int b = 0 ; b < VERSIONBITS_NUM_BITS; b++) {
@@ -4637,7 +4638,7 @@ bool DumpMempool(void)
4637
4638
}
4638
4639
4639
4640
// ! 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) {
4641
4642
if (pindex == nullptr )
4642
4643
return 0.0 ;
4643
4644
0 commit comments