Skip to content

Commit

Permalink
fix(x/twap): geometric twap genesis validation (#4262) (#4263)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5a32685)

Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
mergify[bot] and p0mvn authored Feb 9, 2023
1 parent f954e4f commit 96635bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/twap/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (t TwapRecord) validate() error {
return fmt.Errorf("twap record p1 accumulator cannot be negative, was (%s)", t.P1ArithmeticTwapAccumulator)
}

if t.GeometricTwapAccumulator.IsNil() || t.GeometricTwapAccumulator.IsNegative() {
return fmt.Errorf("twap record geometric accumulator cannot be negative, was (%s)", t.GeometricTwapAccumulator)
if t.GeometricTwapAccumulator.IsNil() {
return fmt.Errorf("twap record geometric accumulator cannot be nil, was (%s)", t.GeometricTwapAccumulator)
}
return nil
}
12 changes: 12 additions & 0 deletions x/twap/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func TestGenesisState_Validate(t *testing.T) {
})
)

withGeometricAcc := func(record TwapRecord, geometricAcc sdk.Dec) TwapRecord {
record.GeometricTwapAccumulator = geometricAcc
return record
}

testCases := map[string]struct {
twapGenesis *GenesisState

Expand All @@ -90,6 +95,13 @@ func TestGenesisState_Validate(t *testing.T) {
"valid empty records": {
twapGenesis: NewGenesisState(basicParams, []TwapRecord{}),
},
"valid geometric twap acc is negative": {
twapGenesis: NewGenesisState(basicParams, []TwapRecord{withGeometricAcc(baseRecord, sdk.NewDec(-1))}),
},
"invalid geometric twap acc is nil": {
twapGenesis: NewGenesisState(basicParams, []TwapRecord{withGeometricAcc(baseRecord, sdk.Dec{})}),
expectedErr: true,
},
"invalid genesis - pool ID doesn't exist": {
twapGenesis: NewGenesisState(
NewParams("week", 48*time.Hour),
Expand Down

0 comments on commit 96635bc

Please sign in to comment.