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: drop change to fee if amount is less than dust #32

Merged

Conversation

Ayush170-Future
Copy link
Contributor

@Ayush170-Future Ayush170-Future commented Feb 16, 2024

This commit refactors the CoinSelector's select() method to properly handle the calculation of change cost. The change amount is now checked against the cost of change, and if the change is less than the cost, the cost is added to the transaction fee. Additionally, it adds a unit test for the CoinSelector.

Fixes: Issue #26

Copy link
Contributor

@emjshrx emjshrx left a comment

Choose a reason for hiding this comment

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

Can we add a test

@Ayush170-Future
Copy link
Contributor Author

Can we add a test

Yes. I'll fix the lint and add a test in the next force push.

@Ayush170-Future
Copy link
Contributor Author

Force pushed:

  • Hopefully fixes Lint
  • Adds a unit test for CoinSelector

Copy link
Collaborator

@theanmolsharma theanmolsharma left a comment

Choose a reason for hiding this comment

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

will continue reviewing after you make these changes

src/wallet/coin-selector.ts Outdated Show resolved Hide resolved
src/wallet/coin-selector.ts Outdated Show resolved Hide resolved
src/wallet/coin-selector.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Force pushed 3c1983e addressing requested changes.

test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Force pushed a9cf64d refactoring the unit test and creating random script for the test transaction output.

src/wallet/coin-selector.ts Outdated Show resolved Hide resolved
src/wallet/coin-selector.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Force pushed 0e8a816, which changed the random data into constant values.

  • I agree with @emjshrx that using random values in unit testing could lead to non-deterministic behavior. Therefore, I ran my code and recorded all the values it produced. I then hard-coded this static data to ensure deterministic results every time.

  • I also covered both cases: when the change > dust and when the change <= dust.

  • Fixed the nits

Copy link
Collaborator

@theanmolsharma theanmolsharma left a comment

Choose a reason for hiding this comment

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

I think this should be the final set of changes required before merging. Nice work and thanks for your contribution.

test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Force pushed ed44b85

  • Used it.each()
  • Added comments for justifying my calculations
  • Replaced less than dust values in the coins with bigger values (more than 1000)
  • Rebased on the main

Copy link
Contributor

@emjshrx emjshrx left a comment

Choose a reason for hiding this comment

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

ack ed44b85. Lets also wait for @theanmolsharma's approval

Copy link
Collaborator

@theanmolsharma theanmolsharma left a comment

Choose a reason for hiding this comment

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

I think we can merge after the comments are addressed.

test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
test/coinselector.spec.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Force pushed 4589fff addressing reviews and rebasing to the main.

Copy link
Collaborator

@theanmolsharma theanmolsharma left a comment

Choose a reason for hiding this comment

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

One last nit and then I'll merge. (Ik I've been saying this for the past 3 reviews, but I promise to merge after this)
Awesome work @Ayush170-Future

packages/wallet/test/coinselector.spec.ts Outdated Show resolved Hide resolved
@Ayush170-Future
Copy link
Contributor Author

Done!!

Addressed the nit

Copy link
Collaborator

@theanmolsharma theanmolsharma left a comment

Choose a reason for hiding this comment

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

ACK 991c16d

@theanmolsharma
Copy link
Collaborator

theanmolsharma commented Feb 29, 2024

@Ayush170-Future you need to sign the commits so that we can merge. I've enabled branch protection rules that only allow signed commits to be merged in main. It's a good FOSS practice to sign commits.
If you don't have a GPG setup now, I can sign the commits for this PR, but all commits in the future must be signed.

Some helpful links:
https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

@Ayush170-Future
Copy link
Contributor Author

@Ayush170-Future you need to sign the commits so that we can merge. I've enabled branch protection rules that only allow signed commits to be merged in main. It's a good FOSS practice to sign commits. If you don't have a GPG setup now, I can sign the commits for this PR, but all commits in the future must be signed.

Yes, please sign the commits in this PR, and thanks for letting me know, I'll set it up and sign my commits from now on.

This commit refactors the CoinSelector class to properly handle the calculation
of change cost. The change amount is now checked against the cost of change,
and if the change is less than the cost, the cost is added to the transaction fee.
This ensures that the transaction output is correctly adjusted based on the
current fee rate and the size of the change output.
@theanmolsharma theanmolsharma merged commit 2d88828 into Bitshala-Incubator:main Feb 29, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants