Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Oct 19, 2023
1 parent 9454f66 commit 6b3628f
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/kernel/coinstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ DataStream TxOutSer(const COutPoint& outpoint, const Coin& coin)
return ss;
}

static void ApplyHashInternal(HashWriter& ss, const Span<const uint8_t> serialized_coin)
{
ss << serialized_coin;
}

static void ApplyHashInternal(MuHash3072& muhash, const Span<const uint8_t> serialized_coin)
{
muhash.Insert(serialized_coin);
}

static void ApplyHashInternal(std::nullptr_t, const Span<const uint8_t> serialized_coin) {}

//! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
//! validation commitments are reliant on the hash constructed by this
//! function.
Expand All @@ -69,7 +81,17 @@ DataStream TxOutSer(const COutPoint& outpoint, const Coin& coin)
//! It is also possible, though very unlikely, that a change in this
//! construction could cause a previously invalid (and potentially malicious)
//! UTXO snapshot to be considered valid.
static void ApplyHash(HashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
template <typename T>
static void ApplyHash(T hash_obj, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
COutPoint outpoint = COutPoint(hash, it->first);
Coin coin = it->second;
ApplyHashInternal(hash_obj, MakeUCharSpan(TxOutSer(outpoint, coin)));
}
}

static void ApplyHashLegacy(HashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
if (it == outputs.begin()) {
Expand All @@ -87,17 +109,6 @@ static void ApplyHash(HashWriter& ss, const uint256& hash, const std::map<uint32
}
}

static void ApplyHash(std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs) {}

static void ApplyHash(MuHash3072& muhash, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
COutPoint outpoint = COutPoint(hash, it->first);
Coin coin = it->second;
muhash.Insert(MakeUCharSpan(TxOutSer(outpoint, coin)));
}
}

static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
assert(!outputs.empty());
Expand All @@ -118,8 +129,6 @@ static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, c
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
assert(pcursor);

PrepareHash(hash_obj, stats);

uint256 prevkey;
std::map<uint32_t, Coin> outputs;
while (pcursor->Valid()) {
Expand Down Expand Up @@ -180,15 +189,6 @@ std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsV
return stats;
}

// The legacy hash serializes the hashBlock
static void PrepareHash(HashWriter& ss, const CCoinsStats& stats)
{
ss << stats.hashBlock;
}
// MuHash does not need the prepare step
static void PrepareHash(MuHash3072& muhash, CCoinsStats& stats) {}
static void PrepareHash(std::nullptr_t, CCoinsStats& stats) {}

static void FinalizeHash(HashWriter& ss, CCoinsStats& stats)
{
stats.hashSerialized = ss.GetHash();
Expand Down

0 comments on commit 6b3628f

Please sign in to comment.