|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2024-present 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-to-sqlite conversion tool""" |
| 6 | +import os.path |
| 7 | +try: |
| 8 | + import sqlite3 |
| 9 | +except ImportError: |
| 10 | + pass |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | + |
| 14 | +from test_framework.key import ECKey |
| 15 | +from test_framework.messages import ( |
| 16 | + COutPoint, |
| 17 | + CTxOut, |
| 18 | +) |
| 19 | +from test_framework.crypto.muhash import MuHash3072 |
| 20 | +from test_framework.script import ( |
| 21 | + CScript, |
| 22 | + CScriptOp, |
| 23 | +) |
| 24 | +from test_framework.script_util import ( |
| 25 | + PAY_TO_ANCHOR, |
| 26 | + key_to_p2pk_script, |
| 27 | + key_to_p2pkh_script, |
| 28 | + key_to_p2wpkh_script, |
| 29 | + keys_to_multisig_script, |
| 30 | + output_key_to_p2tr_script, |
| 31 | + script_to_p2sh_script, |
| 32 | + script_to_p2wsh_script, |
| 33 | +) |
| 34 | +from test_framework.test_framework import BitcoinTestFramework |
| 35 | +from test_framework.util import ( |
| 36 | + assert_equal, |
| 37 | +) |
| 38 | +from test_framework.wallet import MiniWallet |
| 39 | + |
| 40 | + |
| 41 | +def calculate_muhash_from_sqlite_utxos(filename): |
| 42 | + muhash = MuHash3072() |
| 43 | + con = sqlite3.connect(filename) |
| 44 | + cur = con.cursor() |
| 45 | + for (txid_hex, vout, value, coinbase, height, spk_hex) in cur.execute("SELECT * FROM utxos"): |
| 46 | + # serialize UTXO for MuHash (see function `TxOutSer` in the coinstats module) |
| 47 | + utxo_ser = COutPoint(int(txid_hex, 16), vout).serialize() |
| 48 | + utxo_ser += (height * 2 + coinbase).to_bytes(4, 'little') |
| 49 | + utxo_ser += CTxOut(value, bytes.fromhex(spk_hex)).serialize() |
| 50 | + muhash.insert(utxo_ser) |
| 51 | + con.close() |
| 52 | + return muhash.digest()[::-1].hex() |
| 53 | + |
| 54 | + |
| 55 | +class UtxoToSqliteTest(BitcoinTestFramework): |
| 56 | + def set_test_params(self): |
| 57 | + self.num_nodes = 1 |
| 58 | + # we want to create some UTXOs with non-standard output scripts |
| 59 | + self.extra_args = [['-acceptnonstdtxn=1']] |
| 60 | + |
| 61 | + def skip_test_if_missing_module(self): |
| 62 | + self.skip_if_no_py_sqlite3() |
| 63 | + |
| 64 | + def run_test(self): |
| 65 | + node = self.nodes[0] |
| 66 | + wallet = MiniWallet(node) |
| 67 | + key = ECKey() |
| 68 | + |
| 69 | + self.log.info('Create UTXOs with various output script types') |
| 70 | + for i in range(1, 10+1): |
| 71 | + key.generate(compressed=False) |
| 72 | + uncompressed_pubkey = key.get_pubkey().get_bytes() |
| 73 | + key.generate(compressed=True) |
| 74 | + pubkey = key.get_pubkey().get_bytes() |
| 75 | + |
| 76 | + # add output scripts for compressed script type 0 (P2PKH), type 1 (P2SH), |
| 77 | + # types 2-3 (P2PK compressed), types 4-5 (P2PK uncompressed) and |
| 78 | + # for uncompressed scripts (bare multisig, segwit, etc.) |
| 79 | + output_scripts = ( |
| 80 | + key_to_p2pkh_script(pubkey), |
| 81 | + script_to_p2sh_script(key_to_p2pkh_script(pubkey)), |
| 82 | + key_to_p2pk_script(pubkey), |
| 83 | + key_to_p2pk_script(uncompressed_pubkey), |
| 84 | + |
| 85 | + keys_to_multisig_script([pubkey]*i), |
| 86 | + keys_to_multisig_script([uncompressed_pubkey]*i), |
| 87 | + key_to_p2wpkh_script(pubkey), |
| 88 | + script_to_p2wsh_script(key_to_p2pkh_script(pubkey)), |
| 89 | + output_key_to_p2tr_script(pubkey[1:]), |
| 90 | + PAY_TO_ANCHOR, |
| 91 | + CScript([CScriptOp.encode_op_n(i)]*(1000*i)), # large script (up to 10000 bytes) |
| 92 | + ) |
| 93 | + |
| 94 | + # create outputs and mine them in a block |
| 95 | + for output_script in output_scripts: |
| 96 | + wallet.send_to(from_node=node, scriptPubKey=output_script, amount=i, fee=20000) |
| 97 | + self.generate(wallet, 1) |
| 98 | + |
| 99 | + self.log.info('Dump UTXO set via `dumptxoutset` RPC') |
| 100 | + input_filename = os.path.join(self.options.tmpdir, "utxos.dat") |
| 101 | + node.dumptxoutset(input_filename, "latest") |
| 102 | + |
| 103 | + self.log.info('Convert UTXO set from compact-serialized format to sqlite format') |
| 104 | + output_filename = os.path.join(self.options.tmpdir, "utxos.sqlite") |
| 105 | + base_dir = self.config["environment"]["SRCDIR"] |
| 106 | + utxo_to_sqlite_path = os.path.join(base_dir, "contrib", "utxo-tools", "utxo_to_sqlite.py") |
| 107 | + subprocess.run([sys.executable, utxo_to_sqlite_path, input_filename, output_filename], |
| 108 | + check=True, stderr=subprocess.STDOUT) |
| 109 | + |
| 110 | + self.log.info('Verify that both UTXO sets match by comparing their MuHash') |
| 111 | + muhash_sqlite = calculate_muhash_from_sqlite_utxos(output_filename) |
| 112 | + muhash_compact_serialized = node.gettxoutsetinfo('muhash')['muhash'] |
| 113 | + assert_equal(muhash_sqlite, muhash_compact_serialized) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + UtxoToSqliteTest(__file__).main() |
0 commit comments