Skip to content

Commit af79bcf

Browse files
authored
Refactor submit domain (#9)
* Refactor submit domain. Add address submit domain. * Extract a separate class to define address submit.
1 parent d586dae commit af79bcf

File tree

10 files changed

+129
-88
lines changed

10 files changed

+129
-88
lines changed

api/stat_api.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"encoding/json"
5-
"strconv"
65

76
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
87
"github.com/gin-gonic/gin"
@@ -20,7 +19,7 @@ const (
2019
FeeStatType
2120
)
2221

23-
func dashboard(c *gin.Context) (interface{}, error) {
22+
func dashboard(_ *gin.Context) (interface{}, error) {
2423
submitStat, err := db.SubmitStatStore.LastByType(stat.Day)
2524
if err != nil {
2625
return nil, commonApi.ErrInternal(err)
@@ -31,7 +30,7 @@ func dashboard(c *gin.Context) (interface{}, error) {
3130

3231
storageBasicCost := StorageBasicCost{
3332
TokenInfo: *chargeToken,
34-
BasicCostTotal: strconv.FormatUint(submitStat.BaseFeeTotal, 10),
33+
BasicCostTotal: submitStat.BaseFeeTotal,
3534
}
3635
result := Dashboard{
3736
StorageBasicCost: storageBasicCost,
@@ -92,33 +91,33 @@ func getSubmitStatByType(c *gin.Context, t Type) (interface{}, error) {
9291
switch t {
9392
case StorageStatType:
9493
list := make([]DataStat, 0)
95-
for _, stat := range *records {
94+
for _, r := range *records {
9695
list = append(list, DataStat{
97-
StatTime: &stat.StatTime,
98-
FileCount: stat.FileCount,
99-
FileTotal: stat.FileTotal,
100-
DataSize: stat.DataSize,
101-
DataTotal: stat.DataTotal,
96+
StatTime: r.StatTime,
97+
FileCount: r.FileCount,
98+
FileTotal: r.FileTotal,
99+
DataSize: r.DataSize,
100+
DataTotal: r.DataTotal,
102101
})
103102
}
104103
result["list"] = list
105104
case TxStatType:
106105
list := make([]TxStat, 0)
107-
for _, stat := range *records {
106+
for _, r := range *records {
108107
list = append(list, TxStat{
109-
StatTime: &stat.StatTime,
110-
TxCount: stat.FileCount,
111-
TxTotal: stat.FileTotal,
108+
StatTime: r.StatTime,
109+
TxCount: r.FileCount,
110+
TxTotal: r.FileTotal,
112111
})
113112
}
114113
result["list"] = list
115114
case FeeStatType:
116115
list := make([]FeeStat, 0)
117-
for _, stat := range *records {
116+
for _, r := range *records {
118117
list = append(list, FeeStat{
119-
StatTime: &stat.StatTime,
120-
BaseFee: stat.BaseFee,
121-
BaseFeeTotal: stat.BaseFeeTotal,
118+
StatTime: r.StatTime,
119+
BaseFee: r.BaseFee,
120+
BaseFeeTotal: r.BaseFeeTotal,
122121
})
123122
}
124123
result["list"] = list

api/tx_api.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package api
33
import (
44
"encoding/hex"
55
"encoding/json"
6+
"strconv"
7+
"strings"
8+
69
commonApi "github.com/Conflux-Chain/go-conflux-util/api"
710
"github.com/ethereum/go-ethereum/common"
811
"github.com/gin-gonic/gin"
912
"github.com/pkg/errors"
1013
"github.com/sirupsen/logrus"
1114
"github.com/zero-gravity-labs/zerog-storage-scan/store"
1215
"gorm.io/gorm"
13-
"strconv"
14-
"strings"
1516
)
1617

1718
func listTx(c *gin.Context) (interface{}, error) {
@@ -61,12 +62,10 @@ func listTx(c *gin.Context) (interface{}, error) {
6162
RootHash: submit.RootHash,
6263
Address: addrMap[submit.SenderID].Address,
6364
Method: "submit",
65+
Status: submit.Status,
6466
Timestamp: submit.BlockTime.Unix(),
6567
DataSize: submit.Length,
66-
BaseFee: submit.Value,
67-
}
68-
if submit.Finalized {
69-
storageTx.Status = 1
68+
BaseFee: submit.Fee,
7069
}
7170
storageTxs = append(storageTxs, storageTx)
7271
}
@@ -105,18 +104,16 @@ func getTxBrief(c *gin.Context) (interface{}, error) {
105104
From: addrMap[submit.SenderID].Address,
106105
Method: "submit",
107106
RootHash: submit.RootHash,
107+
Status: submit.Status,
108108
DataSize: submit.Length,
109109
CostInfo: &CostInfo{
110110
TokenInfo: *chargeToken,
111-
BasicCost: submit.Value.String(),
111+
BasicCost: submit.Fee,
112112
},
113113
BlockNumber: submit.BlockNumber,
114114
TxHash: submit.TxHash,
115115
Timestamp: uint64(submit.BlockTime.Unix()),
116116
}
117-
if submit.Finalized {
118-
result.Status = 1
119-
}
120117

121118
hash := common.HexToHash(submit.TxHash)
122119
tx, err := sdk.Eth.TransactionByHash(hash)
@@ -158,7 +155,7 @@ func getTxDetail(c *gin.Context) (interface{}, error) {
158155
return nil, errors.Errorf("Unmarshal submit extra error, txSeq %v", *param.TxSeq)
159156
}
160157

161-
var nodes []SubmissionNode
158+
nodes := make([]SubmissionNode, 0)
162159
for _, n := range extra.Submission.Nodes {
163160
nodes = append(nodes, SubmissionNode{
164161
Root: "0x" + hex.EncodeToString(n.Root[:]),

api/types.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package api
22

33
import (
4-
"github.com/shopspring/decimal"
54
"math/big"
65
"strings"
76
"time"
7+
8+
"github.com/shopspring/decimal"
89
)
910

1011
type PageParam struct {
@@ -56,7 +57,7 @@ type TokenInfo struct {
5657

5758
type CostInfo struct {
5859
TokenInfo `json:"tokenInfo"`
59-
BasicCost string `json:"basicCost"`
60+
BasicCost decimal.Decimal `json:"basicCost"`
6061
}
6162

6263
type SubmissionNode struct {
@@ -100,7 +101,7 @@ type TxDetail struct {
100101

101102
type StorageBasicCost struct {
102103
TokenInfo
103-
BasicCostTotal string `json:"basicCostTotal"`
104+
BasicCostTotal decimal.Decimal `json:"basicCostTotal"`
104105
}
105106

106107
type Dashboard struct {
@@ -123,21 +124,21 @@ type FeeStatList struct {
123124
}
124125

125126
type DataStat struct {
126-
StatTime *time.Time `json:"statTime"`
127-
FileCount uint64 `json:"fileCount"`
128-
FileTotal uint64 `json:"fileTotal"`
129-
DataSize uint64 `json:"dataSize"`
130-
DataTotal uint64 `json:"dataTotal"`
127+
StatTime time.Time `json:"statTime"`
128+
FileCount uint64 `json:"fileCount"`
129+
FileTotal uint64 `json:"fileTotal"`
130+
DataSize uint64 `json:"dataSize"`
131+
DataTotal uint64 `json:"dataTotal"`
131132
}
132133

133134
type TxStat struct {
134-
StatTime *time.Time `json:"statTime"`
135-
TxCount uint64 `json:"txCount"`
136-
TxTotal uint64 `json:"txTotal"`
135+
StatTime time.Time `json:"statTime"`
136+
TxCount uint64 `json:"txCount"`
137+
TxTotal uint64 `json:"txTotal"`
137138
}
138139

139140
type FeeStat struct {
140-
StatTime *time.Time `json:"statTime"`
141-
BaseFee uint64 `json:"baseFee"`
142-
BaseFeeTotal uint64 `json:"baseFeeTotal"`
141+
StatTime time.Time `json:"statTime"`
142+
BaseFee decimal.Decimal `json:"baseFee"`
143+
BaseFeeTotal decimal.Decimal `json:"baseFeeTotal"`
143144
}

cmd/data_context.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ package cmd
22

33
import (
44
"context"
5+
"os"
6+
"os/signal"
7+
"sync"
8+
"syscall"
9+
"time"
10+
511
"github.com/Conflux-Chain/go-conflux-util/store/mysql"
612
"github.com/Conflux-Chain/go-conflux-util/viper"
713
providers "github.com/openweb3/go-rpc-provider/provider_wrapper"
814
"github.com/openweb3/web3go"
915
"github.com/sirupsen/logrus"
1016
"github.com/zero-gravity-labs/zerog-storage-client/node"
1117
"github.com/zero-gravity-labs/zerog-storage-scan/store"
12-
"os"
13-
"os/signal"
14-
"sync"
15-
"syscall"
16-
"time"
1718
)
1819

1920
// DataContext context to hold sdk clients for blockchain interoperation.
@@ -44,6 +45,7 @@ var migrationModels = []interface{}{
4445
&store.Block{},
4546
&store.Config{},
4647
&store.Submit{},
48+
&store.AddressSubmit{},
4749
&store.SubmitStat{},
4850
}
4951

stat/stat_submit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (ts *StatSubmit) statBasicRange(tr *TimeRange) (*store.SubmitStat, error) {
102102
DataSize: delta.DataSize,
103103
DataTotal: total.DataSize + delta.DataSize,
104104
BaseFee: delta.BaseFee,
105-
BaseFeeTotal: total.BaseFee + delta.BaseFee,
105+
BaseFeeTotal: total.BaseFee.Add(delta.BaseFee),
106106
}, nil
107107
}
108108

@@ -124,7 +124,7 @@ func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType st
124124
if latestStat != nil {
125125
srcStat.FileCount += latestStat.FileCount
126126
srcStat.DataSize += latestStat.DataSize
127-
srcStat.BaseFee += latestStat.BaseFee
127+
srcStat.BaseFee = srcStat.BaseFee.Add(latestStat.BaseFee)
128128
}
129129

130130
return &store.SubmitStat{
@@ -135,6 +135,6 @@ func (ts *StatSubmit) statRange(rangEnd *time.Time, srcStatType, descStatType st
135135
DataSize: srcStat.DataSize,
136136
DataTotal: destStat.DataSize + srcStat.DataSize,
137137
BaseFee: srcStat.BaseFee,
138-
BaseFeeTotal: destStat.BaseFee + srcStat.BaseFee,
138+
BaseFeeTotal: destStat.BaseFee.Add(srcStat.BaseFee),
139139
}, nil
140140
}

store/store_address_submit.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package store
2+
3+
import (
4+
"time"
5+
6+
"github.com/shopspring/decimal"
7+
)
8+
9+
type AddressSubmit struct {
10+
SenderID uint64 `gorm:"primary_key;autoIncrement:false"`
11+
SubmissionIndex uint64 `gorm:"primary_key;autoIncrement:false"`
12+
RootHash string `gorm:"size:66;index:idx_root"`
13+
Length uint64 `gorm:"not null"`
14+
15+
BlockNumber uint64 `gorm:"not null"`
16+
BlockTime time.Time `gorm:"not null"`
17+
TxHash string `gorm:"size:66;not null"`
18+
19+
Status uint64 `gorm:"not null;default:0"`
20+
Fee decimal.Decimal `gorm:"type:decimal(65);not null"`
21+
}
22+
23+
func (AddressSubmit) TableName() string {
24+
return "address_submits"
25+
}

store/store_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewBlock(data *types.Block) *Block {
1919
blockTime := time.Unix(int64(data.Timestamp), 0)
2020
return &Block{
2121
BlockNumber: data.Number.Uint64(),
22-
Hash: data.Hash.String()[2:],
22+
Hash: data.Hash.String(),
2323
BlockTime: blockTime,
2424
}
2525
}

store/store_submit.go

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@ import (
1414
"gorm.io/gorm"
1515
)
1616

17-
// TODO one table for overall submissions and another table for submissions of specific account.
18-
1917
type Submit struct {
20-
ID uint64 `gorm:"index:idx_sender_id,priority:2"`
18+
SubmissionIndex uint64 `gorm:"primaryKey;autoIncrement:false"`
19+
RootHash string `gorm:"size:66;index:idx_root"`
20+
Sender string `gorm:"-"`
21+
SenderID uint64 `gorm:"not null"`
22+
Length uint64 `gorm:"not null"`
23+
2124
BlockNumber uint64 `gorm:"not null"`
2225
BlockTime time.Time `gorm:"not null"`
2326
TxHash string `gorm:"size:66;not null"`
2427

25-
// TODO use as primary key?
26-
SubmissionIndex uint64 `gorm:"not null;index:idx_seq"`
27-
RootHash string `gorm:"size:66;index:idx_root"`
28-
Sender string `gorm:"-"`
29-
SenderID uint64 `gorm:"not null;index:idx_sender_id,priority:1"`
30-
Length uint64 `gorm:"not null"`
31-
// TODO supports more status, including L1 and L2 status
32-
Finalized bool `gorm:"default:false"`
33-
// TODO change to Fee. how about use decimal(64,18) as sql type?
34-
Value decimal.Decimal `gorm:"type:varchar(78);not null"`
28+
Status uint64 `gorm:"not null;default:0"`
29+
Fee decimal.Decimal `gorm:"type:decimal(65);not null"`
3530

3631
Extra []byte `gorm:"type:mediumText"` // json field
3732
}
@@ -58,13 +53,13 @@ func NewSubmit(blockTime time.Time, log *types.Log, filter *contract.FlowFiltere
5853
}
5954

6055
submit := &Submit{
61-
BlockNumber: log.BlockNumber,
62-
BlockTime: blockTime,
63-
TxHash: log.TxHash.String(),
6456
SubmissionIndex: flowSubmit.SubmissionIndex.Uint64(),
6557
Sender: flowSubmit.Sender.String(),
6658
Length: flowSubmit.Submission.Length.Uint64(),
67-
Value: decimal.NewFromBigInt(big.NewInt(0), 0),
59+
BlockNumber: log.BlockNumber,
60+
BlockTime: blockTime,
61+
TxHash: log.TxHash.String(),
62+
Fee: decimal.NewFromBigInt(big.NewInt(0), 0),
6863
Extra: extra,
6964
}
7065

@@ -86,7 +81,27 @@ func newSubmitStore(db *gorm.DB) *SubmitStore {
8681
}
8782

8883
func (ss *SubmitStore) Add(dbTx *gorm.DB, submits []*Submit) error {
89-
return dbTx.CreateInBatches(submits, batchSizeInsert).Error
84+
addressSubmits := make([]AddressSubmit, 0)
85+
for _, submit := range submits {
86+
addressSubmit := AddressSubmit{
87+
SenderID: submit.SenderID,
88+
SubmissionIndex: submit.SubmissionIndex,
89+
RootHash: submit.RootHash,
90+
Length: submit.Length,
91+
BlockNumber: submit.BlockNumber,
92+
BlockTime: submit.BlockTime,
93+
TxHash: submit.TxHash,
94+
Fee: submit.Fee,
95+
Status: submit.Status,
96+
}
97+
addressSubmits = append(addressSubmits, addressSubmit)
98+
}
99+
100+
if err := dbTx.CreateInBatches(submits, batchSizeInsert).Error; err != nil {
101+
return err
102+
}
103+
104+
return dbTx.CreateInBatches(addressSubmits, batchSizeInsert).Error
90105
}
91106

92107
func (ss *SubmitStore) Pop(dbTx *gorm.DB, block uint64) error {
@@ -96,7 +111,7 @@ func (ss *SubmitStore) Pop(dbTx *gorm.DB, block uint64) error {
96111
func (ss *SubmitStore) Count(startTime, endTime time.Time) (*SubmitStatResult, error) {
97112
var result SubmitStatResult
98113
err := ss.DB.Model(&Submit{}).Select(`count(id) as file_count, IFNULL(sum(length), 0) as data_size,
99-
IFNULL(sum(value), 0) as base_fee`).Where("block_time >= ? and block_time < ?", startTime, endTime).
114+
IFNULL(sum(fee), 0) as base_fee`).Where("block_time >= ? and block_time < ?", startTime, endTime).
100115
Find(&result).Error
101116
if err != nil {
102117
return nil, err
@@ -119,8 +134,9 @@ func (ss *SubmitStore) FirstWithoutRootHash() (*Submit, error) {
119134
return &submit, nil
120135
}
121136

122-
func (ss *SubmitStore) Update(submit *Submit) error {
123-
if err := ss.DB.Model(&submit).Updates(submit).Error; err != nil {
137+
func (ss *SubmitStore) UpdateByPrimaryKey(submit *Submit) error {
138+
if err := ss.DB.Model(&submit).Where("submission_index=?", submit.SubmissionIndex).
139+
Updates(submit).Error; err != nil {
124140
return err
125141
}
126142

0 commit comments

Comments
 (0)