Skip to content

Commit a8a46c4

Browse files
committed
refactor: Simplify ApplyStats and ApplyHash
1 parent 9c8a265 commit a8a46c4

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

src/node/coinstats.cpp

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,6 @@ static uint64_t GetBogoSize(const CScript& scriptPubKey)
2626
scriptPubKey.size() /* scriptPubKey */;
2727
}
2828

29-
static void ApplyHash(CCoinsStats& stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs, std::map<uint32_t, Coin>::const_iterator it)
30-
{
31-
if (it == outputs.begin()) {
32-
ss << hash;
33-
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
34-
}
35-
36-
ss << VARINT(it->first + 1);
37-
ss << it->second.out.scriptPubKey;
38-
ss << VARINT_MODE(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
39-
40-
if (it == std::prev(outputs.end())) {
41-
ss << VARINT(0u);
42-
}
43-
}
44-
45-
static void ApplyHash(CCoinsStats& stats, std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs, std::map<uint32_t, Coin>::const_iterator it) {}
46-
47-
static void ApplyHash(CCoinsStats& stats, MuHash3072& muhash, const uint256& hash, const std::map<uint32_t, Coin>& outputs, std::map<uint32_t, Coin>::const_iterator it)
48-
{
49-
COutPoint outpoint = COutPoint(hash, it->first);
50-
Coin coin = it->second;
51-
52-
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
53-
ss << outpoint;
54-
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
55-
ss << coin.out;
56-
muhash.Insert(MakeUCharSpan(ss));
57-
}
58-
5929
//! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
6030
//! validation commitments are reliant on the hash constructed by this
6131
//! function.
@@ -68,14 +38,45 @@ static void ApplyHash(CCoinsStats& stats, MuHash3072& muhash, const uint256& has
6838
//! It is also possible, though very unlikely, that a change in this
6939
//! construction could cause a previously invalid (and potentially malicious)
7040
//! UTXO snapshot to be considered valid.
71-
template <typename T>
72-
static void ApplyStats(CCoinsStats& stats, T& hash_obj, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
41+
static void ApplyHash(CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
42+
{
43+
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
44+
if (it == outputs.begin()) {
45+
ss << hash;
46+
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
47+
}
48+
49+
ss << VARINT(it->first + 1);
50+
ss << it->second.out.scriptPubKey;
51+
ss << VARINT_MODE(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
52+
53+
if (it == std::prev(outputs.end())) {
54+
ss << VARINT(0u);
55+
}
56+
}
57+
}
58+
59+
static void ApplyHash(std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs) {}
60+
61+
static void ApplyHash(MuHash3072& muhash, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
62+
{
63+
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
64+
COutPoint outpoint = COutPoint(hash, it->first);
65+
Coin coin = it->second;
66+
67+
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
68+
ss << outpoint;
69+
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
70+
ss << coin.out;
71+
muhash.Insert(MakeUCharSpan(ss));
72+
}
73+
}
74+
75+
static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
7376
{
7477
assert(!outputs.empty());
7578
stats.nTransactions++;
7679
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
77-
ApplyHash(stats, hash_obj, hash, outputs, it);
78-
7980
stats.nTransactionOutputs++;
8081
stats.nTotalAmount += it->second.out.nValue;
8182
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
@@ -107,7 +108,8 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
107108
Coin coin;
108109
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
109110
if (!outputs.empty() && key.hash != prevkey) {
110-
ApplyStats(stats, hash_obj, prevkey, outputs);
111+
ApplyStats(stats, prevkey, outputs);
112+
ApplyHash(hash_obj, prevkey, outputs);
111113
outputs.clear();
112114
}
113115
prevkey = key.hash;
@@ -119,7 +121,8 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
119121
pcursor->Next();
120122
}
121123
if (!outputs.empty()) {
122-
ApplyStats(stats, hash_obj, prevkey, outputs);
124+
ApplyStats(stats, prevkey, outputs);
125+
ApplyHash(hash_obj, prevkey, outputs);
123126
}
124127

125128
FinalizeHash(hash_obj, stats);

0 commit comments

Comments
 (0)