Skip to content

Commit

Permalink
Fixed wrong first day/last day calculation depending on factor value. (
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Jul 19, 2024
1 parent af4c5fe commit 6e66985
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/consensys/gnark-crypto v0.13.0 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/containerd v1.7.20 // indirect
github.com/containerd/continuity v0.4.3 // indirect
Expand All @@ -78,7 +78,7 @@ require (
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/ethereum/c-kzg-4844 v1.0.2 // indirect
github.com/ethereum/c-kzg-4844 v1.0.3 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240306133620-7d920df305f0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down Expand Up @@ -140,7 +140,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/moby/sys/mount v0.3.3 // indirect
github.com/moby/sys/mount v0.3.4 // indirect
github.com/moby/sys/mountinfo v0.7.2 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down Expand Up @@ -226,8 +226,10 @@ require (

replace (
github.com/btcsuite/btcd/btcec/v2 => github.com/btcsuite/btcd/btcec/v2 v2.3.4-0.20240701131322-c9fae1ac7cca
github.com/consensys/gnark-crypto => github.com/consensys/gnark-crypto v0.12.1
github.com/containerd/containerd => github.com/containerd/containerd v1.6.19
github.com/distribution/reference => github.com/distribution/reference v0.5.0
github.com/docker/docker => github.com/docker/docker v20.10.3+incompatible
github.com/ethereum/c-kzg-4844 => github.com/ethereum/c-kzg-4844 v1.0.2
github.com/testcontainers/testcontainers-go => github.com/testcontainers/testcontainers-go v0.15.0
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,8 @@ github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8oh
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/moby/sys/mount v0.3.3 h1:fX1SVkXFJ47XWDoeFW4Sq7PdQJnV2QIDZAqjNqgEjUs=
github.com/moby/sys/mount v0.3.3/go.mod h1:PBaEorSNTLG5t/+4EgukEQVlAvVEc6ZjTySwKdqp5K0=
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
github.com/moby/sys/mount v0.3.4 h1:yn5jq4STPztkkzSKpZkLcmjue+bZJ0u2AuQY1iNI1Ww=
github.com/moby/sys/mount v0.3.4/go.mod h1:KcQJMbQdJHPlq5lcYT+/CjatWM4PuxKe+XLSVS4J6Os=
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
Expand Down
16 changes: 8 additions & 8 deletions tokenomics/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
} else {
notBeforeTime = start
notAfterTime = end
firstDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), int(daysInMonth(start)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfStartMonth.Sub(firstDayOfEndMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, firstDayOfEndMonth.Add(stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
firstDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), int(daysInMonth(end)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfEndMonth.Sub(firstDayOfStartMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, firstDayOfStartMonth.Add(stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
}
}
} else {
Expand All @@ -155,10 +155,10 @@ func (r *repository) calculateDates(limit, offset uint64, start, end *time.Time,
} else {
notBeforeTime = end
notAfterTime = start
firstDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), int(daysInMonth(end)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfEndMonth.Sub(firstDayOfStartMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, lastDayOfEndMonth.Add(-1*stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
firstDayOfEndMonth := stdlibtime.Date(end.Year(), end.Month(), 1, 0, 0, 0, 0, stdlibtime.UTC)
lastDayOfStartMonth := stdlibtime.Date(start.Year(), start.Month(), int(daysInMonth(start)), 0, 0, 0, 0, stdlibtime.UTC)
for ix := 0; ix <= int(lastDayOfStartMonth.Sub(firstDayOfEndMonth).Hours()/hoursInADay); ix++ {
dates = append(dates, lastDayOfStartMonth.Add(-1*stdlibtime.Duration(ix)*hoursInADay*stdlibtime.Hour))
}
}
}
Expand Down
90 changes: 89 additions & 1 deletion tokenomics/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestCalculateDates_Staging_Factor1(t *testing.T) {
assert.EqualValues(t, expected, dates)
}

func TestCalculateDates_Factor1_SeparateMonthes(t *testing.T) {
func TestCalculateDates_FactorMinus1_SeparateMonthes(t *testing.T) {
t.Parallel()
repo := &repository{cfg: &Config{
GlobalAggregationInterval: struct {
Expand All @@ -236,6 +236,94 @@ func TestCalculateDates_Factor1_SeparateMonthes(t *testing.T) {
offset := uint64(0)
start := time.New(stdlibtime.Date(2024, 7, 18, 12, 52, 32, 0, stdlibtime.UTC))
end := time.New(stdlibtime.Date(2024, 6, 18, 12, 52, 32, 0, stdlibtime.UTC))
factor := stdlibtime.Duration(-1)

dates, notBeforeTime, notAfterTime := repo.calculateDates(limit, offset, start, end, factor)
assert.Len(t, dates, 61)
assert.Equal(t, end, notBeforeTime)
assert.Equal(t, start, notAfterTime)

expected := []stdlibtime.Time{
stdlibtime.Date(2024, 7, 31, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 30, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 29, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 28, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 27, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 26, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 25, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 24, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 23, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 22, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 21, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 20, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 19, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 18, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 17, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 16, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 15, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 14, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 13, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 12, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 11, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 10, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 9, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 8, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 7, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 6, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 5, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 4, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 3, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 2, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 7, 1, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 30, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 29, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 28, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 27, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 26, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 25, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 24, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 23, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 22, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 21, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 20, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 19, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 18, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 17, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 16, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 15, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 14, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 13, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 12, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 11, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 10, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 9, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 8, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 7, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 6, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 5, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 4, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 3, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 2, 0, 0, 0, 0, stdlibtime.UTC),
stdlibtime.Date(2024, 6, 1, 0, 0, 0, 0, stdlibtime.UTC),
}
assert.EqualValues(t, expected, dates)
}

func TestCalculateDates_Factor1_SeparateMonthes(t *testing.T) {
t.Parallel()
repo := &repository{cfg: &Config{
GlobalAggregationInterval: struct {
Parent stdlibtime.Duration `yaml:"parent"`
Child stdlibtime.Duration `yaml:"child"`
}{
Parent: 24 * stdlibtime.Hour,
Child: stdlibtime.Hour,
},
}}
limit := uint64(24)
offset := uint64(0)
start := time.New(stdlibtime.Date(2024, 6, 18, 12, 52, 32, 0, stdlibtime.UTC))
end := time.New(stdlibtime.Date(2024, 7, 18, 12, 52, 32, 0, stdlibtime.UTC))
factor := stdlibtime.Duration(1)

dates, notBeforeTime, notAfterTime := repo.calculateDates(limit, offset, start, end, factor)
Expand Down

0 comments on commit 6e66985

Please sign in to comment.