Skip to content

Conversation

Sjors
Copy link
Member

@Sjors Sjors commented Oct 25, 2019

Builds on top of #17219

Avoid knapsack when there's no change, by enabling BnB when subtracting fees from outputs. Gotchas:

  1. when coins are pre-selected, it still uses knapsack
  2. it still excludes dust inputs based on their value minus the fees needed to spend them, but it does not use their effective value in BnB. I don't think this matters in practice, because subtractFeesFromOutputs is generally used to spend either all coins or a selected bunch. But there is an edge case where e.g. it can choose between two 0.01 BTC inputs, one SegWit and one legacy, and it won't care.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 25, 2019

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #17331 (Use effective values throughout coin selection by achow101)
  • #17290 (Enable BnB coin selection for preset inputs and subtract fee from outputs by achow101)
  • #17237 (wallet: LearnRelatedScripts only if KeepDestination by promag)

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.

@achow101
Copy link
Member

I don't think this is really the right approach to allowing subtractFeeFromOutputs. Instead of passing along subtractFeeFromOutputs to BnB, I think it makes more sense to just change the effective_values of each input to be nValue and change the non_input_fees to 0, BnB doesn't need to know whether subtractFeeFromOutputs is being used.

I can work on this as an alternative if you'd like.

@Sjors
Copy link
Member Author

Sjors commented Oct 25, 2019

Happy to see an alternative approach. Ideally though, I think BnB should know about subtractFeeFromOutputs. It should try to find a set of coins as close to the target as possible, minizing the fees subtracted from the output. With regular wallets this doesn't matter much, but if you have to choose between spending a P2WPK coin or some 15-of-15 multisig it does.

@achow101
Copy link
Member

Ideally though, I think BnB should know about subtractFeeFromOutputs. It should try to find a set of coins as close to the target as possible, minizing the fees subtracted from the output. With regular wallets this doesn't matter much, but if you have to choose between spending a P2WPK coin or some 15-of-15 multisig it does.

BnB should already be minimizing transaction fees as part of reducing "waste".

@Sjors
Copy link
Member Author

Sjors commented Oct 25, 2019

If the effective value of inputs is equal to their nominal value then, afaik, waste reduction only tries to find the smallest possible set of coins. It won't optimize for which coins are cheapest to spend based on (witness) size.

@achow101
Copy link
Member

I looked more closely at your changes, and it seems like you've actually done what I suggested, just in a more verbose way. It could be done with fewer changes.

@Sjors
Copy link
Member Author

Sjors commented Oct 26, 2019

That's probably because I went all over the place while trying to get this to work, and chased down a few other rabbit holes (like trying to make BnB find a solution with subtractFeeFromOutputs that actually takes input fees into account). Happy to shorten it a bit.

@Sjors Sjors force-pushed the 2019/10/less-knapsack branch from 6c73d97 to d29efe6 Compare October 29, 2019 15:40
@Sjors
Copy link
Member Author

Sjors commented Nov 4, 2019

Closing in favor of #17290, which is simpler and achieves more:

  • it enables BnB with pre-selected coins, which this PR doesn't
  • it has a test that checks that BnB was used (the same effect as the test here that disables knapsack)
  • it still excludes dust inputs (based on input size * fee)

@Sjors Sjors closed this Nov 4, 2019
meshcollider added a commit that referenced this pull request Nov 22, 2019
…t fee from outputs

b007efd Allow BnB when subtract fee from outputs (Andrew Chow)
db15e71 Use BnB when preset inputs are selected (Andrew Chow)

Pull request description:

  Currently we explicitly disable BnB when there are preset inputs selected or when the subtract fee from outputs option is enabled. This PR enables BnB for both cases.

  Kind of an alternative to #17246 (implements the subtract fee from outputs part of it) and borrows a test from there too.

ACKs for top commit:
  instagibbs:
    reACK b007efd
  Sjors:
    re-ACK b007efd

Tree-SHA512: 933276b09b2fa2ab43db7f0b98762f06f6f5fa8606195f96aca9fa1cb71ae4ee7156028dd482b1cada82ddd0996a9daf12ea5c152589fdf192cd96cbc51e99df
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Nov 22, 2019
…subtract fee from outputs

b007efd Allow BnB when subtract fee from outputs (Andrew Chow)
db15e71 Use BnB when preset inputs are selected (Andrew Chow)

Pull request description:

  Currently we explicitly disable BnB when there are preset inputs selected or when the subtract fee from outputs option is enabled. This PR enables BnB for both cases.

  Kind of an alternative to bitcoin#17246 (implements the subtract fee from outputs part of it) and borrows a test from there too.

ACKs for top commit:
  instagibbs:
    reACK bitcoin@b007efd
  Sjors:
    re-ACK b007efd

Tree-SHA512: 933276b09b2fa2ab43db7f0b98762f06f6f5fa8606195f96aca9fa1cb71ae4ee7156028dd482b1cada82ddd0996a9daf12ea5c152589fdf192cd96cbc51e99df
sidhujag pushed a commit to syscoin-core/syscoin that referenced this pull request Nov 10, 2020
…subtract fee from outputs

b007efd Allow BnB when subtract fee from outputs (Andrew Chow)
db15e71 Use BnB when preset inputs are selected (Andrew Chow)

Pull request description:

  Currently we explicitly disable BnB when there are preset inputs selected or when the subtract fee from outputs option is enabled. This PR enables BnB for both cases.

  Kind of an alternative to bitcoin#17246 (implements the subtract fee from outputs part of it) and borrows a test from there too.

ACKs for top commit:
  instagibbs:
    reACK bitcoin@b007efd
  Sjors:
    re-ACK b007efd

Tree-SHA512: 933276b09b2fa2ab43db7f0b98762f06f6f5fa8606195f96aca9fa1cb71ae4ee7156028dd482b1cada82ddd0996a9daf12ea5c152589fdf192cd96cbc51e99df
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants