Skip to content

Conversation

@theStack
Copy link
Contributor

@theStack theStack commented Aug 13, 2023

In wallet-related functional tests we often want to send funds to an address and use the resulting (non-change) UTXO directly after as input for another transaction. Doing that is currently tedious, as it involves finding the index part of the outpoint manually by calling helpers like find_vout_for_address or find_output first. This results in two different txid/vout variables which then again have to be combined to a single dictionary {"txid": ..., "vout": ...} in order to be specified as input for RPCs like createrawtransaction or createpsbt. For example:

txid1 = node1.sendtoaddress(addr1, value1)
vout1 = find_vout_for_address(node1, txid1, addr1)
txid2 = node2.sendtoaddress(addr2, value2)
vout2 = find_vout_for_address(node2, txid2, addr2)
node.createrawtransaction([{'txid': txid1, 'vout': vout1}, {'txid': txid2, 'vout': vout2}], .....)

This PR introduces a helper create_outpoints to immediately return the outpoint as
UTXO dictionary in the common format, making the tests more readable and avoiding unnecessary duplication:

utxo1 = self.create_outpoints(node1, outputs=[{addr1: value1}])[0]
utxo2 = self.create_outpoints(node2, outputs=[{addr2: value2}])[0]
node.createrawtransaction([utxo1, utxo2], .....)

Tests are switched to work with UTXO-objects rather than two individual txid/vout variables accordingly.

The find_output helper is removed, as it seems generally a bad idea to search for an outpoint only based on the output value. If that's really ever needed in the future, it makes probably more sense to add it as an additional parameter to find_vout_of_address. Note that find_output supported specifying a block-hash for where to look for the transaction (being passed on to the getrawtransaction RPC). This seems to be unneeded, as txids are always unique and for the only test that used that parameter (rpc_psbt.py) there was no observed difference in run-time, so it was not reintroduced in the new helper.

There are still some find_vout_of_address calls remaining, used for detecting change outputs or for whenever the sending happens via sendrawtransaction instead, so this PR tackles not all, but the most common case.

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 13, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK BrandonOdiwuor, maflcko, achow101

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #28710 (Remove the legacy wallet and BDB dependency by achow101)
  • #28528 (test: Use test framework utils in functional tests by osagie98)
  • #24142 (Deprecate SubtractFeeFromOutputs by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@theStack theStack force-pushed the test-add_sendtoaddress_helper_returning_utxo branch from 1c21e51 to 63eacf6 Compare August 14, 2023 12:21
@theStack
Copy link
Contributor Author

Force-pushed with the feedback #28264 (comment) tackled and adapted the PR description accordingly.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, two nits only

@theStack theStack force-pushed the test-add_sendtoaddress_helper_returning_utxo branch from 63eacf6 to f1d685b Compare August 14, 2023 18:17
@theStack
Copy link
Contributor Author

Rebased on master (tiny merge conflict due to #28232) and tackled the comments #28264 (comment) and #28264 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review ACK 7154542

The proposed refactor is a commendable improvement, streamlining test cases by leveraging the create_outpoints(...) function to consolidate calls to nodes.sendtoaddress() and find_vout_for_address() into a single invocation.

Suggestion: It might be beneficial to add documentation clarifying that the outputs argument should be an array of dictionaries following the format {address: amount_to_send}. This would enhance usability for future users, eliminating the need to inspect example invocations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: It might be beneficial to add documentation clarifying that the outputs argument should be an array of dictionaries following the format {address: amount_to_send}. This would enhance usability for future users, eliminating the need to inspect example invocations.

Good idea. Made the following (simple) change, I hope that's clear enough for users:

diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py
index 045fd2f5cb..70b3943478 100755
--- a/test/functional/test_framework/test_framework.py
+++ b/test/functional/test_framework/test_framework.py
@@ -699,7 +699,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
         return blocks

     def create_outpoints(self, node, *, outputs):
-        """Send funds to a given list of address/amount targets using the bitcoind
+        """Send funds to a given list of `{address: amount}` targets using the bitcoind
         wallet and return the corresponding outpoints as a list of dictionaries
         `[{"txid": txid, "vout": vout1}, {"txid": txid, "vout": vout2}, ...]`.
         The result can be used to specify inputs for RPCs like `createrawtransaction`,

@maflcko
Copy link
Member

maflcko commented Oct 20, 2023

Are you still working on this?

@theStack
Copy link
Contributor Author

Are you still working on this?

I considered the PR ready since my latest force-push weeks ago (#28264 (comment)). However, will tackle the suggestion in #28264 (comment) though, I must have missed that last paragraph back then.

@theStack theStack force-pushed the test-add_sendtoaddress_helper_returning_utxo branch from f1d685b to 4bec09e Compare October 20, 2023 16:15
@theStack
Copy link
Contributor Author

Rebased on master and tackled #28264 (comment).

This commit introduces a helper `create_outpoints` to execute the
`send` RPC and immediately return the target address outpoints as UTXO
dictionary in the common format, making the tests more readable and
avoiding unnecessary duplication.
@theStack theStack force-pushed the test-add_sendtoaddress_helper_returning_utxo branch from 4bec09e to 50d1ac1 Compare October 24, 2023 09:14
@theStack
Copy link
Contributor Author

Rebased on master again (due to conflict after #28609 has been merged).

Copy link
Contributor

@BrandonOdiwuor BrandonOdiwuor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 50d1ac1

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 50d1ac1 🖨

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: ACK 50d1ac120716ab17f32b28513c0ac9940001a783 🖨
f/xrAIdGqM2CyeNbY9XT0oGt51+NcrDKS00Nej69S1xkQelpkubslZL0jaHX+RUdivnyh2xEo3bJJ/00I7AsDQ==

@achow101
Copy link
Member

ACK 50d1ac1

@achow101 achow101 merged commit 2a349f9 into bitcoin:master Oct 25, 2023
@theStack theStack deleted the test-add_sendtoaddress_helper_returning_utxo branch October 25, 2023 15:14
luke-jr pushed a commit to bitcoinknots/bitcoin that referenced this pull request May 6, 2024
This commit introduces a helper `create_outpoints` to execute the
`send` RPC and immediately return the target address outpoints as UTXO
dictionary in the common format.

Github-Pull: bitcoin#28264
Partial-Rebased-From: 73a339a
@bitcoin bitcoin locked and limited conversation to collaborators Oct 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants