Skip to content

Commit c6099ee

Browse files
authored
Stat layer1 tx. (#12)
1 parent eb70719 commit c6099ee

File tree

9 files changed

+59
-70
lines changed

9 files changed

+59
-70
lines changed

api/api.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/gin-gonic/gin"
77
"github.com/openweb3/web3go"
88
"github.com/sirupsen/logrus"
9-
"github.com/swaggo/files"
10-
"github.com/swaggo/gin-swagger"
9+
swaggerFiles "github.com/swaggo/files"
10+
ginSwagger "github.com/swaggo/gin-swagger"
1111
nhContract "github.com/zero-gravity-labs/zerog-storage-scan/contract"
1212
"github.com/zero-gravity-labs/zerog-storage-scan/docs"
1313
"github.com/zero-gravity-labs/zerog-storage-scan/store"
@@ -16,11 +16,9 @@ import (
1616
const BasePath = "/api"
1717

1818
var (
19-
sdk *web3go.Client
20-
db *store.MysqlStore
21-
chargeToken *TokenInfo
22-
flowAddr string
23-
flowSubmitSig string
19+
sdk *web3go.Client
20+
db *store.MysqlStore
21+
chargeToken *TokenInfo
2422
)
2523

2624
func MustInit(client *web3go.Client, store *store.MysqlStore) {
@@ -49,8 +47,6 @@ func MustInit(client *web3go.Client, store *store.MysqlStore) {
4947
SubmitEventSignature string
5048
}
5149
viperutil.MustUnmarshalKey("flow", &flow)
52-
flowAddr = flow.Address
53-
flowSubmitSig = flow.SubmitEventSignature
5450
}
5551

5652
// @title ZeroGStorage Scan API

api/errors.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
)
66

77
var (
8-
ErrConfigNotFound = commonApi.NewBusinessError(1001, "Config not found", nil)
9-
ErrAddressNotFound = commonApi.NewBusinessError(1002, "Account not found", nil)
108
ErrStatTypeNotSupported = commonApi.NewBusinessError(1003, "Stat type not supported", nil)
119
ErrStorageBaseFeeNotStat = commonApi.NewBusinessError(1004, "Storage base fee not stat", nil)
1210
)

api/stat_api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
7979
for _, r := range records {
8080
list = append(list, TxStat{
8181
StatTime: r.StatTime,
82-
TxCount: r.FileCount,
83-
TxTotal: r.FileTotal,
82+
TxCount: r.TxCount,
83+
TxTotal: r.TxTotal,
8484
})
8585
}
8686
result["list"] = list

cmd/stat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package cmd
22

33
import (
44
"context"
5+
"sync"
6+
57
"github.com/Conflux-Chain/go-conflux-util/viper"
68
"github.com/sirupsen/logrus"
79
"github.com/spf13/cobra"
810
"github.com/zero-gravity-labs/zerog-storage-scan/stat"
9-
"sync"
1011
)
1112

1213
var (

stat/stat.go

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,22 @@ type StatConfig struct {
2626
}
2727

2828
type TimeRange struct {
29-
start *time.Time
30-
end *time.Time
29+
start time.Time
30+
end time.Time
3131
}
3232

3333
type BaseStat struct {
3434
Config *StatConfig
3535
DB *store.MysqlStore
3636
Sdk *web3go.Client
37-
StartTime *time.Time
37+
StartTime time.Time
3838
}
3939

40-
func (bs *BaseStat) defaultRangeStart() (*time.Time, error) {
41-
if bs.Config.BlockOnStatBegin == uint64(0) {
42-
return nil, errors.New("missing block from which the stat begin")
43-
}
44-
45-
block, err := bs.Sdk.Eth.BlockByNumber(types.BlockNumber(bs.Config.BlockOnStatBegin), false)
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
t := time.Unix(int64(block.Timestamp), 0).UTC()
51-
rangeStart := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
52-
53-
return &rangeStart, nil
54-
}
55-
56-
func (bs *BaseStat) calStatRange(rangeStart *time.Time, interval time.Duration) (*TimeRange, error) {
40+
func (bs *BaseStat) calStatRange(rangeStart time.Time, interval time.Duration) (*TimeRange, error) {
5741
rangeEnd := rangeStart.Add(interval)
5842
timeRange := TimeRange{
5943
start: rangeStart,
60-
end: &rangeEnd,
44+
end: rangeEnd,
6145
}
6246
return &timeRange, nil
6347
}
@@ -75,7 +59,7 @@ Range in Hour
7559
current time "2023-01-01 01:00:00", expect range start time "2023-01-01 00:00:00"
7660
current time "2023-01-01 00:00:00", expect range start time "2022-12-31 23:00:00"
7761
*/
78-
func (bs *BaseStat) calStatRangeStart(t *time.Time, statType string) (*time.Time, error) {
62+
func (bs *BaseStat) calStatRangeStart(t time.Time, statType string) (time.Time, error) {
7963
var rangeStart time.Time
8064
timeFormat := t.Format("2006-01-02 15:04:05")
8165

@@ -96,20 +80,20 @@ func (bs *BaseStat) calStatRangeStart(t *time.Time, statType string) (*time.Time
9680
rangeStart = rangeStart.Add(-time.Hour)
9781
}
9882
default:
99-
return nil, errors.Errorf("stat type %v not supported", statType)
83+
return time.Time{}, errors.Errorf("stat type %v not supported", statType)
10084
}
10185

102-
return &rangeStart, nil
86+
return rangeStart, nil
10387
}
10488

105-
func (bs *BaseStat) firstBlockAfterRangeEnd(rangeEnd *time.Time) (uint64, bool, error) {
89+
func (bs *BaseStat) firstBlockAfterRangeEnd(rangeEnd time.Time) (uint64, bool, error) {
10690
return bs.DB.FirstBlockAfterTime(rangeEnd)
10791
}
10892

10993
type Stat interface {
11094
nextTimeRange() (*TimeRange, error)
111-
firstBlockAfterRangeEnd(rangeEnd *time.Time) (uint64, bool, error)
112-
calculateStat(*TimeRange) error
95+
firstBlockAfterRangeEnd(rangeEnd time.Time) (uint64, bool, error)
96+
calculateStat(TimeRange) error
11397
}
11498

11599
type AbsStat struct {
@@ -140,7 +124,7 @@ func (as *AbsStat) DoStat(ctx context.Context, wg *sync.WaitGroup) {
140124
continue
141125
}
142126

143-
err = as.calculateStat(timeRange)
127+
err = as.calculateStat(*timeRange)
144128
if err != nil {
145129
logrus.WithError(err).Error("do stat")
146130
time.Sleep(time.Second * 10)
@@ -154,7 +138,7 @@ func (as *AbsStat) tryAcquireTimeRange() (*TimeRange, error) {
154138
if err != nil {
155139
return nil, err
156140
}
157-
if time.Now().UTC().Before(*timeRange.end) {
141+
if time.Now().UTC().Before(timeRange.end) {
158142
return nil, ErrTimeNotReach
159143
}
160144

@@ -186,7 +170,7 @@ func (as *AbsStat) interrupted(ctx context.Context) bool {
186170
return false
187171
}
188172

189-
func MustDefaultRangeStart(sdk *web3go.Client) *time.Time {
173+
func MustDefaultRangeStart(sdk *web3go.Client) time.Time {
190174
start, err := defaultRangeStart(sdk)
191175
if err != nil {
192176
logrus.WithError(err).Fatal("Failed to get default start time for stat task")
@@ -195,21 +179,21 @@ func MustDefaultRangeStart(sdk *web3go.Client) *time.Time {
195179
return start
196180
}
197181

198-
func defaultRangeStart(sdk *web3go.Client) (*time.Time, error) {
182+
func defaultRangeStart(sdk *web3go.Client) (time.Time, error) {
199183
config := StatConfig{}
200184
viper.MustUnmarshalKey("stat", &config)
201185

202186
if config.BlockOnStatBegin == uint64(0) {
203-
return nil, errors.New("missing block from which the stat begin")
187+
return time.Time{}, errors.New("missing block from which the stat begin")
204188
}
205189

206190
block, err := sdk.Eth.BlockByNumber(types.BlockNumber(config.BlockOnStatBegin), false)
207191
if err != nil {
208-
return nil, err
192+
return time.Time{}, err
209193
}
210194

211195
t := time.Unix(int64(block.Timestamp), 0).UTC()
212196
rangeStart := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
213197

214-
return &rangeStart, nil
198+
return rangeStart, nil
215199
}

stat/stat_submit.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type StatSubmit struct {
1414
statType string
1515
}
1616

17-
func MustNewStatSubmit(cfg *StatConfig, db *store.MysqlStore, sdk *web3go.Client, startTime *time.Time) *AbsStat {
17+
func MustNewStatSubmit(cfg *StatConfig, db *store.MysqlStore, sdk *web3go.Client, startTime time.Time) *AbsStat {
1818
baseStat := &BaseStat{
1919
Config: cfg,
2020
DB: db,
@@ -39,12 +39,12 @@ func (ts *StatSubmit) nextTimeRange() (*TimeRange, error) {
3939
return nil, err
4040
}
4141

42-
var nextRangeStart *time.Time
42+
var nextRangeStart time.Time
4343
if lastStat == nil {
4444
nextRangeStart = ts.StartTime
4545
} else {
4646
t := lastStat.StatTime.Add(store.Intervals[ts.statType])
47-
nextRangeStart = &t
47+
nextRangeStart = t
4848
}
4949

5050
timeRange, err := ts.calStatRange(nextRangeStart, store.Intervals[ts.statType])
@@ -55,7 +55,7 @@ func (ts *StatSubmit) nextTimeRange() (*TimeRange, error) {
5555
return timeRange, nil
5656
}
5757

58-
func (ts *StatSubmit) calculateStat(tr *TimeRange) error {
58+
func (ts *StatSubmit) calculateStat(tr TimeRange) error {
5959
stat, err := ts.statBasicRange(tr)
6060
if err != nil {
6161
return err
@@ -84,29 +84,31 @@ func (ts *StatSubmit) calculateStat(tr *TimeRange) error {
8484
})
8585
}
8686

87-
func (ts *StatSubmit) statBasicRange(tr *TimeRange) (*store.SubmitStat, error) {
88-
delta, err := ts.DB.SubmitStore.Count(*tr.start, *tr.end)
87+
func (ts *StatSubmit) statBasicRange(tr TimeRange) (*store.SubmitStat, error) {
88+
delta, err := ts.DB.SubmitStore.Count(tr.start, tr.end)
8989
if err != nil {
9090
return nil, err
9191
}
92-
total, err := ts.DB.SubmitStatStore.Sum(nil, tr.start, ts.statType)
92+
total, err := ts.DB.SubmitStatStore.Sum(time.Time{}, tr.start, ts.statType)
9393
if err != nil {
9494
return nil, err
9595
}
9696

9797
return &store.SubmitStat{
98-
StatTime: *tr.start,
98+
StatTime: tr.start,
9999
StatType: ts.statType,
100100
FileCount: delta.FileCount,
101101
FileTotal: total.FileCount + delta.FileCount,
102102
DataSize: delta.DataSize,
103103
DataTotal: total.DataSize + delta.DataSize,
104104
BaseFee: delta.BaseFee,
105105
BaseFeeTotal: total.BaseFee.Add(delta.BaseFee),
106+
TxCount: delta.TxCount,
107+
TxTotal: total.TxCount + delta.TxCount,
106108
}, nil
107109
}
108110

109-
func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType string, latestStat *store.SubmitStat) (*store.SubmitStat, error) {
111+
func (ts *StatSubmit) statRange(rangEnd time.Time, srcStatType, descStatType string, latestStat *store.SubmitStat) (*store.SubmitStat, error) {
110112
rangeStart, err := ts.calStatRangeStart(rangEnd, descStatType)
111113
if err != nil {
112114
return nil, err
@@ -116,7 +118,7 @@ func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType st
116118
if err != nil {
117119
return nil, err
118120
}
119-
destStat, err := ts.DB.SubmitStatStore.Sum(nil, rangeStart, descStatType)
121+
destStat, err := ts.DB.SubmitStatStore.Sum(time.Time{}, rangeStart, descStatType)
120122
if err != nil {
121123
return nil, err
122124
}
@@ -125,16 +127,19 @@ func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType st
125127
srcStat.FileCount += latestStat.FileCount
126128
srcStat.DataSize += latestStat.DataSize
127129
srcStat.BaseFee = srcStat.BaseFee.Add(latestStat.BaseFee)
130+
srcStat.TxCount += latestStat.TxCount
128131
}
129132

130133
return &store.SubmitStat{
131-
StatTime: *rangeStart,
134+
StatTime: rangeStart,
132135
StatType: descStatType,
133136
FileCount: srcStat.FileCount,
134137
FileTotal: destStat.FileCount + srcStat.FileCount,
135138
DataSize: srcStat.DataSize,
136139
DataTotal: destStat.DataSize + srcStat.DataSize,
137140
BaseFee: srcStat.BaseFee,
138141
BaseFeeTotal: destStat.BaseFee.Add(srcStat.BaseFee),
142+
TxCount: srcStat.TxCount,
143+
TxTotal: destStat.TxCount + srcStat.TxCount,
139144
}, nil
140145
}

store/store_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (bs *BlockStore) BlockHash(blockNumber uint64) (string, bool, error) {
6868
return blk.Hash, existed, nil
6969
}
7070

71-
func (bs *BlockStore) FirstBlockAfterTime(t *time.Time) (uint64, bool, error) {
71+
func (bs *BlockStore) FirstBlockAfterTime(t time.Time) (uint64, bool, error) {
7272
var blk Block
7373

7474
result := bs.DB.Where("block_time >= ?", t).Order("block_time asc").Limit(1).Find(&blk)

store/store_submit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ func (ss *SubmitStore) Pop(dbTx *gorm.DB, block uint64) error {
117117

118118
func (ss *SubmitStore) Count(startTime, endTime time.Time) (*SubmitStatResult, error) {
119119
var result SubmitStatResult
120-
err := ss.DB.Model(&Submit{}).Select(`count(submission_index) as file_count, IFNULL(sum(length), 0) as data_size,
121-
IFNULL(sum(fee), 0) as base_fee`).Where("block_time >= ? and block_time < ?", startTime, endTime).
122-
Find(&result).Error
120+
err := ss.DB.Model(&Submit{}).Select(`count(submission_index) as file_count,
121+
IFNULL(sum(length), 0) as data_size, IFNULL(sum(fee), 0) as base_fee, count(distinct tx_hash) as tx_count`).
122+
Where("block_time >= ? and block_time < ?", startTime, endTime).Find(&result).Error
123123
if err != nil {
124124
return nil, err
125125
}

store/store_submit_stat.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type SubmitStat struct {
4242
DataTotal uint64 `gorm:"not null;default:0" json:"dataTotal"` // Total Size of storage data by a certain time
4343
BaseFee decimal.Decimal `gorm:"type:decimal(65);not null;default:0" json:"baseFee"` // The base fee for storage
4444
BaseFeeTotal decimal.Decimal `gorm:"type:decimal(65);not null;default:0" json:"baseFeeTotal"` // The total base fee for storage
45+
TxCount uint64 `gorm:"not null;default:0" json:"txCount"` // Number of layer1 transaction in a specific time interval
46+
TxTotal uint64 `gorm:"not null;default:0" json:"txTotal"` // Total number of layer1 transaction by a certain time
4547
}
4648

4749
func (SubmitStat) TableName() string {
@@ -60,7 +62,7 @@ func newSubmitStatStore(db *gorm.DB) *SubmitStatStore {
6062

6163
func (t *SubmitStatStore) LastByType(statType string) (*SubmitStat, error) {
6264
var submitStat SubmitStat
63-
err := t.Store.DB.Where("stat_type = ?", statType).Order("stat_time asc").Last(&submitStat).Error
65+
err := t.Store.DB.Where("stat_type = ?", statType).Order("stat_time desc").Last(&submitStat).Error
6466
if errors.Is(err, gorm.ErrRecordNotFound) {
6567
return nil, nil
6668
}
@@ -74,22 +76,25 @@ type SubmitStatResult struct {
7476
FileCount uint64
7577
DataSize uint64
7678
BaseFee decimal.Decimal
79+
TxCount uint64
7780
}
7881

79-
func (t *SubmitStatStore) Sum(startTime, endTime *time.Time, statType string) (*SubmitStatResult, error) {
80-
if startTime == nil && endTime == nil {
82+
func (t *SubmitStatStore) Sum(startTime, endTime time.Time, statType string) (*SubmitStatResult, error) {
83+
nilTime := time.Time{}
84+
if startTime == nilTime && endTime == nilTime {
8185
return nil, errors.New("At least provide one parameter for startTime and endTime")
8286
}
8387

8488
db := t.DB.Model(&SubmitStat{}).Select(`IFNULL(sum(file_count), 0) as file_count,
85-
IFNULL(sum(data_size), 0) as data_size, IFNULL(sum(base_fee), 0) as base_fee`)
86-
if startTime != nil && endTime != nil {
89+
IFNULL(sum(data_size), 0) as data_size, IFNULL(sum(base_fee), 0) as base_fee,
90+
IFNULL(sum(tx_count), 0) as tx_count`)
91+
if startTime != nilTime && endTime != nilTime {
8792
db = db.Where("stat_type = ? and stat_time >= ? and stat_time < ?", statType, startTime, endTime)
8893
}
89-
if startTime != nil && endTime == nil {
94+
if startTime != nilTime && endTime == nilTime {
9095
db = db.Where("stat_type = ? and stat_time >= ?", statType, startTime)
9196
}
92-
if startTime == nil && endTime != nil {
97+
if startTime == nilTime && endTime != nilTime {
9398
db = db.Where("stat_type = ? and stat_time < ?", statType, endTime)
9499
}
95100

0 commit comments

Comments
 (0)