Skip to content

Commit

Permalink
storagenode/payouts/estimatedpayouts: fix calculations
Browse files Browse the repository at this point in the history
Change-Id: Iaa01ebd06a32c19d3ddc46b52524020e51212a7b
  • Loading branch information
egonelbre committed Feb 1, 2021
1 parent c5ecca1 commit 8a3db08
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
12 changes: 9 additions & 3 deletions storagenode/payouts/estimatedpayouts/estimatedpayouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func RoundFloat(value float64) float64 {

// SetExpectedMonth set current month expectations.
func (estimatedPayout *EstimatedPayout) SetExpectedMonth(now time.Time) {
daysPaste := float64(now.Day() - 1)
timeInMonth := date.UTCEndOfMonth(now)
estimatedPayout.CurrentMonthExpectations = (estimatedPayout.CurrentMonth.Payout / daysPaste) * float64(timeInMonth.Day())
daysPast := float64(now.Day()) - 1
if daysPast < 1 {
daysPast = 1
}

daysPerMonth := float64(date.UTCEndOfMonth(now).Day())
payoutPerDay := estimatedPayout.CurrentMonth.Payout / daysPast

estimatedPayout.CurrentMonthExpectations = payoutPerDay * daysPerMonth
}
35 changes: 24 additions & 11 deletions storagenode/payouts/estimatedpayouts/estimatedpayouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package estimatedpayouts_test

import (
"math"
"testing"
"time"

Expand All @@ -19,19 +20,31 @@ func TestCurrentMonthExpectations(t *testing.T) {
StorageNodeCount: 1,
SatelliteCount: 2,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
estimatedPayout := estimatedpayouts.EstimatedPayout{
CurrentMonth: estimatedpayouts.PayoutMonthly{
Payout: 100,
},
const payout = 100.0

type test struct {
time time.Time
expected float64
}
tests := []test{
// 28 days in month
{time.Date(2021, 2, 1, 16, 0, 0, 0, time.UTC), 2800.00},
{time.Date(2021, 2, 28, 10, 0, 0, 0, time.UTC), 103.70},
// 31 days in month
{time.Date(2021, 3, 1, 19, 0, 0, 0, time.UTC), 3100.0},
{time.Date(2021, 3, 31, 21, 0, 0, 0, time.UTC), 103.33},
}

currentDay := time.Now().Day() - 1
now := time.Now().UTC()
y, m, _ := now.Date()
daysInMonth := time.Date(y, m+1, 1, 0, 0, 0, -1, &time.Location{}).Day()
for _, test := range tests {
estimates := estimatedpayouts.EstimatedPayout{
CurrentMonth: estimatedpayouts.PayoutMonthly{
Payout: payout,
},
}

expectations := (estimatedPayout.CurrentMonth.Payout / float64(currentDay)) * float64(daysInMonth)
estimatedPayout.SetExpectedMonth(now)
require.Equal(t, estimatedPayout.CurrentMonthExpectations, expectations)
estimates.SetExpectedMonth(test.time)
require.False(t, math.IsNaN(estimates.CurrentMonthExpectations))
require.InDelta(t, test.expected, estimates.CurrentMonthExpectations, 0.01)
}
})
}

0 comments on commit 8a3db08

Please sign in to comment.