Skip to content

Commit 6530d00

Browse files
committed
test: add function to convert to json for height_or_hash params
This is necessary for bitcoin-cli because a string without quotes is not a valid json.
1 parent 54d2872 commit 6530d00

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

test/functional/feature_assumeutxo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def check_dump_output(output):
481481

482482
# Use a hash instead of a height
483483
prev_snap_hash = n0.getblockhash(prev_snap_height)
484-
dump_output5 = n0.dumptxoutset('utxos5.dat', rollback=prev_snap_hash)
484+
dump_output5 = n0.dumptxoutset('utxos5.dat', rollback=self.convert_to_json_for_cli(prev_snap_hash))
485485
assert_equal(sha256sum_file(dump_output4['path']), sha256sum_file(dump_output5['path']))
486486

487487
# Ensure n0 is back at the tip

test/functional/feature_coinstatsindex.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class CoinStatsIndexTest(BitcoinTestFramework):
4141
def set_test_params(self):
4242
self.setup_clean_chain = True
4343
self.num_nodes = 2
44-
self.supports_cli = False
4544
self.extra_args = [
4645
[],
4746
["-coinstatsindex"]
@@ -104,7 +103,7 @@ def _test_coin_stats_index(self):
104103
assert_equal(res0, res2)
105104

106105
# Fetch old stats by hash
107-
res3 = index_node.gettxoutsetinfo(hash_option, res0['bestblock'])
106+
res3 = index_node.gettxoutsetinfo(hash_option, self.convert_to_json_for_cli(res0['bestblock']))
108107
del res3['block_info'], res3['total_unspendable_amount']
109108
res3.pop('muhash', None)
110109
assert_equal(res0, res3)
@@ -243,7 +242,7 @@ def _test_coin_stats_index(self):
243242
assert_equal(res12, res10)
244243

245244
self.log.info("Test obtaining info for a non-existent block hash")
246-
assert_raises_rpc_error(-5, "Block not found", index_node.gettxoutsetinfo, hash_type="none", hash_or_height="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", use_index=True)
245+
assert_raises_rpc_error(-5, "Block not found", index_node.gettxoutsetinfo, hash_type="none", hash_or_height=self.convert_to_json_for_cli("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), use_index=True)
247246

248247
def _test_use_index_option(self):
249248
self.log.info("Test use_index option for nodes running the index")
@@ -278,7 +277,7 @@ def _test_reorg_index(self):
278277
assert_not_equal(res["muhash"], res_invalid["muhash"])
279278

280279
# Test that requesting reorged out block by hash is still returning correct results
281-
res_invalid2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=reorg_block)
280+
res_invalid2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=self.convert_to_json_for_cli(reorg_block))
282281
assert_equal(res_invalid2["muhash"], res_invalid["muhash"])
283282
assert_not_equal(res["muhash"], res_invalid2["muhash"])
284283

test/functional/feature_index_prune.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def run_test(self):
6767
for node in filter_nodes:
6868
assert_greater_than(len(node.getblockfilter(tip)['filter']), 0)
6969
for node in stats_nodes:
70-
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=tip)['muhash']
70+
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=self.convert_to_json_for_cli(tip))['muhash']
7171

7272
self.generate(self.nodes[0], 500)
7373
self.sync_index(height=700)
@@ -85,14 +85,14 @@ def run_test(self):
8585
for node in filter_nodes:
8686
assert_greater_than(len(node.getblockfilter(tip)['filter']), 0)
8787
for node in stats_nodes:
88-
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=tip)['muhash']
88+
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=self.convert_to_json_for_cli(tip))['muhash']
8989

9090
self.log.info("check if we can access the blockfilter and coinstats of a pruned block")
9191
height_hash = self.nodes[0].getblockhash(2)
9292
for node in filter_nodes:
9393
assert_greater_than(len(node.getblockfilter(height_hash)['filter']), 0)
9494
for node in stats_nodes:
95-
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=height_hash)['muhash']
95+
assert node.gettxoutsetinfo(hash_type="muhash", hash_or_height=self.convert_to_json_for_cli(height_hash))['muhash']
9696

9797
# mine and sync index up to a height that will later be the pruneheight
9898
self.generate(self.nodes[0], 51)
@@ -106,7 +106,7 @@ def run_test(self):
106106
assert_raises_rpc_error(-1, msg, node.getblockfilter, height_hash)
107107
for node in stats_nodes:
108108
msg = "Querying specific block heights requires coinstatsindex"
109-
assert_raises_rpc_error(-8, msg, node.gettxoutsetinfo, "muhash", height_hash)
109+
assert_raises_rpc_error(-8, msg, node.gettxoutsetinfo, "muhash", self.convert_to_json_for_cli(height_hash))
110110

111111
self.generate(self.nodes[0], 749)
112112

test/functional/rpc_getblockstats.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def add_options(self, parser):
3636
def set_test_params(self):
3737
self.num_nodes = 1
3838
self.setup_clean_chain = True
39-
self.supports_cli = False
4039

4140
def get_stats(self):
4241
return [self.nodes[0].getblockstats(hash_or_height=self.start_height + i) for i in range(self.max_stat_pos+1)]
@@ -120,7 +119,7 @@ def run_test(self):
120119

121120
# Check selecting block by hash too
122121
blockhash = self.expected_stats[i]['blockhash']
123-
stats_by_hash = self.nodes[0].getblockstats(hash_or_height=blockhash)
122+
stats_by_hash = self.nodes[0].getblockstats(hash_or_height=self.convert_to_json_for_cli(blockhash))
124123
assert_equal(stats_by_hash, self.expected_stats[i])
125124

126125
# Make sure each stat can be queried on its own
@@ -162,10 +161,10 @@ def run_test(self):
162161
self.nodes[0].getblockstats, hash_or_height=1, stats=['minfee', f'aaa{inv_sel_stat}'])
163162
# Mainchain's genesis block shouldn't be found on regtest
164163
assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats,
165-
hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
164+
hash_or_height=self.convert_to_json_for_cli('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'))
166165

167166
# Invalid number of args
168-
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, '00', 1, 2)
167+
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, self.convert_to_json_for_cli('00'), 1, 2)
169168
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats)
170169

171170
self.log.info('Test block height 0')
@@ -186,7 +185,7 @@ def run_test(self):
186185
self.log.info("Test when only header is known")
187186
block = self.generateblock(self.nodes[0], output="raw(55)", transactions=[], submit=False)
188187
self.nodes[0].submitheader(block["hex"])
189-
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: self.nodes[0].getblockstats(block['hash']))
188+
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: self.nodes[0].getblockstats(self.convert_to_json_for_cli(block['hash'])))
190189

191190
self.log.info('Test when block is missing')
192191
(self.nodes[0].blocks_path / 'blk00000.dat').rename(self.nodes[0].blocks_path / 'blk00000.dat.backup')

test/functional/test_framework/test_framework.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from enum import Enum
99
import argparse
1010
from datetime import datetime, timezone
11+
import json
1112
import logging
1213
import os
1314
import platform
@@ -1072,3 +1073,8 @@ def is_usdt_compiled(self):
10721073

10731074
def has_blockfile(self, node, filenum: str):
10741075
return (node.blocks_path/ f"blk{filenum}.dat").is_file()
1076+
1077+
def convert_to_json_for_cli(self, text):
1078+
if self.options.usecli:
1079+
return json.dumps(text)
1080+
return text

0 commit comments

Comments
 (0)