Skip to content

Commit 7378f27

Browse files
committed
test: run utxo-to-sqlite script test with spk/txid format option combinations
1 parent b30fca7 commit 7378f27

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

test/functional/tool_utxo_to_sqlite.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test utxo-to-sqlite conversion tool"""
6+
from itertools import product
67
import os.path
78
try:
89
import sqlite3
@@ -15,6 +16,7 @@
1516
from test_framework.messages import (
1617
COutPoint,
1718
CTxOut,
19+
uint256_from_str,
1820
)
1921
from test_framework.crypto.muhash import MuHash3072
2022
from test_framework.script import (
@@ -38,15 +40,33 @@
3840
from test_framework.wallet import MiniWallet
3941

4042

41-
def calculate_muhash_from_sqlite_utxos(filename):
43+
def calculate_muhash_from_sqlite_utxos(filename, txid_format, spk_format):
4244
muhash = MuHash3072()
4345
con = sqlite3.connect(filename)
4446
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+
4666
# 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()
4868
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()
5070
muhash.insert(utxo_ser)
5171
con.close()
5272
return muhash.digest()[::-1].hex()
@@ -100,17 +120,20 @@ def run_test(self):
100120
input_filename = os.path.join(self.options.tmpdir, "utxos.dat")
101121
node.dumptxoutset(input_filename, "latest")
102122

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('')
114137

115138

116139
if __name__ == "__main__":

0 commit comments

Comments
 (0)