Skip to content

Commit fae33f9

Browse files
author
MarcoFalke
committed
Fix assumeutxo crash due to invalid base_blockhash
Can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent fa5668b commit fae33f9

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

src/test/validation_chainstatemanager_tests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
260260
// Coins count is smaller than coins in file
261261
metadata.m_coins_count -= 1;
262262
}));
263+
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
264+
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
265+
// Wrong hash
266+
metadata.m_base_blockhash = uint256::ZERO;
267+
}));
263268
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
264269
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
265270
// Wrong hash

src/validation.cpp

+20-21
Original file line numberDiff line numberDiff line change
@@ -4815,6 +4815,26 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
48154815

48164816
uint256 base_blockhash = metadata.m_base_blockhash;
48174817

4818+
CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
4819+
4820+
if (!snapshot_start_block) {
4821+
// Needed for GetUTXOStats and ExpectedAssumeutxo to determine the height and to avoid a crash when base_blockhash.IsNull()
4822+
LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
4823+
base_blockhash.ToString());
4824+
return false;
4825+
}
4826+
4827+
int base_height = snapshot_start_block->nHeight;
4828+
auto maybe_au_data = ExpectedAssumeutxo(base_height, ::Params());
4829+
4830+
if (!maybe_au_data) {
4831+
LogPrintf("[snapshot] assumeutxo height in snapshot metadata not recognized " /* Continued */
4832+
"(%d) - refusing to load snapshot\n", base_height);
4833+
return false;
4834+
}
4835+
4836+
const AssumeutxoData& au_data = *maybe_au_data;
4837+
48184838
COutPoint outpoint;
48194839
Coin coin;
48204840
const uint64_t coins_count = metadata.m_coins_count;
@@ -4905,15 +4925,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49054925

49064926
assert(coins_cache.GetBestBlock() == base_blockhash);
49074927

4908-
CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));
4909-
4910-
if (!snapshot_start_block) {
4911-
// Needed for GetUTXOStats to determine the height
4912-
LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
4913-
base_blockhash.ToString());
4914-
return false;
4915-
}
4916-
49174928
CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
49184929
auto breakpoint_fnc = [] { /* TODO insert breakpoint here? */ };
49194930

@@ -4927,18 +4938,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
49274938
}
49284939

49294940
// Assert that the deserialized chainstate contents match the expected assumeutxo value.
4930-
4931-
int base_height = snapshot_start_block->nHeight;
4932-
auto maybe_au_data = ExpectedAssumeutxo(base_height, ::Params());
4933-
4934-
if (!maybe_au_data) {
4935-
LogPrintf("[snapshot] assumeutxo height in snapshot metadata not recognized " /* Continued */
4936-
"(%d) - refusing to load snapshot\n", base_height);
4937-
return false;
4938-
}
4939-
4940-
const AssumeutxoData& au_data = *maybe_au_data;
4941-
49424941
if (AssumeutxoHash{stats.hashSerialized} != au_data.hash_serialized) {
49434942
LogPrintf("[snapshot] bad snapshot content hash: expected %s, got %s\n",
49444943
au_data.hash_serialized.ToString(), stats.hashSerialized.ToString());

0 commit comments

Comments
 (0)