From 62d4d47bd5ab13e43160ba3fab3a931fe8f78305 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 11 Jun 2022 01:16:37 +0700 Subject: [PATCH 1/8] fix filtering logic --- x/incentives/keeper/grpc_query.go | 40 ++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index 244d93a24d7..926b3c2eb46 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -72,7 +72,9 @@ func (q Querier) Gauges(goCtx context.Context, req *types.GaugesRequest) (*types if err != nil { panic(err) } - gauges = append(gauges, newGauges...) + if accumulate { + gauges = append(gauges, newGauges...) + } return true, nil }) @@ -99,7 +101,9 @@ func (q Querier) ActiveGauges(goCtx context.Context, req *types.ActiveGaugesRequ if err != nil { panic(err) } - gauges = append(gauges, newGauges...) + if accumulate { + gauges = append(gauges, newGauges...) + } return true, nil }) @@ -122,10 +126,16 @@ func (q Querier) ActiveGaugesPerDenom(goCtx context.Context, req *types.ActiveGa valStore := prefix.NewStore(store, types.KeyPrefixActiveGauges) pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - activeGauges := q.Keeper.GetActiveGauges(ctx) - for _, gauge := range activeGauges { - if gauge.DistributeTo.Denom == req.Denom { - gauges = append(gauges, gauge) + newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) + if err != nil { + panic(err) + } + for i := 0; i < len(newGauges); i++ { + if newGauges[i].DistributeTo.Denom != req.Denom { + return false, nil + } + if accumulate { + gauges = append(gauges, newGauges...) } } return true, nil @@ -153,7 +163,9 @@ func (q Querier) UpcomingGauges(goCtx context.Context, req *types.UpcomingGauges if err != nil { panic(err) } - gauges = append(gauges, newGauges...) + if accumulate { + gauges = append(gauges, newGauges...) + } return true, nil }) @@ -179,10 +191,16 @@ func (q Querier) UpcomingGaugesPerDenom(goCtx context.Context, req *types.Upcomi prefixStore := prefix.NewStore(store, types.KeyPrefixUpcomingGauges) pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - upcomingGauges := q.Keeper.GetUpcomingGauges(ctx) - for _, gauge := range upcomingGauges { - if gauge.DistributeTo.Denom == req.Denom { - gauges = append(gauges, gauge) + newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) + if err != nil { + panic(err) + } + for i := 0; i < len(newGauges); i++ { + if newGauges[i].DistributeTo.Denom != req.Denom { + return false, nil + } + if accumulate { + gauges = append(gauges, newGauges...) } } return true, nil From 66ef1c6c71a0714f3255038711b294b5130e8c99 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 11 Jun 2022 01:16:45 +0700 Subject: [PATCH 2/8] add test --- x/incentives/keeper/grpc_query_test.go | 86 ++++++++++++++++++++++++++ x/incentives/keeper/suite_test.go | 27 ++++++++ 2 files changed, 113 insertions(+) diff --git a/x/incentives/keeper/grpc_query_test.go b/x/incentives/keeper/grpc_query_test.go index 2ab7e02b491..92c73e61573 100644 --- a/x/incentives/keeper/grpc_query_test.go +++ b/x/incentives/keeper/grpc_query_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + // "fmt" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -8,6 +9,7 @@ import ( "github.com/osmosis-labs/osmosis/v7/x/incentives/types" lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" pooltypes "github.com/osmosis-labs/osmosis/v7/x/pool-incentives/types" + query "github.com/cosmos/cosmos-sdk/types/query" ) func (suite *KeeperTestSuite) TestGRPCGaugeByID() { @@ -72,6 +74,16 @@ func (suite *KeeperTestSuite) TestGRPCGauges() { StartTime: startTime, } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) + + // filtering check + for i := 0; i < 10; i++ { + suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}) + suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) + } + + filter := query.PageRequest{Limit: 10} + res, err = suite.querier.Gauges(sdk.WrapSDKContext(suite.Ctx), &types.GaugesRequest{Pagination: &filter}) + suite.Require().Len(res.Data, 10) } func (suite *KeeperTestSuite) TestGRPCActiveGauges() { @@ -107,6 +119,23 @@ func (suite *KeeperTestSuite) TestGRPCActiveGauges() { StartTime: startTime, } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) + + // filtering check + for i := 0; i < 20; i++ { + _, gauge, _, _ := suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}) + suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) + + // set up more 9 active gauges => 10 + if i < 9 { + suite.querier.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge) + } + } + + res, err = suite.querier.ActiveGauges(sdk.WrapSDKContext(suite.Ctx), &types.ActiveGaugesRequest{Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.Data, 5) + + res, err = suite.querier.ActiveGauges(sdk.WrapSDKContext(suite.Ctx), &types.ActiveGaugesRequest{Pagination: &query.PageRequest{Limit: 15}}) + suite.Require().Len(res.Data, 10) } func (suite *KeeperTestSuite) TestGRPCActiveGaugesPerDenom() { @@ -140,6 +169,26 @@ func (suite *KeeperTestSuite) TestGRPCActiveGaugesPerDenom() { StartTime: startTime, } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) + + // filtering check + for i := 0; i < 20; i++ { + _, gauge, _, _ := suite.SetupNewGaugeWithDenom(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}, "pool") + suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) + + // set up 10 active gauges with "pool" denom + if i < 10 { + suite.querier.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge) + } + } + + res, err = suite.querier.ActiveGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.ActiveGaugesPerDenomRequest{Denom: "lptoken", Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.Data, 1) + + res, err = suite.querier.ActiveGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.ActiveGaugesPerDenomRequest{Denom: "pool", Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.Data, 5) + + res, err = suite.querier.ActiveGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.ActiveGaugesPerDenomRequest{Denom: "pool", Pagination: &query.PageRequest{Limit: 15}}) + suite.Require().Len(res.Data, 10) } func (suite *KeeperTestSuite) TestGRPCUpcomingGauges() { @@ -172,6 +221,23 @@ func (suite *KeeperTestSuite) TestGRPCUpcomingGauges() { StartTime: startTime, } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) + + // filtering check + for i := 0; i < 20; i++ { + _, gauge, _, _ := suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}) + suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) + + // set up more 9 active gauges => Upcoming = 1 + (20 -9) = 12 + if i < 9 { + suite.querier.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge) + } + } + + res, err = suite.querier.UpcomingGauges(sdk.WrapSDKContext(suite.Ctx), &types.UpcomingGaugesRequest{Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.Data, 5) + + res, err = suite.querier.UpcomingGauges(sdk.WrapSDKContext(suite.Ctx), &types.UpcomingGaugesRequest{Pagination: &query.PageRequest{Limit: 15}}) + suite.Require().Len(res.Data, 12) } func (suite *KeeperTestSuite) TestGRPCUpcomingGaugesPerDenom() { @@ -210,6 +276,26 @@ func (suite *KeeperTestSuite) TestGRPCUpcomingGaugesPerDenom() { res, err = suite.querier.UpcomingGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &upcomingGaugeRequest) suite.Require().NoError(err) suite.Require().Len(res.UpcomingGauges, 0) + + // filtering check + for i := 0; i < 20; i++ { + _, gauge, _, _ := suite.SetupNewGaugeWithDenom(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}, "pool") + suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) + + // set up 10 active gauges with "pool" denom => 10 upcoming + if i < 10 { + suite.querier.MoveUpcomingGaugeToActiveGauge(suite.Ctx, *gauge) + } + } + + res, err = suite.querier.UpcomingGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.UpcomingGaugesPerDenomRequest{Denom: "lptoken", Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.UpcomingGauges, 0) + + res, err = suite.querier.UpcomingGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.UpcomingGaugesPerDenomRequest{Denom: "pool", Pagination: &query.PageRequest{Limit: 5}}) + suite.Require().Len(res.UpcomingGauges, 5) + + res, err = suite.querier.UpcomingGaugesPerDenom(sdk.WrapSDKContext(suite.Ctx), &types.UpcomingGaugesPerDenomRequest{Denom: "pool", Pagination: &query.PageRequest{Limit: 15}}) + suite.Require().Len(res.UpcomingGauges, 10) } func (suite *KeeperTestSuite) TestGRPCRewardsEst() { diff --git a/x/incentives/keeper/suite_test.go b/x/incentives/keeper/suite_test.go index d30017d7bab..820076ab313 100644 --- a/x/incentives/keeper/suite_test.go +++ b/x/incentives/keeper/suite_test.go @@ -129,6 +129,33 @@ func (suite *KeeperTestSuite) SetupNewGauge(isPerpetual bool, coins sdk.Coins) ( return suite.setupNewGaugeWithDuration(isPerpetual, coins, defaultLockDuration) } +func (suite *KeeperTestSuite) setupNewGaugeWithDenom(isPerpetual bool, coins sdk.Coins, duration time.Duration, denom string) ( + uint64, *types.Gauge, sdk.Coins, time.Time, +) { + addr := sdk.AccAddress([]byte("Gauge_Creation_Addr_")) + startTime2 := time.Now() + distrTo := lockuptypes.QueryCondition{ + LockQueryType: lockuptypes.ByDuration, + Denom: denom, + Duration: duration, + } + + // mints coins so supply exists on chain + mintCoins := sdk.Coins{sdk.NewInt64Coin(distrTo.Denom, 200)} + suite.FundAcc(addr, mintCoins) + + numEpochsPaidOver := uint64(2) + if isPerpetual { + numEpochsPaidOver = uint64(1) + } + gaugeID, gauge := suite.CreateGauge(isPerpetual, addr, coins, distrTo, startTime2, numEpochsPaidOver) + return gaugeID, gauge, coins, startTime2 +} + +func (suite *KeeperTestSuite) SetupNewGaugeWithDenom(isPerpetual bool, coins sdk.Coins, denom string) (uint64, *types.Gauge, sdk.Coins, time.Time) { + return suite.setupNewGaugeWithDenom(isPerpetual, coins, defaultLockDuration, denom) +} + func (suite *KeeperTestSuite) SetupManyLocks(numLocks int, liquidBalance sdk.Coins, coinsPerLock sdk.Coins, lockDuration time.Duration, ) []sdk.AccAddress { From d6811c0d085c0247674409469fa1ec1357c54e0f Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 11 Jun 2022 01:18:21 +0700 Subject: [PATCH 3/8] format --- x/incentives/keeper/grpc_query_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/x/incentives/keeper/grpc_query_test.go b/x/incentives/keeper/grpc_query_test.go index 92c73e61573..4316321043d 100644 --- a/x/incentives/keeper/grpc_query_test.go +++ b/x/incentives/keeper/grpc_query_test.go @@ -6,10 +6,10 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" + query "github.com/cosmos/cosmos-sdk/types/query" "github.com/osmosis-labs/osmosis/v7/x/incentives/types" lockuptypes "github.com/osmosis-labs/osmosis/v7/x/lockup/types" pooltypes "github.com/osmosis-labs/osmosis/v7/x/pool-incentives/types" - query "github.com/cosmos/cosmos-sdk/types/query" ) func (suite *KeeperTestSuite) TestGRPCGaugeByID() { @@ -120,7 +120,7 @@ func (suite *KeeperTestSuite) TestGRPCActiveGauges() { } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) - // filtering check + // filtering check for i := 0; i < 20; i++ { _, gauge, _, _ := suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}) suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) @@ -170,7 +170,7 @@ func (suite *KeeperTestSuite) TestGRPCActiveGaugesPerDenom() { } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) - // filtering check + // filtering check for i := 0; i < 20; i++ { _, gauge, _, _ := suite.SetupNewGaugeWithDenom(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}, "pool") suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) @@ -222,7 +222,7 @@ func (suite *KeeperTestSuite) TestGRPCUpcomingGauges() { } suite.Require().Equal(res.Data[0].String(), expectedGauge.String()) - // filtering check + // filtering check for i := 0; i < 20; i++ { _, gauge, _, _ := suite.SetupNewGauge(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}) suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) @@ -277,7 +277,7 @@ func (suite *KeeperTestSuite) TestGRPCUpcomingGaugesPerDenom() { suite.Require().NoError(err) suite.Require().Len(res.UpcomingGauges, 0) - // filtering check + // filtering check for i := 0; i < 20; i++ { _, gauge, _, _ := suite.SetupNewGaugeWithDenom(false, sdk.Coins{sdk.NewInt64Coin("stake", 3)}, "pool") suite.Ctx = suite.Ctx.WithBlockTime(startTime.Add(time.Second)) From 40a5c38d348d2ed851f591e4b1d3d9fb7fe013f5 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 11 Jun 2022 02:18:44 +0700 Subject: [PATCH 4/8] fix: append element in loop --- x/incentives/keeper/grpc_query.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index 926b3c2eb46..4324a461803 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -135,7 +135,7 @@ func (q Querier) ActiveGaugesPerDenom(goCtx context.Context, req *types.ActiveGa return false, nil } if accumulate { - gauges = append(gauges, newGauges...) + gauges = append(gauges, newGauges[i]) } } return true, nil @@ -200,7 +200,7 @@ func (q Querier) UpcomingGaugesPerDenom(goCtx context.Context, req *types.Upcomi return false, nil } if accumulate { - gauges = append(gauges, newGauges...) + gauges = append(gauges, newGauges[i]) } } return true, nil From b8539ed6f21ca898b8bb0725734ef664d0a4acf1 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Sat, 11 Jun 2022 08:02:11 +0700 Subject: [PATCH 5/8] use range --- x/incentives/keeper/grpc_query.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index 4324a461803..7c286562a2a 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -130,12 +130,12 @@ func (q Querier) ActiveGaugesPerDenom(goCtx context.Context, req *types.ActiveGa if err != nil { panic(err) } - for i := 0; i < len(newGauges); i++ { - if newGauges[i].DistributeTo.Denom != req.Denom { + for _, gauge := range newGauges { + if gauge.DistributeTo.Denom != req.Denom { return false, nil } if accumulate { - gauges = append(gauges, newGauges[i]) + gauges = append(gauges, gauge) } } return true, nil @@ -195,12 +195,12 @@ func (q Querier) UpcomingGaugesPerDenom(goCtx context.Context, req *types.Upcomi if err != nil { panic(err) } - for i := 0; i < len(newGauges); i++ { - if newGauges[i].DistributeTo.Denom != req.Denom { + for _, gauge := range newGauges { + if gauge.DistributeTo.Denom != req.Denom { return false, nil } if accumulate { - gauges = append(gauges, newGauges[i]) + gauges = append(gauges, gauge) } } return true, nil From 7d816223e7bcf1f545f77820c9c78f510e2de7fe Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 14 Jun 2022 22:44:21 +0700 Subject: [PATCH 6/8] optimize code, make filtering into a separate function --- x/incentives/keeper/grpc_query.go | 116 +++++++++--------------------- 1 file changed, 35 insertions(+), 81 deletions(-) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index 7c286562a2a..a6175bd03b5 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -63,21 +63,7 @@ func (q Querier) Gauges(goCtx context.Context, req *types.GaugesRequest) (*types } ctx := sdk.UnwrapSDKContext(goCtx) - gauges := []types.Gauge{} - store := ctx.KVStore(q.Keeper.storeKey) - valStore := prefix.NewStore(store, types.KeyPrefixGauges) - - pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) - if err != nil { - panic(err) - } - if accumulate { - gauges = append(gauges, newGauges...) - } - - return true, nil - }) + pageRes, gauges, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixGauges, "", req.Pagination) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -92,21 +78,8 @@ func (q Querier) ActiveGauges(goCtx context.Context, req *types.ActiveGaugesRequ } ctx := sdk.UnwrapSDKContext(goCtx) - gauges := []types.Gauge{} - store := ctx.KVStore(q.Keeper.storeKey) - valStore := prefix.NewStore(store, types.KeyPrefixActiveGauges) - - pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) - if err != nil { - panic(err) - } - if accumulate { - gauges = append(gauges, newGauges...) - } - return true, nil - }) + pageRes, gauges, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixActiveGauges, "", req.Pagination) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -121,25 +94,7 @@ func (q Querier) ActiveGaugesPerDenom(goCtx context.Context, req *types.ActiveGa } ctx := sdk.UnwrapSDKContext(goCtx) - gauges := []types.Gauge{} - store := ctx.KVStore(q.Keeper.storeKey) - valStore := prefix.NewStore(store, types.KeyPrefixActiveGauges) - - pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) - if err != nil { - panic(err) - } - for _, gauge := range newGauges { - if gauge.DistributeTo.Denom != req.Denom { - return false, nil - } - if accumulate { - gauges = append(gauges, gauge) - } - } - return true, nil - }) + pageRes, gauges, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixActiveGauges, req.Denom, req.Pagination) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -154,21 +109,8 @@ func (q Querier) UpcomingGauges(goCtx context.Context, req *types.UpcomingGauges } ctx := sdk.UnwrapSDKContext(goCtx) - gauges := []types.Gauge{} - store := ctx.KVStore(q.Keeper.storeKey) - valStore := prefix.NewStore(store, types.KeyPrefixUpcomingGauges) - pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) - if err != nil { - panic(err) - } - if accumulate { - gauges = append(gauges, newGauges...) - } - - return true, nil - }) + pageRes, gauges, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixUpcomingGauges, "", req.Pagination) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -186,25 +128,7 @@ func (q Querier) UpcomingGaugesPerDenom(goCtx context.Context, req *types.Upcomi return nil, status.Error(codes.InvalidArgument, "invalid denom") } - gauges := []types.Gauge{} - store := ctx.KVStore(q.Keeper.storeKey) - prefixStore := prefix.NewStore(store, types.KeyPrefixUpcomingGauges) - - pageRes, err := query.FilteredPaginate(prefixStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) - if err != nil { - panic(err) - } - for _, gauge := range newGauges { - if gauge.DistributeTo.Denom != req.Denom { - return false, nil - } - if accumulate { - gauges = append(gauges, gauge) - } - } - return true, nil - }) + pageRes, gauges, err := q.filterByPrefixAndDenom(ctx, types.KeyPrefixUpcomingGauges, req.Denom, req.Pagination) if err != nil { return nil, status.Error(codes.Internal, err.Error()) } @@ -271,3 +195,33 @@ func (q Querier) getGaugeFromIDJsonBytes(ctx sdk.Context, refValue []byte) ([]ty return gauges, nil } + +// Filter gauges based on given prefix type and denom +func (q Querier) filterByPrefixAndDenom(ctx sdk.Context, prefixType []byte, denom string, pagination *query.PageRequest) (*query.PageResponse, []types.Gauge, error) { + gauges := []types.Gauge{} + store := ctx.KVStore(q.Keeper.storeKey) + valStore := prefix.NewStore(store, prefixType) + + pageRes, err := query.FilteredPaginate(valStore, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { + // This may return multiple gauges at once if two gauges start at the same time. + //For now this is treated as an edge case that is not of importance + newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) + if err != nil { + panic(err) + } + if accumulate { + if denom != "" { + for _, gauge := range newGauges { + if gauge.DistributeTo.Denom != denom { + return false, nil + } + gauges = append(gauges, gauge) + } + } else { + gauges = append(gauges, newGauges...) + } + } + return true, nil + }) + return pageRes, gauges, err +} From bc691846990a810eebdc888c3c877afb2a0c8aaf Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Tue, 14 Jun 2022 22:46:50 +0700 Subject: [PATCH 7/8] format --- x/incentives/keeper/grpc_query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index a6175bd03b5..abaff409c8f 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -196,15 +196,15 @@ func (q Querier) getGaugeFromIDJsonBytes(ctx sdk.Context, refValue []byte) ([]ty return gauges, nil } -// Filter gauges based on given prefix type and denom +// Filter gauges based on given prefix type and denom func (q Querier) filterByPrefixAndDenom(ctx sdk.Context, prefixType []byte, denom string, pagination *query.PageRequest) (*query.PageResponse, []types.Gauge, error) { gauges := []types.Gauge{} store := ctx.KVStore(q.Keeper.storeKey) valStore := prefix.NewStore(store, prefixType) pageRes, err := query.FilteredPaginate(valStore, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) { - // This may return multiple gauges at once if two gauges start at the same time. - //For now this is treated as an edge case that is not of importance + // This may return multiple gauges at once if two gauges start at the same time. + // For now this is treated as an edge case that is not of importance newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) if err != nil { panic(err) From 9b9a0cf5a1358b2f314114e65b453d25fe79fe07 Mon Sep 17 00:00:00 2001 From: Hieu Vu <72878483+hieuvubk@users.noreply.github.com> Date: Wed, 15 Jun 2022 10:55:15 +0700 Subject: [PATCH 8/8] add to changelog --- CHANGELOG.md | 1 + x/incentives/keeper/grpc_query.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1d1953084d..fc055795dc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug Fixes * [1700](https://github.com/osmosis-labs/osmosis/pull/1700) Upgrade sdk fork with missing snapshot manager fix. * [1716](https://github.com/osmosis-labs/osmosis/pull/1716) Fix secondary over-LP shares bug with uneven swap amounts in `CalcJoinPoolShares`. +* [1759](https://github.com/osmosis-labs/osmosis/pull/1759) Fix pagination filter in incentives query. ## [v9.0.0 - Nitrogen](https://github.com/osmosis-labs/osmosis/releases/tag/v9.0.0) diff --git a/x/incentives/keeper/grpc_query.go b/x/incentives/keeper/grpc_query.go index abaff409c8f..5fe8e7b45fc 100644 --- a/x/incentives/keeper/grpc_query.go +++ b/x/incentives/keeper/grpc_query.go @@ -207,7 +207,7 @@ func (q Querier) filterByPrefixAndDenom(ctx sdk.Context, prefixType []byte, deno // For now this is treated as an edge case that is not of importance newGauges, err := q.getGaugeFromIDJsonBytes(ctx, value) if err != nil { - panic(err) + return false, err } if accumulate { if denom != "" {