-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
305 lines (257 loc) · 8.59 KB
/
errors.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package transaction_v2
import (
"context"
"errors"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
"github.com/code-payments/code-server/pkg/code/transaction"
"github.com/code-payments/code-server/pkg/solana"
)
const (
maxReasonStringLength = 2048
)
var (
ErrTimedOutReceivingRequest = errors.New("timed out receiving request")
ErrNotPhoneVerified = newIntentDeniedError("not phone verified")
ErrTooManyAccountCreations = newIntentDeniedError("too many account creations")
ErrTooManyPayments = newIntentDeniedError("too many payments")
ErrTooManyNewRelationships = newIntentDeniedError("too many new relationships")
ErrTransactionLimitExceeded = newIntentDeniedError("dollar value exceeds limit")
ErrNotManagedByCode = newIntentDeniedError("at least one account is no longer managed by code")
ErrInvalidSignature = errors.New("invalid signature provided")
ErrMissingSignature = errors.New("at least one signature is missing")
ErrTooManySignatures = errors.New("too many signatures provided")
ErrInvalidActionToUpgrade = errors.New("attempt to upgrade an action that isn't upgradeable")
ErrPrivacyUpgradeMissed = errors.New("opportunity to upgrade the private transaction was missed")
ErrPrivacyAlreadyUpgraded = errors.New("private transaction has already been upgraded")
ErrWaitForNextBlock = errors.New("must wait for next block before attempting privacy upgrade")
ErrNotImplemented = errors.New("feature not implemented")
)
type IntentValidationError struct {
message string
}
func newIntentValidationError(message string) IntentValidationError {
return IntentValidationError{
message: message,
}
}
func newIntentValidationErrorf(format string, args ...any) IntentValidationError {
return newIntentValidationError(fmt.Sprintf(format, args...))
}
func newActionValidationError(action *transactionpb.Action, message string) IntentValidationError {
return newIntentValidationError(fmt.Sprintf("actions[%d]: %s", action.Id, message))
}
func newActionValidationErrorf(action *transactionpb.Action, message string, args ...any) IntentValidationError {
return newActionValidationError(action, fmt.Sprintf(message, args...))
}
func (e IntentValidationError) Error() string {
return e.message
}
type IntentDeniedError struct {
message string
}
func newIntentDeniedError(message string) IntentDeniedError {
return IntentDeniedError{
message: message,
}
}
func newIntentDeniedErrorf(format string, args ...any) IntentDeniedError {
return newIntentDeniedError(fmt.Sprintf(format, args...))
}
func (e IntentDeniedError) Error() string {
return e.message
}
type SwapValidationError struct {
message string
}
func newSwapValidationError(message string) SwapValidationError {
return SwapValidationError{
message: message,
}
}
func newSwapValidationErrorf(format string, args ...any) SwapValidationError {
return newSwapValidationError(fmt.Sprintf(format, args...))
}
func (e SwapValidationError) Error() string {
return e.message
}
type SwapDeniedError struct {
message string
}
func newSwapDeniedError(message string) SwapDeniedError {
return SwapDeniedError{
message: message,
}
}
func newSwapDeniedErrorf(format string, args ...any) SwapDeniedError {
return newSwapDeniedError(fmt.Sprintf(format, args...))
}
func (e SwapDeniedError) Error() string {
return e.message
}
type StaleStateError struct {
message string
}
func newStaleStateError(message string) StaleStateError {
return StaleStateError{
message: message,
}
}
func newStaleStateErrorf(format string, args ...any) StaleStateError {
return newStaleStateError(fmt.Sprintf(format, args...))
}
func newActionWithStaleStateError(action *transactionpb.Action, message string) StaleStateError {
return newStaleStateError(fmt.Sprintf("actions[%d]: %s", action.Id, message))
}
func (e StaleStateError) Error() string {
return e.message
}
func toReasonStringErrorDetails(err error) *transactionpb.ErrorDetails {
if err == nil {
return nil
}
reasonString := err.Error()
if len(reasonString) > maxReasonStringLength {
reasonString = reasonString[:maxReasonStringLength]
}
return &transactionpb.ErrorDetails{
Type: &transactionpb.ErrorDetails_ReasonString{
ReasonString: &transactionpb.ReasonStringErrorDetails{
Reason: reasonString,
},
},
}
}
func toInvalidSignatureErrorDetails(
actionId uint32,
txn solana.Transaction,
signature *commonpb.Signature,
) *transactionpb.ErrorDetails {
// Clear out all signatures, so clients have no way of submitting this transaction
var emptySig solana.Signature
for i := range txn.Signatures {
copy(txn.Signatures[i][:], emptySig[:])
}
return &transactionpb.ErrorDetails{
Type: &transactionpb.ErrorDetails_InvalidSignature{
InvalidSignature: &transactionpb.InvalidSignatureErrorDetails{
ActionId: actionId,
ExpectedTransaction: &commonpb.Transaction{
Value: txn.Marshal(),
},
ProvidedSignature: signature,
},
},
}
}
func handleSubmitIntentError(streamer transactionpb.Transaction_SubmitIntentServer, err error) error {
// gRPC status errors are passed through as is
if _, ok := status.FromError(err); ok {
return err
}
// Case 1: Errors that map to a Code error response
switch err.(type) {
case IntentValidationError:
return handleSubmitIntentStructuredError(
streamer,
transactionpb.SubmitIntentResponse_Error_INVALID_INTENT,
toReasonStringErrorDetails(err),
)
case IntentDeniedError:
return handleSubmitIntentStructuredError(
streamer,
transactionpb.SubmitIntentResponse_Error_DENIED,
toReasonStringErrorDetails(err),
)
case StaleStateError:
return handleSubmitIntentStructuredError(
streamer,
transactionpb.SubmitIntentResponse_Error_STALE_STATE,
toReasonStringErrorDetails(err),
)
}
switch err {
case ErrMissingSignature, ErrTooManySignatures, ErrInvalidSignature:
return handleSubmitIntentStructuredError(
streamer,
transactionpb.SubmitIntentResponse_Error_SIGNATURE_ERROR,
toReasonStringErrorDetails(err),
)
case ErrNotImplemented:
return status.Error(codes.Unimplemented, err.Error())
}
// Case 2: Errors that map to gRPC status errors
switch err {
case ErrTimedOutReceivingRequest, context.DeadlineExceeded:
return status.Error(codes.DeadlineExceeded, err.Error())
case context.Canceled:
return status.Error(codes.Canceled, err.Error())
case transaction.ErrNoAvailableNonces:
return status.Error(codes.Unavailable, "")
}
return status.Error(codes.Internal, "rpc server failure")
}
func handleSubmitIntentStructuredError(streamer transactionpb.Transaction_SubmitIntentServer, code transactionpb.SubmitIntentResponse_Error_Code, errorDetails ...*transactionpb.ErrorDetails) error {
errResp := &transactionpb.SubmitIntentResponse{
Response: &transactionpb.SubmitIntentResponse_Error_{
Error: &transactionpb.SubmitIntentResponse_Error{
Code: code,
ErrorDetails: errorDetails,
},
},
}
return streamer.Send(errResp)
}
func handleSwapError(streamer transactionpb.Transaction_SwapServer, err error) error {
// gRPC status errors are passed through as is
if _, ok := status.FromError(err); ok {
return err
}
// Case 1: Errors that map to a Code error response
switch err.(type) {
case SwapValidationError:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_INVALID_SWAP,
toReasonStringErrorDetails(err),
)
case SwapDeniedError:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_DENIED,
toReasonStringErrorDetails(err),
)
}
switch err {
case ErrInvalidSignature:
return handleSwapStructuredError(
streamer,
transactionpb.SwapResponse_Error_SIGNATURE_ERROR,
toReasonStringErrorDetails(err),
)
case ErrNotImplemented:
return status.Error(codes.Unimplemented, err.Error())
}
// Case 2: Errors that map to gRPC status errors
switch err {
case ErrTimedOutReceivingRequest, context.DeadlineExceeded:
return status.Error(codes.DeadlineExceeded, err.Error())
case context.Canceled:
return status.Error(codes.Canceled, err.Error())
}
return status.Error(codes.Internal, "rpc server failure")
}
func handleSwapStructuredError(streamer transactionpb.Transaction_SwapServer, code transactionpb.SwapResponse_Error_Code, errorDetails ...*transactionpb.ErrorDetails) error {
errResp := &transactionpb.SwapResponse{
Response: &transactionpb.SwapResponse_Error_{
Error: &transactionpb.SwapResponse_Error{
Code: code,
ErrorDetails: errorDetails,
},
},
}
return streamer.Send(errResp)
}