Page MenuHomePhabricator

D17938.id53526.diff
No OneTemporary

D17938.id53526.diff

diff --git a/src/blockindex.h b/src/blockindex.h
--- a/src/blockindex.h
+++ b/src/blockindex.h
@@ -137,25 +137,6 @@
*/
int64_t GetChainTxCount() const { return nChainTx; }
- /**
- * Reset chain tx stats.
- */
- void ResetChainStats();
-
- /**
- * Reset chain tx stats and log a warning if the block is not the snapshot
- * block, and the nChainTx value is not zero or the correct value.
- * Don't modify the value otherwise.
- */
- void MaybeResetChainStats(bool is_snapshot_base_block);
-
- /**
- * Update chain tx stats and return True if this block is the genesis block
- * or all parents have their tx stats set.
- * Else, do nothing and return False.
- */
- bool UpdateChainStats();
-
/**
* Check whether this block and all previous blocks back to the genesis
* block or an assumeutxo snapshot block have reached VALID_TRANSACTIONS
diff --git a/src/blockindex.cpp b/src/blockindex.cpp
--- a/src/blockindex.cpp
+++ b/src/blockindex.cpp
@@ -33,46 +33,6 @@
nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString());
}
-void CBlockIndex::ResetChainStats() {
- nChainTx = 0;
-}
-
-void CBlockIndex::MaybeResetChainStats(bool is_snapshot_base_block) {
- // Typically nChainTx will be 0 at this point, but it can be nonzero if this
- // is a pruned block which is being downloaded again, or if this is an
- // assumeutxo snapshot block which has a hardcoded nChainTx value from the
- // snapshot metadata. If the pindex is not the snapshot block and the
- // nChainTx value is not zero, assert that value is actually correct.
- unsigned int correct_value = nTx + (pprev ? pprev->nChainTx : 0);
- if (!Assume(nChainTx == 0 || nChainTx == correct_value ||
- is_snapshot_base_block)) {
- LogPrintf("Internal bug detected: block %d has unexpected nChainTx %i "
- "that should be %i. Please report this issue here: %s\n",
- nHeight, nChainTx, correct_value, PACKAGE_BUGREPORT);
- ResetChainStats();
- }
-}
-
-bool CBlockIndex::UpdateChainStats() {
- unsigned int correct_value = nTx + (pprev ? pprev->nChainTx : 0);
- // Before setting nChainTx, assert that it is 0 or already set to
- // the correct value. This assert will fail after receiving the
- // assumeutxo snapshot block if assumeutxo snapshot metadata has an
- // incorrect hardcoded AssumeutxoData::nChainTx value.
- if (!Assume(nChainTx == 0 || nChainTx == correct_value)) {
- LogPrintf("Internal bug detected: block %d has unexpected nChainTx %i "
- "that should be %i. Please report this issue here: %s\n",
- nHeight, nChainTx, correct_value, PACKAGE_BUGREPORT);
- }
-
- if (pprev == nullptr || pprev->nChainTx > 0) {
- nChainTx = correct_value;
- return true;
- }
-
- return false;
-}
-
const CBlockIndex *CBlockIndex::GetAncestor(int height) const {
if (height > nHeight || height < 0) {
return nullptr;
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -308,14 +308,17 @@
// basis of snapshot load (see PopulateAndValidateSnapshot()).
// Pruned nodes may have deleted the block.
if (pindex->nTx > 0) {
+ const unsigned int prevNChainTx =
+ pindex->pprev ? pindex->pprev->nChainTx : 0;
if (m_snapshot_height && pindex->nHeight == *m_snapshot_height &&
pindex->GetBlockHash() == *snapshot_blockhash) {
- Assert(pindex->pprev);
// Should have been set above; don't disturb it with code below.
Assert(pindex->nChainTx > 0);
- } else if (!pindex->UpdateChainStats() && pindex->pprev) {
+ } else if (prevNChainTx == 0 && pindex->pprev) {
+ pindex->nChainTx = 0;
m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
- pindex->ResetChainStats();
+ } else {
+ pindex->nChainTx = prevNChainTx + pindex->nTx;
}
}
diff --git a/src/test/fuzz/chain.cpp b/src/test/fuzz/chain.cpp
--- a/src/test/fuzz/chain.cpp
+++ b/src/test/fuzz/chain.cpp
@@ -36,10 +36,6 @@
(void)disk_block_index->GetUndoPos();
(void)disk_block_index->HaveNumChainTxs();
(void)disk_block_index->IsValid();
- (void)disk_block_index->UpdateChainStats();
- (void)disk_block_index->MaybeResetChainStats(
- fuzzed_data_provider.ConsumeBool());
- (void)disk_block_index->ResetChainStats();
}
const CBlockHeader block_header = disk_block_index->GetBlockHeader();
diff --git a/src/validation.cpp b/src/validation.cpp
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4156,7 +4156,23 @@
CBlockIndex *pindexNew,
const FlatFilePos &pos) {
pindexNew->nTx = block.vtx.size();
- pindexNew->MaybeResetChainStats(pindexNew == GetSnapshotBaseBlock());
+ // Typically nChainTX will be 0 at this point, but it can be nonzero if this
+ // is a pruned block which is being downloaded again, or if this is an
+ // assumeutxo snapshot block which has a hardcoded m_chain_tx_count value
+ // from the snapshot metadata. If the pindex is not the snapshot block and
+ // the nChainTx value is not zero, assert that value is actually correct.
+ auto prev_tx_sum = [](CBlockIndex &block) {
+ return block.nTx + (block.pprev ? block.pprev->nChainTx : 0);
+ };
+ if (!Assume(pindexNew->nChainTx == 0 ||
+ pindexNew->nChainTx == prev_tx_sum(*pindexNew) ||
+ pindexNew == GetSnapshotBaseBlock())) {
+ LogPrintf("Internal bug detected: block %d has unexpected nChainTx %i "
+ "that should be %i. Please report this issue here: %s\n",
+ pindexNew->nHeight, pindexNew->nChainTx,
+ prev_tx_sum(*pindexNew), PACKAGE_BUGREPORT);
+ pindexNew->nChainTx = 0;
+ }
pindexNew->nSize = ::GetSerializeSize(block, PROTOCOL_VERSION);
pindexNew->nFile = pos.nFile;
pindexNew->nDataPos = pos.nPos;
@@ -4165,7 +4181,7 @@
pindexNew->RaiseValidity(BlockValidity::TRANSACTIONS);
m_blockman.m_dirty_blockindex.insert(pindexNew);
- if (pindexNew->UpdateChainStats()) {
+ if (pindexNew->pprev == nullptr || pindexNew->pprev->HaveNumChainTxs()) {
// If pindexNew is the genesis block or all parents are
// BLOCK_VALID_TRANSACTIONS.
std::deque<CBlockIndex *> queue;
@@ -4176,7 +4192,19 @@
while (!queue.empty()) {
CBlockIndex *pindex = queue.front();
queue.pop_front();
- pindex->UpdateChainStats();
+ // Before setting nChainTx, assert that it is 0 or already set to
+ // the correct value. This assert will fail after receiving the
+ // assumeutxo snapshot block if assumeutxo snapshot metadata has an
+ // incorrect hardcoded AssumeutxoData::nChainTx value.
+ if (!Assume(pindex->nChainTx == 0 ||
+ pindex->nChainTx == prev_tx_sum(*pindex))) {
+ LogPrintf(
+ "Internal bug detected: block %d has unexpected nChainTx "
+ "%i that should be %i. Please report this issue here: %s\n",
+ pindex->nHeight, pindex->nChainTx, prev_tx_sum(*pindex),
+ PACKAGE_BUGREPORT);
+ }
+ pindex->nChainTx = prev_tx_sum(*pindex);
if (pindex->nSequenceId == 0) {
// We assign a sequence is when transaction are received to
// prevent a miner from being able to broadcast a block but not

File Metadata

Mime Type
text/plain
Expires
Tue, May 20, 21:40 (15 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5864951
Default Alt Text
D17938.id53526.diff (7 KB)

Event Timeline