Skip to content

Commit

Permalink
test: Add test for gettxoutsetinfo RPC with MuHash
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Jan 30, 2021
1 parent 0d3b2f6 commit 6ccc8fc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/functional/feature_utxo_set_hash.py
@@ -0,0 +1,78 @@
#!/usr/bin/env python3
# Copyright (c) 2020-2021 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test UTXO set hash value calculation in gettxoutsetinfo."""

import struct

from test_framework.blocktools import create_transaction
from test_framework.messages import (
CBlock,
COutPoint,
FromHex,
)
from test_framework.muhash import MuHash3072
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

class UTXOSetHashTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True

def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def test_muhash_implementation(self):
self.log.info("Test MuHash implementation consistency")

node = self.nodes[0]

# Generate 100 blocks and remove the first since we plan to spend its
# coinbase
block_hashes = node.generate(100)
blocks = list(map(lambda block: FromHex(CBlock(), node.getblock(block, False)), block_hashes))
spending = blocks.pop(0)

# Create a spending transaction and mine a block which includes it
tx = create_transaction(node, spending.vtx[0].rehash(), node.getnewaddress(), amount=49)
txid = node.sendrawtransaction(hexstring=tx.serialize_with_witness().hex(), maxfeerate=0)

tx_block = node.generateblock(output=node.getnewaddress(), transactions=[txid])
blocks.append(FromHex(CBlock(), node.getblock(tx_block['hash'], False)))

# Serialize the outputs that should be in the UTXO set and add them to
# a MuHash object
muhash = MuHash3072()

for height, block in enumerate(blocks):
# The Genesis block coinbase is not part of the UTXO set and we
# spent the first mined block
height += 2

for tx in block.vtx:
for n, tx_out in enumerate(tx.vout):
coinbase = 1 if not tx.vin[0].prevout.hash else 0

# Skip witness commitment
if (coinbase and n > 0):
continue

data = COutPoint(int(tx.rehash(), 16), n).serialize()
data += struct.pack("<i", height * 2 + coinbase)
data += tx_out.serialize()

muhash.insert(data)

finalized = muhash.digest()
node_muhash = node.gettxoutsetinfo("muhash")['muhash']

assert_equal(finalized[::-1].hex(), node_muhash)

def run_test(self):
self.test_muhash_implementation()


if __name__ == '__main__':
UTXOSetHashTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Expand Up @@ -202,6 +202,7 @@
'feature_notifications.py',
'rpc_getblockfilter.py',
'rpc_invalidateblock.py',
'feature_utxo_set_hash.py',
'feature_rbf.py',
'mempool_packages.py',
'mempool_package_onemore.py',
Expand Down

0 comments on commit 6ccc8fc

Please sign in to comment.