Skip to content

Commit

Permalink
Fix assumeutxo crash due to invalid base_blockhash
Browse files Browse the repository at this point in the history
Can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
  • Loading branch information
MarcoFalke committed May 11, 2021
1 parent fa5668b commit fae33f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/test/validation_chainstatemanager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
// Coins count is smaller than coins in file
metadata.m_coins_count -= 1;
}));
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
// Wrong hash
metadata.m_base_blockhash = uint256::ZERO;
}));
BOOST_REQUIRE(!CreateAndActivateUTXOSnapshot(
m_node, m_path_root, [](CAutoFile& auto_infile, SnapshotMetadata& metadata) {
// Wrong hash
Expand Down
41 changes: 20 additions & 21 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4815,6 +4815,26 @@ bool ChainstateManager::PopulateAndValidateSnapshot(

uint256 base_blockhash = metadata.m_base_blockhash;

CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));

if (!snapshot_start_block) {
// Needed for GetUTXOStats and ExpectedAssumeutxo to determine the height and to avoid a crash when base_blockhash.IsNull()
LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
base_blockhash.ToString());
return false;
}

int base_height = snapshot_start_block->nHeight;
auto maybe_au_data = ExpectedAssumeutxo(base_height, ::Params());

if (!maybe_au_data) {
LogPrintf("[snapshot] assumeutxo height in snapshot metadata not recognized " /* Continued */
"(%d) - refusing to load snapshot\n", base_height);
return false;
}

const AssumeutxoData& au_data = *maybe_au_data;

COutPoint outpoint;
Coin coin;
const uint64_t coins_count = metadata.m_coins_count;
Expand Down Expand Up @@ -4905,15 +4925,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(

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

CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main, return m_blockman.LookupBlockIndex(base_blockhash));

if (!snapshot_start_block) {
// Needed for GetUTXOStats to determine the height
LogPrintf("[snapshot] Did not find snapshot start blockheader %s\n",
base_blockhash.ToString());
return false;
}

CCoinsStats stats{CoinStatsHashType::HASH_SERIALIZED};
auto breakpoint_fnc = [] { /* TODO insert breakpoint here? */ };

Expand All @@ -4927,18 +4938,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
}

// Assert that the deserialized chainstate contents match the expected assumeutxo value.

int base_height = snapshot_start_block->nHeight;
auto maybe_au_data = ExpectedAssumeutxo(base_height, ::Params());

if (!maybe_au_data) {
LogPrintf("[snapshot] assumeutxo height in snapshot metadata not recognized " /* Continued */
"(%d) - refusing to load snapshot\n", base_height);
return false;
}

const AssumeutxoData& au_data = *maybe_au_data;

if (AssumeutxoHash{stats.hashSerialized} != au_data.hash_serialized) {
LogPrintf("[snapshot] bad snapshot content hash: expected %s, got %s\n",
au_data.hash_serialized.ToString(), stats.hashSerialized.ToString());
Expand Down

0 comments on commit fae33f9

Please sign in to comment.