|
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 |
| -import os.path |
| 6 | +import os |
7 | 7 | try:
|
8 | 8 | import sqlite3
|
9 | 9 | except ImportError:
|
|
38 | 38 | from test_framework.wallet import MiniWallet
|
39 | 39 |
|
40 | 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 | + |
41 | 55 | class UtxoToSqliteTest(BitcoinTestFramework):
|
42 | 56 | def set_test_params(self):
|
43 | 57 | self.num_nodes = 1
|
@@ -94,21 +108,22 @@ def run_test(self):
|
94 | 108 | check=True, stderr=subprocess.STDOUT)
|
95 | 109 |
|
96 | 110 | 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) |
109 | 112 | muhash_compact_serialized = node.gettxoutsetinfo('muhash')['muhash']
|
110 | 113 | assert_equal(muhash_sqlite, muhash_compact_serialized)
|
111 | 114 |
|
| 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 | + |
112 | 127 |
|
113 | 128 | if __name__ == "__main__":
|
114 | 129 | UtxoToSqliteTest(__file__).main()
|
0 commit comments