Skip to content

Commit

Permalink
fix: validator list sort.
Browse files Browse the repository at this point in the history
  • Loading branch information
romever committed Aug 8, 2024
1 parent e315bad commit 4a9b09c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
10 changes: 4 additions & 6 deletions api/internal/logic/validator/validatorlistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ func NewValidatorListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Val
}

func (l *ValidatorListLogic) ValidatorList(req *types.ValidatorListRequest) (resp *types.ValidatorListResponse, err error) {
v, err := l.svcCtx.LocalCache.MarketCache.Take("list", func() (interface{}, error) {
var orderBy, sortType string
cacheKey := fmt.Sprintf("%s_%s", req.OrderBy, req.Sort)
v, err := l.svcCtx.LocalCache.MarketCache.Take(cacheKey, func() (interface{}, error) {
orderBy, sortType := "escrow", "desc"
switch req.OrderBy {
case "escrowChange24":
orderBy = "escrow_24h"
case "commission":
orderBy = "commission"
default:
orderBy = "escrow"
sortType = "desc"
}
if sortType == "asc" {
if req.OrderBy == "asc" {
sortType = "asc"
}

Expand Down
1 change: 1 addition & 0 deletions docs/sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS block_signature
CONSTRAINT unique_height_validator_address UNIQUE (height, validator_address)
);
CREATE INDEX IF NOT EXISTS block_signature_validator_address_idx ON block_signature (validator_address);
CREATE INDEX IF NOT EXISTS block_signature_timestamp_idx ON block_signature (timestamp);

CREATE TABLE IF NOT EXISTS delegator
(
Expand Down
18 changes: 15 additions & 3 deletions job/model/blocksignaturemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type (
BlockSignatureModel interface {
blockSignatureModel
SessionInsert(ctx context.Context, session sqlx.Session, data *BlockSignature) (sql.Result, error)
CountSigns(ctx context.Context, signAddresses []string, from int64) (int64, error)
CountSigns(ctx context.Context, signAddresses []string, from int64, startTime int64, endTime int64) (int64, error)
FindBlocks(ctx context.Context, pageable common.Pageable) ([]*BlockSignature, error)
}

Expand All @@ -39,7 +39,7 @@ func (m *customBlockSignatureModel) SessionInsert(ctx context.Context, session s
return ret, err
}

func (m *customBlockSignatureModel) CountSigns(ctx context.Context, signAddresses []string, from int64) (int64, error) {
func (m *customBlockSignatureModel) CountSigns(ctx context.Context, signAddresses []string, from int64, startTime int64, endTime int64) (int64, error) {
query := fmt.Sprintf("select count(*) from %s where validator_address in (", m.table)
vars := make([]interface{}, 0)
for i, signAddress := range signAddresses {
Expand All @@ -48,9 +48,21 @@ func (m *customBlockSignatureModel) CountSigns(ctx context.Context, signAddresse
}
query = query[:len(query)-1] + ")"

paramIndex := len(signAddresses)
if from > 0 {
query += fmt.Sprintf(" and height > $%d and height <= $%d", len(signAddresses)+1, len(signAddresses)+2)
query += fmt.Sprintf(" and height > $%d and height <= $%d", paramIndex+1, paramIndex+2)
vars = append(vars, from, from+common.UptimeHeight)
paramIndex = paramIndex + 2
}
if startTime > 0 {
query += fmt.Sprintf(" and timestamp > $%d", paramIndex+1)
vars = append(vars, startTime)
paramIndex = paramIndex + 1
}
if endTime > 0 {
query += fmt.Sprintf(" and timestamp <= $%d", paramIndex+1)
vars = append(vars, endTime)
paramIndex = paramIndex + 1
}
var resp int64
err := m.conn.QueryRowCtx(ctx, &resp, query, vars...)
Expand Down
2 changes: 1 addition & 1 deletion job/model/validatormodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *customValidatorModel) FindListByConsensusAddress(ctx context.Context, c
}

func (m *customValidatorModel) FindAll(ctx context.Context, orderBy, sort string) ([]*Validator, error) {
query := fmt.Sprintf("select %s from %s order by %s %s", validatorRows, m.table, orderBy, sort)
query := fmt.Sprintf("select %s from %s order by nodes desc,%s %s", validatorRows, m.table, orderBy, sort)
var resp []*Validator
err := m.conn.QueryRowsCtx(ctx, &resp, query)
switch err {
Expand Down
4 changes: 2 additions & 2 deletions job/task/validator_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,15 @@ func ValidatorConsensusSync(ctx context.Context, svcCtx *svc.ServiceContext) {
}
validator.Proposals = proposalsCount
//signs
signsCount, err := svcCtx.BlockSignatureModel.CountSigns(ctx, consAddresses, 0)
signsCount, err := svcCtx.BlockSignatureModel.CountSigns(ctx, consAddresses, 0, 0, 0)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(ctx, "CountSigns error, %v", err)
return
}
validator.Signs = signsCount
validator.Score = signsCount*common.SignScore + proposalsCount*common.ProposalScore

statsCount, err := svcCtx.BlockSignatureModel.CountSigns(ctx, consAddresses, nodeHeight-common.UptimeHeight)
statsCount, err := svcCtx.BlockSignatureModel.CountSigns(ctx, consAddresses, nodeHeight-common.UptimeHeight, 0, 0)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(ctx, "Validator CountSigns error, %v", err)
return
Expand Down

0 comments on commit 4a9b09c

Please sign in to comment.