Skip to content

Commit

Permalink
Fix: sort validators by power (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Sep 7, 2024
1 parent 4c1cd46 commit 0b8e185
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 14 deletions.
5 changes: 3 additions & 2 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions cmd/api/handler/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,25 @@ func (handler *ValidatorHandler) Get(c echo.Context) error {
return c.JSON(http.StatusOK, responses.NewValidator(validator))
}

type validatorsRequest struct {
Limit int `query:"limit" validate:"omitempty,min=1,max=100"`
Offset int `query:"offset" validate:"omitempty,min=0"`
Sort string `query:"sort" validate:"omitempty,oneof=asc desc"`
}

func (p *validatorsRequest) SetDefault() {
if p.Limit == 0 {
p.Limit = 10
}
if p.Sort == "" {
p.Sort = asc
}
}

// List godoc
//
// @Summary List validators
// @Description List validators
// @Description List validators sorted by power
// @Tags validator
// @ID list-validator
// @Param limit query integer false "Count of requested entities" mininum(1) maximum(100)
Expand All @@ -83,20 +98,20 @@ func (handler *ValidatorHandler) Get(c echo.Context) error {
// @Failure 500 {object} Error
// @Router /v1/validators [get]
func (handler *ValidatorHandler) List(c echo.Context) error {
req, err := bindAndValidate[listRequest](c)
req, err := bindAndValidate[validatorsRequest](c)
if err != nil {
return badRequestError(c, err)
}
req.SetDefault()

validators, err := handler.validators.List(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
validators, err := handler.validators.ListByPower(c.Request().Context(), req.Limit, req.Offset, pgSort(req.Sort))
if err != nil {
return handleError(c, err, handler.validators)
}

response := make([]responses.Validator, len(validators))
for i := range validators {
response[i] = *responses.NewValidator(validators[i])
response[i] = *responses.NewValidator(&validators[i])
}

return returnArray(c, response)
Expand Down
9 changes: 5 additions & 4 deletions cmd/api/handler/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ func (s *ValidatorTestSuite) TestList() {
c.SetPath("/validators")

s.validators.EXPECT().
List(gomock.Any(), uint64(10), uint64(0), pgSort("asc")).
Return([]*storage.Validator{
&testValidator,
}, nil)
ListByPower(gomock.Any(), 10, 0, pgSort("asc")).
Return([]storage.Validator{
testValidator,
}, nil).
Times(1)

s.Require().NoError(s.handler.List(c))
s.Require().Equal(http.StatusOK, rec.Code)
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/validator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/storage/postgres/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ func createIndices(ctx context.Context, conn *database.Bun) error {
Exec(ctx); err != nil {
return err
}
if _, err := tx.NewCreateIndex().
IfNotExists().
Model((*storage.Validator)(nil)).
Index("validator_power_idx").
ColumnExpr("power").
Exec(ctx); err != nil {
return err
}

// Bridge
if _, err := tx.NewCreateIndex().
Expand Down
14 changes: 14 additions & 0 deletions internal/storage/postgres/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package postgres

import (
"context"

"github.com/celenium-io/astria-indexer/internal/storage"
"github.com/dipdup-net/go-lib/database"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/dipdup-net/indexer-sdk/pkg/storage/postgres"
)

Expand All @@ -20,3 +23,14 @@ func NewValidator(db *database.Bun) *Validator {
Table: postgres.NewTable[*storage.Validator](db),
}
}

func (v *Validator) ListByPower(ctx context.Context, limit, offset int, order sdk.SortOrder) (validators []storage.Validator, err error) {
query := v.DB().NewSelect().
Model(&validators).
Offset(offset)
query = limitScope(query, limit)
query = sortScope(query, "power", order)

err = query.Scan(ctx)
return
}
26 changes: 26 additions & 0 deletions internal/storage/postgres/validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package postgres

import (
"context"
"time"

"github.com/dipdup-net/indexer-sdk/pkg/storage"
)

func (s *StorageTestSuite) TestValidatorListByPower() {
ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer ctxCancel()

validators, err := s.storage.Validator.ListByPower(ctx, 1, 0, storage.SortOrderDesc)
s.Require().NoError(err)
s.Require().Len(validators, 1)

val := validators[0]
s.Require().EqualValues(3, val.Id)
s.Require().EqualValues("2", val.Power.String())
s.Require().EqualValues("node2", val.Name)
s.Require().EqualValues("astria1c220qfmjrwqlk939ca5a5z2rjxryyr9m3ah8gl", val.Address)
}
5 changes: 5 additions & 0 deletions internal/storage/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
package storage

import (
"context"

pkgTypes "github.com/celenium-io/astria-indexer/pkg/types"

"github.com/dipdup-net/indexer-sdk/pkg/storage"
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/shopspring/decimal"
"github.com/uptrace/bun"
)

//go:generate mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock -typed
type IValidator interface {
storage.Table[*Validator]

ListByPower(ctx context.Context, limit, offset int, order sdk.SortOrder) ([]Validator, error)
}

type Validator struct {
Expand Down
2 changes: 1 addition & 1 deletion test/data/validator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
pubkey_type: tendermint/PubKeyEd25519
pubkey: 0x96f43a8448928f1e580864d69fe44e093c5a82a1d4a80c59086d7e67976cda45
name: node2
power: 1
power: 2
height: 0

0 comments on commit 0b8e185

Please sign in to comment.