Skip to content

Commit

Permalink
fix: validator sign stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
romever committed Nov 18, 2024
1 parent eac903a commit 5a1bd00
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
24 changes: 17 additions & 7 deletions api/internal/logic/cacherefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/zeromicro/go-zero/core/stores/sqlx"
"oasisscan-backend/api/internal/svc"
"oasisscan-backend/api/internal/types"
"time"
)

func SignStatsCacheJob(ctx context.Context, svcCtx *svc.ServiceContext) {
Expand Down Expand Up @@ -54,23 +55,32 @@ func SignStatsCacheJob(ctx context.Context, svcCtx *svc.ServiceContext) {
continue
}

validatorSigns, err := svcCtx.BlockSignatureModel.ValidatorSignStats(ctx, signAddresses, 11)
if err != nil {
logc.Errorf(ctx, "FindBlockCountDays error: %v", err)
continue
}
if validatorSigns == nil {
continue
}
signMap := make(map[time.Time]int64, 0)
for _, sign := range validatorSigns {
signMap[sign.Day] = sign.Count
}

var timeResp []int64
for i := len(days) - 1; i > 0; i-- {
startDay := days[i].Day
endDay := days[i].Day.AddDate(0, 0, 1)

timeResp = append(timeResp, endDay.Unix())

signs, err := svcCtx.BlockSignatureModel.CountSigns(ctx, signAddresses, 0, &startDay, &endDay)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logc.Errorf(ctx, "CountSigns error: %v", err)
continue
}
signCount := signMap[startDay]

stats = append(stats, &types.ValidatorSignStatsInfo{
DateTime: uint64(endDay.Unix()),
DateTime: uint64(startDay.Unix()),
Expected: uint64(days[i].Count),
Actual: uint64(signs),
Actual: uint64(signCount),
})
}

Expand Down
8 changes: 6 additions & 2 deletions api/oasisscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ func main() {
//cache cron job
cr := cron.New(cron.WithChain(cron.DelayIfStillRunning(cron.DefaultLogger), cron.Recover(cron.DefaultLogger)))

////init
//logic.SignStatsCacheJob(context.Background(), svcCtx)
//init
go func() {
ctx := context.Background()
logc.Infof(ctx, "Initial signStatsCacheJob start...")
logic.SignStatsCacheJob(ctx, svcCtx)
}()

/** validator sign stats **/
_, err = cr.AddFunc("@every 10m", func() {
Expand Down
28 changes: 28 additions & 0 deletions job/model/blocksignaturemodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
SessionInsert(ctx context.Context, session sqlx.Session, data *BlockSignature) (sql.Result, error)
BatchSessionInsert(ctx context.Context, session sqlx.Session, data []*BlockSignature) (sql.Result, error)
CountSigns(ctx context.Context, signAddresses []string, from int64, startTime *time.Time, endTime *time.Time) (int64, error)
ValidatorSignStats(ctx context.Context, signAddresses []string, days int64) ([]*BlockCountDay, error)
FindBlocksByHeight(ctx context.Context, signAddresses []string, startHeight int64) ([]*BlockSignature, error)
RefreshBlockCountDaysView(ctx context.Context) error
FindBlockCountDays(ctx context.Context) ([]*BlockCountDay, error)
Expand Down Expand Up @@ -120,6 +121,33 @@ func (m *customBlockSignatureModel) CountSigns(ctx context.Context, signAddresse
}
}

func (m *customBlockSignatureModel) ValidatorSignStats(ctx context.Context, signAddresses []string, days int64) ([]*BlockCountDay, error) {
query := fmt.Sprintf("select DATE_TRUNC('day', timestamp) AS day, count(distinct height) from %s where validator_address in (", m.table)
vars := make([]interface{}, 0)
for i, signAddress := range signAddresses {
query += fmt.Sprintf("$%d,", i+1)
vars = append(vars, signAddress)
}
query = query[:len(query)-1] + ")"

paramIndex := len(signAddresses)
if days > 0 {
query += fmt.Sprintf(" and timestamp >= now() - interval '$%d days'", paramIndex+1)
vars = append(vars, days)
paramIndex = paramIndex + 1
}
var resp []*BlockCountDay
err := m.conn.QueryRowCtx(ctx, &resp, query, vars...)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

func (m *customBlockSignatureModel) FindBlocksByHeight(ctx context.Context, signAddresses []string, startHeight int64) ([]*BlockSignature, error) {
var resp []*BlockSignature
query := fmt.Sprintf("select %s from %s where validator_address in (", blockSignatureRows, m.table)
Expand Down

0 comments on commit 5a1bd00

Please sign in to comment.