mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-11-20 02:25:40 +01:00
refactor: Simplify ApplyStats and ApplyHash
This commit is contained in:
parent
9c8a265fd2
commit
a8a46c4b3c
@ -26,36 +26,6 @@ static uint64_t GetBogoSize(const CScript& scriptPubKey)
|
|||||||
scriptPubKey.size() /* scriptPubKey */;
|
scriptPubKey.size() /* scriptPubKey */;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
if (it == outputs.begin()) {
|
|
||||||
ss << hash;
|
|
||||||
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
|
|
||||||
}
|
|
||||||
|
|
||||||
ss << VARINT(it->first + 1);
|
|
||||||
ss << it->second.out.scriptPubKey;
|
|
||||||
ss << VARINT_MODE(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
|
|
||||||
|
|
||||||
if (it == std::prev(outputs.end())) {
|
|
||||||
ss << VARINT(0u);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {}
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
COutPoint outpoint = COutPoint(hash, it->first);
|
|
||||||
Coin coin = it->second;
|
|
||||||
|
|
||||||
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
|
|
||||||
ss << outpoint;
|
|
||||||
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
|
|
||||||
ss << coin.out;
|
|
||||||
muhash.Insert(MakeUCharSpan(ss));
|
|
||||||
}
|
|
||||||
|
|
||||||
//! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
|
//! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
|
||||||
//! validation commitments are reliant on the hash constructed by this
|
//! validation commitments are reliant on the hash constructed by this
|
||||||
//! function.
|
//! function.
|
||||||
@ -68,14 +38,45 @@ static void ApplyHash(CCoinsStats& stats, MuHash3072& muhash, const uint256& has
|
|||||||
//! It is also possible, though very unlikely, that a change in this
|
//! It is also possible, though very unlikely, that a change in this
|
||||||
//! construction could cause a previously invalid (and potentially malicious)
|
//! construction could cause a previously invalid (and potentially malicious)
|
||||||
//! UTXO snapshot to be considered valid.
|
//! UTXO snapshot to be considered valid.
|
||||||
template <typename T>
|
static void ApplyHash(CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
||||||
static void ApplyStats(CCoinsStats& stats, T& hash_obj, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
{
|
||||||
|
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
||||||
|
if (it == outputs.begin()) {
|
||||||
|
ss << hash;
|
||||||
|
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << VARINT(it->first + 1);
|
||||||
|
ss << it->second.out.scriptPubKey;
|
||||||
|
ss << VARINT_MODE(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
|
||||||
|
|
||||||
|
if (it == std::prev(outputs.end())) {
|
||||||
|
ss << VARINT(0u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
|
||||||
|
ss << outpoint;
|
||||||
|
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
|
||||||
|
ss << coin.out;
|
||||||
|
muhash.Insert(MakeUCharSpan(ss));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
||||||
{
|
{
|
||||||
assert(!outputs.empty());
|
assert(!outputs.empty());
|
||||||
stats.nTransactions++;
|
stats.nTransactions++;
|
||||||
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
||||||
ApplyHash(stats, hash_obj, hash, outputs, it);
|
|
||||||
|
|
||||||
stats.nTransactionOutputs++;
|
stats.nTransactionOutputs++;
|
||||||
stats.nTotalAmount += it->second.out.nValue;
|
stats.nTotalAmount += it->second.out.nValue;
|
||||||
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
|
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
|
||||||
@ -107,7 +108,8 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
|
|||||||
Coin coin;
|
Coin coin;
|
||||||
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
|
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
|
||||||
if (!outputs.empty() && key.hash != prevkey) {
|
if (!outputs.empty() && key.hash != prevkey) {
|
||||||
ApplyStats(stats, hash_obj, prevkey, outputs);
|
ApplyStats(stats, prevkey, outputs);
|
||||||
|
ApplyHash(hash_obj, prevkey, outputs);
|
||||||
outputs.clear();
|
outputs.clear();
|
||||||
}
|
}
|
||||||
prevkey = key.hash;
|
prevkey = key.hash;
|
||||||
@ -119,7 +121,8 @@ static bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats&
|
|||||||
pcursor->Next();
|
pcursor->Next();
|
||||||
}
|
}
|
||||||
if (!outputs.empty()) {
|
if (!outputs.empty()) {
|
||||||
ApplyStats(stats, hash_obj, prevkey, outputs);
|
ApplyStats(stats, prevkey, outputs);
|
||||||
|
ApplyHash(hash_obj, prevkey, outputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
FinalizeHash(hash_obj, stats);
|
FinalizeHash(hash_obj, stats);
|
||||||
|
Loading…
Reference in New Issue
Block a user