From 96635bcbd3030f85782088cf9b5d3adab6f232b1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:57:23 -0500 Subject: [PATCH] fix(x/twap): geometric twap genesis validation (#4262) (#4263) (cherry picked from commit 5a32685b10c8e4c27f8403a6064fb48c68207326) Co-authored-by: Roman --- x/twap/types/genesis.go | 4 ++-- x/twap/types/genesis_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/x/twap/types/genesis.go b/x/twap/types/genesis.go index fdd273bcd7b..67a892bd9a9 100644 --- a/x/twap/types/genesis.go +++ b/x/twap/types/genesis.go @@ -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 } diff --git a/x/twap/types/genesis_test.go b/x/twap/types/genesis_test.go index bc65efe03ad..29e300331f6 100644 --- a/x/twap/types/genesis_test.go +++ b/x/twap/types/genesis_test.go @@ -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 @@ -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),