Skip to content

Commit

Permalink
Drop db domains including erc20transfer, tx, coststat and txstat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Argeric committed Feb 28, 2024
1 parent b410f47 commit 2ebdd9e
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 447 deletions.
36 changes: 9 additions & 27 deletions api/stat_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,37 @@ package api

import (
"encoding/json"
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"gorm.io/gorm"
"strconv"
)

// TO add logic when refactor submit db domain
func dashboard(c *gin.Context) (interface{}, error) {
dataUplinkRate, exist, err := db.ConfigStore.Get(store.CfgDataUplinkRate)
if err != nil {
return nil, commonApi.ErrInternal(err)
}
if !exist {
return nil, ErrConfigNotFound
}

costStat, err := db.CostStatStore.LastByType(stat.Day)
if err != nil {
return nil, err
}
if costStat == nil {
return nil, errors.New("Storage basic cost not stat.")
}

storageBasicCost := StorageBasicCost{
TokenInfo: *chargeToken,
BasicCostTotal: strconv.FormatUint(costStat.BasicCostTotal, 10),
TokenInfo: *chargeToken,
}
result := Dashboard{
AverageUplinkRate: dataUplinkRate,
StorageBasicCost: storageBasicCost,
StorageBasicCost: storageBasicCost,
}

return result, nil
}

// TO add logic when refactor submit db domain
func listTxStat(c *gin.Context) (interface{}, error) {
return queryStat(c, db.DB.Model(&store.TxStat{}), new([]store.TxStat))
return nil, nil
}

func listDataStat(c *gin.Context) (interface{}, error) {
return queryStat(c, db.DB.Model(&store.SubmitStat{}), new([]store.SubmitStat))
}

// TO add logic when refactor submit db domain
func listBasicCostStat(c *gin.Context) (interface{}, error) {
return queryStat(c, db.DB.Model(&store.CostStat{}), new([]store.CostStat))
return nil, nil
}

func queryStat(c *gin.Context, dbRaw *gorm.DB, records interface{}) (interface{}, error) {
Expand Down
22 changes: 8 additions & 14 deletions api/tx_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package api

import (
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"gorm.io/gorm"
"strconv"
"strings"
"time"
)

// TO add logic when refactor submit db domain
func listTx(c *gin.Context) (interface{}, error) {
var param listTxParam
if err := c.ShouldBind(&param); err != nil {
Expand Down Expand Up @@ -52,23 +53,16 @@ func listTx(c *gin.Context) (interface{}, error) {
if err != nil {
return nil, err
}
txMap, err := db.MapTxHashToTx(txHashes)
if err != nil {
return nil, err
}

storageTxs := make([]StorageTx, 0)
for _, submit := range *submits {
tx := txMap[submit.TxHash]
storageTx := StorageTx{
TxSeq: submit.SubmissionIndex,
BlockNum: submit.BlockNumber,
TxHash: "0x" + submit.TxHash,
RootHash: "0x" + submit.RootHash,
Address: addrMap[submit.SenderId].Address,
Method: "submit",
Status: tx.Status,
Timestamp: tx.CreatedAt.Unix(),
TxSeq: submit.SubmissionIndex,
BlockNum: submit.BlockNumber,
TxHash: "0x" + submit.TxHash,
RootHash: "0x" + submit.RootHash,
Address: addrMap[submit.SenderId].Address,
Method: "submit",
}
storageTxs = append(storageTxs, storageTx)
}
Expand Down
10 changes: 0 additions & 10 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,7 @@ type Dashboard struct {
AverageUplinkRate string `json:"averageUplinkRate"`
}

type TxStatList struct {
Total int64 `json:"total"`
List []store.TxStat `json:"list"`
}

type DataStatList struct {
Total int64 `json:"total"`
List []store.SubmitStat `json:"list"`
}

type BasicCostStatList struct {
Total int64 `json:"total"`
List []store.CostStat `json:"list"`
}
8 changes: 2 additions & 6 deletions cmd/data_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"github.com/Conflux-Chain/go-conflux-util/store/mysql"
"github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/zero-gravity-labs/zerog-storage-client/node"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
providers "github.com/openweb3/go-rpc-provider/provider_wrapper"
"github.com/openweb3/web3go"
"github.com/sirupsen/logrus"
"github.com/zero-gravity-labs/zerog-storage-client/node"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -43,11 +43,7 @@ var migrationModels = []interface{}{
&store.Address{},
&store.Block{},
&store.Submit{},
&store.Tx{},
&store.TxStat{},
&store.SubmitStat{},
&store.Erc20Transfer{},
&store.CostStat{},
&store.Config{},
}

Expand Down
42 changes: 7 additions & 35 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,29 @@ type MysqlStore struct {
*AddressStore
*BlockStore
*SubmitStore
*TxStore
*TxStatStore
*SubmitStatStore
*Erc20TransferStore
*CostStatStore
*ConfigStore
}

func MustNewStore(db *gorm.DB) *MysqlStore {
return &MysqlStore{
Store: mysql.NewStore(db),
AddressStore: newAddressStore(db),
BlockStore: newBlockStore(db),
SubmitStore: newSubmitStore(db),
TxStore: newTxStore(db),
TxStatStore: newTxStatStore(db),
SubmitStatStore: newSubmitStatStore(db),
Erc20TransferStore: newErc20TransferStore(db),
CostStatStore: newCostStatStore(db),
ConfigStore: newConfigStore(db),
Store: mysql.NewStore(db),
AddressStore: newAddressStore(db),
BlockStore: newBlockStore(db),
SubmitStore: newSubmitStore(db),
SubmitStatStore: newSubmitStatStore(db),
ConfigStore: newConfigStore(db),
}
}

func (ms *MysqlStore) Push(block *Block, txs []*Tx, transfers []*Erc20Transfer, submits []*Submit) error {
func (ms *MysqlStore) Push(block *Block, submits []*Submit) error {

return ms.Store.DB.Transaction(func(dbTx *gorm.DB) error {
// save blocks
if err := ms.BlockStore.Add(dbTx, block); err != nil {
return errors.WithMessage(err, "failed to save block")
}

// save txs
if len(txs) > 0 {
if err := ms.TxStore.Add(dbTx, txs); err != nil {
return errors.WithMessage(err, "failed to save txs")
}
}

// save transfers
if len(transfers) > 0 {
if err := ms.Erc20TransferStore.Add(dbTx, transfers); err != nil {
return errors.WithMessage(err, "failed to save transfers")
}
}

// save flow submits
if len(submits) > 0 {
if err := ms.SubmitStore.Add(dbTx, submits); err != nil {
Expand All @@ -84,15 +62,9 @@ func (ms *MysqlStore) Pop(block uint64) error {
if err := ms.BlockStore.Pop(dbTx, block); err != nil {
return errors.WithMessage(err, "failed to remove block")
}
if err := ms.TxStore.Pop(dbTx, block); err != nil {
return errors.WithMessage(err, "failed to remove txs")
}
if err := ms.SubmitStore.Pop(dbTx, block); err != nil {
return errors.WithMessage(err, "failed to remove flow submits")
}
if err := ms.Erc20TransferStore.Pop(dbTx, block); err != nil {
return errors.WithMessage(err, "failed to remove flow transfers")
}
return nil
})
}
Expand Down
78 changes: 0 additions & 78 deletions store/store_cost_stat.go

This file was deleted.

Loading

0 comments on commit 2ebdd9e

Please sign in to comment.