Skip to content

Commit cd68c86

Browse files
test
1 parent 2d03a07 commit cd68c86

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

src/index/coinstatsindex.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/kernel/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ class CRegTestParams : public CChainParams
618618
m_assumeutxo_data = {
619619
{ // For use by unit tests
620620
.height = 110,
621-
.hash_serialized = AssumeutxoHash{uint256{"6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1"}},
621+
.hash_serialized = AssumeutxoHash{uint256{"96081d2bf33374a10056f43e978c2d77b333c0541ee917aae2d878476d930056"}},
622622
.m_chain_tx_count = 111,
623623
.blockhash = consteval_ctor(uint256{"696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"}),
624624
},

src/node/blockstorage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
255255
// to be downloaded again in order to consider its chain, at which
256256
// point it would be considered as a candidate for
257257
// m_blocks_unlinked or setBlockIndexCandidates.
258-
auto range = m_blocks_unlinked.equal_range(pindex->pprev);
258+
auto range = m_blocks_unlinked.equal_range(pindex->pprev ? pindex->pprev : pindex);
259259
while (range.first != range.second) {
260260
std::multimap<CBlockIndex*, CBlockIndex*>::iterator _it = range.first;
261261
range.first++;
@@ -685,7 +685,7 @@ bool BlockManager::ReadBlockUndo(CBlockUndo& blockundo, const CBlockIndex& index
685685
uint256 hashChecksum;
686686
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
687687
try {
688-
verifier << index.pprev->GetBlockHash();
688+
verifier << (index.pprev ? index.pprev->GetBlockHash() : uint256::ZERO);
689689
verifier >> blockundo;
690690
filein >> hashChecksum;
691691
} catch (const std::exception& e) {
@@ -966,7 +966,7 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
966966

967967
// Calculate & write checksum
968968
HashWriter hasher{};
969-
hasher << block.pprev->GetBlockHash();
969+
hasher << (block.pprev ? block.pprev->GetBlockHash() : uint256::ZERO);
970970
hasher << blockundo;
971971
fileout << hasher.GetHash();
972972

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,15 @@ struct SnapshotTestSetup : TestChain100Setup {
188188
LOCK(::cs_main);
189189
BOOST_CHECK(!chainman.IsSnapshotValidated());
190190
BOOST_CHECK(!node::FindSnapshotChainstateDir(chainman.m_options.datadir));
191+
192+
const CBlock& genesis = chainman.GetParams().GenesisBlock();
193+
if (std::find(m_coinbase_txns.begin(), m_coinbase_txns.end(), genesis.vtx[0]) == m_coinbase_txns.end()) {
194+
m_coinbase_txns.push_back(genesis.vtx[0]);
195+
}
191196
}
192197

193198
size_t initial_size;
194-
size_t initial_total_coins{100};
199+
size_t initial_total_coins{101};
195200

196201
// Make some initial assertions about the contents of the chainstate.
197202
{

src/test/validation_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ BOOST_AUTO_TEST_CASE(test_assumeutxo)
142142
}
143143

144144
const auto out110 = *params->AssumeutxoForHeight(110);
145-
BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1");
145+
BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "96081d2bf33374a10056f43e978c2d77b333c0541ee917aae2d878476d930056");
146146
BOOST_CHECK_EQUAL(out110.m_chain_tx_count, 111U);
147147

148148
const auto out110_2 = *params->AssumeutxoForBlockhash(uint256{"696e92821f65549c7ee134edceeeeaaa4105647a3c4fd9f298c0aec0ab50425c"});
149-
BOOST_CHECK_EQUAL(out110_2.hash_serialized.ToString(), "6657b736d4fe4db0cbc796789e812d5dba7f5c143764b1b6905612f1830609d1");
149+
BOOST_CHECK_EQUAL(out110_2.hash_serialized.ToString(), "96081d2bf33374a10056f43e978c2d77b333c0541ee917aae2d878476d930056");
150150
BOOST_CHECK_EQUAL(out110_2.m_chain_tx_count, 111U);
151151
}
152152

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,7 @@ int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out, cons
22692269
if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
22702270

22712271
// We have to exclude the genesis coinbase from this check because its height actually is zero and the wallet will think there is database corruption otherwise
2272-
if (undo.nHeight == 0 && out.hash != genesisTxid) {
2272+
if (undo.nHeight == 0 && (!undo.fCoinBase || out.hash != genesisTxid)) {
22732273
// Missing undo metadata (height and coinbase). Older versions included this
22742274
// information only in undo records for the last spend of a transactions'
22752275
// outputs. This implies that it must be present for some other output of the same tx.
@@ -2742,7 +2742,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
27422742
return true;
27432743
}
27442744

2745-
if (pindex->GetBlockHash() != params.GetConsensus().hashGenesisBlock && !m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
2745+
if (!m_blockman.WriteBlockUndo(blockundo, state, *pindex)) {
27462746
return false;
27472747
}
27482748

0 commit comments

Comments
 (0)