Skip to content

Commit

Permalink
Log sync height info (#13)
Browse files Browse the repository at this point in the history
* Log sync heights of blockchain and storage node.

* Regen swagger doc.
  • Loading branch information
Argeric authored Mar 11, 2024
1 parent c6099ee commit 1b1b6e8
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func init() {
// dashboardHandler godoc
//
// @Summary Statistics dashboard
// @Description Query statistics dashboard includes `average uplink rate` and `storage base fee`
// @Description Query statistics dashboard includes `storage fee` and `log sync height`
// @Tags statistic
// @Produce json
// @Success 200 {object} api.BusinessError{Data=Dashboard}
Expand Down
2 changes: 2 additions & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
)

var (
ErrConfigNotFound = commonApi.NewBusinessError(1001, "Config not found", nil)
ErrAddressNotFound = commonApi.NewBusinessError(1002, "Account not found", nil)
ErrStatTypeNotSupported = commonApi.NewBusinessError(1003, "Stat type not supported", nil)
ErrStorageBaseFeeNotStat = commonApi.NewBusinessError(1004, "Storage base fee not stat", nil)
)
17 changes: 17 additions & 0 deletions api/stat_api.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package api

import (
"encoding/json"

commonApi "github.com/Conflux-Chain/go-conflux-util/api"
"github.com/gin-gonic/gin"
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
)

Expand All @@ -15,6 +18,19 @@ const (
)

func dashboard(_ *gin.Context) (interface{}, error) {
value, exist, err := db.ConfigStore.Get(store.KeyLogSyncInfo)
if err != nil {
return nil, commonApi.ErrInternal(err)
}
if !exist {
return nil, ErrConfigNotFound
}

var logSyncInfo stat.LogSyncInfo
if err := json.Unmarshal([]byte(value), &logSyncInfo); err != nil {
return nil, commonApi.ErrInternal(err)
}

submitStat, err := db.SubmitStatStore.LastByType(store.Day)
if err != nil {
return nil, commonApi.ErrInternal(err)
Expand All @@ -29,6 +45,7 @@ func dashboard(_ *gin.Context) (interface{}, error) {
}
result := Dashboard{
StorageBasicCost: storageBasicCost,
LogSyncInfo: logSyncInfo,
}

return result, nil
Expand Down
2 changes: 2 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/shopspring/decimal"
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
)

type PageParam struct {
Expand Down Expand Up @@ -111,6 +112,7 @@ type StorageBasicCost struct {

type Dashboard struct {
StorageBasicCost `json:"storageBasicCost"`
stat.LogSyncInfo `json:"logSyncInfo"`
}

type DataStatList struct {
Expand Down
2 changes: 2 additions & 0 deletions cmd/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func startStatService(*cobra.Command, []string) {

stSubmit := stat.MustNewStatSubmit(&cfg, dataCtx.DB, dataCtx.Eth, startTime)
go stSubmit.DoStat(ctx, &wg)
stSyncStatus := stat.MustNewSyncStatusStat(dataCtx.DB, dataCtx.L2Sdk)
go stSyncStatus.DoStat(ctx, &wg)

GracefulShutdown(&wg, cancel)
}
24 changes: 19 additions & 5 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const docTemplate = `{
"paths": {
"/statistic/dashboard": {
"get": {
"description": "Query statistics dashboard includes ` + "`" + `average uplink rate` + "`" + ` and ` + "`" + `storage base fee` + "`" + `",
"description": "Query statistics dashboard includes ` + "`" + `storage fee` + "`" + ` and ` + "`" + `log sync height` + "`" + `",
"produces": [
"application/json"
],
Expand Down Expand Up @@ -525,7 +525,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"basicCost": {
"type": "string"
"type": "number"
},
"tokenInfo": {
"$ref": "#/definitions/api.TokenInfo"
Expand All @@ -535,6 +535,9 @@ const docTemplate = `{
"api.Dashboard": {
"type": "object",
"properties": {
"logSyncInfo": {
"$ref": "#/definitions/stat.LogSyncInfo"
},
"storageBasicCost": {
"$ref": "#/definitions/api.StorageBasicCost"
}
Expand Down Expand Up @@ -578,10 +581,10 @@ const docTemplate = `{
"type": "object",
"properties": {
"baseFee": {
"type": "integer"
"type": "number"
},
"baseFeeTotal": {
"type": "integer"
"type": "number"
},
"statTime": {
"type": "string"
Expand Down Expand Up @@ -609,7 +612,7 @@ const docTemplate = `{
"type": "string"
},
"basicCostTotal": {
"type": "string"
"type": "number"
},
"decimals": {
"type": "integer"
Expand Down Expand Up @@ -802,6 +805,17 @@ const docTemplate = `{
},
"big.Int": {
"type": "object"
},
"stat.LogSyncInfo": {
"type": "object",
"properties": {
"l2LogSyncHeight": {
"type": "integer"
},
"logSyncHeight": {
"type": "integer"
}
}
}
}
}`
Expand Down
24 changes: 19 additions & 5 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"paths": {
"/statistic/dashboard": {
"get": {
"description": "Query statistics dashboard includes `average uplink rate` and `storage base fee`",
"description": "Query statistics dashboard includes `storage fee` and `log sync height`",
"produces": [
"application/json"
],
Expand Down Expand Up @@ -517,7 +517,7 @@
"type": "object",
"properties": {
"basicCost": {
"type": "string"
"type": "number"
},
"tokenInfo": {
"$ref": "#/definitions/api.TokenInfo"
Expand All @@ -527,6 +527,9 @@
"api.Dashboard": {
"type": "object",
"properties": {
"logSyncInfo": {
"$ref": "#/definitions/stat.LogSyncInfo"
},
"storageBasicCost": {
"$ref": "#/definitions/api.StorageBasicCost"
}
Expand Down Expand Up @@ -570,10 +573,10 @@
"type": "object",
"properties": {
"baseFee": {
"type": "integer"
"type": "number"
},
"baseFeeTotal": {
"type": "integer"
"type": "number"
},
"statTime": {
"type": "string"
Expand Down Expand Up @@ -601,7 +604,7 @@
"type": "string"
},
"basicCostTotal": {
"type": "string"
"type": "number"
},
"decimals": {
"type": "integer"
Expand Down Expand Up @@ -794,6 +797,17 @@
},
"big.Int": {
"type": "object"
},
"stat.LogSyncInfo": {
"type": "object",
"properties": {
"l2LogSyncHeight": {
"type": "integer"
},
"logSyncHeight": {
"type": "integer"
}
}
}
}
}
21 changes: 15 additions & 6 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ definitions:
api.CostInfo:
properties:
basicCost:
type: string
type: number
tokenInfo:
$ref: '#/definitions/api.TokenInfo'
type: object
api.Dashboard:
properties:
logSyncInfo:
$ref: '#/definitions/stat.LogSyncInfo'
storageBasicCost:
$ref: '#/definitions/api.StorageBasicCost'
type: object
Expand Down Expand Up @@ -44,9 +46,9 @@ definitions:
api.FeeStat:
properties:
baseFee:
type: integer
type: number
baseFeeTotal:
type: integer
type: number
statTime:
type: string
type: object
Expand All @@ -64,7 +66,7 @@ definitions:
address:
type: string
basicCostTotal:
type: string
type: number
decimals:
type: integer
name:
Expand Down Expand Up @@ -190,6 +192,13 @@ definitions:
type: object
big.Int:
type: object
stat.LogSyncInfo:
properties:
l2LogSyncHeight:
type: integer
logSyncHeight:
type: integer
type: object
info:
contact: {}
description: Use any http client to fetch data from the ZeroGStorage Scan.
Expand All @@ -198,8 +207,8 @@ info:
paths:
/statistic/dashboard:
get:
description: Query statistics dashboard includes `average uplink rate` and `storage
base fee`
description: Query statistics dashboard includes `storage fee` and `log sync
height`
produces:
- application/json
responses:
Expand Down
86 changes: 86 additions & 0 deletions stat/stat_sync_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package stat

import (
"context"
"encoding/json"
"sync"
"time"

viperutil "github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/zero-gravity-labs/zerog-storage-client/node"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"gorm.io/gorm"
)

type LogSyncInfoStat struct {
db *store.MysqlStore
l2Sdk *node.Client
interval time.Duration
}

func MustNewSyncStatusStat(db *store.MysqlStore, l2Sdk *node.Client) *LogSyncInfoStat {
var stat struct {
StatIntervalSyncStatus time.Duration `default:"1s"`
}
viperutil.MustUnmarshalKey("stat", &stat)

return &LogSyncInfoStat{
db: db,
l2Sdk: l2Sdk,
interval: stat.StatIntervalSyncStatus,
}
}

func (s *LogSyncInfoStat) DoStat(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
logrus.Info("Stat log sync info starting")

for {
select {
case <-ctx.Done():
return
default:
}

var submit store.Submit
err := s.db.Store.DB.Last(&submit).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
logrus.WithError(err).Info("No submit record to update log sync info")
time.Sleep(10 * time.Second)
continue
}
if err != nil {
logrus.WithError(err).Error("Failed to get submit record to update log sync info")
time.Sleep(10 * time.Second)
continue
}

zgStatus, err := s.l2Sdk.ZeroGStorage().GetStatus()
if err != nil {
logrus.WithError(err).Error("Failed to get zg status to update log sync info")
time.Sleep(10 * time.Second)
continue
}

status := LogSyncInfo{
LogSyncHeight: submit.BlockNumber,
L2LogSyncHeight: zgStatus.LogSyncHeight,
}
statusBytes, err := json.Marshal(status)
if err != nil {
logrus.WithError(err).Error("Failed to marshal log sync info")
continue
}

if err := s.db.ConfigStore.Upsert(store.KeyLogSyncInfo, string(statusBytes)); err != nil {
logrus.WithError(err).Error("Update log sync info error.")
time.Sleep(10 * time.Second)
continue
}

time.Sleep(s.interval)
}
}
6 changes: 6 additions & 0 deletions stat/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package stat

type LogSyncInfo struct {
LogSyncHeight uint64 `json:"logSyncHeight"`
L2LogSyncHeight uint64 `json:"l2LogSyncHeight"`
}
4 changes: 4 additions & 0 deletions store/store_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"gorm.io/gorm/clause"
)

const (
KeyLogSyncInfo = "LogSyncInfo"
)

type Config struct {
Name string `gorm:"size:32;primaryKey"`
Value string `gorm:"size:512"`
Expand Down

0 comments on commit 1b1b6e8

Please sign in to comment.