|
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test utxo-to-sqlite conversion tool"""
|
| 6 | +from itertools import product |
6 | 7 | import os.path
|
7 | 8 | try:
|
8 | 9 | import sqlite3
|
|
15 | 16 | from test_framework.messages import (
|
16 | 17 | COutPoint,
|
17 | 18 | CTxOut,
|
| 19 | + uint256_from_str, |
18 | 20 | )
|
19 | 21 | from test_framework.crypto.muhash import MuHash3072
|
20 | 22 | from test_framework.script import (
|
|
38 | 40 | from test_framework.wallet import MiniWallet
|
39 | 41 |
|
40 | 42 |
|
41 |
| -def calculate_muhash_from_sqlite_utxos(filename): |
| 43 | +def calculate_muhash_from_sqlite_utxos(filename, txid_format, spk_format): |
42 | 44 | muhash = MuHash3072()
|
43 | 45 | con = sqlite3.connect(filename)
|
44 | 46 | cur = con.cursor()
|
45 |
| - for (txid_hex, vout, value, coinbase, height, spk_hex) in cur.execute("SELECT * FROM utxos"): |
| 47 | + for (txid, vout, value, coinbase, height, spk) in cur.execute("SELECT * FROM utxos"): |
| 48 | + match txid_format: |
| 49 | + case "hex": |
| 50 | + assert type(txid) is str |
| 51 | + txid_bytes = bytes.fromhex(txid)[::-1] |
| 52 | + case "raw": |
| 53 | + assert type(txid) is bytes |
| 54 | + txid_bytes = txid |
| 55 | + case "rawle": |
| 56 | + assert type(txid) is bytes |
| 57 | + txid_bytes = txid[::-1] |
| 58 | + match spk_format: |
| 59 | + case "hex": |
| 60 | + assert type(spk) is str |
| 61 | + spk_bytes = bytes.fromhex(spk) |
| 62 | + case "raw": |
| 63 | + assert type(spk) is bytes |
| 64 | + spk_bytes = spk |
| 65 | + |
46 | 66 | # serialize UTXO for MuHash (see function `TxOutSer` in the coinstats module)
|
47 |
| - utxo_ser = COutPoint(int(txid_hex, 16), vout).serialize() |
| 67 | + utxo_ser = COutPoint(uint256_from_str(txid_bytes), vout).serialize() |
48 | 68 | utxo_ser += (height * 2 + coinbase).to_bytes(4, 'little')
|
49 |
| - utxo_ser += CTxOut(value, bytes.fromhex(spk_hex)).serialize() |
| 69 | + utxo_ser += CTxOut(value, spk_bytes).serialize() |
50 | 70 | muhash.insert(utxo_ser)
|
51 | 71 | con.close()
|
52 | 72 | return muhash.digest()[::-1].hex()
|
@@ -100,17 +120,20 @@ def run_test(self):
|
100 | 120 | input_filename = os.path.join(self.options.tmpdir, "utxos.dat")
|
101 | 121 | node.dumptxoutset(input_filename, "latest")
|
102 | 122 |
|
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) |
| 123 | + for i, (txid_format, spk_format) in enumerate(product(["hex", "raw", "rawle"], ["hex", "raw"])): |
| 124 | + self.log.info(f'Test utxo-to-sqlite script using txid format "{txid_format}" and spk format "{spk_format}" ({i+1})') |
| 125 | + self.log.info('-> Convert UTXO set from compact-serialized format to sqlite format') |
| 126 | + output_filename = os.path.join(self.options.tmpdir, f"utxos_{i+1}.sqlite") |
| 127 | + base_dir = self.config["environment"]["SRCDIR"] |
| 128 | + utxo_to_sqlite_path = os.path.join(base_dir, "contrib", "utxo-tools", "utxo_to_sqlite.py") |
| 129 | + arguments = [input_filename, output_filename, f'--txid={txid_format}', f'--spk={spk_format}'] |
| 130 | + subprocess.run([sys.executable, utxo_to_sqlite_path] + arguments, check=True, stderr=subprocess.STDOUT) |
| 131 | + |
| 132 | + self.log.info('-> Verify that both UTXO sets match by comparing their MuHash') |
| 133 | + muhash_sqlite = calculate_muhash_from_sqlite_utxos(output_filename, txid_format, spk_format) |
| 134 | + muhash_compact_serialized = node.gettxoutsetinfo('muhash')['muhash'] |
| 135 | + assert_equal(muhash_sqlite, muhash_compact_serialized) |
| 136 | + self.log.info('') |
114 | 137 |
|
115 | 138 |
|
116 | 139 | if __name__ == "__main__":
|
|
0 commit comments