Skip to content

Conversation

@morcos
Copy link
Contributor

@morcos morcos commented Dec 21, 2016

Resolves #9466

I feel bad making the wallet coin selection even more spaghetti like, but I think this is a huge improvement over the existing code and it's relatively simple to understand.

Essentially once you pick coins and if you have change, then try to reduce that change to meet the fee instead of repicking coins. Since we aim to have change of .01 BTC if we can't make an exact match initially, this is usually more than sufficient to be reduced only slightly to pay for the fee.

I think for 0.15, we could take on a wholesale rewrite of the algorithm, but thats too much to ask for before 0.14. This however might be worthwhile.

I'm open to suggestions on how to make this cleaner or clearer.

EDIT: suggest viewing diff with ?w=1

EDIT 2: This is much simpler now. Also includes another commit to remove further cases of overpaying fees.

@maflcko
Copy link
Member

maflcko commented Dec 30, 2016

Concept ACK

(Tries to solve issue #4082 et al.)

@gmaxwell
Copy link
Contributor

gmaxwell commented Jan 4, 2017

Concept ACK but will be simpler when rebased on #9465.

@morcos
Copy link
Contributor Author

morcos commented Jan 5, 2017

This has been rebased on #9465 (thanks @gmaxwell)

It is now much simpler.
I added a second commit to almost completely resolve #9466

@morcos morcos changed the title Make a second pass with same coins in CreateTransaction. Smarter coordination of change and fee in CreateTransaction. Jan 5, 2017
@laanwj laanwj added this to the 0.14.0 milestone Jan 5, 2017
@gmaxwell
Copy link
Contributor

gmaxwell commented Jan 5, 2017

Beautiful. utACK. Should be rebased on the latest nit-update of 9465 (sorry about that), I expect it will do so cleanly.

We should open an issue to also resolve these behaviors when nSubtractFeeFromAmount is used; I can understand why you didn't cover those in this PR; though they should only be of comparable complexity to this change.

@sipa
Copy link
Member

sipa commented Jan 5, 2017

Needs rebase on #9465 merge.

Copy link
Member

@sdaftuar sdaftuar left a comment

Choose a reason for hiding this comment

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

Looks good, though I guess it needs to be rebased.

Copy link
Member

Choose a reason for hiding this comment

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

I guess there's an edge case left here, where if there's no change output and the fee difference is very large, then we won't reduce the fee?

It might be nice to refactor the change-adding logic so that we could add change in that scenario, though I don't want to hold up this PR which is already a strict improvement, do you think it's worth adding a comment that points this out though for future reference?

Copy link
Contributor

Choose a reason for hiding this comment

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

it's pretty easy to compute exactly the amount of additional fee adding a change output would imply. So the test becomes if not subtractfrom and excessfee >= CENT/2 + changeaddfee; then set the change to excessfee-changeaddfee. Doing so without creating a hairball with change added in multiple places... less fun.

Similarly, the remove side could consider eliminating the change output, if doing so would not overpay by too much. (A conservative value would be the changeaddfee but a larger value would be a lot more useful) both both probably better done in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes my concern is the gap between desired change amounts (CENT/2) and fee overpayments is quite big. Most of the time even if a fee overpayment was bigger than dust, it wouldn't be so big that you'd actually want to create an output with it.

I think fixing that should be combined with more robust handling of the minimum output size a wallet would create. Perhaps then there would be cases where we feel we got unlucky with coin selection and we try again to have less overpayment or more reasonable size change.

The coin selection algorithm is stupid now. Trying to find an exact match makes no sense in a world where every transaction pays fee. So I'd rather not continue adding patches and improvements before we try to improve the basic strategy. I'm happy to give that a shot after 0.14. And I think we can avoid most of these problems from the beginning...

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.

utACK 53171a915e17e83814328cc3792554159a950757, though I'd prefer to have the edge cases mentioned by @sdaftuar and me documented as todos.

Copy link
Member

@maflcko maflcko Jan 6, 2017

Choose a reason for hiding this comment

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

I might be missing something, but instead of "Do not", the comment should read "TODO". Otherwise, it would imply that it is ok for the payee to pay excessive fee whereas for the payer it is not.

Copy link
Member

Choose a reason for hiding this comment

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

can you explain why CENT is used instead of MIN_CHANGE, please?

Copy link
Member

Choose a reason for hiding this comment

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

nit: No need to mention code specific values in the comment

morcos added 2 commits January 6, 2017 10:12
Once we've picked coins and dummy-signed the transaction to calculate fee, if we don't have sufficient fee, then try to meet the fee by reducing change before resorting to picking new coins.
…ler transaction.

On repeated calls to SelectCoins we try to meet the fee necessary for the last transaction, the new fee required might be smaller, so increase our change by the difference if we can.
@morcos
Copy link
Contributor Author

morcos commented Jan 6, 2017

Rebased after #9465 and addressed comments.

@sipa
Copy link
Member

sipa commented Jan 6, 2017

utACK 20449ef

@maflcko
Copy link
Member

maflcko commented Jan 6, 2017

utACK 20449ef. Thanks for addressing the feedback so quick!

@sdaftuar
Copy link
Member

sdaftuar commented Jan 6, 2017

utACK 20449ef

@gmaxwell
Copy link
Contributor

gmaxwell commented Jan 6, 2017

re-ACK.

@murchandamus
Copy link
Contributor

utACK 20449ef

@TheBlueMatt
Copy link
Contributor

utACK 20449ef

@sipa sipa merged commit 20449ef into bitcoin:master Jan 9, 2017
sipa added a commit that referenced this pull request Jan 9, 2017
…ion.

20449ef Don't overpay fee if we have selected new coins that result in a smaller transaction. (Alex Morcos)
42f5ce4 Try to reduce change output to make needed fee in CreateTransaction (Alex Morcos)
@RHavar
Copy link
Contributor

RHavar commented Feb 3, 2017

What release will this be in? I've been losing a lot of money, to what I suspect is this bug, just $50 last transaction:
https://live.blockcypher.com/btc/tx/a92a609f1ebdcd5a2b658a35ed562ac8661a90daf773a7a807a71bd8e1990a6a/

$ bitcoin-cli estimatefee 4
0.00101654

$ bitcoin-cli listunspent | grep amount | wc -l
692

codablock pushed a commit to codablock/dash that referenced this pull request Jan 18, 2018
…ransaction.

20449ef Don't overpay fee if we have selected new coins that result in a smaller transaction. (Alex Morcos)
42f5ce4 Try to reduce change output to make needed fee in CreateTransaction (Alex Morcos)
andvgal pushed a commit to energicryptocurrency/gen2-energi that referenced this pull request Jan 6, 2019
…ransaction.

20449ef Don't overpay fee if we have selected new coins that result in a smaller transaction. (Alex Morcos)
42f5ce4 Try to reduce change output to make needed fee in CreateTransaction (Alex Morcos)
CryptoCentric pushed a commit to absolute-community/absolute that referenced this pull request Feb 26, 2019
…ransaction.

20449ef Don't overpay fee if we have selected new coins that result in a smaller transaction. (Alex Morcos)
42f5ce4 Try to reduce change output to make needed fee in CreateTransaction (Alex Morcos)
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 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.

Prevent fee overpayment when nFeeNeeded decreases in CreateTransaction iterations

10 participants