diff --git a/changelog.md b/changelog.md index 434d69161e..439e33b100 100644 --- a/changelog.md +++ b/changelog.md @@ -17,6 +17,7 @@ * [3170](https://github.com/zeta-chain/node/pull/3170) - revamp TSS package in zetaclient * [3291](https://github.com/zeta-chain/node/pull/3291) - revamp zetaclient initialization (+ graceful shutdown) +* [2863](https://github.com/zeta-chain/node/pull/2863) - refactor zetacore to delete matured ballots and add a migration script to remove all old ballots. ### Fixes diff --git a/x/observer/keeper/grpc_query_ballot.go b/x/observer/keeper/grpc_query_ballot.go index 86ebd3aeec..4e67536520 100644 --- a/x/observer/keeper/grpc_query_ballot.go +++ b/x/observer/keeper/grpc_query_ballot.go @@ -68,10 +68,12 @@ func (k Keeper) Ballots(goCtx context.Context, req *types.QueryBallotsRequest) ( if req.Pagination == nil { req.Pagination = &query.PageRequest{} } - if req.Pagination.Limit == 0 || req.Pagination.Limit > 100 { + + if req.Pagination.Limit > 100 { req.Pagination.Limit = 100 } - + // The ballots are not sorted in any particular order therefore this query only has limited usefulness + // if the number of ballots is too large pageRes, err := query.Paginate(ballotStore, req.Pagination, func(_ []byte, value []byte) error { var ballot types.Ballot if err := k.cdc.Unmarshal(value, &ballot); err != nil { diff --git a/x/observer/keeper/grpc_query_ballot_test.go b/x/observer/keeper/grpc_query_ballot_test.go index d00956f741..4f7ae65ca5 100644 --- a/x/observer/keeper/grpc_query_ballot_test.go +++ b/x/observer/keeper/grpc_query_ballot_test.go @@ -5,6 +5,7 @@ import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" "github.com/stretchr/testify/require" keepertest "github.com/zeta-chain/node/testutil/keeper" @@ -135,6 +136,60 @@ func TestKeeper_BallotByIdentifier(t *testing.T) { BallotStatus: ballot.BallotStatus, }, res) }) + + t.Run("should return 100 ballots if more exist and limit is not provided", func(t *testing.T) { + k, ctx, _, _ := keepertest.ObserverKeeper(t) + wctx := sdk.WrapSDKContext(ctx) + numOfBallots := 1000 + + ballots := make([]types.Ballot, numOfBallots) + for i := 0; i < numOfBallots; i++ { + ballot := types.Ballot{ + Index: "", + BallotIdentifier: fmt.Sprintf("index-%d", i), + VoterList: []string{sample.AccAddress()}, + Votes: []types.VoteType{types.VoteType_SuccessObservation}, + BallotStatus: types.BallotStatus_BallotInProgress, + BallotCreationHeight: 1, + BallotThreshold: sdk.MustNewDecFromStr("0.5"), + } + k.SetBallot(ctx, &ballot) + ballots[i] = ballot + } + + res, err := k.Ballots(wctx, &types.QueryBallotsRequest{}) + require.NoError(t, err) + require.Len(t, res.Ballots, 100) + }) + + t.Run("should return limit number of ballots if limit is provided", func(t *testing.T) { + k, ctx, _, _ := keepertest.ObserverKeeper(t) + wctx := sdk.WrapSDKContext(ctx) + numOfBallots := 1000 + + ballots := make([]types.Ballot, numOfBallots) + for i := 0; i < numOfBallots; i++ { + ballot := types.Ballot{ + Index: "", + BallotIdentifier: fmt.Sprintf("index-%d", i), + VoterList: []string{sample.AccAddress()}, + Votes: []types.VoteType{types.VoteType_SuccessObservation}, + BallotStatus: types.BallotStatus_BallotInProgress, + BallotCreationHeight: 1, + BallotThreshold: sdk.MustNewDecFromStr("0.5"), + } + k.SetBallot(ctx, &ballot) + ballots[i] = ballot + } + + res, err := k.Ballots(wctx, &types.QueryBallotsRequest{ + Pagination: &query.PageRequest{ + Limit: 10, + }, + }) + require.NoError(t, err) + require.Len(t, res.Ballots, 10) + }) } func TestKeeper_Ballots(t *testing.T) { @@ -153,9 +208,7 @@ func TestKeeper_Ballots(t *testing.T) { res, err := k.Ballots(wctx, &types.QueryBallotsRequest{}) require.NoError(t, err) - require.Equal(t, &types.QueryBallotsResponse{ - Ballots: []types.Ballot{}, - }, res) + require.Empty(t, res.Ballots) }) t.Run("should return all ballots", func(t *testing.T) {