Skip to content

Commit

Permalink
chore: Merge to release branch (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Tsitrin <[email protected]>
Co-authored-by: Michael Tsitrin <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2024
1 parent c3f5b51 commit 868c14a
Show file tree
Hide file tree
Showing 61 changed files with 6,329 additions and 578 deletions.
4 changes: 2 additions & 2 deletions app/apptesting/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// AssertEventEmitted asserts that ctx's event manager has emitted the given number of events
// of the given type.
func (s *KeeperTestHelper) AssertEventEmitted(ctx sdk.Context, eventTypeExpected string, numEventsExpected int) {
func (s *KeeperTestHelper) AssertEventEmitted(ctx sdk.Context, eventTypeExpected string, numEventsExpected int, msgAndArgs ...interface{}) {
allEvents := ctx.EventManager().Events()
// filter out other events
actualEvents := make([]sdk.Event, 0)
Expand All @@ -16,7 +16,7 @@ func (s *KeeperTestHelper) AssertEventEmitted(ctx sdk.Context, eventTypeExpected
actualEvents = append(actualEvents, event)
}
}
s.Equal(numEventsExpected, len(actualEvents))
s.Equal(numEventsExpected, len(actualEvents), msgAndArgs...)
}

func (s *KeeperTestHelper) FindEvent(events []sdk.Event, name string) sdk.Event {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/osmosis-labs/osmosis/v15
go 1.19

require (
cosmossdk.io/math v1.0.0-rc.0
cosmossdk.io/math v1.2.0
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.46.15
github.com/cosmos/iavl v0.19.6
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231121111300-10b823f40204
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231219114210-d548bc8ea087
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
Expand Down Expand Up @@ -204,7 +204,7 @@ require (
github.com/golangci/misspell v0.4.0 // indirect
github.com/golangci/revgrep v0.0.0-20220804021717-745bb2f7c2e6 // indirect
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/flatbuffers v2.0.8+incompatible // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/orderedcode v0.0.1 // indirect
github.com/gordonklaus/ineffassign v0.0.0-20230107090616-13ace0543b28 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoIS
collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE=
cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w=
cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE=
cosmossdk.io/math v1.0.0-rc.0 h1:ml46ukocrAAoBpYKMidF0R2tQJ1Uxfns0yH8wqgMAFc=
cosmossdk.io/math v1.0.0-rc.0/go.mod h1:Ygz4wBHrgc7g0N+8+MrnTfS9LLn9aaTGa9hKopuym5k=
cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig=
cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down Expand Up @@ -502,8 +502,8 @@ github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac h1:opbrjaN/L8
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQxaLAeM=
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231121111300-10b823f40204 h1:9MBZ2seXiN5OPOn5F9RoQ0yUEHhNSCFJw4xRR/sUxnk=
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231121111300-10b823f40204/go.mod h1:SghQsf3RvKet1w8Sr+WJAxmKM0uKlhxcaNZBKaPnvPM=
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231219114210-d548bc8ea087 h1:fAmkTkcGW8sLDYMSFAupVD7BsFtIi3KRITS5DIiHMDQ=
github.com/dymensionxyz/dymension v1.0.0-rc1.0.20231219114210-d548bc8ea087/go.mod h1:gdszhnhMNx1MTUQp1BzlrMHY9CcSra0hNxTVoio32Gs=
github.com/dymensionxyz/osmosis/osmomath v0.0.4-dymension-v0.1 h1:R8yipQFGrLYa8yRUTeDBU66LAHMU3hHZbR+xd0G/shU=
github.com/dymensionxyz/osmosis/osmomath v0.0.4-dymension-v0.1/go.mod h1:SkB6rRJ3m6cXqW5q9/36P4Z74+DSjXpBSCnvJ4LT1Iw=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
Expand Down Expand Up @@ -722,8 +722,9 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
github.com/google/btree v1.1.2 h1:xf4v41cLI2Z6FxbKm+8Bu+m8ifhj15JuZ9sa0jZCMUU=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/flatbuffers v1.11.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/flatbuffers v1.12.1 h1:MVlul7pQNoDzWRLTw5imwYsl+usrS1TXG2H4jg6ImGw=
github.com/google/flatbuffers v1.12.1/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/flatbuffers v2.0.8+incompatible h1:ivUb1cGomAB101ZM1T0nOiWz9pSrTMoa9+EiY7igmkM=
github.com/google/flatbuffers v2.0.8+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down
17 changes: 17 additions & 0 deletions osmomath/binary_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ func (e ErrTolerance) CompareBigDec(expected BigDec, actual BigDec) int {
return 0
}

// EqualCoins returns true iff the two coins are equal within the ErrTolerance constraints and false otherwise.
// TODO: move error tolerance functions to a separate file.
func (e ErrTolerance) EqualCoins(expectedCoins sdk.Coins, actualCoins sdk.Coins) bool {
if len(expectedCoins) < len(actualCoins) {
return false
}

for _, expectedCoin := range expectedCoins {
curCoinEqual := e.Compare(expectedCoin.Amount, actualCoins.AmountOf(expectedCoin.Denom))
if curCoinEqual != 0 {
return false
}
}

return true
}

// Binary search inputs between [lowerbound, upperbound] to a monotonic increasing function f.
// We stop once f(found_input) meets the ErrTolerance constraints.
// If we perform more than maxIterations (or equivalently lowerbound = upperbound), we return an error.
Expand Down
43 changes: 41 additions & 2 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (

var (
precisionReuse = new(big.Int).Exp(big.NewInt(10), big.NewInt(Precision), nil)
precisionReuseSDK = new(big.Int).Exp(big.NewInt(10), big.NewInt(sdk.Precision), nil)
fivePrecision = new(big.Int).Quo(precisionReuse, big.NewInt(2))
precisionMultipliers []*big.Int
zeroInt = big.NewInt(0)
Expand Down Expand Up @@ -305,6 +306,17 @@ func (d BigDec) MulTruncate(d2 BigDec) BigDec {
return BigDec{chopped}
}

// multiplication round up
func (d BigDec) MulRoundUp(d2 BigDec) BigDec {
mul := new(big.Int).Mul(d.i, d2.i)
chopped := chopPrecisionAndRoundUpBigDec(mul)

if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
}
return BigDec{chopped}
}

// multiplication
func (d BigDec) MulInt(i BigInt) BigDec {
mul := new(big.Int).Mul(d.i, i.i)
Expand Down Expand Up @@ -381,7 +393,7 @@ func (d BigDec) QuoRoundUp(d2 BigDec) BigDec {
mul.Mul(mul, precisionReuse)

quo := new(big.Int).Quo(mul, d2.i)
chopped := chopPrecisionAndRoundUp(quo)
chopped := chopPrecisionAndRoundUpBigDec(quo)

if chopped.BitLen() > maxDecBitLen {
panic("Int overflow")
Expand Down Expand Up @@ -557,6 +569,13 @@ func (d BigDec) SDKDec() sdk.Dec {
return truncatedDec
}

// SDKDecRoundUp returns the Sdk.Dec representation of a BigDec.
// Round up at precision end.
// Values in any additional decimal places are truncated.
func (d BigDec) SDKDecRoundUp() sdk.Dec {
return sdk.NewDecFromBigIntWithPrec(chopPrecisionAndRoundUpSDKDec(d.i), sdk.Precision)
}

// BigDecFromSdkDec returns the BigDec representation of an SDKDec.
// Values in any additional decimal places are truncated.
func BigDecFromSDKDec(d sdk.Dec) BigDec {
Expand Down Expand Up @@ -628,8 +647,18 @@ func chopPrecisionAndRound(d *big.Int) *big.Int {
}
}

// chopPrecisionAndRoundUpBigDec removes a Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUpBigDec(d *big.Int) *big.Int {
return chopPrecisionAndRoundUp(d, precisionReuse)
}

// chopPrecisionAndRoundUpSDKDec removes sdk.Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUpSDKDec(d *big.Int) *big.Int {
return chopPrecisionAndRoundUp(d, precisionReuseSDK)
}

// chopPrecisionAndRoundUp removes a Precision amount of rightmost digits and rounds up.
func chopPrecisionAndRoundUp(d *big.Int) *big.Int {
func chopPrecisionAndRoundUp(d *big.Int, precisionReuse *big.Int) *big.Int {
// remove the negative and add it back when returning
if d.Sign() == -1 {
// make d positive, compute chopped value, and then un-mutate d
Expand Down Expand Up @@ -887,12 +916,16 @@ func MaxDec(d1, d2 BigDec) BigDec {

// DecEq returns true if two given decimals are equal.
// Intended to be used with require/assert: require.True(t, DecEq(...))
//
//nolint:thelper
func DecEq(t *testing.T, exp, got BigDec) (*testing.T, bool, string, string, string) {
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
}

// DecApproxEq returns true if the differences between two given decimals are smaller than the tolerance range.
// Intended to be used with require/assert: require.True(t, DecEq(...))
//
//nolint:thelper
func DecApproxEq(t *testing.T, d1 BigDec, d2 BigDec, tol BigDec) (*testing.T, bool, string, string, string) {
diff := d1.Sub(d2).Abs()
return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String()
Expand Down Expand Up @@ -1019,6 +1052,9 @@ func (d BigDec) PowerIntegerMut(power uint64) BigDec {
// If a greater exponent is given, the function panics.
// The error is not bounded but expected to be around 10^-18, use with care.
// See the underlying Exp2, LogBase2 and Mul for the details of their bounds.
// WARNING: This function is broken for base < 1. The reason is that logarithm function is
// negative between zero and 1, and the Exp2(k) is undefined for negative k.
// As a result, this function panics if called for d < 1.
func (d BigDec) Power(power BigDec) BigDec {
if d.IsNegative() {
panic(fmt.Sprintf("negative base is not supported for Power(), base was (%s)", d))
Expand All @@ -1038,6 +1074,9 @@ func (d BigDec) Power(power BigDec) BigDec {
if d.IsZero() {
return ZeroDec()
}
if d.LT(OneDec()) {
panic(fmt.Sprintf("Power() is not supported for base < 1, base was (%s)", d))
}
if d.Equal(twoBigDec) {
return Exp2(power)
}
Expand Down
Loading

0 comments on commit 868c14a

Please sign in to comment.