Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet: Add max_tx_weight to transaction funding options (take 2) #29523

Merged

Conversation

ismaelsadeeq
Copy link
Member

This PR taken over from #29264

The PR added an option max_tx_weight to transaction funding RPC's that ensures the resulting transaction weight does not exceed the specified max_tx_weight limit.

If max_tx_weight is not given MAX_STANDARD_TX_WEIGHT is used as the max threshold.

This PR addressed outstanding review comments in #29264

For more context and rationale behind this PR see https://delvingbitcoin.org/t/lightning-transactions-with-v3-and-ephemeral-anchors/418/11?u=instagibbs

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 1, 2024

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 furszy, rkrux, achow101
Concept ACK murchandamus
Stale ACK Eunovo

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

Conflicts

No conflicts as of last run.

@ismaelsadeeq
Copy link
Member Author

Some C.I jobs are failing, putting in draft while I try to address that.

Copy link
Contributor

@murchandamus murchandamus left a comment

Choose a reason for hiding this comment

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

Concept ACK

src/wallet/spend.cpp Outdated Show resolved Hide resolved
src/wallet/coinselection.cpp Outdated Show resolved Hide resolved
src/wallet/coinselection.h Outdated Show resolved Hide resolved
src/wallet/coinselection.h Outdated Show resolved Hide resolved
src/wallet/rpc/spend.cpp Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch 2 times, most recently from cddf4e9 to 52e4812 Compare March 21, 2024 16:46
src/wallet/spend.cpp Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from 52e4812 to 53ee67d Compare March 22, 2024 12:01
test/functional/rpc_psbt.py Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from 53ee67d to 3a64b3a Compare April 4, 2024 10:51
@ismaelsadeeq
Copy link
Member Author

Force pushed from 53ee67d to 3a64b3a compare diff

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Any chance to turn the first commit into an scripted-diff?

@Eunovo
Copy link

Eunovo commented Apr 4, 2024

Tested ACK 3a64b3a
Verified fuzz tests

@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from 3a64b3a to 3d76c37 Compare April 5, 2024 15:59
@ismaelsadeeq
Copy link
Member Author

ismaelsadeeq commented Apr 5, 2024

Any chance to turn the first commit into an scripted-diff?

The commit message was misleading It's not just renaming max_weight, it also updates the input parameter docstring to match the new name, some comment updates, and also the fuzz test renames has more info like low_max_selection_weight and high_max_selection_weight.

I've updated the commit message of the first commit 2706393

@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the
documentation.

Possibly this is due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.

Leave a comment here, if you need help tracking down a confusing failure.

Debug: https://github.com/bitcoin/bitcoin/runs/23495200918

@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from 3d76c37 to e7500ec Compare May 3, 2024 08:18
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from b24b7a9 to 845d16c Compare June 10, 2024 10:30
@ismaelsadeeq
Copy link
Member Author

Rebased to fix conflict after #28366 was merged.
b24b7a9a6..845d16c1b

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

few small comments

src/wallet/spend.cpp Outdated Show resolved Hide resolved
src/wallet/spend.cpp Outdated Show resolved Hide resolved
@@ -48,6 +48,7 @@

MAX_BLOCK_SIGOPS = 20000
MAX_BLOCK_SIGOPS_WEIGHT = MAX_BLOCK_SIGOPS * WITNESS_SCALE_FACTOR
MAX_STANDARD_TX_WEIGHT = 400000
Copy link
Member

Choose a reason for hiding this comment

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

nit: could replace this line with this constant.

Copy link
Member Author

Choose a reason for hiding this comment

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

leaving this as is, as I suspect there might be other places that needs to be replaced also.
this and others can come in a separate PR ?

@ismaelsadeeq
Copy link
Member Author

thanks for your review @furszy will force push to address this comments shortly.

@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch 2 times, most recently from 1ae619f to a120fc7 Compare June 21, 2024 16:22
Copy link

@rkrux rkrux left a comment

Choose a reason for hiding this comment

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

Concept ACK a120fc7, but I would look at 723a8da commit a bit more again; make, test/functional are successful though.

Following transaction funding RPCs are affected with the addition of max_tx_weight option:

  1. fundrawtransaction
  2. walletcreatefundedpsbt
  3. send

src/wallet/coinselection.cpp Show resolved Hide resolved
size_t change_output_size = 0;
int change_output_size = 0;
Copy link

Choose a reason for hiding this comment

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

This change ensures consistency in transaction size and weight calculation
within the wallet and prevents conversion overflow when calculating
max_selection_weight.

