Skip to content

Commit ef18d08

Browse files
committed
merge bitcoin#19145: Add hash_type MUHASH for gettxoutsetinfo
1 parent c7eb44a commit ef18d08

File tree

10 files changed

+199
-52
lines changed

10 files changed

+199
-52
lines changed

src/crypto/muhash.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace {
1717
using limb_t = Num3072::limb_t;
1818
using double_limb_t = Num3072::double_limb_t;
1919
constexpr int LIMB_SIZE = Num3072::LIMB_SIZE;
20-
constexpr int LIMBS = Num3072::LIMBS;
2120
/** 2^3072 - 1103717, the largest 3072-bit safe prime number, is used as the modulus. */
2221
constexpr limb_t MAX_PRIME_DIFF = 1103717;
2322

@@ -123,7 +122,7 @@ inline void square_n_mul(Num3072& in_out, const int sq, const Num3072& mul)
123122

124123
} // namespace
125124

126-
/** Indicates wether d is larger than the modulus. */
125+
/** Indicates whether d is larger than the modulus. */
127126
bool Num3072::IsOverflow() const
128127
{
129128
if (this->limbs[0] <= std::numeric_limits<limb_t>::max() - MAX_PRIME_DIFF) return false;
@@ -276,18 +275,33 @@ void Num3072::Divide(const Num3072& a)
276275
if (this->IsOverflow()) this->FullReduce();
277276
}
278277

279-
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
280-
Num3072 out{};
281-
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
282-
unsigned char tmp[BYTE_SIZE];
283-
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, BYTE_SIZE);
278+
Num3072::Num3072(const unsigned char (&data)[BYTE_SIZE]) {
279+
for (int i = 0; i < LIMBS; ++i) {
280+
if (sizeof(limb_t) == 4) {
281+
this->limbs[i] = ReadLE32(data + 4 * i);
282+
} else if (sizeof(limb_t) == 8) {
283+
this->limbs[i] = ReadLE64(data + 8 * i);
284+
}
285+
}
286+
}
287+
288+
void Num3072::ToBytes(unsigned char (&out)[BYTE_SIZE]) {
284289
for (int i = 0; i < LIMBS; ++i) {
285290
if (sizeof(limb_t) == 4) {
286-
out.limbs[i] = ReadLE32(tmp + 4 * i);
291+
WriteLE32(out + i * 4, this->limbs[i]);
287292
} else if (sizeof(limb_t) == 8) {
288-
out.limbs[i] = ReadLE64(tmp + 8 * i);
293+
WriteLE64(out + i * 8, this->limbs[i]);
289294
}
290295
}
296+
}
297+
298+
Num3072 MuHash3072::ToNum3072(Span<const unsigned char> in) {
299+
unsigned char tmp[Num3072::BYTE_SIZE];
300+
301+
uint256 hashed_in = (CHashWriter(SER_DISK, 0) << in).GetSHA256();
302+
ChaCha20(hashed_in.data(), hashed_in.size()).Keystream(tmp, Num3072::BYTE_SIZE);
303+
Num3072 out{tmp};
304+
291305
return out;
292306
}
293307

@@ -301,14 +315,8 @@ void MuHash3072::Finalize(uint256& out) noexcept
301315
m_numerator.Divide(m_denominator);
302316
m_denominator.SetToOne(); // Needed to keep the MuHash object valid
303317

304-
unsigned char data[384];
305-
for (int i = 0; i < LIMBS; ++i) {
306-
if (sizeof(limb_t) == 4) {
307-
WriteLE32(data + i * 4, m_numerator.limbs[i]);
308-
} else if (sizeof(limb_t) == 8) {
309-
WriteLE64(data + i * 8, m_numerator.limbs[i]);
310-
}
311-
}
318+
unsigned char data[Num3072::BYTE_SIZE];
319+
m_numerator.ToBytes(data);
312320

313321
out = (CHashWriter(SER_DISK, 0) << data).GetSHA256();
314322
}

src/crypto/muhash.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Num3072
2222
Num3072 GetInverse() const;
2323

2424
public:
25+
static constexpr size_t BYTE_SIZE = 384;
2526

2627
#ifdef HAVE___INT128
2728
typedef unsigned __int128 double_limb_t;
@@ -48,8 +49,10 @@ class Num3072
4849
void Divide(const Num3072& a);
4950
void SetToOne();
5051
void Square();
52+
void ToBytes(unsigned char (&out)[BYTE_SIZE]);
5153

5254
Num3072() { this->SetToOne(); };
55+
Num3072(const unsigned char (&data)[BYTE_SIZE]);
5356

5457
SERIALIZE_METHODS(Num3072, obj)
5558
{
@@ -78,7 +81,7 @@ class Num3072
7881
* arbitrary subset of the update operations, allowing them to be
7982
* efficiently combined later.
8083
*
81-
* Muhash does not support checking if an element is already part of the
84+
* MuHash does not support checking if an element is already part of the
8285
* set. That is why this class does not enforce the use of a set as the
8386
* data it represents because there is no efficient way to do so.
8487
* It is possible to add elements more than once and also to remove
@@ -91,8 +94,6 @@ class Num3072
9194
class MuHash3072
9295
{
9396
private:
94-
static constexpr size_t BYTE_SIZE = 384;
95-
9697
Num3072 m_numerator;
9798
Num3072 m_denominator;
9899

src/node/coinstats.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <node/coinstats.h>
77

88
#include <coins.h>
9+
#include <crypto/muhash.h>
910
#include <hash.h>
1011
#include <serialize.h>
1112
#include <uint256.h>
@@ -27,21 +28,48 @@ static uint64_t GetBogoSize(const CScript& scriptPubKey)
2728
scriptPubKey.size() /* scriptPubKey */;
2829
}
2930

30-
static void ApplyStats(CCoinsStats& stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
31+
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)
32+
{
33+
if (it == outputs.begin()) {
34+
ss << hash;
35+
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
36+
}
37+
38+
ss << VARINT(it->first + 1);
39+
ss << it->second.out.scriptPubKey;
40+
ss << VARINT(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
41+
42+
if (it == std::prev(outputs.end())) {
43+
ss << VARINT(0u);
44+
}
45+
}
46+
47+
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) {}
48+
49+
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)
50+
{
51+
COutPoint outpoint = COutPoint(hash, it->first);
52+
Coin coin = it->second;
53+
54+
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
55+
ss << outpoint;
56+
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
57+
ss << coin.out;
58+
muhash.Insert(MakeUCharSpan(ss));
59+
}
60+
61+
template <typename T>
62+
static void ApplyStats(CCoinsStats& stats, T& hash_obj, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
3163
{
3264
assert(!outputs.empty());
33-
ss << hash;
34-
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
3565
stats.nTransactions++;
36-
for (const auto& output : outputs) {
37-
ss << VARINT(output.first + 1);
38-
ss << output.second.out.scriptPubKey;
39-
ss << VARINT(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
66+
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
67+
ApplyHash(stats, hash_obj, hash, outputs, it);
68+
4069
stats.nTransactionOutputs++;
41-
stats.nTotalAmount += output.second.out.nValue;
42-
stats.nBogoSize += GetBogoSize(output.second.out.scriptPubKey);
70+
stats.nTotalAmount += it->second.out.nValue;
71+
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
4372
}
44-
ss << VARINT(0u);
4573
}
4674

4775
static void ApplyStats(CCoinsStats& stats, std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
@@ -105,6 +133,10 @@ bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, CoinStatsHashType hash_t
105133
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
106134
return GetUTXOStats(view, stats, ss);
107135
}
136+
case(CoinStatsHashType::MUHASH): {
137+
MuHash3072 muhash;
138+
return GetUTXOStats(view, stats, muhash);
139+
}
108140
case(CoinStatsHashType::NONE): {
109141
return GetUTXOStats(view, stats, nullptr);
110142
}
@@ -117,10 +149,18 @@ static void PrepareHash(CHashWriter& ss, CCoinsStats& stats)
117149
{
118150
ss << stats.hashBlock;
119151
}
152+
// MuHash does not need the prepare step
153+
static void PrepareHash(MuHash3072& muhash, CCoinsStats& stats) {}
120154
static void PrepareHash(std::nullptr_t, CCoinsStats& stats) {}
121155

122156
static void FinalizeHash(CHashWriter& ss, CCoinsStats& stats)
123157
{
124158
stats.hashSerialized = ss.GetHash();
125159
}
160+
static void FinalizeHash(MuHash3072& muhash, CCoinsStats& stats)
161+
{
162+
uint256 out;
163+
muhash.Finalize(out);
164+
stats.hashSerialized = out;
165+
}
126166
static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}

src/node/coinstats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class CCoinsView;
1515

1616
enum class CoinStatsHashType {
1717
HASH_SERIALIZED,
18+
MUHASH,
1819
NONE,
1920
};
2021

src/rpc/blockchain.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,15 +1236,28 @@ static UniValue pruneblockchain(const JSONRPCRequest& request)
12361236
return uint64_t(block->nHeight);
12371237
}
12381238

