Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#30226: test: add validation for gettxout RPC re…
Browse files Browse the repository at this point in the history
…sponse

723440c test framework, wallet: rename get_scriptPubKey method to get_output_script (Alfonso Roman Zubeldia)
fa0232a test: add validation for gettxout RPC response (Alfonso Roman Zubeldia)

Pull request description:

  Added a new test in `test/functional/rpc_blockchain.py` to validate the gettxout RPC response. This new test ensures all response elements are verified, including `bestblock`, `confirmations`, `value`, `coinbase`, and `scriptPubKey` details.

  Also renamed the method `get_scriptPubKey` from `test/functional/test_framework/wallet.py` to the modern name `get_output_script` as suggested by maflcko (bitcoin/bitcoin#30226 (comment))

ACKs for top commit:
  fjahr:
    reACK 723440c
  maflcko:
    lgtm ACK 723440c
  brunoerg:
    code review ACK 723440c

Tree-SHA512: 3384578909d2e7548cef302c5b8a9fed5b82dfc942892503ad4a05e73f5cceafad1eab3af9a27e54aef3db7631f8935298d6b882c70d2f02a9a75b8e3c209b6c
  • Loading branch information
fanquake committed Feb 5, 2025
2 parents 1334ca6 + 723440c commit b9c2418
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion test/functional/feature_coinstatsindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _test_coin_stats_index(self):
# Generate and send a normal tx with two outputs
tx1 = self.wallet.send_to(
from_node=node,
scriptPubKey=self.wallet.get_scriptPubKey(),
scriptPubKey=self.wallet.get_output_script(),
amount=21 * COIN,
)

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_framework_miniwallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_wallet_tagging(self):
tag = ''.join(random.choice(string.ascii_letters) for _ in range(20))
self.log.debug(f"-> ({i}) tag name: {tag}")
tagged_wallet = MiniWallet(node, tag_name=tag)
untagged_wallet.send_to(from_node=node, scriptPubKey=tagged_wallet.get_scriptPubKey(), amount=100000)
untagged_wallet.send_to(from_node=node, scriptPubKey=tagged_wallet.get_output_script(), amount=100000)
tagged_wallet.rescan_utxos()
tagged_wallet.send_self_transfer(from_node=node)
self.generate(node, 1) # clear mempool
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_rbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def make_utxo(self, node, amount, *, confirmed=True, scriptPubKey=None):
confirmed - txout created will be confirmed in the blockchain;
unconfirmed otherwise.
"""
tx = self.wallet.send_to(from_node=node, scriptPubKey=scriptPubKey or self.wallet.get_scriptPubKey(), amount=amount)
tx = self.wallet.send_to(from_node=node, scriptPubKey=scriptPubKey or self.wallet.get_output_script(), amount=amount)

if confirmed:
mempool_size = len(node.getrawmempool())
Expand Down
29 changes: 29 additions & 0 deletions test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- getdeploymentinfo
- getchaintxstats
- gettxoutsetinfo
- gettxout
- getblockheader
- getdifficulty
- getnetworkhashps
Expand Down Expand Up @@ -90,6 +91,7 @@ def run_test(self):
self._test_getblockchaininfo()
self._test_getchaintxstats()
self._test_gettxoutsetinfo()
self._test_gettxout()
self._test_getblockheader()
self._test_getdifficulty()
self._test_getnetworkhashps()
Expand Down Expand Up @@ -400,6 +402,33 @@ def _test_gettxoutsetinfo(self):
# Unknown hash_type raises an error
assert_raises_rpc_error(-8, "'foo hash' is not a valid hash_type", node.gettxoutsetinfo, "foo hash")

def _test_gettxout(self):
self.log.info("Validating gettxout RPC response")
node = self.nodes[0]

# Get the best block hash and the block, which
# should only include the coinbase transaction.
best_block_hash = node.getbestblockhash()
block = node.getblock(best_block_hash)
assert_equal(block['nTx'], 1)

# Get the transaction ID of the coinbase tx and
# the transaction output.
txid = block['tx'][0]
txout = node.gettxout(txid, 0)

# Validate the gettxout response
assert_equal(txout['bestblock'], best_block_hash)
assert_equal(txout['confirmations'], 1)
assert_equal(txout['value'], 25)
assert_equal(txout['scriptPubKey']['address'], self.wallet.get_address())
assert_equal(txout['scriptPubKey']['hex'], self.wallet.get_output_script().hex())
decoded_script = node.decodescript(self.wallet.get_output_script().hex())
assert_equal(txout['scriptPubKey']['asm'], decoded_script['asm'])
assert_equal(txout['scriptPubKey']['desc'], decoded_script['desc'])
assert_equal(txout['scriptPubKey']['type'], decoded_script['type'])
assert_equal(txout['coinbase'], True)

def _test_getblockheader(self):
self.log.info("Test getblockheader")
node = self.nodes[0]
Expand Down
2 changes: 1 addition & 1 deletion test/functional/test_framework/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def generate(self, num_blocks, **kwargs):
self.rescan_utxos()
return blocks

def get_scriptPubKey(self):
def get_output_script(self):
return self._scriptPubKey

def get_descriptor(self):
Expand Down

0 comments on commit b9c2418

Please sign in to comment.