-
Notifications
You must be signed in to change notification settings - Fork 402
/
endpoint.go
238 lines (199 loc) · 6.63 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package payouts
import (
"context"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/rpc"
"storj.io/common/rpc/rpcstatus"
"storj.io/common/storj"
"storj.io/storj/private/date"
"storj.io/storj/storagenode/trust"
)
// Client encapsulates HeldAmountClient with underlying connection.
//
// architecture: Client
type Client struct {
conn *rpc.Conn
pb.DRPCHeldAmountClient
}
// Close closes underlying client connection.
func (c *Client) Close() error {
return c.conn.Close()
}
// Endpoint retrieves info from satellites using an rpc client.
//
// architecture: Endpoint
type Endpoint struct {
log *zap.Logger
dialer rpc.Dialer
trust *trust.Pool
}
// NewEndpoint creates new instance of endpoint.
func NewEndpoint(log *zap.Logger, dialer rpc.Dialer, trust *trust.Pool) *Endpoint {
return &Endpoint{
log: log,
dialer: dialer,
trust: trust,
}
}
// GetPaystub retrieves held amount for particular satellite from satellite using RPC.
func (endpoint *Endpoint) GetPaystub(ctx context.Context, satelliteID storj.NodeID, period string) (_ *PayStub, err error) {
defer mon.Task()(&ctx)(&err)
client, err := endpoint.dial(ctx, satelliteID)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
requestedPeriod, err := date.PeriodToTime(period)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
resp, err := client.GetPayStub(ctx, &pb.GetHeldAmountRequest{Period: requestedPeriod})
if err != nil {
if rpcstatus.Code(err) == rpcstatus.OutOfRange {
return nil, ErrNoPayStubForPeriod.Wrap(err)
}
return nil, ErrPayoutService.Wrap(err)
}
return &PayStub{
Period: period[0:7],
SatelliteID: satelliteID,
Created: resp.CreatedAt,
Codes: resp.Codes,
UsageAtRest: resp.UsageAtRest,
UsageGet: resp.UsageGet,
UsagePut: resp.UsagePut,
UsageGetRepair: resp.UsageGetRepair,
UsagePutRepair: resp.UsagePutRepair,
UsageGetAudit: resp.UsageGetAudit,
CompAtRest: resp.CompAtRest,
CompGet: resp.CompGet,
CompPut: resp.CompPut,
CompGetRepair: resp.CompGetRepair,
CompPutRepair: resp.CompPutRepair,
CompGetAudit: resp.CompGetAudit,
SurgePercent: resp.SurgePercent,
Held: resp.Held,
Owed: resp.Owed,
Disposed: resp.Disposed,
Paid: resp.Paid,
Distributed: resp.Distributed,
}, nil
}
// GetAllPaystubs retrieves all paystubs for particular satellite.
func (endpoint *Endpoint) GetAllPaystubs(ctx context.Context, satelliteID storj.NodeID) (_ []PayStub, err error) {
defer mon.Task()(&ctx)(&err)
client, err := endpoint.dial(ctx, satelliteID)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
resp, err := client.GetAllPaystubs(ctx, &pb.GetAllPaystubsRequest{})
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
var payStubs []PayStub
for i := 0; i < len(resp.Paystub); i++ {
paystub := PayStub{
Period: resp.Paystub[i].Period.String()[0:7],
SatelliteID: satelliteID,
Created: resp.Paystub[i].CreatedAt,
Codes: resp.Paystub[i].Codes,
UsageAtRest: resp.Paystub[i].UsageAtRest,
UsageGet: resp.Paystub[i].UsageGet,
UsagePut: resp.Paystub[i].UsagePut,
UsageGetRepair: resp.Paystub[i].UsageGetRepair,
UsagePutRepair: resp.Paystub[i].UsagePutRepair,
UsageGetAudit: resp.Paystub[i].UsageGetAudit,
CompAtRest: resp.Paystub[i].CompAtRest,
CompGet: resp.Paystub[i].CompGet,
CompPut: resp.Paystub[i].CompPut,
CompGetRepair: resp.Paystub[i].CompGetRepair,
CompPutRepair: resp.Paystub[i].CompPutRepair,
CompGetAudit: resp.Paystub[i].CompGetAudit,
SurgePercent: resp.Paystub[i].SurgePercent,
Held: resp.Paystub[i].Held,
Owed: resp.Paystub[i].Owed,
Disposed: resp.Paystub[i].Disposed,
Paid: resp.Paystub[i].Paid,
Distributed: resp.Paystub[i].Distributed,
}
payStubs = append(payStubs, paystub)
}
return payStubs, nil
}
// GetPayment retrieves payment data from particular satellite using grpc.
func (endpoint *Endpoint) GetPayment(ctx context.Context, satelliteID storj.NodeID, period string) (_ *Payment, err error) {
defer mon.Task()(&ctx)(&err)
client, err := endpoint.dial(ctx, satelliteID)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
requestedPeriod, err := date.PeriodToTime(period)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
resp, err := client.GetPayment(ctx, &pb.GetPaymentRequest{Period: requestedPeriod})
if err != nil {
if rpcstatus.Code(err) == rpcstatus.OutOfRange {
return nil, nil
}
return nil, ErrPayoutService.Wrap(err)
}
return &Payment{
ID: resp.Id,
Created: resp.CreatedAt,
SatelliteID: satelliteID,
Period: period[0:7],
Amount: resp.Amount,
Receipt: resp.Receipt,
Notes: resp.Notes,
}, nil
}
// GetAllPayments retrieves all payments for particular satellite.
func (endpoint *Endpoint) GetAllPayments(ctx context.Context, satelliteID storj.NodeID) (_ []Payment, err error) {
defer mon.Task()(&ctx)(&err)
client, err := endpoint.dial(ctx, satelliteID)
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
defer func() { err = errs.Combine(err, client.Close()) }()
resp, err := client.GetAllPayments(ctx, &pb.GetAllPaymentsRequest{})
if err != nil {
return nil, ErrPayoutService.Wrap(err)
}
var payments []Payment
for i := 0; i < len(resp.Payment); i++ {
payment := Payment{
ID: resp.Payment[i].Id,
Created: resp.Payment[i].CreatedAt,
SatelliteID: satelliteID,
Period: resp.Payment[i].Period.String()[0:7],
Amount: resp.Payment[i].Amount,
Receipt: resp.Payment[i].Receipt,
Notes: resp.Payment[i].Notes,
}
payments = append(payments, payment)
}
return payments, nil
}
// dial dials the SnoPayout client for the satellite by id.
func (endpoint *Endpoint) dial(ctx context.Context, satelliteID storj.NodeID) (_ *Client, err error) {
defer mon.Task()(&ctx)(&err)
nodeurl, err := endpoint.trust.GetNodeURL(ctx, satelliteID)
if err != nil {
return nil, errs.New("unable to find satellite %s: %w", satelliteID, err)
}
conn, err := endpoint.dialer.DialNodeURL(ctx, nodeurl)
if err != nil {
return nil, errs.New("unable to connect to the satellite %s: %w", satelliteID, err)
}
return &Client{
conn: conn,
DRPCHeldAmountClient: pb.NewDRPCHeldAmountClient(conn),
}, nil
}