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.
This commit is contained in:
Martin Zumsande 2025-04-16 17:36:06 -04:00
parent abcec01ed5
commit 31ddbe34dd
4 changed files with 16 additions and 10 deletions

View file

@ -38,6 +38,7 @@ from test_framework.util import (
assert_equal,
assert_not_equal,
assert_raises_rpc_error,
convert_to_json_for_cli,
ensure_for,
sha256sum_file,
try_rpc,
@ -481,7 +482,7 @@ class AssumeutxoTest(BitcoinTestFramework):
# Use a hash instead of a height
prev_snap_hash = n0.getblockhash(prev_snap_height)
dump_output5 = n0.dumptxoutset('utxos5.dat', rollback=prev_snap_hash)
dump_output5 = n0.dumptxoutset('utxos5.dat', rollback=convert_to_json_for_cli(self.options.usecli, prev_snap_hash))
assert_equal(sha256sum_file(dump_output4['path']), sha256sum_file(dump_output5['path']))
# TODO: This is a hack to set m_best_header to the correct value after

View file

@ -30,6 +30,7 @@ from test_framework.util import (
assert_not_equal,
assert_equal,
assert_raises_rpc_error,
convert_to_json_for_cli,
)
from test_framework.wallet import (
MiniWallet,
@ -41,7 +42,6 @@ class CoinStatsIndexTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 2
self.supports_cli = False
self.extra_args = [
[],
["-coinstatsindex"]
@ -104,7 +104,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
assert_equal(res0, res2)
# Fetch old stats by hash
res3 = index_node.gettxoutsetinfo(hash_option, res0['bestblock'])
res3 = index_node.gettxoutsetinfo(hash_option, convert_to_json_for_cli(self.options.usecli, res0['bestblock']))
del res3['block_info'], res3['total_unspendable_amount']
res3.pop('muhash', None)
assert_equal(res0, res3)
@ -244,7 +244,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
assert_equal(res12, res10)
self.log.info("Test obtaining info for a non-existent block hash")
assert_raises_rpc_error(-5, "Block not found", index_node.gettxoutsetinfo, hash_type="none", hash_or_height="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", use_index=True)
assert_raises_rpc_error(-5, "Block not found", index_node.gettxoutsetinfo, hash_type="none", hash_or_height=convert_to_json_for_cli(self.options.usecli, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), use_index=True)
def _test_use_index_option(self):
self.log.info("Test use_index option for nodes running the index")
@ -279,7 +279,7 @@ class CoinStatsIndexTest(BitcoinTestFramework):
assert_not_equal(res["muhash"], res_invalid["muhash"])
# Test that requesting reorged out block by hash is still returning correct results
res_invalid2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=reorg_block)
res_invalid2 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=convert_to_json_for_cli(self.options.usecli, reorg_block))
assert_equal(res_invalid2["muhash"], res_invalid["muhash"])
assert_not_equal(res["muhash"], res_invalid2["muhash"])

View file

@ -12,6 +12,7 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
convert_to_json_for_cli,
)
import json
import os
@ -35,7 +36,6 @@ class GetblockstatsTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.supports_cli = False
def get_stats(self):
return [self.nodes[0].getblockstats(hash_or_height=self.start_height + i) for i in range(self.max_stat_pos+1)]
@ -119,7 +119,7 @@ class GetblockstatsTest(BitcoinTestFramework):
# Check selecting block by hash too
blockhash = self.expected_stats[i]['blockhash']
stats_by_hash = self.nodes[0].getblockstats(hash_or_height=blockhash)
stats_by_hash = self.nodes[0].getblockstats(hash_or_height=convert_to_json_for_cli(self.options.usecli, blockhash))
assert_equal(stats_by_hash, self.expected_stats[i])
# Make sure each stat can be queried on its own
@ -161,10 +161,10 @@ class GetblockstatsTest(BitcoinTestFramework):
self.nodes[0].getblockstats, hash_or_height=1, stats=['minfee', f'aaa{inv_sel_stat}'])
# Mainchain's genesis block shouldn't be found on regtest
assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats,
hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
hash_or_height=convert_to_json_for_cli(self.options.usecli,'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'))
# Invalid number of args
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, '00', 1, 2)
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, convert_to_json_for_cli(self.options.usecli,'00'), 1, 2)
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats)
self.log.info('Test block height 0')
@ -185,7 +185,7 @@ class GetblockstatsTest(BitcoinTestFramework):
self.log.info("Test when only header is known")
block = self.generateblock(self.nodes[0], output="raw(55)", transactions=[], submit=False)
self.nodes[0].submitheader(block["hex"])
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: self.nodes[0].getblockstats(block['hash']))
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: self.nodes[0].getblockstats(convert_to_json_for_cli(self.options.usecli,block['hash'])))
self.log.info('Test when block is missing')
(self.nodes[0].blocks_path / 'blk00000.dat').rename(self.nodes[0].blocks_path / 'blk00000.dat.backup')

View file

@ -610,3 +610,8 @@ def sync_txindex(test_framework, node):
sync_start = int(time.time())
test_framework.wait_until(lambda: node.getindexinfo("txindex")["txindex"]["synced"])
test_framework.log.debug(f"Synced in {time.time() - sync_start} seconds")
def convert_to_json_for_cli(cli, text):
if cli:
return json.dumps(text)
return text