@@ -118,29 +118,31 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
118118 const CAmount block_subsidy{GetBlockSubsidy (block.height , Params ().GetConsensus ())};
119119 m_total_subsidy += block_subsidy;
120120
121- // Ignore genesis block
122- if (block.height > 0 ) {
121+ // Include genesis block
122+ if (block.height >= 0 ) {
123123 // pindex variable gives indexing code access to node internals. It
124124 // will be removed in upcoming commit
125125 const CBlockIndex* pindex = WITH_LOCK (cs_main, return m_chainstate->m_blockman .LookupBlockIndex (block.hash ));
126126 if (!m_chainstate->m_blockman .ReadBlockUndo (block_undo, *pindex)) {
127127 return false ;
128128 }
129129
130- std::pair<uint256, DBVal> read_out;
131- if (!m_db->Read (DBHeightKey (block.height - 1 ), read_out)) {
132- return false ;
133- }
130+ if (block.height > 0 ) {
131+ std::pair<uint256, DBVal> read_out;
132+ if (!m_db->Read (DBHeightKey (block.height - 1 ), read_out)) {
133+ return false ;
134+ }
134135
135- uint256 expected_block_hash{*Assert (block.prev_hash )};
136- if (read_out.first != expected_block_hash) {
137- LogPrintf (" WARNING: previous block header belongs to unexpected block %s; expected %s\n " ,
138- read_out.first .ToString (), expected_block_hash.ToString ());
136+ uint256 expected_block_hash{*Assert (block.prev_hash )};
137+ if (read_out.first != expected_block_hash) {
138+ LogPrintf (" WARNING: previous block header belongs to unexpected block %s; expected %s\n " ,
139+ read_out.first .ToString (), expected_block_hash.ToString ());
139140
140- if (!m_db->Read (DBHashKey (expected_block_hash), read_out)) {
141- LogError (" %s: previous block header not found; expected %s\n " ,
142- __func__, expected_block_hash.ToString ());
143- return false ;
141+ if (!m_db->Read (DBHashKey (expected_block_hash), read_out)) {
142+ LogError (" %s: previous block header not found; expected %s\n " ,
143+ __func__, expected_block_hash.ToString ());
144+ return false ;
145+ }
144146 }
145147 }
146148
@@ -413,25 +415,27 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
413415 const CAmount block_subsidy{GetBlockSubsidy (pindex->nHeight , Params ().GetConsensus ())};
414416 m_total_subsidy -= block_subsidy;
415417
416- // Ignore genesis block
417- if (pindex->nHeight > 0 ) {
418+ // Include genesis block
419+ if (pindex->nHeight >= 0 ) {
418420 if (!m_chainstate->m_blockman .ReadBlockUndo (block_undo, *pindex)) {
419421 return false ;
420422 }
421423
422- if (!m_db->Read (DBHeightKey (pindex->nHeight - 1 ), read_out)) {
423- return false ;
424- }
424+ if (pindex->nHeight > 0 ) {
425+ if (!m_db->Read (DBHeightKey (pindex->nHeight - 1 ), read_out)) {
426+ return false ;
427+ }
425428
426- uint256 expected_block_hash{pindex->pprev ->GetBlockHash ()};
427- if (read_out.first != expected_block_hash) {
428- LogPrintf (" WARNING: previous block header belongs to unexpected block %s; expected %s\n " ,
429- read_out.first .ToString (), expected_block_hash.ToString ());
429+ uint256 expected_block_hash{pindex->pprev ->GetBlockHash ()};
430+ if (read_out.first != expected_block_hash) {
431+ LogPrintf (" WARNING: previous block header belongs to unexpected block %s; expected %s\n " ,
432+ read_out.first .ToString (), expected_block_hash.ToString ());
430433
431- if (!m_db->Read (DBHashKey (expected_block_hash), read_out)) {
432- LogError (" %s: previous block header not found; expected %s\n " ,
433- __func__, expected_block_hash.ToString ());
434- return false ;
434+ if (!m_db->Read (DBHashKey (expected_block_hash), read_out)) {
435+ LogError (" %s: previous block header not found; expected %s\n " ,
436+ __func__, expected_block_hash.ToString ());
437+ return false ;
438+ }
435439 }
436440 }
437441 }
0 commit comments