Skip to content

Commit

Permalink
refactor: Use type-safe assumeutxo hash
Browse files Browse the repository at this point in the history
This avoids accidentally mixing it up with other hashes (like block
hashes).
  • Loading branch information
MarcoFalke committed May 11, 2021
1 parent 0000007 commit fa5668b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,11 @@ class CRegTestParams : public CChainParams {
m_assumeutxo_data = MapAssumeutxo{
{
110,
{uint256S("0x1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618"), 110},
{AssumeutxoHash{uint256S("0x1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618")}, 110},
},
{
210,
{uint256S("0x9c5ed99ef98544b34f8920b6d1802f72ac28ae6e2bd2bd4c316ff10c230df3f2"), 210},
{AssumeutxoHash{uint256S("0x9c5ed99ef98544b34f8920b6d1802f72ac28ae6e2bd2bd4c316ff10c230df3f2")}, 210},
},
};

Expand Down
7 changes: 6 additions & 1 deletion src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <consensus/params.h>
#include <primitives/block.h>
#include <protocol.h>
#include <util/hash_type.h>

#include <memory>
#include <vector>
Expand All @@ -25,14 +26,18 @@ struct CCheckpointData {
}
};

struct AssumeutxoHash : public BaseHash<uint256> {
explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
};

/**
* Holds configuration for use during UTXO snapshot load and validation. The contents
* here are security critical, since they dictate which UTXO snapshots are recognized
* as valid.
*/
struct AssumeutxoData {
//! The expected hash of the deserialized UTXO set.
const uint256 hash_serialized;
const AssumeutxoHash hash_serialized;

//! Used to populate the nChainTx value, which is used during BlockManager::LoadBlockIndex().
//!
Expand Down
4 changes: 2 additions & 2 deletions src/test/validation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ BOOST_AUTO_TEST_CASE(test_assumeutxo)
}

const auto out110 = *ExpectedAssumeutxo(110, *params);
BOOST_CHECK_EQUAL(out110.hash_serialized, uint256S("1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618"));
BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
BOOST_CHECK_EQUAL(out110.nChainTx, (unsigned int)110);

const auto out210 = *ExpectedAssumeutxo(210, *params);
BOOST_CHECK_EQUAL(out210.hash_serialized, uint256S("9c5ed99ef98544b34f8920b6d1802f72ac28ae6e2bd2bd4c316ff10c230df3f2"));
BOOST_CHECK_EQUAL(out210.hash_serialized.ToString(), "9c5ed99ef98544b34f8920b6d1802f72ac28ae6e2bd2bd4c316ff10c230df3f2");
BOOST_CHECK_EQUAL(out210.nChainTx, (unsigned int)210);
}

Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4939,7 +4939,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(

const AssumeutxoData& au_data = *maybe_au_data;

if (stats.hashSerialized != au_data.hash_serialized) {
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());
return false;
Expand Down

0 comments on commit fa5668b

Please sign in to comment.