Are the inconsistencies because size_t is platform dependent and int is not (at least for the purposes of core because of this: https://github.com/bitcoin/bitcoin/blob/v26.0/src/compat/assumptions.h#L44)?
I still need to digest this comment: #29523 (comment)

Copy link
Member Author

@ismaelsadeeq ismaelsadeeq Jun 25, 2024

Choose a reason for hiding this comment

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

It's simply just to ensure consistency in transaction size and weight calculation within the wallet and prevents conversion overflow when calculating max_selection_weight as it happened here https://cirrus-ci.com/task/6341256662482944


I think it definitely will be worth it to define a datatype for size in the entire codebase.
But I am limiting this change to affect only the wallet size variables change_output_size, change_spend_size and tx_noinputs_size, because they relate to this PR.

test/functional/rpc_psbt.py Outdated Show resolved Hide resolved
test/functional/rpc_psbt.py Outdated Show resolved Hide resolved
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from a120fc7 to b3ac117 Compare June 25, 2024 10:13
achow101 added a commit that referenced this pull request Jun 26, 2024
…x weight

72b2268 wallet: notify when preset + automatic inputs exceed max weight (furszy)

Pull request description:

  Small change. Found it while finishing my review on #29523. This does not interfere with it.

  Basically, we are erroring out early when the automatic coin selection process exceeds the maximum weight, but we are not doing so when the user-preselected inputs combined with the wallet-selected inputs exceed the maximum weight.
  This change avoids signing all inputs before erroring out and introduces test coverage for `fundrawtransaction`.

ACKs for top commit:
  achow101:
    ACK 72b2268
  tdb3:
    re ACK for 72b2268
  rkrux:
    tACK [72b2268](72b2268)
  ismaelsadeeq:
    utACK 72b2268

Tree-SHA512: d77be19231023383a9c79a5d66b642dcbc6ebfc31a363e0b9f063c44898720a7859ec211cdbc0914ac7a3bfdf15e52fb8fc20d97f171431f70492c0f159dbc36
Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

Code reviewed b3ac117, getting closer. Left few topics.

src/wallet/spend.cpp Outdated Show resolved Hide resolved
src/wallet/spend.cpp Outdated Show resolved Hide resolved
src/wallet/spend.cpp Outdated Show resolved Hide resolved
test/functional/rpc_psbt.py Outdated Show resolved Hide resolved
test/functional/rpc_psbt.py Outdated Show resolved Hide resolved

return FinishTransaction(pwallet, options, rawTx);
auto result = FinishTransaction(pwallet, options, rawTx);
result.pushKV("weight", tx_size.weight);
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about using tx_size, we would be returning an estimation of the final size of tx. Not the real/current tx weight.

What about getting the current tx weight by serializing the CMutableTransaction object that appears inside FinishTransaction and explain in the RPC return value description that the returned weight will not be the final one if the transaction is not complete.

Copy link

Choose a reason for hiding this comment

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

Interesting, I agree with this point. Returning the exact size of the transaction would make the RPC definition more robust.

Copy link

Choose a reason for hiding this comment

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

This commit (along with this^ suggestion) b3ac117 can be a separate PR as well, doesn't seem necessary for this PR to be merged if I didn't miss anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's not needed.
I removed this commit
This can come as a separate PR, the size output can also be added to the other funding RPC's?

…ght` name

- This commit renames the coin selection algorithms input parameter `max_weight`
  to `max_selection_weight` for clarity.

  The parameter represent the maximum weight of the UTXOs the coin selection algorithm
  should select, not the transaction maximum weight.

- The commit updates the parameter docstring to provide correct description.

- Also updates coin selection unit and fuzzing test variables to match the new name.
`CoinGrinder` will also produce change output, listing all the
Coin selection algorithms that produces change output is not maintainable,
just infer that remaining algorithms all might produce change.
…_size` and `tx_noinputs_size` to `int`

- This change ensures consistency in transaction size and weight calculation
  within the wallet and prevents conversion overflow when calculating
  `max_selection_weight`.
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from b3ac117 to d8febf6 Compare June 27, 2024 14:20
This allows a transaction's weight to be bound under a certain
weight if possible and desired. This can be beneficial for future
RBF attempts, or whenever a more restricted spend topology is
desired.

Co-authored-by: Greg Sanders <gsanders87@gmail.com>
@ismaelsadeeq ismaelsadeeq force-pushed the 02-2024-add-max-weight-to-tx-funding branch from d8febf6 to 734076c Compare June 27, 2024 14:31
@ismaelsadeeq
Copy link
Member Author

Thanks for the reviews @furszy @rkrux

Force-pushed from b3ac117 to d8febf6
Compare diff b3ac1179ff..734076c6d


Changes:

  • Rebased after PR 30309 and updated the maximum transaction weight check to use the user passed value when given or default MAX_STANDARD_TX_WEIGHT.
  • Removed miscellaneous logs.
  • Added back the removed line in the rpc_psbt.py functional test according PEP guidelines.
  • Removed the last commit that returned the estimated size in the send RPC based on the review here.

Copy link
Member

@furszy furszy left a comment

Choose a reason for hiding this comment

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

utACK 734076c


coin_selection_params.m_max_tx_weight = coin_control.m_max_tx_weight.value_or(MAX_STANDARD_TX_WEIGHT);
int minimum_tx_weight = MIN_STANDARD_TX_NONWITNESS_SIZE * WITNESS_SCALE_FACTOR;
if (coin_selection_params.m_max_tx_weight.value() < minimum_tx_weight || coin_selection_params.m_max_tx_weight.value() > MAX_STANDARD_TX_WEIGHT) {
Copy link
Member

Choose a reason for hiding this comment

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

In 734076c:

nano nit: shorter

Suggested change
if (coin_selection_params.m_max_tx_weight.value() < minimum_tx_weight || coin_selection_params.m_max_tx_weight.value() > MAX_STANDARD_TX_WEIGHT) {
if (*coin_selection_params.m_max_tx_weight < minimum_tx_weight || *coin_selection_params.m_max_tx_weight > MAX_STANDARD_TX_WEIGHT) {

@DrahtBot DrahtBot requested review from Eunovo and rkrux June 27, 2024 16:22
Copy link

@rkrux rkrux left a comment

Choose a reason for hiding this comment

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

reACK 734076c

Ran make and functional tests, all successful.

@ismaelsadeeq This diff comparison b3ac117..734076c contains a lot of unrelated changes though.

@ismaelsadeeq
Copy link
Member Author

@ismaelsadeeq This diff comparison b3ac117..734076c contains a lot of unrelated changes though.

Yes that's because of the recent rebase.

@achow101
Copy link
Member

ACK 734076c

@achow101 achow101 merged commit efbf4e7 into bitcoin:master Jul 17, 2024
16 checks passed
@ismaelsadeeq ismaelsadeeq deleted the 02-2024-add-max-weight-to-tx-funding branch July 17, 2024 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants