2021-01-07 19:17:46 +00:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package multinode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-05 18:10:58 +01:00
|
|
|
"time"
|
2021-01-07 19:17:46 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/rpc/rpcstatus"
|
|
|
|
"storj.io/storj/private/multinodepb"
|
|
|
|
"storj.io/storj/storagenode/apikeys"
|
2021-01-14 16:41:36 +00:00
|
|
|
"storj.io/storj/storagenode/payouts"
|
2021-05-05 18:10:58 +01:00
|
|
|
"storj.io/storj/storagenode/payouts/estimatedpayouts"
|
2021-01-07 19:17:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ multinodepb.DRPCPayoutServer = (*PayoutEndpoint)(nil)
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// PayoutEndpoint implements multinode payouts endpoint.
|
2021-01-07 19:17:46 +00:00
|
|
|
//
|
|
|
|
// architecture: Endpoint
|
|
|
|
type PayoutEndpoint struct {
|
2021-03-29 09:58:04 +01:00
|
|
|
multinodepb.DRPCPayoutUnimplementedServer
|
|
|
|
|
2021-05-05 18:10:58 +01:00
|
|
|
log *zap.Logger
|
|
|
|
apiKeys *apikeys.Service
|
|
|
|
estimatedPayouts *estimatedpayouts.Service
|
|
|
|
db payouts.DB
|
2021-01-07 19:17:46 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:41:36 +00:00
|
|
|
// NewPayoutEndpoint creates new multinode payouts endpoint.
|
2021-05-05 18:10:58 +01:00
|
|
|
func NewPayoutEndpoint(log *zap.Logger, apiKeys *apikeys.Service, estimatedPayouts *estimatedpayouts.Service, db payouts.DB) *PayoutEndpoint {
|
2021-01-07 19:17:46 +00:00
|
|
|
return &PayoutEndpoint{
|
2021-05-05 18:10:58 +01:00
|
|
|
log: log,
|
|
|
|
apiKeys: apiKeys,
|
|
|
|
estimatedPayouts: estimatedPayouts,
|
|
|
|
db: db,
|
2021-01-07 19:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Earned returns total earned amount.
|
|
|
|
func (payout *PayoutEndpoint) Earned(ctx context.Context, req *multinodepb.EarnedRequest) (_ *multinodepb.EarnedResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = authenticate(ctx, payout.apiKeys, req.GetHeader()); err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
earned, err := payout.db.GetTotalEarned(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &multinodepb.EarnedResponse{
|
|
|
|
Total: earned,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-02-04 15:44:31 +00:00
|
|
|
|
|
|
|
// EarnedPerSatellite returns total earned amount per satellite.
|
|
|
|
func (payout *PayoutEndpoint) EarnedPerSatellite(ctx context.Context, req *multinodepb.EarnedPerSatelliteRequest) (_ *multinodepb.EarnedPerSatelliteResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = authenticate(ctx, payout.apiKeys, req.GetHeader()); err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp multinodepb.EarnedPerSatelliteResponse
|
|
|
|
satelliteIDs, err := payout.db.GetPayingSatellitesIDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(satelliteIDs); i++ {
|
|
|
|
earned, err := payout.db.GetEarnedAtSatellite(ctx, satelliteIDs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.EarnedSatellite = append(resp.EarnedSatellite, &multinodepb.EarnedSatellite{
|
|
|
|
Total: earned,
|
|
|
|
SatelliteId: satelliteIDs[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|
2021-05-05 18:10:58 +01:00
|
|
|
|
|
|
|
// EstimatedPayoutTotal returns estimated earnings for current month from all satellites.
|
|
|
|
func (payout *PayoutEndpoint) EstimatedPayoutTotal(ctx context.Context, req *multinodepb.EstimatedPayoutTotalRequest) (_ *multinodepb.EstimatedPayoutTotalResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = authenticate(ctx, payout.apiKeys, req.GetHeader()); err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
estimated, err := payout.estimatedPayouts.GetAllSatellitesEstimatedPayout(ctx, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.EstimatedPayoutTotalResponse{}, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &multinodepb.EstimatedPayoutTotalResponse{EstimatedEarnings: estimated.CurrentMonthExpectations}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EstimatedPayoutSatellite returns estimated earnings for current month from specific satellite.
|
|
|
|
func (payout *PayoutEndpoint) EstimatedPayoutSatellite(ctx context.Context, req *multinodepb.EstimatedPayoutSatelliteRequest) (_ *multinodepb.EstimatedPayoutSatelliteResponse, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err = authenticate(ctx, payout.apiKeys, req.GetHeader()); err != nil {
|
|
|
|
return nil, rpcstatus.Wrap(rpcstatus.Unauthenticated, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
estimated, err := payout.estimatedPayouts.GetSatelliteEstimatedPayout(ctx, req.SatelliteId, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return &multinodepb.EstimatedPayoutSatelliteResponse{}, rpcstatus.Wrap(rpcstatus.Internal, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &multinodepb.EstimatedPayoutSatelliteResponse{EstimatedEarnings: estimated.CurrentMonthExpectations}, nil
|
|
|
|
}
|