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

refactor(osmomath): limit pow iterations in osmomath #6627

Merged
merged 4 commits into from
Oct 6, 2023

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Oct 3, 2023

Closes: #XXX

What is the purpose of the change

Limits the number of Pow iterations:
To make sure that we don't hit the loop iteration bound too often
under normal conditions, this change was backported to v19.x (mainnet at the time of writing)
and tested for no app-hash over 10000 blocks.

go test -benchmem -run=^$ -bench ^BenchmarkSlowPowCases$ github.com/osmosis-labs/osmosis/osmomath -count=6
goos: linux
goarch: amd64
pkg: github.com/osmosis-labs/osmosis/osmomath
cpu: 12th Gen Intel(R) Core(TM) i7-1260P
BenchmarkSlowPowCases-16              76          14897602 ns/op         3760805 B/op     168182 allocs/op
BenchmarkSlowPowCases-16              78          14858077 ns/op         3760792 B/op     168181 allocs/op
BenchmarkSlowPowCases-16              79          15163556 ns/op         3760784 B/op     168181 allocs/op
BenchmarkSlowPowCases-16              79          15022864 ns/op         3760810 B/op     168182 allocs/op
BenchmarkSlowPowCases-16              85          15068015 ns/op         3760784 B/op     168182 allocs/op
BenchmarkSlowPowCases-16              68          15159170 ns/op         3760789 B/op     168182 allocs/op
PASS
ok      github.com/osmosis-labs/osmosis/osmomath        7.105s

Testing and Verifying

  • This change is a trivial rework / code cleanup without any test coverage.
  • Was tested by backporting to the v19.x release line

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

@github-actions
Copy link
Contributor

github-actions bot commented Oct 3, 2023

Important Notice

This PR modifies an in-repo Go module. It is one of:

  • osmomath
  • osmoutils
  • x/ibc-hooks
  • x/epochs

The dependent Go modules, especially the root one, will have to be
updated to reflect the changes. Failing to do so might cause e2e to fail.

Please follow the instructions below:

  1. Open https://github.com/osmosis-labs/osmosis/actions/workflows/go-mod-auto-bump.yml
  2. Provide the current branch name
  3. On success, confirm if an automated commit corretly updated the go.mod and go.sum files

Please let us know if you need any help.

@p0mvn p0mvn added the V:state/breaking State machine breaking PR label Oct 3, 2023
@p0mvn p0mvn marked this pull request as ready for review October 3, 2023 14:10
Copy link
Member

@pysel pysel 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 to me!

osmomath/math.go Outdated
@@ -8,6 +8,8 @@ import (
// TODO: Analyze choice here.
var powPrecision, _ = NewDecFromStr("0.00000001")

const powIterationLimit = 800_000
Copy link
Member

Choose a reason for hiding this comment

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

asking to learn: is there a specific reason for this number?

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually changed this to a lower value of 150_000

It has shown a 15ms worst case under benchmarks and was tested to be infrequent to occur under normal operations when backporting this to a v19.x release line

@p0mvn p0mvn marked this pull request as draft October 4, 2023 09:26
@p0mvn p0mvn force-pushed the roman/limit-num-iterations-pow branch from d7a3a19 to b489a40 Compare October 4, 2023 09:27
@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Oct 4, 2023
@p0mvn p0mvn marked this pull request as ready for review October 4, 2023 14:05
@p0mvn p0mvn mentioned this pull request Oct 4, 2023
Comment on lines 342 to 356
name: "11single asset - (almost 1 == tokenIn / liquidity ratio), token in weight is smaller than the other token, with zero spread factor",
spreadFactor: osmomath.MustNewDecFromStr("0"),
poolAssets: []balancer.PoolAsset{
{
Token: sdk.NewInt64Coin("uosmo", 500_000),
Weight: osmomath.NewInt(100),
},
{
Token: sdk.NewInt64Coin("uatom", 1e12),
Weight: osmomath.NewInt(1000),
},
},
// Increasing this by 1 would cause a panic due to pow iteration limit reached
tokensIn: sdk.NewCoins(sdk.NewInt64Coin("uosmo", 499_990)),
expectShares: osmomath.NewIntFromUint64(6_504_012_121_638_943_579),
Copy link
Member

Choose a reason for hiding this comment

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

Need a little more background here to completely understand. So, now with this change, if a pool gets into this state, the amount of tokens in that can be swapped is reduced due to iteration limits?

Copy link
Member Author

Choose a reason for hiding this comment

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

The original tokenIn value now causes an iteration limit. The test covering that behavior is kept.

The new token in value of 499990 is the closest one to the old that does not panic

Copy link
Member

@mattverse mattverse left a comment

Choose a reason for hiding this comment

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

The change lgtm, we didn't include this in a release yet right?
I feel like this change should definitely be state breaking thus should be included in the next major version upgrade

@@ -8,6 +8,8 @@ import (
// TODO: Analyze choice here.
var powPrecision, _ = NewDecFromStr("0.00000001")

const powIterationLimit = 150_000
Copy link
Member

Choose a reason for hiding this comment

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

Curious, how was this chosen? 👀

Copy link
Member Author

Choose a reason for hiding this comment

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

@p0mvn
Copy link
Member Author

p0mvn commented Oct 5, 2023

The change lgtm, we didn't include this in a release yet right? I feel like this change should definitely be state breaking thus should be included in the next major version upgrade

This is v20.

@p0mvn p0mvn merged commit a1d76ab into main Oct 6, 2023
1 check passed
@p0mvn p0mvn deleted the roman/limit-num-iterations-pow branch October 6, 2023 15:58
@github-actions github-actions bot mentioned this pull request Apr 15, 2024
@github-actions github-actions bot mentioned this pull request May 1, 2024
@github-actions github-actions bot mentioned this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/gamm Changes, features and bugs related to the gamm module. V:state/breaking State machine breaking PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants