2020-03-13 14:01:12 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
package payouts
|
2020-03-13 14:01:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-09 18:43:06 +01:00
|
|
|
"database/sql"
|
2020-07-16 16:50:15 +01:00
|
|
|
"errors"
|
2020-03-16 04:28:03 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-06-24 16:57:22 +01:00
|
|
|
"time"
|
2020-03-13 14:01:12 +00:00
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-05-29 09:52:10 +01:00
|
|
|
"storj.io/common/storj"
|
2020-03-18 11:27:42 +00:00
|
|
|
"storj.io/storj/private/date"
|
2020-05-27 17:27:28 +01:00
|
|
|
"storj.io/storj/storagenode/reputation"
|
2020-07-09 18:43:06 +01:00
|
|
|
"storj.io/storj/storagenode/satellites"
|
2020-03-13 14:01:12 +00:00
|
|
|
"storj.io/storj/storagenode/trust"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-09-21 12:06:43 +01:00
|
|
|
// ErrPayoutService defines payout service error.
|
2021-01-14 16:41:36 +00:00
|
|
|
ErrPayoutService = errs.Class("payouts service error")
|
2020-03-13 14:01:12 +00:00
|
|
|
|
2020-03-26 14:53:15 +00:00
|
|
|
// ErrBadPeriod defines that period has wrong format.
|
|
|
|
ErrBadPeriod = errs.Class("wrong period format")
|
|
|
|
|
2020-03-13 14:01:12 +00:00
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
2020-12-05 16:01:42 +00:00
|
|
|
// Service retrieves info from satellites using an rpc client.
|
2020-03-13 14:01:12 +00:00
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
stefanSatellite storj.NodeID
|
|
|
|
|
2020-05-27 17:27:28 +01:00
|
|
|
db DB
|
|
|
|
reputationDB reputation.DB
|
2020-07-09 18:43:06 +01:00
|
|
|
satellitesDB satellites.DB
|
2020-07-02 14:54:32 +01:00
|
|
|
trust *trust.Pool
|
2020-03-13 14:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:57:02 +01:00
|
|
|
// NewService creates new instance of service.
|
2020-11-04 14:38:15 +00:00
|
|
|
func NewService(log *zap.Logger, db DB, reputationDB reputation.DB, satelliteDB satellites.DB, trust *trust.Pool) (_ *Service, err error) {
|
|
|
|
id, err := storj.NodeIDFromString("118UWpMCHzs6CvSgWd9BfFVjw5K9pZbJjkfZJexMtSkmKxvvAW")
|
|
|
|
if err != nil {
|
|
|
|
return &Service{}, err
|
2020-03-13 14:01:12 +00:00
|
|
|
}
|
2020-11-04 14:38:15 +00:00
|
|
|
|
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
stefanSatellite: id,
|
|
|
|
db: db,
|
|
|
|
reputationDB: reputationDB,
|
|
|
|
satellitesDB: satelliteDB,
|
|
|
|
trust: trust,
|
|
|
|
}, nil
|
2020-03-13 14:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
// SatellitePayStubMonthly retrieves held amount for particular satellite for selected month from storagenode database.
|
|
|
|
func (service *Service) SatellitePayStubMonthly(ctx context.Context, satelliteID storj.NodeID, period string) (payStub *PayStub, err error) {
|
2020-03-15 23:24:04 +00:00
|
|
|
defer mon.Task()(&ctx, &satelliteID, &period)(&err)
|
|
|
|
|
2020-03-16 01:32:52 +00:00
|
|
|
payStub, err = service.db.GetPayStub(ctx, satelliteID, period)
|
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-03-16 01:32:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:25:05 +01:00
|
|
|
payStub.UsageAtRestTbM()
|
|
|
|
|
2020-03-16 01:32:52 +00:00
|
|
|
return payStub, nil
|
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
// AllPayStubsMonthly retrieves held amount for all satellites per selected period from storagenode database.
|
|
|
|
func (service *Service) AllPayStubsMonthly(ctx context.Context, period string) (payStubs []PayStub, err error) {
|
2020-03-16 01:32:52 +00:00
|
|
|
defer mon.Task()(&ctx, &period)(&err)
|
|
|
|
|
|
|
|
payStubs, err = service.db.AllPayStubs(ctx, period)
|
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return payStubs, ErrPayoutService.Wrap(err)
|
2020-03-16 01:32:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:25:05 +01:00
|
|
|
for i := 0; i < len(payStubs); i++ {
|
|
|
|
payStubs[i].UsageAtRestTbM()
|
|
|
|
}
|
|
|
|
|
2020-03-16 01:32:52 +00:00
|
|
|
return payStubs, nil
|
2020-03-15 23:24:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
// SatellitePayStubPeriod retrieves held amount for all satellites for selected months from storagenode database.
|
|
|
|
func (service *Service) SatellitePayStubPeriod(ctx context.Context, satelliteID storj.NodeID, periodStart, periodEnd string) (payStubs []PayStub, err error) {
|
2020-03-16 04:28:03 +00:00
|
|
|
defer mon.Task()(&ctx, &satelliteID, &periodStart, &periodEnd)(&err)
|
|
|
|
|
|
|
|
periods, err := parsePeriodRange(periodStart, periodEnd)
|
|
|
|
if err != nil {
|
2020-03-26 14:53:15 +00:00
|
|
|
return []PayStub{}, err
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, period := range periods {
|
|
|
|
payStub, err := service.db.GetPayStub(ctx, satelliteID, period)
|
|
|
|
if err != nil {
|
|
|
|
if ErrNoPayStubForPeriod.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-26 14:53:15 +00:00
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
return []PayStub{}, ErrPayoutService.Wrap(err)
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 14:53:15 +00:00
|
|
|
payStubs = append(payStubs, *payStub)
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:25:05 +01:00
|
|
|
for i := 0; i < len(payStubs); i++ {
|
|
|
|
payStubs[i].UsageAtRestTbM()
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:28:03 +00:00
|
|
|
return payStubs, nil
|
|
|
|
}
|
|
|
|
|
2020-07-02 14:54:32 +01:00
|
|
|
// AllPayStubsPeriod retrieves held amount for all satellites for selected range of months from storagenode database.
|
|
|
|
func (service *Service) AllPayStubsPeriod(ctx context.Context, periodStart, periodEnd string) (payStubs []PayStub, err error) {
|
2020-03-16 04:28:03 +00:00
|
|
|
defer mon.Task()(&ctx, &periodStart, &periodEnd)(&err)
|
|
|
|
|
|
|
|
periods, err := parsePeriodRange(periodStart, periodEnd)
|
|
|
|
if err != nil {
|
2020-03-26 14:53:15 +00:00
|
|
|
return []PayStub{}, err
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, period := range periods {
|
|
|
|
payStub, err := service.db.AllPayStubs(ctx, period)
|
|
|
|
if err != nil {
|
2020-03-26 14:53:15 +00:00
|
|
|
if ErrNoPayStubForPeriod.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
return []PayStub{}, ErrPayoutService.Wrap(err)
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
payStubs = append(payStubs, payStub...)
|
|
|
|
}
|
|
|
|
|
2020-07-11 20:25:05 +01:00
|
|
|
for i := 0; i < len(payStubs); i++ {
|
|
|
|
payStubs[i].UsageAtRestTbM()
|
|
|
|
}
|
|
|
|
|
2020-03-16 04:28:03 +00:00
|
|
|
return payStubs, nil
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// SatellitePeriods retrieves all periods for concrete satellite in which we have some payouts data.
|
2020-05-18 19:37:16 +01:00
|
|
|
func (service *Service) SatellitePeriods(ctx context.Context, satelliteID storj.NodeID) (_ []string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return service.db.SatellitePeriods(ctx, satelliteID)
|
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// AllPeriods retrieves all periods in which we have some payouts data.
|
2020-05-18 19:37:16 +01:00
|
|
|
func (service *Service) AllPeriods(ctx context.Context) (_ []string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
return service.db.AllPeriods(ctx)
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:27:28 +01:00
|
|
|
// AllHeldbackHistory retrieves heldback history for all satellites from storagenode database.
|
2020-09-10 15:08:25 +01:00
|
|
|
func (service *Service) AllHeldbackHistory(ctx context.Context) (result []SatelliteHeldHistory, err error) {
|
2020-05-27 17:27:28 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2020-11-04 14:38:15 +00:00
|
|
|
satellitesIDs := service.trust.GetSatellites(ctx)
|
2020-04-28 14:07:50 +01:00
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
satellitesIDs = append(satellitesIDs, service.stefanSatellite)
|
|
|
|
for i := 0; i < len(satellitesIDs); i++ {
|
2020-09-10 15:08:25 +01:00
|
|
|
var history SatelliteHeldHistory
|
2020-04-28 14:07:50 +01:00
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
helds, err := service.db.SatellitesHeldbackHistory(ctx, satellitesIDs[i])
|
2020-05-27 17:27:28 +01:00
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-04-28 14:07:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-16 23:35:44 +00:00
|
|
|
if helds == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
disposed, err := service.db.SatellitesDisposedHistory(ctx, satellitesIDs[i])
|
2020-06-24 16:57:22 +01:00
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-06-24 16:57:22 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
for i, amountPeriod := range helds {
|
2020-05-27 17:27:28 +01:00
|
|
|
switch i {
|
|
|
|
case 0, 1, 2:
|
2020-09-10 15:08:25 +01:00
|
|
|
history.HoldForFirstPeriod += amountPeriod.Amount
|
|
|
|
history.TotalHeld += amountPeriod.Amount
|
2020-05-27 17:27:28 +01:00
|
|
|
case 3, 4, 5:
|
2020-09-10 15:08:25 +01:00
|
|
|
history.HoldForSecondPeriod += amountPeriod.Amount
|
|
|
|
history.TotalHeld += amountPeriod.Amount
|
2020-05-27 17:27:28 +01:00
|
|
|
case 6, 7, 8:
|
2020-09-10 15:08:25 +01:00
|
|
|
history.HoldForThirdPeriod += amountPeriod.Amount
|
|
|
|
history.TotalHeld += amountPeriod.Amount
|
2020-05-27 17:27:28 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:57:22 +01:00
|
|
|
history.TotalDisposed = disposed
|
2020-11-04 14:38:15 +00:00
|
|
|
history.SatelliteID = satellitesIDs[i]
|
2020-12-16 23:35:44 +00:00
|
|
|
history.SatelliteName = "stefan-benten"
|
2020-11-04 14:38:15 +00:00
|
|
|
|
|
|
|
if satellitesIDs[i] != service.stefanSatellite {
|
|
|
|
url, err := service.trust.GetNodeURL(ctx, satellitesIDs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
|
|
|
}
|
|
|
|
history.SatelliteName = url.Address
|
2020-05-27 17:27:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
stats, err := service.reputationDB.Get(ctx, satellitesIDs[i])
|
2020-05-27 17:27:28 +01:00
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-05-27 17:27:28 +01:00
|
|
|
}
|
2020-04-28 14:07:50 +01:00
|
|
|
|
2020-12-14 17:34:17 +00:00
|
|
|
history.JoinedAt = stats.JoinedAt.Round(time.Minute)
|
2020-05-27 17:27:28 +01:00
|
|
|
result = append(result, history)
|
2020-04-28 14:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// AllSatellitesPayoutPeriod retrieves paystub and payment receipt for specific month from all satellites.
|
|
|
|
func (service *Service) AllSatellitesPayoutPeriod(ctx context.Context, period string) (result []SatellitePayoutForPeriod, err error) {
|
2020-07-09 18:43:06 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
satelliteIDs := service.trust.GetSatellites(ctx)
|
2020-11-04 14:38:15 +00:00
|
|
|
|
|
|
|
satelliteIDs = append(satelliteIDs, service.stefanSatellite)
|
2020-07-09 18:43:06 +01:00
|
|
|
for i := 0; i < len(satelliteIDs); i++ {
|
2020-09-10 15:08:25 +01:00
|
|
|
var payoutForPeriod SatellitePayoutForPeriod
|
2020-07-09 18:43:06 +01:00
|
|
|
paystub, err := service.db.GetPayStub(ctx, satelliteIDs[i], period)
|
|
|
|
if err != nil {
|
|
|
|
if ErrNoPayStubForPeriod.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 16:15:08 +01:00
|
|
|
receipt, err := service.db.GetReceipt(ctx, satelliteIDs[i], period)
|
|
|
|
if err != nil {
|
2020-09-14 15:40:52 +01:00
|
|
|
if !ErrNoPayStubForPeriod.Has(err) {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-09-08 16:15:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 18:43:06 +01:00
|
|
|
stats, err := service.reputationDB.Get(ctx, satelliteIDs[i])
|
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
satellite, err := service.satellitesDB.GetSatellite(ctx, satelliteIDs[i])
|
|
|
|
if err != nil {
|
2020-07-16 16:50:15 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-09-10 15:08:25 +01:00
|
|
|
payoutForPeriod.IsExitComplete = false
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 14:38:15 +00:00
|
|
|
if satelliteIDs[i] != service.stefanSatellite {
|
|
|
|
url, err := service.trust.GetNodeURL(ctx, satelliteIDs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
payoutForPeriod.SatelliteURL = url.Address
|
2020-07-24 16:50:35 +01:00
|
|
|
}
|
|
|
|
|
2020-07-09 18:43:06 +01:00
|
|
|
if satellite.Status == satellites.ExitSucceeded {
|
2020-09-10 15:08:25 +01:00
|
|
|
payoutForPeriod.IsExitComplete = true
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if paystub.SurgePercent == 0 {
|
|
|
|
paystub.SurgePercent = 100
|
|
|
|
}
|
|
|
|
|
2020-09-14 15:40:52 +01:00
|
|
|
earned, surge := paystub.GetEarnedWithSurge()
|
2020-07-28 11:04:07 +01:00
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
periodTime := Period(paystub.Period)
|
|
|
|
|
|
|
|
heldPeriod, err := periodTime.Time()
|
2020-07-28 11:04:07 +01:00
|
|
|
if err != nil {
|
2020-09-10 15:08:25 +01:00
|
|
|
return nil, ErrPayoutService.Wrap(err)
|
2020-07-28 11:04:07 +01:00
|
|
|
}
|
2020-08-04 13:32:47 +01:00
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
heldPercent := GetHeldRate(stats.JoinedAt, heldPeriod)
|
|
|
|
payoutForPeriod.Held = paystub.Held
|
|
|
|
payoutForPeriod.Receipt = receipt
|
|
|
|
payoutForPeriod.Surge = surge
|
|
|
|
payoutForPeriod.AfterHeld = surge - paystub.Held
|
|
|
|
payoutForPeriod.Age = int64(date.MonthsCountSince(stats.JoinedAt))
|
|
|
|
payoutForPeriod.Disposed = paystub.Disposed
|
|
|
|
payoutForPeriod.Earned = earned
|
|
|
|
payoutForPeriod.SatelliteID = satelliteIDs[i].String()
|
|
|
|
payoutForPeriod.SurgePercent = paystub.SurgePercent
|
|
|
|
payoutForPeriod.Paid = paystub.Paid
|
|
|
|
payoutForPeriod.HeldPercent = heldPercent
|
|
|
|
|
|
|
|
result = append(result, payoutForPeriod)
|
2020-07-09 18:43:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:53:15 +00:00
|
|
|
// TODO: move to separate struct.
|
2020-03-16 04:28:03 +00:00
|
|
|
func parsePeriodRange(periodStart, periodEnd string) (periods []string, err error) {
|
|
|
|
var yearStart, yearEnd, monthStart, monthEnd int
|
|
|
|
|
|
|
|
start := strings.Split(periodStart, "-")
|
|
|
|
if len(start) != 2 {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period start has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
end := strings.Split(periodEnd, "-")
|
|
|
|
if len(start) != 2 {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period end has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
yearStart, err = strconv.Atoi(start[0])
|
|
|
|
if err != nil {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period start has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
monthStart, err = strconv.Atoi(start[1])
|
|
|
|
if err != nil || monthStart > 12 || monthStart < 1 {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period start has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
yearEnd, err = strconv.Atoi(end[0])
|
|
|
|
if err != nil {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period end has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
monthEnd, err = strconv.Atoi(end[1])
|
|
|
|
if err != nil || monthEnd > 12 || monthEnd < 1 {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period end has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
if yearEnd < yearStart {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
if yearEnd == yearStart && monthEnd < monthStart {
|
2020-03-26 14:53:15 +00:00
|
|
|
return nil, ErrBadPeriod.New("period has wrong format")
|
2020-03-16 04:28:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for ; yearStart <= yearEnd; yearStart++ {
|
|
|
|
lastMonth := 12
|
|
|
|
if yearStart == yearEnd {
|
|
|
|
lastMonth = monthEnd
|
|
|
|
}
|
|
|
|
for ; monthStart <= lastMonth; monthStart++ {
|
|
|
|
format := "%d-%d"
|
|
|
|
if monthStart < 10 {
|
|
|
|
format = "%d-0%d"
|
|
|
|
}
|
|
|
|
periods = append(periods, fmt.Sprintf(format, yearStart, monthStart))
|
|
|
|
}
|
|
|
|
|
|
|
|
monthStart = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return periods, nil
|
|
|
|
}
|
2020-07-11 20:25:05 +01:00
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
// GetHeldRate returns held rate for specific period from join date of node.
|
2020-09-04 13:05:13 +01:00
|
|
|
func GetHeldRate(joinTime time.Time, requestTime time.Time) (heldRate float64) {
|
2020-09-10 15:08:25 +01:00
|
|
|
monthsSinceJoin := date.MonthsBetweenDates(joinTime, requestTime)
|
|
|
|
switch monthsSinceJoin {
|
2020-08-10 13:29:34 +01:00
|
|
|
case 0, 1, 2:
|
|
|
|
heldRate = 75
|
|
|
|
case 3, 4, 5:
|
|
|
|
heldRate = 50
|
|
|
|
case 6, 7, 8:
|
|
|
|
heldRate = 25
|
|
|
|
default:
|
|
|
|
heldRate = 0
|
|
|
|
}
|
|
|
|
|
2020-09-10 15:08:25 +01:00
|
|
|
return heldRate
|
2020-08-10 13:29:34 +01:00
|
|
|
}
|