Skip to content

Commit

Permalink
Add rewards sum to overview
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Jul 17, 2024
1 parent d9349f5 commit ec611ed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/handler/overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type OverviewResp struct {
SmeshersCount uint64 `json:"smeshers_count"`
LayersCount uint64 `json:"layers_count"`
RewardsCount uint64 `json:"rewards_count"`
RewardsSum uint64 `json:"rewards_sum"`
TransactionsCount uint64 `json:"transactions_count"`
NumUnits uint64 `json:"num_units"`
}
Expand Down Expand Up @@ -45,11 +46,12 @@ func Overview(c echo.Context) error {
}
overviewResp.LayersCount = layersCount

rewardsCount, err := cc.StorageClient.GetRewardsCount(cc.Storage)
rewardsSum, rewardsCount, err := cc.StorageClient.GetRewardsSum(cc.Storage)
if err != nil {
log.Warning("failed to get rewards count: %v", err)
return c.NoContent(http.StatusInternalServerError)
}
overviewResp.RewardsSum = rewardsSum
overviewResp.RewardsCount = rewardsCount

transactionsCount, err := cc.StorageClient.GetTransactionsCount(cc.Storage)
Expand Down
5 changes: 3 additions & 2 deletions api/storage/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package storage

import "github.com/spacemeshos/go-spacemesh/sql"

func (c *Client) GetRewardsCount(db *sql.Database) (count uint64, err error) {
_, err = db.Exec(`SELECT COUNT(*) FROM rewards`,
func (c *Client) GetRewardsSum(db *sql.Database) (sum uint64, count uint64, err error) {
_, err = db.Exec(`SELECT COUNT(*), SUM(total_reward) FROM rewards`,
func(stmt *sql.Statement) {
},
func(stmt *sql.Statement) bool {
count = uint64(stmt.ColumnInt64(0))
sum = uint64(stmt.ColumnInt64(1))
return true
})
return
Expand Down
12 changes: 12 additions & 0 deletions api/storage/smesher.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func (c *Client) GetSmeshersCount(db *sql.Database) (count uint64, err error) {
return
}

func (c *Client) GetSmeshersByEpochCount(db *sql.Database, epoch uint64) (count uint64, err error) {
_, err = db.Exec(`SELECT COUNT(*) FROM (SELECT DISTINCT pubkey FROM atxs WHERE epoch = ?1)`,
func(stmt *sql.Statement) {
stmt.BindInt64(1, int64(epoch-1))
},
func(stmt *sql.Statement) bool {
count = uint64(stmt.ColumnInt64(0))
return true
})
return
}

func (c *Client) GetSmesher(db *sql.Database, pubkey []byte) (smesher *Smesher, err error) {
smesher = &Smesher{}
_, err = db.Exec(`SELECT pubkey, coinbase, effective_num_units, COUNT(*) as atxs FROM atxs WHERE pubkey = ?1 GROUP BY pubkey ORDER BY epoch DESC LIMIT 1;`,
Expand Down
3 changes: 2 additions & 1 deletion api/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ type DatabaseClient interface {

GetAccountsCount(db *sql.Database) (uint64, error)
GetSmeshersCount(db *sql.Database) (uint64, error)
GetSmeshersByEpochCount(db *sql.Database, epoch uint64) (uint64, error)
GetLayersCount(db *sql.Database) (uint64, error)
GetRewardsCount(db *sql.Database) (uint64, error)
GetRewardsSum(db *sql.Database) (uint64, uint64, error)
GetTransactionsCount(db *sql.Database) (uint64, error)
GetTotalNumUnits(db *sql.Database) (uint64, error)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func main() {
address.SetAddressConfig("stest")
log.Info(`network HRP set to "stest"`)
}
log.Info("layers per epoch: %d", layersPerEpoch)
log.Info("debug: %v", debug)
log.Info("sqlite path: %s", sqlitePathStringFlag)

cache.Init()

Expand Down

0 comments on commit ec611ed

Please sign in to comment.