Skip to content

Commit

Permalink
add a query for all ballots
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 19, 2024
1 parent 68bd73a commit b074dda
Show file tree
Hide file tree
Showing 9 changed files with 844 additions and 200 deletions.
14 changes: 14 additions & 0 deletions proto/zetachain/zetacore/observer/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ service Query {
returns (QueryOperationalFlagsResponse) {
option (google.api.http).get = "/zeta-chain/observer/operationalFlags";
}

// Query all ballots
rpc Ballots(QueryBallotsRequest) returns (QueryBallotsResponse) {
option (google.api.http).get = "/zeta-chain/observer/ballots";
}
}

message QueryBallotsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryBallotsResponse {
repeated Ballot ballots = 1 [ (gogoproto.nullable) = false ];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryOperationalFlagsRequest {}
Expand Down
1 change: 0 additions & 1 deletion x/emissions/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func DistributeObserverRewards(
var (
slashAmount = params.ObserverSlashAmount
maturityBlocks = params.BallotMaturityBlocks

maturedBallots []string
)

Expand Down
1 change: 1 addition & 0 deletions x/observer/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GetQueryCmd(_ string) *cobra.Command {
CmdGetAllTssFundsMigrator(),
CmdGetTssFundsMigrator(),
CmdShowOperationalFlags(),
CmdAllBallots(),
)

return cmd
Expand Down
25 changes: 25 additions & 0 deletions x/observer/client/cli/query_ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ func CmdBallotByIdentifier() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdAllBallots() *cobra.Command {
cmd := &cobra.Command{
Use: "list-ballots",
Short: "Query all ballots",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {

Check failure on line 48 in x/observer/client/cli/query_ballot.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryBallotsRequest{}

res, err := queryClient.Ballots(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
4 changes: 2 additions & 2 deletions x/observer/keeper/ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (k Keeper) AddBallotToList(ctx sdk.Context, ballot types.Ballot) {

// ClearMaturedBallotsAndBallotList deletes all matured ballots and the list of ballots for a given height.
// It also emits an event for each ballot deleted.
func (k Keeper) ClearMaturedBallotsAndBallotList(ctx sdk.Context, maturityBlocks int64) {
maturedBallotsHeight := getMaturedBallotHeight(ctx, maturityBlocks)
func (k Keeper) ClearMaturedBallotsAndBallotList(ctx sdk.Context, maturityBlocksParam int64) {
maturedBallotsHeight := getMaturedBallotHeight(ctx, maturityBlocksParam)

// Fetch all the matured ballots, return if no matured ballots are found
// For the current implementation, this should never happen as ClearMaturedBallotsAndBallotList is only called after the Distribution of the rewards,
Expand Down
13 changes: 13 additions & 0 deletions x/observer/keeper/grpc_query_ballot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ func (k Keeper) BallotByIdentifier(
BallotStatus: ballot.BallotStatus,
}, nil
}

func (k Keeper) Ballots(goCtx context.Context, req *types.QueryBallotsRequest) (*types.QueryBallotsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
ballotList := k.GetAllBallots(ctx)
ballots := make([]types.Ballot, len(ballotList))
for i, ballot := range ballotList {
ballots[i] = *ballot
}
return &types.QueryBallotsResponse{Ballots: ballots}, nil
}
47 changes: 47 additions & 0 deletions x/observer/keeper/grpc_query_ballot_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -135,3 +136,49 @@ func TestKeeper_BallotByIdentifier(t *testing.T) {
}, res)
})
}

func TestKeeper_Ballots(t *testing.T) {
t.Run("should error if req is nil", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

res, err := k.Ballots(wctx, nil)
require.Nil(t, res)
require.Error(t, err)
})

t.Run("should return empty list if no ballots", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

res, err := k.Ballots(wctx, &types.QueryBallotsRequest{})
require.NoError(t, err)
require.Equal(t, &types.QueryBallotsResponse{
Ballots: []types.Ballot{},
}, res)
})

t.Run("should return all ballots", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)
wctx := sdk.WrapSDKContext(ctx)

ballots := make([]types.Ballot, 10)
for i := 0; i < 10; 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.ElementsMatch(t, ballots, res.Ballots)
})
}
Loading

0 comments on commit b074dda

Please sign in to comment.