forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_error.go
322 lines (270 loc) · 9.2 KB
/
transaction_error.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package solana
import (
jsn "encoding/json"
"fmt"
)
type TransactionError struct {
cause error
raw interface{}
}
func (v *TransactionError) Error() string {
return v.cause.Error()
}
func (v *TransactionError) Unwrap() error {
return v.cause
}
func (v *TransactionError) Cause() error {
return v.cause
}
func (v *TransactionError) RawError() interface{} {
return v.raw
}
func ParseTransactionError(tx *Transaction, raw interface{}) (*TransactionError, bool) {
switch t := raw.(type) {
case string:
err, ok := parseTransactionErrorString(t)
if !ok {
return nil, false
}
return &TransactionError{
cause: err,
raw: raw,
}, true
case map[string]interface{}:
err, ok := parseTransactionErrorObject(tx, t)
if !ok {
return nil, false
}
return &TransactionError{
cause: err,
raw: raw,
}, true
default:
return nil, false
}
}
func parseTransactionErrorString(err string) (error, bool) {
switch err {
case "AccountInUse":
return TransactionError_AccountInUse{}, true
case "AccountLoadedTwice":
return TransactionError_AccountLoadedTwice{}, true
case "AccountNotFound":
return TransactionError_AccountNotFound{}, true
case "ProgramAccountNotFound":
return TransactionError_ProgramAccountNotFound{}, true
case "InsufficientFundsForFee":
return TransactionError_InsufficientFundsForFee{}, true
case "InvalidAccountForFee":
return TransactionError_InvalidAccountForFee{}, true
case "AlreadyProcessed":
return TransactionError_AlreadyProcessed{}, true
case "BlockhashNotFound":
return TransactionError_BlockhashNotFound{}, true
case "CallChainTooDeep":
return TransactionError_CallChainTooDeep{}, true
case "MissingSignatureForFee":
return TransactionError_MissingSignatureForFee{}, true
case "InvalidAccountIndex":
return TransactionError_InvalidAccountIndex{}, true
case "SignatureFailure":
return TransactionError_SignatureFailure{}, true
case "InvalidProgramForExecution":
return TransactionError_InvalidProgramForExecution{}, true
case "SanitizeFailure":
return TransactionError_SanitizeFailure{}, true
case "ClusterMaintenance":
return TransactionError_ClusterMaintenance{}, true
case "AccountBorrowOutstanding":
return TransactionError_AccountBorrowOutstanding{}, true
default:
return TransactionError_Undefined(err), true
}
}
func parseTransactionErrorObject(tx *Transaction, err map[string]interface{}) (error, bool) {
switch {
case hasKey(err, "InstructionError"):
fields, ok := err["InstructionError"].([]interface{})
if !ok {
return nil, false
}
return parseInstructionError(tx, fields)
case hasKey(err, "InsufficientFundsForRent"):
obj, ok := err["InsufficientFundsForRent"].(map[string]interface{})
if !ok {
return nil, false
}
return parseInsufficientFundsForRent(tx, obj)
default:
return nil, false
}
}
func parseInstructionError(
tx *Transaction, fields []interface{},
) (*TransactionError_InstructionError, bool) {
if len(fields) != 2 {
return nil, false
}
index, ok := asFloat64(fields[0])
if !ok {
return nil, false
}
var progID *PublicKey
if tx != nil {
in := tx.Message.Instructions[int(index)]
prog, rErr := tx.ResolveProgramIDIndex(in.ProgramIDIndex)
if rErr != nil {
return nil, false //nolint: nilerr
}
progID = &prog
}
cause, ok := ParseInstructionError(fields[1], progID)
if !ok {
return nil, false
}
return &TransactionError_InstructionError{
Index: int32(index),
Cause: cause,
}, true
}
func parseInsufficientFundsForRent(
tx *Transaction, obj map[string]interface{},
) (*TransactionError_InsufficientFundsForRent, bool) {
i, ok := obj["account_index"].(float64)
if !ok {
return nil, false
}
var acc *PublicKey
if tx != nil {
accs, err := tx.AccountMetaList()
if err != nil {
return nil, false
}
acc = accs[int(i)].PublicKey.ToPointer()
}
return &TransactionError_InsufficientFundsForRent{
AccountIndex: int(i),
Account: acc,
}, true
}
func hasKey(m map[string]interface{}, k string) bool {
_, ok := m[k]
return ok
}
func asFloat64(v interface{}) (float64, bool) {
index, ok := v.(float64)
if ok {
return index, true
}
s, ok := v.(jsn.Number)
if !ok {
return 0, false
}
index, err := s.Float64()
if err != nil {
return 0, false
}
return index, true
}
type TransactionError_Undefined string
func (v TransactionError_Undefined) Error() string {
return string(v)
}
// Defined [here](https://github.com/solana-labs/solana/blob/c0c60386544ec9a9ec7119229f37386d9f070523/sdk/src/transaction/error.rs#L13).
// An account is already being processed in another transaction in a way that
// does not support parallelism
type TransactionError_AccountInUse struct{}
func (TransactionError_AccountInUse) Error() string { return "Account in use" }
// A `Pubkey` appears twice in the transaction's `account_keys`. Instructions
// can reference `Pubkey`s more than once but the message must contain a list
// with no duplicate keys
type TransactionError_AccountLoadedTwice struct{}
func (TransactionError_AccountLoadedTwice) Error() string { return "Account loaded twice" }
// Attempt to debit an account but found no record of a prior credit.
type TransactionError_AccountNotFound struct{}
func (TransactionError_AccountNotFound) Error() string {
return "Attempt to debit an account but found no record of a prior credit."
}
// Attempt to load a program that does not exist
type TransactionError_ProgramAccountNotFound struct{}
func (TransactionError_ProgramAccountNotFound) Error() string {
return "Attempt to load a program that does not exist"
}
// The from `Pubkey` does not have sufficient balance to pay the fee to
// schedule the transaction
type TransactionError_InsufficientFundsForFee struct{}
func (TransactionError_InsufficientFundsForFee) Error() string { return "Insufficient funds for fee" }
type TransactionError_InsufficientFundsForRent struct {
AccountIndex int
Account *PublicKey
}
func (v TransactionError_InsufficientFundsForRent) Error() string {
acc := fmt.Sprintf("account at index '%d'", v.AccountIndex)
if v.Account != nil {
acc = fmt.Sprintf("%q", v.Account)
}
return fmt.Sprintf("Insufficient funds for rent in %s", acc)
}
// This account may not be used to pay transaction fees
type TransactionError_InvalidAccountForFee struct{}
func (TransactionError_InvalidAccountForFee) Error() string {
return "This account may not be used to pay transaction fees"
}
// The bank has seen this transaction before. This can occur under normal
// operation when a UDP packet is duplicated, as a user error from a client not
// updating its `recent_blockhash`, or as a double-spend attack.
type TransactionError_AlreadyProcessed struct{}
func (TransactionError_AlreadyProcessed) Error() string {
return "This transaction has already been processed"
}
// The bank has not seen the given `recent_blockhash` or the transaction is too
// old and the `recent_blockhash` has been discarded.
type TransactionError_BlockhashNotFound struct{}
func (TransactionError_BlockhashNotFound) Error() string { return "Blockhash not found" }
// An error occurred while processing an instruction.
type TransactionError_InstructionError struct {
Index int32
Cause InstructionError
}
func (v *TransactionError_InstructionError) Error() string {
return fmt.Sprintf("Error processing instruction %d: %s", v.Index, v.Cause.Error())
}
func (v *TransactionError_InstructionError) Unwrap() error { return v.Cause }
// Loader call chain is too deep
type TransactionError_CallChainTooDeep struct{}
func (TransactionError_CallChainTooDeep) Error() string { return "Loader call chain is too deep" }
// Transaction requires a fee but has no signature present
type TransactionError_MissingSignatureForFee struct{}
func (TransactionError_MissingSignatureForFee) Error() string {
return "Transaction requires a fee but has no signature present"
}
// Transaction contains an invalid account reference
type TransactionError_InvalidAccountIndex struct{}
func (TransactionError_InvalidAccountIndex) Error() string {
return "Transaction contains an invalid account reference"
}
// Transaction did not pass signature verification
type TransactionError_SignatureFailure struct{}
func (TransactionError_SignatureFailure) Error() string {
return "Transaction did not pass signature verification"
}
// This program may not be used for executing instructions
type TransactionError_InvalidProgramForExecution struct{}
func (TransactionError_InvalidProgramForExecution) Error() string {
return "This program may not be used for executing instructions"
}
// Transaction failed to sanitize accounts offsets correctly implies that
// account locks are not taken for this TX, and should not be unlocked.
type TransactionError_SanitizeFailure struct{}
func (TransactionError_SanitizeFailure) Error() string {
return "Transaction failed to sanitize accounts offsets correctly"
}
type TransactionError_ClusterMaintenance struct{}
func (TransactionError_ClusterMaintenance) Error() string {
return "Transactions are currently disabled due to cluster maintenance"
}
// Transaction processing left an account with an outstanding borrowed reference
type TransactionError_AccountBorrowOutstanding struct{}
func (TransactionError_AccountBorrowOutstanding) Error() string {
return "Transaction processing left an account with an outstanding borrowed reference"
}