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

fix: Pool Coin Decimal Truncation During Deposit #446

Merged

Conversation

dongsam
Copy link
Contributor

@dongsam dongsam commented Sep 29, 2021

Description

closes: #445

For now, it is a draft version that reproduces the above issue and solves it at the PoC level, requiring more testing and edge case simulation.

Tasks

  • Fix ExecuteDeposit function
  • Add test code to verify that the issue has been resolved well
  • Update MintingPoolCoinsInvariant and DepositInvariant
  • Simulation to find edge cases
  • Remove print codes temporary written for debugging

Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

@dongsam dongsam changed the title fix: wip poc for reproduce and fix poolcoin truncation fix: Pool Coin Decimal Truncation During Deposit Sep 29, 2021
@codecov
Copy link

codecov bot commented Oct 7, 2021

Codecov Report

Merging #446 (f7df497) into develop (e118e21) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #446      +/-   ##
===========================================
- Coverage    85.10%   85.09%   -0.01%     
===========================================
  Files           23       23              
  Lines         2618     2604      -14     
===========================================
- Hits          2228     2216      -12     
+ Misses         226      225       -1     
+ Partials       164      163       -1     
Impacted Files Coverage Δ
x/liquidity/keeper/invariants.go 66.31% <100.00%> (+1.79%) ⬆️
x/liquidity/keeper/liquidity_pool.go 83.16% <100.00%> (-0.50%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e118e21...f7df497. Read the comment docs.

@dongsam dongsam force-pushed the 445-fix-poolcoin-truncation-issue branch from 1d0d2e2 to e7913a1 Compare October 7, 2021 09:56
* commit '9e955b0b7dba4c4e9a09e6d2415ce4181b62e90b':
  feat: bump cosmos sdk to v0.44.1
  feat: add markdown link checker github workflow
@dongsam dongsam force-pushed the 445-fix-poolcoin-truncation-issue branch from e7913a1 to fb4cb94 Compare October 7, 2021 10:00
Comment on lines +255 to +256
sdk.NewCoin(depositCoins[0].Denom, lastReserveCoinA.Amount.ToDec().Mul(mintRate).TruncateInt()),
sdk.NewCoin(depositCoins[1].Denom, lastReserveCoinB.Amount.ToDec().Mul(mintRate).TruncateInt()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
sdk.NewCoin(depositCoins[0].Denom, lastReserveCoinA.Amount.ToDec().Mul(mintRate).TruncateInt()),
sdk.NewCoin(depositCoins[1].Denom, lastReserveCoinB.Amount.ToDec().Mul(mintRate).TruncateInt()),
sdk.NewCoin(depositCoins[0].Denom, lastReserveCoinA.Amount.ToDec().MulTruncate(mintRate).TruncateInt()),
sdk.NewCoin(depositCoins[1].Denom, lastReserveCoinB.Amount.ToDec().MulTruncate(mintRate).TruncateInt()),

What do you think about using MulTruncate instead of Mul here? @hallazzang @typark391 @kogisin
I think both will have the same result but that is more intuitive, or is there a case that is not?

Copy link
Contributor

Choose a reason for hiding this comment

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

Using MulTruncate here can cause(very rarely) a depositor to take advantage against the pool.
Since the result of MulTruncate is always less or equal than the result of Mul, I suggest to use Mul here.
How do others think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right. Let's keep it Mul first, reproduce the corresponding edge case, analyze it, and specify it on spec through other PRs.

x/liquidity/keeper/liquidity_pool_test.go Outdated Show resolved Hide resolved
@dongsam dongsam marked this pull request as ready for review October 17, 2021 21:10
@dongsam dongsam requested review from hallazzang and jaybxyz October 17, 2021 21:11
@dongsam
Copy link
Contributor Author

dongsam commented Oct 17, 2021

Logic ExecuteDeposit was simplified, so the hit line was reduced, even codecov failed, and the actual functional coverage was not lowered.

@hallazzang hallazzang self-requested a review October 18, 2021 03:43
@dongsam dongsam merged commit 616985f into tendermint:develop Oct 18, 2021
dongsam added a commit that referenced this pull request Oct 25, 2021
* feat: add markdown link checker github workflow

* feat: bump cosmos sdk to v0.44.1

* feat: bump cosmos-sdk to v0.44.2

* fix: Pool Coin Decimal Truncation During Deposit (#446)

* fix: wip poc for reproduce and fix poolcoin truncation

* fix: simplify calculation logic and add more tests

* fix: use equality check in MintingPoolCoinsInvariant

* test: fix expected value

due to the change in deposit logic, expected value in test
also changed

* test: add test for MintingPoolCoinsInvariant

* fix: update deposit truncation logic and simulation ordering

* docs: update changelog and readme

* fix: revert MulTruncate to Mul on Deposit

Co-authored-by: Hanjun Kim <[email protected]>

* Fix: add overflow checking and test codes for cover edge cases (#458)

* test: add testcase for cover small withdrawal case

* test: add test case for CreatePool

* fix: refactor and optimize depleted pool validation

* feat: add overflow checking logic

* chore: add testcase and remove comments

* test: add test code for big deposit

* fix: apply PR suggestions

* fix: add overflow checking logic and test cases

Co-authored-by: Hanjun Kim <[email protected]>
Co-authored-by: typark391 <[email protected]>

Co-authored-by: kogisin <[email protected]>
Co-authored-by: Hanjun Kim <[email protected]>
Co-authored-by: typark391 <[email protected]>
dongsam added a commit to b-harvest/liquidity that referenced this pull request Jul 11, 2022
* feat: add markdown link checker github workflow	 9d729d0

* Merge pull request tendermint#453 from b-harvest/kogisin/451-bump-sdk-v0.44.1

feat: bump Cosmos SDK from v0.44.0 to v0.44.1
(cherry picked from commit 9e955b0)

* Merge pull request tendermint#455 from b-harvest/454-bump-sdk-v0.44.2

feat: bump cosmos-sdk to v0.44.2
(cherry picked from commit e118e21)

* fix: Pool Coin Decimal Truncation During Deposit (tendermint#446)

* fix: wip poc for reproduce and fix poolcoin truncation

* fix: simplify calculation logic and add more tests

* fix: use equality check in MintingPoolCoinsInvariant

* test: fix expected value

due to the change in deposit logic, expected value in test
also changed

* test: add test for MintingPoolCoinsInvariant

* fix: update deposit truncation logic and simulation ordering

* docs: update changelog and readme

* fix: revert MulTruncate to Mul on Deposit

Co-authored-by: Hanjun Kim <[email protected]>
(cherry picked from commit 616985f)

* Fix: add overflow checking and test codes for cover edge cases (tendermint#458)

* test: add testcase for cover small withdrawal case

* test: add test case for CreatePool

* fix: refactor and optimize depleted pool validation

* feat: add overflow checking logic

* chore: add testcase and remove comments

* test: add test code for big deposit

* fix: apply PR suggestions

* fix: add overflow checking logic and test cases
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.

Pool Coin Decimal Truncation During Deposit
2 participants