1239+
CoinStatsHashType ParseHashType(const std::string& hash_type_input)
1240+
{
1241+
if (hash_type_input == "hash_serialized_2") {
1242+
return CoinStatsHashType::HASH_SERIALIZED;
1243+
} else if (hash_type_input == "muhash") {
1244+
return CoinStatsHashType::MUHASH;
1245+
} else if (hash_type_input == "none") {
1246+
return CoinStatsHashType::NONE;
1247+
} else {
1248+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s is not a valid hash_type", hash_type_input));
1249+
}
1250+
}
1251+
12391252
static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
12401253
{
1241-
if (request.fHelp || request.params.size() != 0)
1254+
if (request.fHelp || request.params.size() > 1)
12421255
throw std::runtime_error(
12431256
RPCHelpMan{"gettxoutsetinfo",
12441257
"\nReturns statistics about the unspent transaction output set.\n"
12451258
"Note this call may take some time.\n",
12461259
{
1247-
{"hash_type", RPCArg::Type::STR, /* default */ "hash_serialized_2", "Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'none'."},
1260+
{"hash_type", RPCArg::Type::STR, /* default */ "hash_serialized_2", "Which UTXO set hash should be calculated. Options: 'hash_serialized_2' (the legacy algorithm), 'muhash', 'none'."},
12481261
},
12491262
RPCResult{
12501263
RPCResult::Type::OBJ, "", "",
@@ -1255,6 +1268,7 @@ static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
12551268
{RPCResult::Type::NUM, "txouts", "The number of unspent transaction outputs"},
12561269
{RPCResult::Type::NUM, "bogosize", "A meaningless metric for UTXO set size"},
12571270
{RPCResult::Type::STR_HEX, "hash_serialized_2", "The serialized hash (only present if 'hash_serialized_2' hash_type is chosen)"},
1271+
{RPCResult::Type::STR_HEX, "muhash", "The serialized hash (only present if 'muhash' hash_type is chosen)"},
12581272
{RPCResult::Type::NUM, "disk_size", "The estimated size of the chainstate on disk"},
12591273
{RPCResult::Type::STR_AMOUNT, "total_amount", "The total amount"},
12601274
}},
@@ -1269,7 +1283,7 @@ static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
12691283
CCoinsStats stats;
12701284
::ChainstateActive().ForceFlushStateToDisk();
12711285

1272-
const CoinStatsHashType hash_type = ParseHashType(request.params[0], CoinStatsHashType::HASH_SERIALIZED);
1286+
const CoinStatsHashType hash_type{request.params[0].isNull() ? CoinStatsHashType::HASH_SERIALIZED : ParseHashType(request.params[0].get_str())};
12731287

12741288
CCoinsView* coins_view = WITH_LOCK(cs_main, return &ChainstateActive().CoinsDB());
12751289
if (GetUTXOStats(coins_view, stats, hash_type)) {
@@ -1281,6 +1295,9 @@ static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
12811295
if (hash_type == CoinStatsHashType::HASH_SERIALIZED) {
12821296
ret.pushKV("hash_serialized_2", stats.hashSerialized.GetHex());
12831297
}
1298+
if (hash_type == CoinStatsHashType::MUHASH) {
1299+
ret.pushKV("muhash", stats.hashSerialized.GetHex());
1300+
}
12841301
ret.pushKV("disk_size", stats.nDiskSize);
12851302
ret.pushKV("total_amount", ValueFromAmount(stats.nTotalAmount));
12861303
} else {

src/rpc/util.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,6 @@ bool ParseBoolV(const UniValue& v, const std::string &strName)
161161
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be true, false, yes, no, 1 or 0 (not '"+strBool+"')");
162162
}
163163

164-
CoinStatsHashType ParseHashType(const UniValue& param, const CoinStatsHashType default_type)
165-
{
166-
if (param.isNull()) {
167-
return default_type;
168-
} else {
169-
std::string hash_type_input = param.get_str();
170-
171-
if (hash_type_input == "hash_serialized_2") {
172-
return CoinStatsHashType::HASH_SERIALIZED;
173-
} else if (hash_type_input == "none") {
174-
return CoinStatsHashType::NONE;
175-
} else {
176-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%d is not a valid hash_type", hash_type_input));
177-
}
178-
}
179-
}
180-
181164
std::string HelpExampleCli(const std::string& methodname, const std::string& args)
182165
{
183166
return "> dash-cli " + methodname + " " + args + "\n";

src/rpc/util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ extern int64_t ParseInt64V(const UniValue& v, const std::string &strName);
6969
extern double ParseDoubleV(const UniValue& v, const std::string &strName);
7070
extern bool ParseBoolV(const UniValue& v, const std::string &strName);
7171

72-
CoinStatsHashType ParseHashType(const UniValue& param, const CoinStatsHashType default_type);
73-
7472
extern CAmount AmountFromValue(const UniValue& value);
7573
extern std::string HelpExampleCli(const std::string& methodname, const std::string& args);
7674
extern std::string HelpExampleRpc(const std::string& methodname, const std::string& args);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020-2021 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test UTXO set hash value calculation in gettxoutsetinfo."""
6+
7+
import struct
8+
9+
from test_framework.blocktools import create_transaction
10+
from test_framework.messages import (
11+
CBlock,
12+
COutPoint,
13+
FromHex,
14+
)
15+
from test_framework.muhash import MuHash3072
16+
from test_framework.test_framework import BitcoinTestFramework
17+
from test_framework.util import assert_equal
18+
19+
class UTXOSetHashTest(BitcoinTestFramework):
20+
def set_test_params(self):
21+
self.num_nodes = 1
22+
self.setup_clean_chain = True
23+
24+
def skip_test_if_missing_module(self):
25+
self.skip_if_no_wallet()
26+
27+
def test_deterministic_hash_results(self):
28+
self.log.info("Test deterministic UTXO set hash results")
29+
30+
# These depend on the setup_clean_chain option, the chain loaded from the cache
31+
assert_equal(self.nodes[0].gettxoutsetinfo()['hash_serialized_2'], "b61ee2cb582d2f4f94493f3d480e9a59d064706e98a12be0f335a3eeadd5678a")
32+
assert_equal(self.nodes[0].gettxoutsetinfo("muhash")['muhash'], "dd5ad2a105c2d29495f577245c357409002329b9f4d6182c0af3dc2f462555c8")
33+
34+
def test_muhash_implementation(self):
35+
self.log.info("Test MuHash implementation consistency")
36+
37+
node = self.nodes[0]
38+
39+
# Generate 100 blocks and remove the first since we plan to spend its
40+
# coinbase
41+
block_hashes = node.generate(100)
42+
blocks = list(map(lambda block: FromHex(CBlock(), node.getblock(block, False)), block_hashes))
43+
spending = blocks.pop(0)
44+
45+
# Create a spending transaction and mine a block which includes it
46+
tx = create_transaction(node, spending.vtx[0].rehash(), node.getnewaddress(), amount=49)
47+
txid = node.sendrawtransaction(hexstring=tx.serialize().hex(), maxfeerate=0)
48+
49+
tx_block = node.generateblock(node.getnewaddress(), [txid])['hash']
50+
blocks.append(FromHex(CBlock(), node.getblock(tx_block, False)))
51+
52+
# Serialize the outputs that should be in the UTXO set and add them to
53+
# a MuHash object
54+
muhash = MuHash3072()
55+
56+
for height, block in enumerate(blocks):
57+
# The Genesis block coinbase is not part of the UTXO set and we
58+
# spent the first mined block
59+
height += 2
60+
61+
for tx in block.vtx:
62+
for n, tx_out in enumerate(tx.vout):
63+
coinbase = 1 if not tx.vin[0].prevout.hash else 0
64+
65+
# Skip witness commitment
66+
if (coinbase and n > 0):
67+
continue
68+
69+
data = COutPoint(int(tx.rehash(), 16), n).serialize()
70+
data += struct.pack("<i", height * 2 + coinbase)
71+
data += tx_out.serialize()
72+
73+
muhash.insert(data)
74+
75+
finalized = muhash.digest()
76+
node_muhash = node.gettxoutsetinfo("muhash")['muhash']
77+
78+
assert_equal(finalized[::-1].hex(), node_muhash)
79+
80+
def run_test(self):
81+
self.test_deterministic_hash_results()
82+
self.test_muhash_implementation()
83+
84+
85+
if __name__ == '__main__':
86+
UTXOSetHashTest().main()

0 commit comments

Comments
 (0)