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

TransactionFactory - Fix fee percentage calculation for self spend #11507

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

soosr
Copy link
Collaborator

@soosr soosr commented Sep 18, 2023

During #11120 I found that the calculation is wrong in the case of self spend.
I added a test for it.

Issue: #11120 (review)
Explanation: #11120 (comment)

ping for review @zkSNACKs/code-team

Comment on lines +210 to +213
// In this scenario since the amount changes as the fee changes, we need to compare against the total sum / 2,
// as with this, we will make sure the fee cannot be higher than the amount.
decimal inputSumDecimal = spentCoins.Sum(x => x.Amount.ToDecimal(MoneyUnit.BTC));
feePercentage = 100 * (feeDecimal / (inputSumDecimal / 2));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand the change. Why divided by 2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently, on master, we don't allow the build of a transaction where the fee is higher than the amount to be sent.
To have the same functionality in the case of ChangeStrategy.AllRemainingCustom we have to compare against the input sum / 2 because that is the maximum fee that we allow.

Examples:

Input: 1 BTC
Fee: 0.2 BTC
Output: 0.8 BTC
feePercentage: 100 * (0.2 / (1 BTC / 2) = 40%

Input: 1 BTC
Fee: 0.5 BTC
Output: 0.5 BTC
feePercentage: 100 * (0.5 / (1 BTC / 2) = 100% // It pays the maximum that is allowed

Input: 1 BTC
Fee: 0.6 BTC
Output: 0.4 BTC
feePercentage: 100 * (0.6 / (1 BTC / 2) = 120% // Overpays as the fee is higher than the amount.

Note: In the case of self spend the total outgoing amount is 0, so the fee PC calculation allowed a transaction like this when the ChangeStrategy was AllRemainingCustom
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

It makes sense to me.

Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

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

ACK

@yahiheb
Copy link
Collaborator

yahiheb commented Sep 19, 2023

@soosr @molnard I am not sure how to test this.
On master I cannot reproduce the issue #11120 (review) because I encountered it only on #11120, commit be1645c.

@soosr
Copy link
Collaborator Author

soosr commented Sep 19, 2023

@soosr @molnard I am not sure how to test this. On master I cannot reproduce the issue #11120 (review) because I encountered it only on #11120, commit be1645c.

It is not really possible to test it on master as be1645c commit was an alternative way on how to send whole coins. But with that commit, I would have touched other files too to make it work properly in every case, so I just dropped the idea.

Currently, Full Privacy and Better Privacy suggestions could make a transaction with ChangeStrategy.AllRemainingCustom (Depending on the conditions), and ChangeAvoidance suggestion will always make ChangeStrategy.AllRemainingCustom transactions.
So in the current usage of master, the issue can only appear there, but it is definitely not easy to setup a test case with them.

So just rely on the test I added.

Copy link
Collaborator

@molnard molnard left a comment

Choose a reason for hiding this comment

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

tACK - played with the test with different values - OK.

@molnard molnard merged commit 0889452 into WalletWasabi:master Sep 22, 2023
5 of 7 checks passed
@molnard
Copy link
Collaborator

molnard commented Sep 22, 2023

Well done. 🏅

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

4 participants