Skip to content

Commit 6ccc8fc

Browse files
committed
test: Add test for gettxoutsetinfo RPC with MuHash
1 parent 0d3b2f6 commit 6ccc8fc

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_muhash_implementation(self):
28+
self.log.info("Test MuHash implementation consistency")
29+
30+
node = self.nodes[0]
31+
32+
# Generate 100 blocks and remove the first since we plan to spend its
33+
# coinbase
34+
block_hashes = node.generate(100)
35+
blocks = list(map(lambda block: FromHex(CBlock(), node.getblock(block, False)), block_hashes))
36+
spending = blocks.pop(0)
37+
38+
# Create a spending transaction and mine a block which includes it
39+
tx = create_transaction(node, spending.vtx[0].rehash(), node.getnewaddress(), amount=49)
40+
txid = node.sendrawtransaction(hexstring=tx.serialize_with_witness().hex(), maxfeerate=0)
41+
42+
tx_block = node.generateblock(output=node.getnewaddress(), transactions=[txid])
43+
blocks.append(FromHex(CBlock(), node.getblock(tx_block['hash'], False)))
44+
45+
# Serialize the outputs that should be in the UTXO set and add them to
46+
# a MuHash object
47+
muhash = MuHash3072()
48+
49+
for height, block in enumerate(blocks):
50+
# The Genesis block coinbase is not part of the UTXO set and we
51+
# spent the first mined block
52+
height += 2
53+
54+
for tx in block.vtx:
55+
for n, tx_out in enumerate(tx.vout):
56+
coinbase = 1 if not tx.vin[0].prevout.hash else 0
57+
58+
# Skip witness commitment
59+
if (coinbase and n > 0):
60+
continue
61+
62+
data = COutPoint(int(tx.rehash(), 16), n).serialize()
63+
data += struct.pack("<i", height * 2 + coinbase)
64+
data += tx_out.serialize()
65+
66+
muhash.insert(data)
67+
68+
finalized = muhash.digest()
69+
node_muhash = node.gettxoutsetinfo("muhash")['muhash']
70+
71+
assert_equal(finalized[::-1].hex(), node_muhash)
72+
73+
def run_test(self):
74+
self.test_muhash_implementation()
75+
76+
77+
if __name__ == '__main__':
78+
UTXOSetHashTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
'feature_notifications.py',
203203
'rpc_getblockfilter.py',
204204
'rpc_invalidateblock.py',
205+
'feature_utxo_set_hash.py',
205206
'feature_rbf.py',
206207
'mempool_packages.py',
207208
'mempool_package_onemore.py',

0 commit comments

Comments
 (0)