Skip to content

Commit

Permalink
Count atxs
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Aug 30, 2024
1 parent 11485fc commit da25b8d
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package history

import (
"context"
"github.com/spacemeshos/explorer-backend/utils"
"math"

"time"

Expand Down Expand Up @@ -63,7 +65,7 @@ func NewHistory(ctx context.Context, bus *client.Bus, dbUrl string, dbName strin
}

func (h *History) Run() {
period := time.Duration(h.storage.NetworkInfo.LayerDuration) * time.Second / 2
period := time.Duration(h.storage.NetworkInfo.LayerDuration) * time.Second
if period > time.Minute {
period = time.Minute
}
Expand Down Expand Up @@ -153,6 +155,36 @@ func (h *History) GetStats() *api.Message {
for _, epoch := range epochs {
log.Info("History: stats for epoch %v: %v", epoch.Number, epoch.Stats)
age := now - epoch.Start
atxs, _ := h.storage.GetActivations(context.Background(), &bson.D{{Key: "targetEpoch", Value: epoch.Number}})
if atxs != nil {
smeshers := make(map[string]int64)
for _, atx := range atxs {
var commitmentSize int64
var smesher string
for _, e := range atx {
if e.Key == "smesher" {
smesher, _ = e.Value.(string)
continue
}
if e.Key == "commitmentSize" {
if value, ok := e.Value.(int64); ok {
commitmentSize = value
} else if value, ok := e.Value.(int32); ok {
commitmentSize = int64(value)
}
}
}
if smesher != "" {
smeshers[smesher] += commitmentSize
epoch.Stats.Current.Security += commitmentSize
}
}
epoch.Stats.Current.Smeshers = int64(len(smeshers))
// degree_of_decentralization is defined as: 0.5 * (min(n,1e4)^2/1e8) + 0.5 * (1 - gini_coeff(last_100_epochs))
a := math.Min(float64(epoch.Stats.Current.Smeshers), 1e4)
// todo replace to utils.CalcDecentralCoefficient
epoch.Stats.Current.Decentral = int64(100.0 * (0.5*(a*a)/1e8 + 0.5*(1.0-utils.Gini(smeshers))))
}
message.Smeshers[i].Amt = epoch.Stats.Current.Smeshers
message.Smeshers[i].Epoch = epoch.Number
message.Smeshers[i].Age = age
Expand Down

0 comments on commit da25b8d

Please sign in to comment.