|
| 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() |
0 commit comments