Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor store coce #8

Merged
merged 7 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions api/stat_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package api

import (
"encoding/json"
"strconv"

commonApi "github.com/Conflux-Chain/go-conflux-util/api"
"github.com/gin-gonic/gin"
"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"
)

type Type int
Expand Down Expand Up @@ -93,7 +94,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
list := make([]DataStat, 0)
for _, stat := range *records {
list = append(list, DataStat{
StatTime: stat.StatTime,
StatTime: &stat.StatTime,
FileCount: stat.FileCount,
FileTotal: stat.FileTotal,
DataSize: stat.DataSize,
Expand All @@ -105,7 +106,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
list := make([]TxStat, 0)
for _, stat := range *records {
list = append(list, TxStat{
StatTime: stat.StatTime,
StatTime: &stat.StatTime,
TxCount: stat.FileCount,
TxTotal: stat.FileTotal,
})
Expand All @@ -115,7 +116,7 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
list := make([]FeeStat, 0)
for _, stat := range *records {
list = append(list, FeeStat{
StatTime: stat.StatTime,
StatTime: &stat.StatTime,
BaseFee: stat.BaseFee,
BaseFeeTotal: stat.BaseFeeTotal,
})
Expand Down
7 changes: 4 additions & 3 deletions stat/stat_submit.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package stat

import (
"time"

"github.com/openweb3/web3go"
"github.com/pkg/errors"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"gorm.io/gorm"
"time"
)

type StatSubmit struct {
Expand Down Expand Up @@ -94,7 +95,7 @@ func (ts *StatSubmit) statBasicRange(tr *TimeRange) (*store.SubmitStat, error) {
}

return &store.SubmitStat{
StatTime: tr.start,
StatTime: *tr.start,
StatType: ts.statType,
FileCount: delta.FileCount,
FileTotal: total.FileCount + delta.FileCount,
Expand Down Expand Up @@ -127,7 +128,7 @@ func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType st
}

return &store.SubmitStat{
StatTime: rangeStart,
StatTime: *rangeStart,
StatType: descStatType,
FileCount: srcStat.FileCount,
FileTotal: destStat.FileCount + srcStat.FileCount,
Expand Down
13 changes: 0 additions & 13 deletions store/eth_data.go

This file was deleted.

11 changes: 5 additions & 6 deletions store/store_address.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package store

import (
"time"

"github.com/Conflux-Chain/go-conflux-util/store/mysql"
"gorm.io/gorm"
"time"
)

type Address struct {
Expand All @@ -26,7 +27,7 @@ func newAddressStore(db *gorm.DB) *AddressStore {
}
}

func (as *AddressStore) Add(dbTx *gorm.DB, address string, blockTime time.Time) (uint64, error) {
func (as *AddressStore) Add(address string, blockTime time.Time) (uint64, error) {
var addr Address
existed, err := as.Store.Exists(&addr, "address = ?", address) //TODO using LRU cache for improving the query performance
if err != nil {
Expand All @@ -40,10 +41,8 @@ func (as *AddressStore) Add(dbTx *gorm.DB, address string, blockTime time.Time)
Address: address,
BlockTime: blockTime,
}
if dbTx == nil {
dbTx = as.Store.DB
}
if err := dbTx.Create(&addr).Error; err != nil {

if err := as.DB.Create(&addr).Error; err != nil {
return 0, err
}

Expand Down
5 changes: 3 additions & 2 deletions store/store_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package store

import (
"database/sql"
"time"

"github.com/Conflux-Chain/go-conflux-util/store/mysql"
"github.com/openweb3/web3go/types"
"gorm.io/gorm"
"time"
)

type Block struct {
BlockNumber uint64 `gorm:"primaryKey;autoIncrement:false"`
Hash string `gorm:"size:66;not null"`
BlockTime time.Time `gorm:"not null;index:idx_block_time,sort:desc"`
BlockTime time.Time `gorm:"not null;index:idx_block_time"`
}

func NewBlock(data *types.Block) *Block {
Expand Down
32 changes: 19 additions & 13 deletions store/store_submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ package store

import (
"encoding/json"
"math/big"
"time"

"github.com/Conflux-Chain/go-conflux-util/store/mysql"
"github.com/ethereum/go-ethereum/common"
"github.com/openweb3/web3go/types"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
"github.com/zero-gravity-labs/zerog-storage-client/contract"
"gorm.io/gorm"
"math/big"
"time"
)

// TODO one table for overall submissions and another table for submissions of specific account.

type Submit struct {
ID uint64 `gorm:"index:idx_sender_id,priority:2"`
BlockNumber uint64 `gorm:"not null;index:idx_bn"`
BlockTime time.Time `gorm:"not null;index:idx_blockTime,sort:desc"`
TxHash string `gorm:"size:66;not null;index:idx_hash"`

SubmissionIndex uint64 `gorm:"not null"`
RootHash string `gorm:"size:66;index:idx_root"`
Sender string `gorm:"-"`
SenderID uint64 `gorm:"not null;index:idx_sender_id,priority:1"`
Length uint64 `gorm:"not null"`
Finalized bool `gorm:"default:false"`
Value decimal.Decimal `gorm:"type:varchar(78);not null"`
BlockNumber uint64 `gorm:"not null"`
BlockTime time.Time `gorm:"not null"`
TxHash string `gorm:"size:66;not null"`

// TODO use as primary key?
SubmissionIndex uint64 `gorm:"not null;index:idx_seq"`
RootHash string `gorm:"size:66;index:idx_root"`
Sender string `gorm:"-"`
SenderID uint64 `gorm:"not null;index:idx_sender_id,priority:1"`
Length uint64 `gorm:"not null"`
// TODO supports more status, including L1 and L2 status
Finalized bool `gorm:"default:false"`
// TODO change to Fee. how about use decimal(64,18) as sql type?
Value decimal.Decimal `gorm:"type:varchar(78);not null"`

Extra []byte `gorm:"type:mediumText"` // json field
}
Expand Down
32 changes: 17 additions & 15 deletions store/store_submit_stat.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package store

import (
"time"

"github.com/Conflux-Chain/go-conflux-util/store/mysql"
"github.com/pkg/errors"
"gorm.io/gorm"
"time"
)

type SubmitStat struct {
ID uint64 `gorm:"primaryKey" json:"-"`
StatTime *time.Time `gorm:"not null;index:idx_statTime_statType,unique,priority:1" json:"statTime"`
StatType string `gorm:"type:char(3);not null;index:idx_statTime_statType,unique,priority:2" json:"-"`
FileCount uint64 `gorm:"not null;default:0" json:"fileCount"` // Number of files in a specific time interval
FileTotal uint64 `gorm:"not null;default:0" json:"fileTotal"` // Total number of files by a certain time
DataSize uint64 `gorm:"not null;default:0" json:"dataSize"` // Size of storage data in a specific time interval
DataTotal uint64 `gorm:"not null;default:0" json:"dataTotal"` // Total Size of storage data by a certain time
BaseFee uint64 `gorm:"not null;default:0" json:"baseFee"` // The base fee for storage
BaseFeeTotal uint64 `gorm:"not null;default:0" json:"baseFeeTotal"` // The total base fee for storage
ID uint64 `json:"-"`
StatType string `gorm:"size:4;not null;uniqueIndex:idx_statType_statTime,priority:1" json:"-"`
StatTime time.Time `gorm:"not null;uniqueIndex:idx_statType_statTime,priority:2" json:"statTime"`
FileCount uint64 `gorm:"not null;default:0" json:"fileCount"` // Number of files in a specific time interval
FileTotal uint64 `gorm:"not null;default:0" json:"fileTotal"` // Total number of files by a certain time
DataSize uint64 `gorm:"not null;default:0" json:"dataSize"` // Size of storage data in a specific time interval
DataTotal uint64 `gorm:"not null;default:0" json:"dataTotal"` // Total Size of storage data by a certain time
// TODO not enough for blockchain value of decimals 18.
BaseFee uint64 `gorm:"not null;default:0" json:"baseFee"` // The base fee for storage
BaseFeeTotal uint64 `gorm:"not null;default:0" json:"baseFeeTotal"` // The total base fee for storage
}

func (SubmitStat) TableName() string {
Expand All @@ -35,7 +37,7 @@ func newSubmitStatStore(db *gorm.DB) *SubmitStatStore {

func (t *SubmitStatStore) LastByType(statType string) (*SubmitStat, error) {
var submitStat SubmitStat
err := t.Store.DB.Where("stat_type = ?", statType).Last(&submitStat).Error
err := t.Store.DB.Where("stat_type = ?", statType).Order("stat_time asc").Last(&submitStat).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
Expand All @@ -59,13 +61,13 @@ func (t *SubmitStatStore) Sum(startTime, endTime *time.Time, statType string) (*
db := t.DB.Model(&SubmitStat{}).Select(`IFNULL(sum(file_count), 0) as file_count,
IFNULL(sum(data_size), 0) as data_size, IFNULL(sum(base_fee), 0) as base_fee`)
if startTime != nil && endTime != nil {
db = db.Where("stat_time >= ? and stat_time < ? and stat_type = ?", startTime, endTime, statType)
db = db.Where("stat_type = ? and stat_time >= ? and stat_time < ?", statType, startTime, endTime)
}
if startTime != nil && endTime == nil {
db = db.Where("stat_time >= ? and stat_type = ?", startTime, statType)
db = db.Where("stat_type = ? and stat_time >= ?", statType, startTime)
}
if startTime == nil && endTime != nil {
db = db.Where("stat_time < ? and stat_type = ?", endTime, statType)
db = db.Where("stat_type = ? and stat_time < ?", statType, endTime)
}

var sum SubmitStatResult
Expand All @@ -82,5 +84,5 @@ func (t *SubmitStatStore) Add(dbTx *gorm.DB, submitStat []*SubmitStat) error {
}

func (t *SubmitStatStore) Del(dbTx *gorm.DB, submitStat *SubmitStat) error {
return dbTx.Where("stat_time = ? and stat_type = ?", submitStat.StatTime, submitStat.StatType).Delete(&SubmitStat{}).Error
return dbTx.Where("stat_type = ? and stat_time = ?", submitStat.StatType, submitStat.StatTime).Delete(&SubmitStat{}).Error
}
22 changes: 13 additions & 9 deletions sync/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@ package sync

import (
"context"

set "github.com/deckarep/golang-set"
"github.com/ethereum/go-ethereum/common"
"github.com/openweb3/go-rpc-provider"
"github.com/openweb3/web3go"
"github.com/openweb3/web3go/types"
"github.com/pkg/errors"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
)

var (
ErrNotFound = errors.New("not found")
ErrChainReorged = errors.New("chain re-orged")
)

type EthData struct {
Number uint64
Block *types.Block
Receipts map[common.Hash]*types.Receipt
Logs []types.Log
}

func isTxExecutedInBlock(tx *types.TransactionDetail, receipt *types.Receipt) bool {
return tx != nil && receipt.Status != nil && *receipt.Status < 2
}

func getEthDataByReceipts(w3c *web3go.Client, blockNumber uint64) (*store.EthData, error) {
func getEthDataByReceipts(w3c *web3go.Client, blockNumber uint64) (*EthData, error) {
// get block
block, err := w3c.Eth.BlockByNumber(types.BlockNumber(blockNumber), true)
if err != nil {
Expand Down Expand Up @@ -48,13 +55,10 @@ func getEthDataByReceipts(w3c *web3go.Client, blockNumber uint64) (*store.EthDat
}
for i := 0; i < len(blockTxs); i++ {
tx := blockTxs[i]
var receipt *types.Receipt
receipt = &blockReceipts[i]
receipt := &blockReceipts[i]

// check re-org
switch {
case receipt == nil: // receipt shouldn't be nil unless chain re-org
return nil, errors.WithMessage(ErrChainReorged, "tx receipt nil")
case receipt.BlockHash != block.Hash:
return nil, errors.WithMessagef(ErrChainReorged, "receipt block hash mismatch, rcptBlkHash %v, blkHash %v",
receipt.BlockHash, block.Hash)
Expand All @@ -68,10 +72,10 @@ func getEthDataByReceipts(w3c *web3go.Client, blockNumber uint64) (*store.EthDat
txReceipts[tx.Hash] = receipt
}

return &store.EthData{Number: blockNumber, Block: block, Receipts: txReceipts}, nil
return &EthData{Number: blockNumber, Block: block, Receipts: txReceipts}, nil
}

func getEthDataByLogs(w3c *web3go.Client, blockNumber uint64, flowAddr common.Address, flowSubmitSig common.Hash) (*store.EthData, error) {
func getEthDataByLogs(w3c *web3go.Client, blockNumber uint64, flowAddr common.Address, flowSubmitSig common.Hash) (*EthData, error) {
// get block
block, err := w3c.Eth.BlockByNumber(types.BlockNumber(blockNumber), true)
if err != nil {
Expand Down Expand Up @@ -111,7 +115,7 @@ func getEthDataByLogs(w3c *web3go.Client, blockNumber uint64, flowAddr common.Ad
logs = append(logs, log)
}

return &store.EthData{Number: blockNumber, Block: block, Logs: logs}, nil
return &EthData{Number: blockNumber, Block: block, Logs: logs}, nil
}

func batchGetFlowSubmits(w3c *web3go.Client, blockFrom, blockTo uint64, flowAddr common.Address,
Expand Down
9 changes: 5 additions & 4 deletions sync/catchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package sync

import (
"context"
"math/big"
"strings"
"time"

viperutil "github.com/Conflux-Chain/go-conflux-util/viper"
"github.com/ethereum/go-ethereum/common"
"github.com/openweb3/web3go"
Expand All @@ -10,9 +14,6 @@ import (
"github.com/sirupsen/logrus"
nhContract "github.com/zero-gravity-labs/zerog-storage-scan/contract"
"github.com/zero-gravity-labs/zerog-storage-scan/store"
"math/big"
"strings"
"time"
)

type CatchupSyncer struct {
Expand Down Expand Up @@ -215,7 +216,7 @@ func (s *CatchupSyncer) convertSubmits(logs []types.Log, blockNum2TimeMap map[ui
return nil, err
}

senderId, err := s.db.AddressStore.Add(nil, submit.Sender, blockTime)
senderId, err := s.db.AddressStore.Add(submit.Sender, blockTime)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading