Skip to content

Commit

Permalink
Balance history aggregation fix: use days instead of hours (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Jul 4, 2024
1 parent 9978ea4 commit 7474861
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 1,507 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/containerd v1.7.18 // indirect
github.com/containerd/containerd v1.7.19 // indirect
github.com/containerd/continuity v0.4.3 // indirect
github.com/containerd/errdefs v0.1.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
Expand Down
74 changes: 36 additions & 38 deletions tokenomics/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,66 +105,64 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
const (
hoursInADay = 24
)
calculatedLimit := (limit / hoursInADay) * uint64(r.cfg.GlobalAggregationInterval.Parent/r.cfg.GlobalAggregationInterval.Child)
var calculatedLimit, mappedOffset uint64
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
calculatedLimit = (limit / hoursInADay) * uint64(r.cfg.GlobalAggregationInterval.Parent/r.cfg.GlobalAggregationInterval.Child)
} else {
calculatedLimit = (limit / hoursInADay)
}
var afterStartPadding, beforeStartPadding uint64
if factor > 0 {
if r.cfg.GlobalAggregationInterval.Child == r.cfg.GlobalAggregationInterval.Parent && r.cfg.GlobalAggregationInterval.Child == 24*stdlibtime.Hour {
afterStartPadding = 0
beforeStartPadding = 0
} else if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
afterStartPadding = 60 - uint64(start.Add(stdlibtime.Duration(-calculatedLimit*uint64(stdlibtime.Minute))).Minute())
beforeStartPadding = uint64(start.Add(stdlibtime.Duration(-calculatedLimit * uint64(stdlibtime.Minute))).Minute())
} else {
afterStartPadding = 24 - uint64(start.Add(stdlibtime.Duration(calculatedLimit*uint64(stdlibtime.Hour))).Hour())
beforeStartPadding = uint64(start.Add(stdlibtime.Duration(-calculatedLimit * uint64(stdlibtime.Hour))).Hour())
}
} else {
if r.cfg.GlobalAggregationInterval.Child == r.cfg.GlobalAggregationInterval.Parent && r.cfg.GlobalAggregationInterval.Child == 24*stdlibtime.Hour {
beforeStartPadding = 0
afterStartPadding = 0
} else if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
beforeStartPadding = uint64(start.Add(stdlibtime.Duration(-calculatedLimit*uint64(stdlibtime.Minute))).Minute()) + 1
} else {
beforeStartPadding = uint64(start.Add(stdlibtime.Duration(-calculatedLimit*uint64(stdlibtime.Hour))).Hour()) + 1
}
}
mappedLimit := calculatedLimit + beforeStartPadding + afterStartPadding
mappedOffset := (offset / hoursInADay) * uint64(r.cfg.GlobalAggregationInterval.Parent/r.cfg.GlobalAggregationInterval.Child)
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
mappedOffset = (offset / hoursInADay) * uint64(r.cfg.GlobalAggregationInterval.Parent/r.cfg.GlobalAggregationInterval.Child)
} else {
mappedOffset = (offset / hoursInADay)
}
dates = make([]stdlibtime.Time, 0, mappedLimit)
if factor > 0 {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(-stdlibtime.Duration(beforeStartPadding)*r.cfg.GlobalAggregationInterval.Child).Add(ix*factor*r.cfg.GlobalAggregationInterval.Child).Truncate(r.cfg.GlobalAggregationInterval.Child))
}
if r.cfg.GlobalAggregationInterval.Child == r.cfg.GlobalAggregationInterval.Parent && r.cfg.GlobalAggregationInterval.Child == 24*stdlibtime.Hour {
notBeforeTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Child).Add(stdlibtime.Duration(mappedOffset * uint64(24*stdlibtime.Hour))))
notAfterTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Child).Add(stdlibtime.Duration((calculatedLimit + mappedOffset) * uint64(24*stdlibtime.Hour))))
} else if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Hour {
notBeforeTime = time.New(start.Add(stdlibtime.Duration(mappedOffset * uint64(stdlibtime.Hour))))
notAfterTime = time.New(start.Add(stdlibtime.Duration((calculatedLimit + mappedOffset) * uint64(stdlibtime.Hour))))
} else {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(-stdlibtime.Duration(beforeStartPadding)*r.cfg.GlobalAggregationInterval.Child).Add(ix*factor*r.cfg.GlobalAggregationInterval.Child).Truncate(r.cfg.GlobalAggregationInterval.Child))
}
notBeforeTime = time.New(start.Add(stdlibtime.Duration(mappedOffset * uint64(stdlibtime.Minute))))
notAfterTime = time.New(start.Add(stdlibtime.Duration((calculatedLimit + mappedOffset) * uint64(stdlibtime.Minute))))
} else {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(-stdlibtime.Duration(beforeStartPadding)*r.cfg.GlobalAggregationInterval.Parent).Add(ix*factor*r.cfg.GlobalAggregationInterval.Parent).Truncate(r.cfg.GlobalAggregationInterval.Parent))
}
notBeforeTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Parent).Add(stdlibtime.Duration(mappedOffset * uint64(r.cfg.GlobalAggregationInterval.Parent))))
notAfterTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Parent).Add(stdlibtime.Duration((calculatedLimit + mappedOffset) * uint64(r.cfg.GlobalAggregationInterval.Parent))))
}
if notAfterTime.UnixNano() > end.UnixNano() {
if r.cfg.GlobalAggregationInterval.Child == r.cfg.GlobalAggregationInterval.Parent && r.cfg.GlobalAggregationInterval.Child == 24*stdlibtime.Hour {
notAfterTime = time.New(end.Truncate(r.cfg.GlobalAggregationInterval.Child))
} else {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
notAfterTime = end
} else {
notAfterTime = time.New(end.Truncate(r.cfg.GlobalAggregationInterval.Parent))
}
}
} else {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(ix*factor*r.cfg.GlobalAggregationInterval.Child).Truncate(r.cfg.GlobalAggregationInterval.Child))
}
if r.cfg.GlobalAggregationInterval.Child == r.cfg.GlobalAggregationInterval.Parent && r.cfg.GlobalAggregationInterval.Child == 24*stdlibtime.Hour {
notBeforeTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Child).Add(stdlibtime.Duration((-calculatedLimit - mappedOffset) * uint64(24*stdlibtime.Hour))))
notAfterTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Child).Add(stdlibtime.Duration(-mappedOffset * uint64(24*stdlibtime.Hour))))
} else if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Hour {
notBeforeTime = time.New(start.Add(stdlibtime.Duration((-calculatedLimit - mappedOffset) * uint64(stdlibtime.Hour))))
notAfterTime = time.New(start.Add(stdlibtime.Duration(-mappedOffset * uint64(stdlibtime.Hour))))
} else {
if r.cfg.GlobalAggregationInterval.Child == stdlibtime.Minute {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(ix*factor*r.cfg.GlobalAggregationInterval.Child).Truncate(r.cfg.GlobalAggregationInterval.Child))
}
notBeforeTime = time.New(start.Add(stdlibtime.Duration((-calculatedLimit - mappedOffset) * uint64(stdlibtime.Minute))))
notAfterTime = time.New(start.Add(stdlibtime.Duration(-mappedOffset * uint64(stdlibtime.Minute))))
} else {
for ix := stdlibtime.Duration(mappedOffset); ix < stdlibtime.Duration(mappedLimit+mappedOffset); ix++ {
dates = append(dates, start.Add(ix*factor*r.cfg.GlobalAggregationInterval.Parent).Truncate(r.cfg.GlobalAggregationInterval.Parent))
}
notBeforeTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Parent).Add(stdlibtime.Duration((-calculatedLimit - mappedOffset) * uint64(r.cfg.GlobalAggregationInterval.Parent))))
notAfterTime = time.New(start.Truncate(r.cfg.GlobalAggregationInterval.Parent).Add(stdlibtime.Duration(-mappedOffset * uint64(r.cfg.GlobalAggregationInterval.Parent))))
}
}

Expand Down
Loading

0 comments on commit 7474861

Please sign in to comment.