diff --git a/api/api.go b/api/api.go index 7bfbaea..fa4e9c8 100644 --- a/api/api.go +++ b/api/api.go @@ -78,8 +78,12 @@ func RegisterRouter(router *gin.Engine) { txsRoute.GET("", listTxsHandler) txsRoute.GET(":txSeq", getTxHandler) + rewardsRoute := apiRoute.Group("/rewards") + rewardsRoute.GET("", listRewardsHandler) + accountsRoute := apiRoute.Group("/accounts") accountsRoute.GET(":address/txs", listAddressTxsHandler) + accountsRoute.GET(":address/rewards", listAddressRewardsHandler) router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) } @@ -160,7 +164,7 @@ func listFeeStatsHandler(c *gin.Context) { // listTxsHandler godoc // // @Summary Storage transaction list -// @Description Query storage transactions, support address and root hash filter +// @Description Query storage transactions // @Tags transaction // @Accept json // @Produce json @@ -170,7 +174,7 @@ func listFeeStatsHandler(c *gin.Context) { // @Failure 600 {object} api.BusinessError // @Router /txs [get] func listTxsHandler(c *gin.Context) { - api.Wrap(listStorageTx)(c) + api.Wrap(listStorageTxs)(c) } // getTxHandler godoc @@ -188,6 +192,22 @@ func getTxHandler(c *gin.Context) { api.Wrap(getStorageTx)(c) } +// listRewardsHandler godoc +// +// @Summary Storage reward list +// @Description Query storage rewards +// @Tags reward +// @Accept json +// @Produce json +// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0) +// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10) +// @Success 200 {object} api.BusinessError{Data=RewardList} +// @Failure 600 {object} api.BusinessError +// @Router /rewards [get] +func listRewardsHandler(c *gin.Context) { + api.Wrap(listStorageRewards)(c) +} + // listAddressTxsHandler godoc // // @Summary Account's storage transaction list @@ -203,5 +223,22 @@ func getTxHandler(c *gin.Context) { // @Failure 600 {object} api.BusinessError // @Router /accounts/{address}/txs [get] func listAddressTxsHandler(c *gin.Context) { - api.Wrap(listAddressStorageTx)(c) + api.Wrap(listAddressStorageTxs)(c) +} + +// listAddressRewardsHandler godoc +// +// @Summary Account's storage reward list +// @Description Query storage rewards for specified account +// @Tags account +// @Accept json +// @Produce json +// @Param address path string false "The submitter address of the uploaded file" +// @Param skip query int false "The number of skipped records, usually it's pageSize * (pageNumber - 1)" minimum(0) default(0) +// @Param limit query int false "The number of records displayed on the page" minimum(1) maximum(100) default(10) +// @Success 200 {object} api.BusinessError{Data=RewardList} +// @Failure 600 {object} api.BusinessError +// @Router /accounts/{address}/rewards [get] +func listAddressRewardsHandler(c *gin.Context) { + api.Wrap(listAddressStorageRewards)(c) } diff --git a/api/reward_api.go b/api/reward_api.go new file mode 100644 index 0000000..bd50efd --- /dev/null +++ b/api/reward_api.go @@ -0,0 +1,104 @@ +package api + +import ( + "github.com/0glabs/0g-storage-scan/store" + commonApi "github.com/Conflux-Chain/go-conflux-util/api" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func listStorageRewards(c *gin.Context) (interface{}, error) { + var param PageParam + if err := c.ShouldBind(¶m); err != nil { + return nil, err + } + + total, rewards, err := listRewards(nil, param.isDesc(), param.Skip, param.Limit) + if err != nil { + return nil, err + } + + return convertStorageRewards(total, rewards) +} +func listAddressStorageRewards(c *gin.Context) (interface{}, error) { + address := c.Param("address") + if address == "" { + logrus.Error("Failed to parse nil address") + return nil, errors.Errorf("Biz error, nil address %v", address) + } + addr, exist, err := db.AddressStore.Get(address) + if err != nil { + return nil, commonApi.ErrInternal(err) + } + if !exist { + return RewardList{}, nil + } + addrIDPtr := &addr.ID + + var param PageParam + if err := c.ShouldBind(¶m); err != nil { + return nil, err + } + + total, rewards, err := listRewards(addrIDPtr, param.isDesc(), param.Skip, param.Limit) + if err != nil { + return nil, err + } + + return convertStorageRewards(total, rewards) +} + +func listRewards(addressID *uint64, idDesc bool, skip, limit int) (int64, []store.Reward, error) { + if addressID == nil { + return db.RewardStore.List(idDesc, skip, limit) + } + + total, addrRewards, err := db.AddressRewardStore.List(addressID, idDesc, skip, limit) + if err != nil { + return 0, nil, err + } + + rewards := make([]store.Reward, 0) + for _, ar := range addrRewards { + rewards = append(rewards, store.Reward{ + PricingIndex: ar.PricingIndex, + MinerID: ar.MinerID, + Amount: ar.Amount, + BlockNumber: ar.BlockNumber, + BlockTime: ar.BlockTime, + TxHash: ar.TxHash, + }) + } + + return total, rewards, nil +} + +func convertStorageRewards(total int64, rewards []store.Reward) (*RewardList, error) { + addrIDs := make([]uint64, 0) + for _, r := range rewards { + addrIDs = append(addrIDs, r.MinerID) + } + addrMap, err := db.BatchGetAddresses(addrIDs) + if err != nil { + return nil, err + } + + storageRewards := make([]Reward, 0) + for _, r := range rewards { + storageReward := Reward{ + RewardSeq: r.PricingIndex, + Miner: addrMap[r.MinerID].Address, + Amount: r.Amount, + BlockNumber: r.BlockNumber, + TxHash: r.TxHash, + Timestamp: r.BlockTime.Unix(), + } + storageRewards = append(storageRewards, storageReward) + } + + return &RewardList{ + Total: total, + List: storageRewards, + }, nil +} diff --git a/api/tx_api.go b/api/tx_api.go index da98f00..067268e 100644 --- a/api/tx_api.go +++ b/api/tx_api.go @@ -4,109 +4,19 @@ import ( "encoding/json" "strconv" - "github.com/0glabs/0g-storage-scan/store" + "github.com/0glabs/0g-storage-client/core" + commonApi "github.com/Conflux-Chain/go-conflux-util/api" + + "github.com/0glabs/0g-storage-scan/store" "github.com/ethereum/go-ethereum/common" "github.com/gin-gonic/gin" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) -func listSubmits(addressID *uint64, rootHash *string, idDesc bool, skip, limit int) (int64, []store.Submit, error) { - if addressID == nil { - return db.SubmitStore.List(rootHash, idDesc, skip, limit) - } - - total, addrSubmits, err := db.AddressSubmitStore.List(addressID, rootHash, idDesc, skip, limit) - if err != nil { - return 0, nil, err - } - - submits := make([]store.Submit, 0) - for _, as := range addrSubmits { - submits = append(submits, store.Submit{ - SubmissionIndex: as.SubmissionIndex, - RootHash: as.RootHash, - SenderID: as.SenderID, - Length: as.Length, - BlockNumber: as.BlockNumber, - BlockTime: as.BlockTime, - TxHash: as.TxHash, - Status: as.Status, - TotalSegNum: as.TotalSegNum, - UploadedSegNum: as.UploadedSegNum, - Fee: as.Fee, - }) - } - - return total, submits, nil -} - -func convertTxList(total int64, submits []store.Submit) (*StorageTxList, error) { - addrIDs := make([]uint64, 0) - for _, submit := range submits { - addrIDs = append(addrIDs, submit.SenderID) - } - addrMap, err := db.BatchGetAddresses(addrIDs) - if err != nil { - return nil, err - } - - storageTxs := make([]StorageTxInfo, 0) - for _, submit := range submits { - storageTx := StorageTxInfo{ - TxSeq: submit.SubmissionIndex, - BlockNumber: submit.BlockNumber, - TxHash: submit.TxHash, - RootHash: submit.RootHash, - From: addrMap[submit.SenderID].Address, - Method: "submit", - Status: submit.Status, - Segments: submit.TotalSegNum, - UploadedSegments: submit.UploadedSegNum, - Timestamp: submit.BlockTime.Unix(), - DataSize: submit.Length, - StorageFee: submit.Fee, - } - storageTxs = append(storageTxs, storageTx) - } - - return &StorageTxList{ - Total: total, - List: storageTxs, - }, nil -} - -func listAddressStorageTx(c *gin.Context) (interface{}, error) { - address := c.Param("address") - if address == "" { - logrus.Error("Failed to parse nil address") - return nil, errors.Errorf("Biz error, nil address %v", address) - } - addr, exist, err := db.AddressStore.Get(address) - if err != nil { - return nil, commonApi.ErrInternal(err) - } - if !exist { - return TxList{}, nil - } - addrIDPtr := &addr.ID - - var param listAddressStorageTxParam - if err := c.ShouldBind(¶m); err != nil { - return nil, err - } - - total, submits, err := listSubmits(addrIDPtr, param.RootHash, param.isDesc(), param.Skip, param.Limit) - if err != nil { - return nil, err - } - - return convertTxList(total, submits) -} - -func listStorageTx(c *gin.Context) (interface{}, error) { - var param listStorageTxParam +func listStorageTxs(c *gin.Context) (interface{}, error) { + var param PageParam if err := c.ShouldBind(¶m); err != nil { return nil, err } @@ -116,7 +26,7 @@ func listStorageTx(c *gin.Context) (interface{}, error) { return nil, err } - return convertTxList(total, submits) + return convertStorageTxs(total, submits) } func getStorageTx(c *gin.Context) (interface{}, error) { @@ -162,8 +72,10 @@ func getStorageTx(c *gin.Context) (interface{}, error) { return nil, errors.Errorf("Unmarshal submit extra error, txSeq %v", txSeq) } result.StartPosition = extra.StartPos.Uint64() - result.EndPosition = extra.StartPos.Uint64() + submit.Length + trunksWithoutPadding := (submit.Length-1)/core.DefaultChunkSize + 1 + result.EndPosition = extra.StartPos.Uint64() + trunksWithoutPadding result.Segments = submit.TotalSegNum + result.UploadedSegments = submit.UploadedSegNum hash := common.HexToHash(submit.TxHash) tx, err := sdk.Eth.TransactionByHash(hash) @@ -182,3 +94,96 @@ func getStorageTx(c *gin.Context) (interface{}, error) { return result, nil } + +func listAddressStorageTxs(c *gin.Context) (interface{}, error) { + address := c.Param("address") + if address == "" { + logrus.Error("Failed to parse nil address") + return nil, errors.Errorf("Biz error, nil address %v", address) + } + addr, exist, err := db.AddressStore.Get(address) + if err != nil { + return nil, commonApi.ErrInternal(err) + } + if !exist { + return StorageTxList{}, nil + } + addrIDPtr := &addr.ID + + var param listAddressStorageTxParam + if err := c.ShouldBind(¶m); err != nil { + return nil, err + } + + total, submits, err := listSubmits(addrIDPtr, param.RootHash, param.isDesc(), param.Skip, param.Limit) + if err != nil { + return nil, err + } + + return convertStorageTxs(total, submits) +} + +func listSubmits(addressID *uint64, rootHash *string, idDesc bool, skip, limit int) (int64, []store.Submit, error) { + if addressID == nil { + return db.SubmitStore.List(rootHash, idDesc, skip, limit) + } + + total, addrSubmits, err := db.AddressSubmitStore.List(addressID, rootHash, idDesc, skip, limit) + if err != nil { + return 0, nil, err + } + + submits := make([]store.Submit, 0) + for _, as := range addrSubmits { + submits = append(submits, store.Submit{ + SubmissionIndex: as.SubmissionIndex, + RootHash: as.RootHash, + SenderID: as.SenderID, + Length: as.Length, + BlockNumber: as.BlockNumber, + BlockTime: as.BlockTime, + TxHash: as.TxHash, + Status: as.Status, + TotalSegNum: as.TotalSegNum, + UploadedSegNum: as.UploadedSegNum, + Fee: as.Fee, + }) + } + + return total, submits, nil +} + +func convertStorageTxs(total int64, submits []store.Submit) (*StorageTxList, error) { + addrIDs := make([]uint64, 0) + for _, submit := range submits { + addrIDs = append(addrIDs, submit.SenderID) + } + addrMap, err := db.BatchGetAddresses(addrIDs) + if err != nil { + return nil, err + } + + storageTxs := make([]StorageTxInfo, 0) + for _, submit := range submits { + storageTx := StorageTxInfo{ + TxSeq: submit.SubmissionIndex, + BlockNumber: submit.BlockNumber, + TxHash: submit.TxHash, + RootHash: submit.RootHash, + From: addrMap[submit.SenderID].Address, + Method: "submit", + Status: submit.Status, + Segments: submit.TotalSegNum, + UploadedSegments: submit.UploadedSegNum, + Timestamp: submit.BlockTime.Unix(), + DataSize: submit.Length, + StorageFee: submit.Fee, + } + storageTxs = append(storageTxs, storageTx) + } + + return &StorageTxList{ + Total: total, + List: storageTxs, + }, nil +} diff --git a/api/types.go b/api/types.go index 19976d9..36d5c65 100644 --- a/api/types.go +++ b/api/types.go @@ -5,12 +5,18 @@ import ( "time" "github.com/0glabs/0g-storage-scan/stat" + "github.com/shopspring/decimal" ) type PageParam struct { - Skip int `form:"skip,default=0" binding:"omitempty,gte=0"` - Limit int `form:"limit,default=10" binding:"omitempty,lte=2000"` + Skip int `form:"skip,default=0" binding:"omitempty,gte=0"` + Limit int `form:"limit,default=10" binding:"omitempty,lte=2000"` + Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"` +} + +func (sp *PageParam) isDesc() bool { + return strings.EqualFold(sp.Sort, "desc") } type statParam struct { @@ -18,111 +24,6 @@ type statParam struct { MinTimestamp *int `form:"minTimestamp" binding:"omitempty,number"` MaxTimestamp *int `form:"maxTimestamp" binding:"omitempty,number"` IntervalType string `form:"intervalType,default=day" binding:"omitempty,oneof=hour day"` - Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"` -} - -func (sp *statParam) isDesc() bool { - return strings.EqualFold(sp.Sort, "desc") -} - -type listTxParam struct { - PageParam - Address *string `form:"address" binding:"omitempty"` - RootHash *string `form:"rootHash" binding:"omitempty"` - Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"` -} - -func (sp *listTxParam) isDesc() bool { - return strings.EqualFold(sp.Sort, "desc") -} - -type queryTxParam struct { - TxSeq *uint64 `uri:"txSeq" form:"txSeq" binding:"required,number,gte=0"` -} - -// StorageTx model info -// @Description Submission information -type StorageTx struct { - TxSeq uint64 `json:"txSeq"` // Submission index in submit event - BlockNum uint64 `json:"blockNum"` // The block where the submit event is emitted - TxHash string `json:"txHash"` // The transaction where the submit event is emitted - RootHash string `json:"rootHash"` // Merkle root of the file to upload - Address string `json:"address"` // File uploader address - Method string `json:"method"` // The name of the submit event is always `submit` - Status uint8 `json:"status"` // File upload status, 0-not uploaded,1-uploading,2-uploaded - Timestamp int64 `json:"timestamp"` // The block time when submit event emits - DataSize uint64 `json:"dataSize"` // File size in bytes - BaseFee decimal.Decimal `json:"baseFee"` // The storage fee required to upload the file -} - -// TokenInfo model info -// @Description Charge token information -type TokenInfo struct { - Address string `json:"address"` // The address of the token contract - Name string `json:"name"` // Token name - Symbol string `json:"symbol"` // Token symbol - Decimals uint8 `json:"decimals"` // Token decimals - Native bool `json:"native"` // True is native token, otherwise is not -} - -// CostInfo model info -// @Description Charge fee information -type CostInfo struct { - TokenInfo `json:"tokenInfo"` // Charge token info - BasicCost decimal.Decimal `json:"basicCost"` // Charge fee -} - -// TxList model info -// @Description Submission information list -type TxList struct { - Total int64 `json:"total"` // The total number of submission returned - List []StorageTx `json:"list"` // Submission list -} - -// TxBrief model info -// @Description Submission brief information -type TxBrief struct { - TxSeq string `json:"txSeq"` // Submission index in submit event - From string `json:"from"` // File uploader address - Method string `json:"method"` // The name of the submit event is always `submit` - - RootHash string `json:"rootHash"` // Merkle root of the file to upload - DataSize uint64 `json:"dataSize"` // File size in bytes - Expiration uint64 `json:"expiration"` // Expiration date of the uploaded file - CostInfo *CostInfo `json:"costInfo"` // Charge fee information - - BlockNumber uint64 `json:"blockNumber"` // The block where the submit event is emitted - TxHash string `json:"txHash"` // The transaction where the submit event is emitted - Timestamp uint64 `json:"timestamp"` // The block time when submit event emits - Status uint8 `json:"status"` // The status of the transaction on layer1 - GasFee uint64 `json:"gasFee"` // The gas fee of the transaction on layer1 - GasUsed uint64 `json:"gasUsed"` // The gas used of the transaction on layer1 - GasLimit uint64 `json:"gasLimit"` // The gas limit of the transaction on layer1 -} - -// TxDetail model info -// @Description Submission detail information -type TxDetail struct { - TxSeq string `json:"txSeq"` // Submission index in submit event - RootHash string `json:"rootHash"` // Merkle root of the file to upload - - StartPos uint64 `json:"startPos"` // The starting position of the file stored in the storage node - EndPos uint64 `json:"endPos"` // The ending position of the file stored in the storage node - PieceCounts uint64 `json:"pieceCounts"` // The total number of segments the file is split into -} - -// StorageBasicCost model info -// @Description Storage fee information -type StorageBasicCost struct { - TokenInfo // Charge token info - BasicCostTotal decimal.Decimal `json:"basicCostTotal"` // Total storage fee -} - -// Dashboard model info -// @Description Storage status information -type Dashboard struct { - StorageBasicCost `json:"storageBasicCost"` // Storage fee information - stat.LogSyncInfo `json:"logSyncInfo"` // Synchronization information of submit event } // DataStatList model info @@ -172,25 +73,6 @@ type FeeStat struct { StorageFeeTotal decimal.Decimal `json:"storageFeeTotal"` // The total base fee for storage by a certain time } -type listStorageTxParam struct { - PageParam - Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"` -} - -func (sp *listStorageTxParam) isDesc() bool { - return strings.EqualFold(sp.Sort, "desc") -} - -type listAddressStorageTxParam struct { - PageParam - RootHash *string `form:"rootHash" binding:"omitempty"` - Sort string `form:"sort,default=desc" binding:"omitempty,oneof=asc desc"` -} - -func (sp *listAddressStorageTxParam) isDesc() bool { - return strings.EqualFold(sp.Sort, "desc") -} - // Summary model info // @Description Storage summary information type Summary struct { @@ -205,6 +87,28 @@ type StorageFeeStat struct { StorageFeeTotal decimal.Decimal `json:"storageFeeTotal"` // Total storage fee } +// TokenInfo model info +// @Description Charge token information +type TokenInfo struct { + Address string `json:"address"` // The address of the token contract + Name string `json:"name"` // Token name + Symbol string `json:"symbol"` // Token symbol + Decimals uint8 `json:"decimals"` // Token decimals + Native bool `json:"native"` // True is native token, otherwise is not +} + +type listAddressStorageTxParam struct { + PageParam + RootHash *string `form:"rootHash" binding:"omitempty"` +} + +// StorageTxList model info +// @Description Submission information list +type StorageTxList struct { + Total int64 `json:"total"` // The total number of submission returned + List []StorageTxInfo `json:"list"` // Submission list +} + // StorageTxInfo model info // @Description Submission transaction information type StorageTxInfo struct { @@ -225,13 +129,6 @@ type StorageTxInfo struct { UploadedSegments uint64 `json:"uploadedSegments"` // The number of segments the file has been uploaded } -// StorageTxList model info -// @Description Submission information list -type StorageTxList struct { - Total int64 `json:"total"` // The total number of submission returned - List []StorageTxInfo `json:"list"` // Submission list -} - // StorageTxDetail model info // @Description Submission transaction information type StorageTxDetail struct { @@ -245,9 +142,10 @@ type StorageTxDetail struct { StorageFee decimal.Decimal `json:"storageFee"` // The storage fee required to upload the file Status uint8 `json:"status"` // File upload status, 0-not uploaded,1-uploading,2-uploaded - StartPosition uint64 `json:"startPosition"` // The starting position of the file stored in the storage node - EndPosition uint64 `json:"endPosition"` // The ending position of the file stored in the storage node - Segments uint64 `json:"segments"` // The total number of segments the file is split into + StartPosition uint64 `json:"startPosition"` // The starting position of the file stored in the storage node + EndPosition uint64 `json:"endPosition"` // The ending position of the file stored in the storage node + Segments uint64 `json:"segments"` // The total number of segments the file is split into + UploadedSegments uint64 `json:"uploadedSegments"` // The number of segments the file has been uploaded BlockNumber uint64 `json:"blockNumber"` // The block where the submit event is emitted TxHash string `json:"txHash"` // The transaction where the submit event is emitted @@ -258,9 +156,20 @@ type StorageTxDetail struct { GasLimit uint64 `json:"gasLimit"` // The gas limit of the transaction on layer1 } -// StorageFee model info -// @Description Storage fee information -type StorageFee struct { - TokenInfo `json:"chargeToken"` // Charge token info - StorageFee decimal.Decimal `json:"storageFee"` // Storage fee +// RewardList model info +// @Description Miner reward list +type RewardList struct { + Total int64 `json:"total"` // The total number of miner reward returned + List []Reward `json:"list"` // Miner reward list +} + +// Reward model info +// @Description Reward information +type Reward struct { + RewardSeq uint64 `json:"rewardSeq"` // Pricing index for reward + Miner string `json:"miner"` // Miner address + Amount decimal.Decimal `json:"amount"` // The reward amount + BlockNumber uint64 `json:"blockNumber"` // The block where the reward event is emitted + TxHash string `json:"txHash"` // The transaction where the reward event is emitted + Timestamp int64 `json:"timestamp"` // The block time when reward event emits } diff --git a/store/store.go b/store/store.go index 7219dd1..dc707e8 100644 --- a/store/store.go +++ b/store/store.go @@ -176,3 +176,9 @@ func MaxTimestamp(maxTimestamp int) func(db *gorm.DB) *gorm.DB { return db.Where("stat_time <= ?", maxTimestamp) } } + +func MinerID(mi uint64) func(db *gorm.DB) *gorm.DB { + return func(db *gorm.DB) *gorm.DB { + return db.Where("miner_id = ?", mi) + } +} diff --git a/store/store_address_reward.go b/store/store_address_reward.go index 4217a3b..41793b3 100644 --- a/store/store_address_reward.go +++ b/store/store_address_reward.go @@ -38,3 +38,27 @@ func (ars *AddressRewardStore) Add(dbTx *gorm.DB, addressRewards []AddressReward func (ars *AddressRewardStore) Pop(dbTx *gorm.DB, block uint64) error { return dbTx.Where("block_number >= ?", block).Delete(&AddressReward{}).Error } + +func (ars *AddressRewardStore) List(addressID *uint64, idDesc bool, skip, limit int) (int64, []AddressReward, error) { + dbRaw := ars.DB.Model(&AddressReward{}) + var conds []func(db *gorm.DB) *gorm.DB + if addressID != nil { + conds = append(conds, MinerID(*addressID)) + } + dbRaw.Scopes(conds...) + + var orderBy string + if idDesc { + orderBy = "pricing_index DESC" + } else { + orderBy = "pricing_index ASC" + } + + list := new([]AddressReward) + total, err := ars.Store.ListByOrder(dbRaw, orderBy, skip, limit, list) + if err != nil { + return 0, nil, err + } + + return total, *list, nil +} diff --git a/store/store_reward.go b/store/store_reward.go index 54e92c7..1b1e19f 100644 --- a/store/store_reward.go +++ b/store/store_reward.go @@ -59,3 +59,22 @@ func (rs *RewardStore) Add(dbTx *gorm.DB, rewards []*Reward) error { func (rs *RewardStore) Pop(dbTx *gorm.DB, block uint64) error { return dbTx.Where("block_number >= ?", block).Delete(&Reward{}).Error } + +func (rs *RewardStore) List(idDesc bool, skip, limit int) (int64, []Reward, error) { + dbRaw := rs.DB.Model(&Reward{}) + + var orderBy string + if idDesc { + orderBy = "pricing_index DESC" + } else { + orderBy = "pricing_index ASC" + } + + list := new([]Reward) + total, err := rs.Store.ListByOrder(dbRaw, orderBy, skip, limit, list) + if err != nil { + return 0, nil, err + } + + return total, *list, nil +} diff --git a/sync/blockchain.go b/sync/blockchain.go index 0730bcd..f8f818f 100644 --- a/sync/blockchain.go +++ b/sync/blockchain.go @@ -122,19 +122,6 @@ func getEthDataByLogs(w3c *web3go.Client, blockNumber uint64, addresses []common return &EthData{Number: blockNumber, Block: block, Logs: logs}, nil } -func batchGetFlowSubmits(w3c *web3go.Client, blockFrom, blockTo uint64, flowAddr common.Address, - flowSubmitSig common.Hash) ([]types.Log, error) { - bnFrom := types.NewBlockNumber(int64(blockFrom)) - bnTo := types.NewBlockNumber(int64(blockTo)) - logFilter := types.FilterQuery{ - FromBlock: &bnFrom, - ToBlock: &bnTo, - Addresses: []common.Address{flowAddr}, - Topics: [][]common.Hash{{flowSubmitSig}}, - } - return w3c.Eth.Logs(logFilter) -} - func batchGetLogs(w3c *web3go.Client, blockFrom, blockTo uint64, addresses []common.Address, topics [][]common.Hash) ([]types.Log, error) { bnFrom := types.NewBlockNumber(int64(blockFrom)) diff --git a/sync/storage.go b/sync/storage.go index fa0a021..4b4ef4d 100644 --- a/sync/storage.go +++ b/sync/storage.go @@ -75,12 +75,13 @@ func (ss *StorageSyncer) syncFileInfo() error { } } else { submit.Status = uint8(store.Uploaded) + submit.UploadedSegNum = submit.TotalSegNum // Field `uploadedSegNum` is set 0 by rpc when `finalized` is true } addressSubmit := store.AddressSubmit{ SenderID: s.SenderID, SubmissionIndex: s.SubmissionIndex, - UploadedSegNum: info.UploadedSegNum, + UploadedSegNum: submit.UploadedSegNum, Status: submit.Status, }