Skip to content

Commit 1f15b6e

Browse files
committed
test: add test for utxo-to-sqlite conversion using named pipe
1 parent 082333d commit 1f15b6e

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

test/functional/tool_utxo_to_sqlite.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +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-
import os.path
6+
import os
77
try:
88
import sqlite3
99
except ImportError:
@@ -38,6 +38,20 @@
3838
from test_framework.wallet import MiniWallet
3939

4040

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+
4155
class UtxoToSqliteTest(BitcoinTestFramework):
4256
def set_test_params(self):
4357
self.num_nodes = 1
@@ -94,21 +108,22 @@ def run_test(self):
94108
check=True, stderr=subprocess.STDOUT)
95109

96110
self.log.info('Verify that both UTXO sets match by comparing their MuHash')
97-
muhash = MuHash3072()
98-
con = sqlite3.connect(output_filename)
99-
cur = con.cursor()
100-
for (txid_hex, vout, value, coinbase, height, spk_hex) in cur.execute("SELECT * FROM utxos"):
101-
# serialize UTXO for MuHash (see function `TxOutSer` in the coinstats module)
102-
utxo_ser = COutPoint(int(txid_hex, 16), vout).serialize()
103-
utxo_ser += (height * 2 + coinbase).to_bytes(4, 'little')
104-
utxo_ser += CTxOut(value, bytes.fromhex(spk_hex)).serialize()
105-
muhash.insert(utxo_ser)
106-
con.close()
107-
108-
muhash_sqlite = muhash.digest()[::-1].hex()
111+
muhash_sqlite = calculate_muhash_from_sqlite_utxos(output_filename)
109112
muhash_compact_serialized = node.gettxoutsetinfo('muhash')['muhash']
110113
assert_equal(muhash_sqlite, muhash_compact_serialized)
111114

115+
self.log.info('Convert UTXO set directly (without intermediate dump) via named pipe')
116+
fifo_filename = os.path.join(self.options.tmpdir, "utxos.fifo")
117+
os.mkfifo(fifo_filename)
118+
output_direct_filename = os.path.join(self.options.tmpdir, "utxos_direct.sqlite")
119+
p = subprocess.Popen([sys.executable, utxo_to_sqlite_path, fifo_filename, output_direct_filename],
120+
stderr=subprocess.STDOUT)
121+
node.dumptxoutset(fifo_filename, "latest")
122+
p.wait()
123+
muhash_direct_sqlite = calculate_muhash_from_sqlite_utxos(output_direct_filename)
124+
assert_equal(muhash_sqlite, muhash_direct_sqlite)
125+
os.remove(fifo_filename)
126+
112127

113128
if __name__ == "__main__":
114129
UtxoToSqliteTest(__file__).main()

0 commit comments

Comments
 (0)