2021-02-05 11:37:59 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package payouts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2021-04-22 19:50:42 +01:00
|
|
|
"storj.io/drpc"
|
2021-02-05 11:37:59 +00:00
|
|
|
"storj.io/storj/multinode/nodes"
|
|
|
|
"storj.io/storj/private/multinodepb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
// Error is an error class for payouts service error.
|
2021-04-28 09:06:17 +01:00
|
|
|
Error = errs.Class("payouts")
|
2021-02-05 11:37:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service exposes all payouts related logic.
|
|
|
|
//
|
|
|
|
// architecture: Service
|
|
|
|
type Service struct {
|
|
|
|
log *zap.Logger
|
|
|
|
dialer rpc.Dialer
|
|
|
|
nodes nodes.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates new instance of Service.
|
|
|
|
func NewService(log *zap.Logger, dialer rpc.Dialer, nodes nodes.DB) *Service {
|
|
|
|
return &Service{
|
|
|
|
log: log,
|
|
|
|
dialer: dialer,
|
|
|
|
nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// Earned retrieves all nodes earned amount for all time.
|
|
|
|
func (service *Service) Earned(ctx context.Context) (earned int64, err error) {
|
2021-02-05 11:37:59 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
storageNodes, err := service.nodes.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range storageNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
amount, err := service.earned(ctx, node)
|
2021-02-05 11:37:59 +00:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, Error.Wrap(err)
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
earned += amount
|
|
|
|
}
|
|
|
|
|
|
|
|
return earned, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// EarnedSatellite retrieves all nodes earned amount for all time per satellite.
|
|
|
|
func (service *Service) EarnedSatellite(ctx context.Context) (earned []SatelliteSummary, err error) {
|
2021-02-05 11:37:59 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
storageNodes, err := service.nodes.List(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var listSatellites storj.NodeIDList
|
2021-06-16 17:46:25 +01:00
|
|
|
var listNodesEarnedPerSatellite []multinodepb.EarnedPerSatelliteResponse
|
2021-02-05 11:37:59 +00:00
|
|
|
|
|
|
|
for _, node := range storageNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
earnedPerSatellite, err := service.earnedSatellite(ctx, node)
|
2021-02-05 11:37:59 +00:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, Error.Wrap(err)
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listNodesEarnedPerSatellite = append(listNodesEarnedPerSatellite, earnedPerSatellite)
|
|
|
|
for i := 0; i < len(earnedPerSatellite.EarnedSatellite); i++ {
|
|
|
|
listSatellites = append(listSatellites, earnedPerSatellite.EarnedSatellite[i].SatelliteId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if listSatellites == nil {
|
|
|
|
return []SatelliteSummary{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueSatelliteIDs := listSatellites.Unique()
|
|
|
|
for t := 0; t < len(uniqueSatelliteIDs); t++ {
|
|
|
|
earned = append(earned, SatelliteSummary{
|
|
|
|
SatelliteID: uniqueSatelliteIDs[t],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(listNodesEarnedPerSatellite); i++ {
|
|
|
|
singleNodeEarnedPerSatellite := listNodesEarnedPerSatellite[i].EarnedSatellite
|
|
|
|
for j := 0; j < len(singleNodeEarnedPerSatellite); j++ {
|
|
|
|
for k := 0; k < len(earned); k++ {
|
|
|
|
if singleNodeEarnedPerSatellite[j].SatelliteId == earned[k].SatelliteID {
|
|
|
|
earned[k].Earned += singleNodeEarnedPerSatellite[j].Total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return earned, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// Summary returns all satellites all time stats.
|
|
|
|
func (service *Service) Summary(ctx context.Context) (_ Summary, err error) {
|
2021-05-05 18:34:10 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var summary Summary
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
info, err := service.summary(ctx, node)
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:34:10 +01:00
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:01:16 +01:00
|
|
|
summary.Add(info.Held, info.Paid, node.ID, node.Name)
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// SummaryPeriod returns all satellites stats for specific period.
|
|
|
|
func (service *Service) SummaryPeriod(ctx context.Context, period string) (_ Summary, err error) {
|
2021-05-05 18:34:10 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var summary Summary
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
info, err := service.summaryPeriod(ctx, node, period)
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:34:10 +01:00
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:01:16 +01:00
|
|
|
summary.Add(info.Held, info.Paid, node.ID, node.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// SummarySatellite returns specific satellite all time stats.
|
|
|
|
func (service *Service) SummarySatellite(ctx context.Context, satelliteID storj.NodeID) (_ Summary, err error) {
|
2021-05-05 21:01:16 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
var summary Summary
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return Summary{}, Error.Wrap(err)
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
info, err := service.summarySatellite(ctx, node, satelliteID)
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:01:16 +01:00
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary.Add(info.Held, info.Paid, node.ID, node.Name)
|
|
|
|
}
|
2021-05-05 18:34:10 +01:00
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// SummarySatellitePeriod returns specific satellite stats for specific period.
|
|
|
|
func (service *Service) SummarySatellitePeriod(ctx context.Context, satelliteID storj.NodeID, period string) (_ Summary, err error) {
|
2021-05-05 21:01:16 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
var summary Summary
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-05-25 17:48:45 +01:00
|
|
|
info, err := service.summarySatellitePeriod(ctx, node, satelliteID, period)
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:01:16 +01:00
|
|
|
return Summary{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
summary.Add(info.Held, info.Paid, node.ID, node.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// summarySatellite returns payout info for single satellite, for specific node.
|
|
|
|
func (service *Service) summarySatellite(ctx context.Context, node nodes.Node, satelliteID storj.NodeID) (info *multinodepb.PayoutInfo, err error) {
|
2021-05-05 21:01:16 +01:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return &multinodepb.PayoutInfo{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-05 21:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-05 21:01:16 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-05 21:01:16 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.SatelliteSummary(ctx, &multinodepb.SatelliteSummaryRequest{Header: header, SatelliteId: satelliteID})
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.PayoutInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.PayoutInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// summarySatellitePeriod returns satellite payout info for specific node for specific period.
|
|
|
|
func (service *Service) summarySatellitePeriod(ctx context.Context, node nodes.Node, satelliteID storj.NodeID, period string) (info *multinodepb.PayoutInfo, err error) {
|
2021-05-05 21:01:16 +01:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return &multinodepb.PayoutInfo{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-05 21:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-05 21:01:16 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-05 21:01:16 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.SatellitePeriodSummary(ctx, &multinodepb.SatellitePeriodSummaryRequest{Header: header, SatelliteId: satelliteID, Period: period})
|
2021-05-05 21:01:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.PayoutInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.PayoutInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// summaryPeriod returns node's payout info for specific period.
|
|
|
|
func (service *Service) summaryPeriod(ctx context.Context, node nodes.Node, period string) (info *multinodepb.PayoutInfo, err error) {
|
2021-05-05 18:34:10 +01:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return &multinodepb.PayoutInfo{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-05 18:34:10 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.AllSatellitesPeriodSummary(ctx, &multinodepb.AllSatellitesPeriodSummaryRequest{Header: header, Period: period})
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.PayoutInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.PayoutInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// summary returns node's total payout info.
|
|
|
|
func (service *Service) summary(ctx context.Context, node nodes.Node) (info *multinodepb.PayoutInfo, err error) {
|
2021-05-05 18:34:10 +01:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return &multinodepb.PayoutInfo{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-05 18:34:10 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-05 18:34:10 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.AllSatellitesSummary(ctx, &multinodepb.AllSatellitesSummaryRequest{Header: header})
|
2021-05-05 18:34:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.PayoutInfo{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.PayoutInfo, nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
// NodeExpectations returns node's estimated and undistributed earnings.
|
|
|
|
func (service *Service) NodeExpectations(ctx context.Context, nodeID storj.NodeID) (_ Expectations, err error) {
|
2021-05-05 18:10:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-05-24 16:10:30 +01:00
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{}, Error.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
expectation, err := service.nodeExpectations(ctx, node)
|
2021-05-24 16:10:30 +01:00
|
|
|
if err != nil {
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{}, Error.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
return expectation, nil
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
// Expectations returns all nodes estimated and undistributed earnings.
|
|
|
|
func (service *Service) Expectations(ctx context.Context) (_ Expectations, err error) {
|
2021-05-05 18:10:58 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
var expectations Expectations
|
2021-05-05 18:10:58 +01:00
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
listNodes, err := service.nodes.List(ctx)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{}, Error.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:17:34 +00:00
|
|
|
for _, node := range listNodes {
|
2021-05-24 18:13:47 +01:00
|
|
|
expectation, err := service.nodeExpectations(ctx, node)
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
if nodes.ErrNodeNotReachable.Has(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{}, Error.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
expectations.Undistributed += expectation.Undistributed
|
|
|
|
expectations.CurrentMonthEstimation += expectation.CurrentMonthEstimation
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
return expectations, nil
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:50:42 +01:00
|
|
|
// HeldAmountSummary retrieves held amount history summary for a particular node.
|
|
|
|
func (service *Service) HeldAmountSummary(ctx context.Context, nodeID storj.NodeID) (_ []HeldAmountSummary, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return nil, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-04-22 19:50:42 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
nodeClient := multinodepb.NewDRPCNodeClient(conn)
|
|
|
|
|
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-04-22 19:50:42 +01:00
|
|
|
}
|
|
|
|
trusted, err := nodeClient.TrustedSatellites(ctx, &multinodepb.TrustedSatellitesRequest{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
history, err := service.heldAmountHistory(ctx, node, conn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
trustedSatellites := trusted.GetTrustedSatellites()
|
|
|
|
|
|
|
|
var summary []HeldAmountSummary
|
|
|
|
for _, satelliteHistory := range history {
|
|
|
|
satelliteSummary := HeldAmountSummary{
|
|
|
|
SatelliteID: satelliteHistory.SatelliteID,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, trustedSatellite := range trustedSatellites {
|
|
|
|
if satelliteSummary.SatelliteID.Compare(trustedSatellite.NodeId) == 0 {
|
2021-06-02 20:18:22 +01:00
|
|
|
satelliteSummary.SatelliteAddress = trustedSatellite.GetAddress()
|
2021-04-22 19:50:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(satelliteHistory.HeldAmounts) == 0 {
|
|
|
|
summary = append(summary, satelliteSummary)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
satelliteSummary.PeriodCount = len(satelliteHistory.HeldAmounts)
|
|
|
|
|
|
|
|
for i, heldAmount := range satelliteHistory.HeldAmounts {
|
|
|
|
switch i {
|
|
|
|
case 1, 2, 3:
|
|
|
|
satelliteSummary.FirstQuarter += heldAmount.Amount
|
|
|
|
case 4, 5, 6:
|
|
|
|
satelliteSummary.SecondQuarter += heldAmount.Amount
|
|
|
|
case 7, 8, 9:
|
|
|
|
satelliteSummary.ThirdQuarter += heldAmount.Amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
summary = append(summary, satelliteSummary)
|
|
|
|
}
|
|
|
|
|
|
|
|
return summary, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// heldAmountHistory retrieves held amount history for a particular node.
|
|
|
|
func (service *Service) heldAmountHistory(ctx context.Context, node nodes.Node, conn drpc.Conn) (_ []HeldAmountHistory, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2021-05-25 17:48:45 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutsClient(conn)
|
2021-04-22 19:50:42 +01:00
|
|
|
|
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-04-22 19:50:42 +01:00
|
|
|
}
|
|
|
|
resp, err := payoutClient.HeldAmountHistory(ctx, &multinodepb.HeldAmountHistoryRequest{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var history []HeldAmountHistory
|
|
|
|
for _, pbHistory := range resp.GetHistory() {
|
|
|
|
var heldAmounts []HeldAmount
|
|
|
|
|
|
|
|
for _, pbHeldAmount := range pbHistory.GetHeldAmounts() {
|
|
|
|
heldAmounts = append(heldAmounts, HeldAmount{
|
|
|
|
Period: pbHeldAmount.GetPeriod(),
|
|
|
|
Amount: pbHeldAmount.GetAmount(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
history = append(history, HeldAmountHistory{
|
|
|
|
SatelliteID: pbHistory.SatelliteId,
|
|
|
|
HeldAmounts: heldAmounts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return history, nil
|
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
// nodeExpectations retrieves data from a single node.
|
|
|
|
func (service *Service) nodeExpectations(ctx context.Context, node nodes.Node) (_ Expectations, err error) {
|
2021-05-05 18:10:58 +01:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return Expectations{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-05 18:10:58 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
estimated, err := payoutClient.EstimatedPayoutTotal(ctx, &multinodepb.EstimatedPayoutTotalRequest{Header: header})
|
2021-05-05 18:10:58 +01:00
|
|
|
if err != nil {
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
undistributed, err := payoutClient.Undistributed(ctx, &multinodepb.UndistributedRequest{Header: header})
|
|
|
|
if err != nil {
|
|
|
|
return Expectations{}, Error.Wrap(err)
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 18:13:47 +01:00
|
|
|
return Expectations{Undistributed: undistributed.Total, CurrentMonthEstimation: estimated.EstimatedEarnings}, nil
|
2021-05-05 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// earned returns earned from node.
|
|
|
|
func (service *Service) earned(ctx context.Context, node nodes.Node) (_ int64, err error) {
|
2021-02-05 11:37:59 +00:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return 0, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-02-05 11:37:59 +00:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amount, err := payoutClient.Earned(ctx, &multinodepb.EarnedRequest{Header: header})
|
|
|
|
if err != nil {
|
|
|
|
return 0, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount.Total, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// earnedSatellite returns earned split by satellites.
|
2021-06-16 17:46:25 +01:00
|
|
|
func (service *Service) earnedSatellite(ctx context.Context, node nodes.Node) (_ multinodepb.EarnedPerSatelliteResponse, err error) {
|
2021-02-05 11:37:59 +00:00
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return multinodepb.EarnedPerSatelliteResponse{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-02-05 11:37:59 +00:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.EarnedPerSatellite(ctx, &multinodepb.EarnedPerSatelliteRequest{Header: header})
|
2021-02-05 11:37:59 +00:00
|
|
|
if err != nil {
|
2021-06-16 17:46:25 +01:00
|
|
|
return multinodepb.EarnedPerSatelliteResponse{}, Error.Wrap(err)
|
2021-02-05 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *response, nil
|
|
|
|
}
|
2021-05-25 13:02:20 +01:00
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// PaystubSatellitePeriod returns specific satellite paystub for specific period.
|
|
|
|
func (service *Service) PaystubSatellitePeriod(ctx context.Context, period string, nodeID, satelliteID storj.NodeID) (_ Paystub, err error) {
|
2021-05-24 21:40:31 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return Paystub{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return Paystub{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-24 21:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-24 21:40:31 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-24 21:40:31 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.SatellitePeriodPaystub(ctx, &multinodepb.SatellitePeriodPaystubRequest{
|
2021-05-24 21:40:31 +01:00
|
|
|
Header: header,
|
|
|
|
SatelliteId: satelliteID,
|
|
|
|
Period: period,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Paystub{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Paystub{
|
|
|
|
UsageAtRest: response.Paystub.UsageAtRest,
|
|
|
|
UsageGet: response.Paystub.UsageGet,
|
|
|
|
UsageGetRepair: response.Paystub.UsageGetRepair,
|
|
|
|
UsageGetAudit: response.Paystub.UsageGetAudit,
|
|
|
|
CompAtRest: response.Paystub.CompAtRest,
|
|
|
|
CompGet: response.Paystub.CompGet,
|
|
|
|
CompGetRepair: response.Paystub.CompGetRepair,
|
|
|
|
CompGetAudit: response.Paystub.CompGetAudit,
|
|
|
|
Held: response.Paystub.Held,
|
|
|
|
Paid: response.Paystub.Paid,
|
|
|
|
Distributed: response.Paystub.Distributed,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: response.Paystub.Disposed,
|
2021-05-24 21:40:31 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PaystubPeriod returns all satellites paystub for specific period.
|
|
|
|
func (service *Service) PaystubPeriod(ctx context.Context, period string, nodeID storj.NodeID) (_ Paystub, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return Paystub{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return Paystub{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-24 21:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-24 21:40:31 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-24 21:40:31 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.PeriodPaystub(ctx, &multinodepb.PeriodPaystubRequest{
|
2021-05-24 21:40:31 +01:00
|
|
|
Header: header,
|
|
|
|
Period: period,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Paystub{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Paystub{
|
|
|
|
UsageAtRest: response.Paystub.UsageAtRest,
|
|
|
|
UsageGet: response.Paystub.UsageGet,
|
|
|
|
UsageGetRepair: response.Paystub.UsageGetRepair,
|
|
|
|
UsageGetAudit: response.Paystub.UsageGetAudit,
|
|
|
|
CompAtRest: response.Paystub.CompAtRest,
|
|
|
|
CompGet: response.Paystub.CompGet,
|
|
|
|
CompGetRepair: response.Paystub.CompGetRepair,
|
|
|
|
CompGetAudit: response.Paystub.CompGetAudit,
|
|
|
|
Held: response.Paystub.Held,
|
|
|
|
Paid: response.Paystub.Paid,
|
|
|
|
Distributed: response.Paystub.Distributed,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: response.Paystub.Disposed,
|
2021-05-24 21:40:31 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-25 17:48:45 +01:00
|
|
|
// PaystubSatellite returns specific satellite summed paystubs.
|
|
|
|
func (service *Service) PaystubSatellite(ctx context.Context, nodeID, satelliteID storj.NodeID) (_ Paystub, err error) {
|
2021-05-25 13:02:20 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{}, Error.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return Paystub{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-25 13:02:20 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
response, err := payoutClient.SatellitePaystub(ctx, &multinodepb.SatellitePaystubRequest{
|
2021-05-25 13:02:20 +01:00
|
|
|
Header: header,
|
|
|
|
SatelliteId: satelliteID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{}, Error.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{
|
2021-05-25 13:02:20 +01:00
|
|
|
UsageAtRest: response.Paystub.UsageAtRest,
|
|
|
|
UsageGet: response.Paystub.UsageGet,
|
|
|
|
UsageGetRepair: response.Paystub.UsageGetRepair,
|
|
|
|
UsageGetAudit: response.Paystub.UsageGetAudit,
|
|
|
|
CompAtRest: response.Paystub.CompAtRest,
|
|
|
|
CompGet: response.Paystub.CompGet,
|
|
|
|
CompGetRepair: response.Paystub.CompGetRepair,
|
|
|
|
CompGetAudit: response.Paystub.CompGetAudit,
|
|
|
|
Held: response.Paystub.Held,
|
|
|
|
Paid: response.Paystub.Paid,
|
|
|
|
Distributed: response.Paystub.Distributed,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: response.Paystub.Disposed,
|
2021-05-25 13:02:20 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Paystub returns summed all paystubs.
|
2021-05-24 21:40:31 +01:00
|
|
|
func (service *Service) Paystub(ctx context.Context, nodeID storj.NodeID) (_ Paystub, err error) {
|
2021-05-25 13:02:20 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
node, err := service.nodes.Get(ctx, nodeID)
|
|
|
|
if err != nil {
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{}, Error.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := service.dialer.DialNodeURL(ctx, storj.NodeURL{
|
|
|
|
ID: node.ID,
|
|
|
|
Address: node.PublicAddress,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-03-26 22:17:34 +00:00
|
|
|
return Paystub{}, nodes.ErrNodeNotReachable.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, conn.Close())
|
|
|
|
}()
|
|
|
|
|
2021-06-16 17:46:25 +01:00
|
|
|
payoutClient := multinodepb.NewDRPCPayoutClient(conn)
|
2021-05-25 13:02:20 +01:00
|
|
|
header := &multinodepb.RequestHeader{
|
2022-08-12 20:50:30 +01:00
|
|
|
ApiKey: node.APISecret[:],
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := payoutClient.Paystub(ctx, &multinodepb.PaystubRequest{
|
|
|
|
Header: header,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{}, Error.Wrap(err)
|
2021-05-25 13:02:20 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 21:40:31 +01:00
|
|
|
return Paystub{
|
2021-05-25 13:02:20 +01:00
|
|
|
UsageAtRest: response.Paystub.UsageAtRest,
|
|
|
|
UsageGet: response.Paystub.UsageGet,
|
|
|
|
UsageGetRepair: response.Paystub.UsageGetRepair,
|
|
|
|
UsageGetAudit: response.Paystub.UsageGetAudit,
|
|
|
|
CompAtRest: response.Paystub.CompAtRest,
|
|
|
|
CompGet: response.Paystub.CompGet,
|
|
|
|
CompGetRepair: response.Paystub.CompGetRepair,
|
|
|
|
CompGetAudit: response.Paystub.CompGetAudit,
|
|
|
|
Held: response.Paystub.Held,
|
|
|
|
Paid: response.Paystub.Paid,
|
|
|
|
Distributed: response.Paystub.Distributed,
|
2021-06-01 16:02:50 +01:00
|
|
|
Disposed: response.Paystub.Disposed,
|
2021-05-25 13:02:20 +01:00
|
|
|
}, nil
|
|
|
|
}
|