-
Notifications
You must be signed in to change notification settings - Fork 42
/
tx_json.go
485 lines (355 loc) · 16 KB
/
tx_json.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (c) 2019 Perlin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package wavelet
import (
"bytes"
"encoding/binary"
"encoding/hex"
"io/ioutil"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
"github.com/valyala/fastjson"
)
const (
// PayloadParamNameRecipient defines a string representation of the recipient payload param.
PayloadParamNameRecipient = "recipient"
// PayloadParamNameAmount defines a string representation of the amount payload param.
PayloadParamNameAmount = "amount"
// PayloadParamNameGasLimit defines a string representation of the gas_limit payload param.
PayloadParamNameGasLimit = "gas_limit"
// PayloadParamNameGasDeposit defines a string representation of the gas_deposit payload param.
PayloadParamNameGasDeposit = "gas_deposit"
// PayloadParamNameFuncName defines a string representation of the fn_name payload param.
PayloadParamNameFuncName = "fn_name"
// PayloadParamNameFuncPayload defines a string representation of the fn_payload payload param.
PayloadParamNameFuncPayload = "fn_payload"
// PayloadParamNameOperation defines a string representation of the operation payload param.
PayloadParamNameOperation = "operation"
// PayloadParamNameContractCode defines a string representation of the contract_code payload param.
PayloadParamNameContractCode = "contract_code"
uint64Str = `uint64`
uint32Str = `uint32`
uint16Str = `uint16`
uint8Str = `uint8`
)
var (
// ErrNoTag defines an error describing an empty Tag.
ErrNoTag = errors.New("no tag specified")
// ErrInvalidTag defines an error describing an invalid TransactionTag.
ErrInvalidTag = errors.New("tag is invalid")
// ErrCouldNotParse defines an error describing the inability to parse a given payload.
ErrCouldNotParse = errors.New("could not parse the given payload")
// ErrNilField defines an error describing a field value equal to nil.
ErrNilField = errors.New("field is nil")
// ErrInvalidOperation defines an error describing an invalid operation value.
ErrInvalidOperation = errors.New("operation is invalid")
// ErrInvalidAccountIDSize defines an error describing an invalid account ID size.
ErrInvalidAccountIDSize = errors.New("account ID is of an invalid size")
)
/* BEGIN EXPORTED METHODS */
// ParseJSON parses the given JSON payload input.
func ParseJSON(data []byte, tag string) ([]byte, error) {
if tag == "" { // Check no transaction tag
return nil, ErrNoTag // Return no tag error
}
if err := fastjson.ValidateBytes(data); err != nil { // Check not valid JSON
return nil, err // Return found error
}
if intTag := sys.TagLabels[tag]; intTag >= sys.TagBatch || intTag < 0 { // nolint:staticcheck
return nil, ErrInvalidTag // Return invalid tag error
}
switch tag { // Handle different tag types
case "transfer":
return parseTransfer(data) // Parse
case "stake":
return parseStake(data) // Parse
case "contract":
return parseContract(data) // Parse
case "batch":
return parseBatch(data) // Parse
}
return nil, ErrCouldNotParse // Return error (shouldn't ever get here)
}
/* END EXPORTED METHODS */
/* BEGIN INTERNAL METHODS */
/*
BEGIN TAG HANDLERS
*/
// parseTransfer parses a transaction payload with the transfer tag.
func parseTransfer(data []byte) ([]byte, error) {
payload := bytes.NewBuffer(nil) // Initialize buffer
var p fastjson.Parser // Initialize parser
json, err := p.Parse(string(data)) // Parse json
if err != nil { // Check for errors
return nil, err // Return found error
}
if !json.Exists("recipient") || !json.Exists("amount") { // Check no value
return nil, ErrNilField // Return nil field error
}
decodedRecipient, err := hex.DecodeString(string(json.GetStringBytes(PayloadParamNameRecipient)))
if err != nil {
return nil, err // Return found error
}
if len(decodedRecipient) != SizeAccountID { // Check invalid length
return nil, ErrInvalidAccountIDSize // Return invalid recipient ID error
}
var recipient AccountID // Initialize ID buffer
copy(recipient[:], decodedRecipient) // Copy address to buffer
_, err = payload.Write(recipient[:]) // Write recipient value
if err != nil { // Check for errors
return nil, err // Return found error
}
decodedAmount := json.GetUint64(PayloadParamNameAmount) // Get amount value
var amount [8]byte // Initialize integer buffer
binary.LittleEndian.PutUint64(amount[:], decodedAmount) // Write to buffer
_, err = payload.Write(amount[:]) // Write amount value
if err != nil { // Check for errors
return nil, err // Return found error
}
if json.Exists(PayloadParamNameGasLimit) { // Check has gas limit value
decodedGasLimit := json.GetUint64(PayloadParamNameGasLimit) // Get uint64 gas limit value
var gasLimit [8]byte // Initialize integer buffer
binary.LittleEndian.PutUint64(gasLimit[:], decodedGasLimit) // Write to buffer
_, err = payload.Write(gasLimit[:]) // Write gas limit
if err != nil { // Check for errors
return nil, err // Return found error
}
}
var gasDeposit [8]byte // Initialize integer buffer
if json.Exists(PayloadParamNameGasDeposit) { // Check has gas deposit value
decodedGasDeposit := json.GetUint64(PayloadParamNameGasDeposit) // Get uint64 gas deposit value
binary.LittleEndian.PutUint64(gasDeposit[:], decodedGasDeposit) // Write to buffer
}
_, err = payload.Write(gasDeposit[:]) // Write gas deposit
if err != nil {
return nil, err
}
if json.Exists(PayloadParamNameFuncName) { // Check has function name
funcName := string(json.GetStringBytes(PayloadParamNameFuncName)) // Get function name
var funcNameLength [8]byte // Initialize dedicated len buffer
binary.LittleEndian.PutUint32(funcNameLength[:4], uint32(len(funcName))) // Write to buffer
payload.Write(funcNameLength[:4]) // Write name length
payload.WriteString(funcName) // Write function name
if json.Exists(PayloadParamNameFuncPayload) { // Check has function payload
var intBuf [8]byte // Initialize integer buffer
params := bytes.NewBuffer(nil) // Initialize payload buffer
for _, payloadValue := range json.GetArray(PayloadParamNameFuncPayload) { // nolint:dupl
if !payloadValue.Exists("type") { // Check does not declare type
return nil, ErrNilField // Return nil field error
}
payloadType := string(payloadValue.GetStringBytes("type")) // Get payload type
switch payloadType { // Handle different payload types
case "string":
value := string(payloadValue.GetStringBytes("value")) // Get value
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(value))) // Write value length to buffer
params.Write(intBuf[:4]) // Write to buffer
params.WriteString(value) // Write value
case "bytes":
value := payloadValue.GetStringBytes("value") // Get value
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(value))) // Write value length to buffer
params.Write(intBuf[:4]) // Write to buffer
params.Write(value) // Write value
case uint8Str, uint16Str, uint32Str, uint64Str:
value := payloadValue.GetInt64("value") // Get value
switch payloadType { // Handle different int sizes
case uint8Str:
params.WriteByte(byte(value)) // Write value
case uint16Str:
binary.LittleEndian.PutUint16(intBuf[:2], uint16(value)) // Write to buffer
params.Write(intBuf[:2]) // Write value
case uint32Str:
binary.LittleEndian.PutUint32(intBuf[:4], uint32(value)) // Write to buffer
params.Write(intBuf[:4]) // Write value
case uint64Str:
binary.LittleEndian.PutUint64(intBuf[:8], uint64(value)) // Write to buffer
params.Write(intBuf[:8]) // Write value
}
case "hex":
value := string(payloadValue.GetStringBytes("value")) // Get value
buf, err := hex.DecodeString(value) // Decode value
if err != nil { // Check for errors
return nil, err // Return found error
}
params.Write(buf) // Write value
}
}
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(params.Bytes())))
payload.Write(intBuf[:4]) // Write length of function parameters
payload.Write(params.Bytes()) // Write parameters
}
}
return payload.Bytes(), nil // Return payload
}
// parseStake parses a transaction payload with the stake tag.
func parseStake(data []byte) ([]byte, error) {
payload := bytes.NewBuffer(nil) // Initialize buffer
var p fastjson.Parser // Initialize parser
json, err := p.Parse(string(data)) // Parse json
if err != nil { // Check for errors
return nil, err // Return found error
}
if !json.Exists(PayloadParamNameOperation) { // Check no value
return nil, ErrNilField // Return nil field error
}
var operation byte // Initialize operation buffer
operationInt := json.GetInt(PayloadParamNameOperation) // Get operation code
if sys.Tag(operationInt) >= sys.TagBatch || sys.Tag(operationInt) < 0 { // nolint:staticcheck
return nil, ErrInvalidOperation // Return invalid operation error
}
switch json.GetInt(PayloadParamNameOperation) { // Handle different operations
case 0:
operation = sys.WithdrawStake // Set operation
case 1:
operation = sys.PlaceStake // Set operation
case 2:
operation = sys.WithdrawReward // Set operation
}
decodedAmount := uint64(json.GetFloat64(PayloadParamNameAmount)) // Get amount value
var amount [8]byte // Initialize integer buffer
binary.LittleEndian.PutUint64(amount[:], decodedAmount) // Write to buffer
err = payload.WriteByte(operation) // Write operation
if err != nil { // Check for errors
return nil, err // Return found error
}
_, err = payload.Write(amount[:]) // Write amount
if err != nil { // Check for errors
return nil, err // Return found error
}
return payload.Bytes(), nil // Return payload
}
// parseContract parses a transaction payload with the contract tag.
func parseContract(data []byte) ([]byte, error) {
payload := bytes.NewBuffer(nil) // Initialize buffer
var p fastjson.Parser // Initialize parser
json, err := p.Parse(string(data)) // Parse json
if err != nil { // Check for errors
return nil, err // Return found error
}
if !json.Exists(PayloadParamNameGasLimit) || !json.Exists("contract_code") { // Check no value
return nil, ErrNilField // Return nil field error
}
decodedGasLimit := json.GetUint64(PayloadParamNameGasLimit) // Get uint64 gas limit value
var gasLimit [8]byte // Initialize integer buffer
binary.LittleEndian.PutUint64(gasLimit[:], decodedGasLimit) // Write to buffer
_, err = payload.Write(gasLimit[:]) // Write gas limit
if err != nil { // Check for errors
return nil, err // Return found error
}
var decodedGasDeposit uint64
if json.Exists(PayloadParamNameGasDeposit) {
decodedGasDeposit = json.GetUint64(PayloadParamNameGasDeposit) // Get uiint64 gas deposit value
}
var gasDeposit [8]byte
binary.LittleEndian.PutUint64(gasDeposit[:], decodedGasDeposit) // Write to buffer
_, err = payload.Write(gasDeposit[:]) // Write gas deposit
if err != nil {
return nil, err
}
if json.Exists("fn_payload") { // Check has function payload
var intBuf [8]byte // Initialize integer buffer
params := bytes.NewBuffer(nil) // Initialize payload buffer
for _, payloadValue := range json.GetArray("fn_payload") { // nolint:dupl
if !payloadValue.Exists("type") { // Check does not declare type
return nil, ErrNilField // Return nil field error
}
payloadType := string(payloadValue.GetStringBytes("type")) // Get payload type
switch payloadType { // Handle different payload types
case "string":
value := string(payloadValue.GetStringBytes("value")) // Get value
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(value))) // Write value length to buffer
params.Write(intBuf[:4]) // Write to buffer
params.WriteString(value) // Write value
case "bytes":
value := payloadValue.GetStringBytes("value") // Get value
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(value))) // Write value length to buffer
params.Write(intBuf[:4]) // Write to buffer
params.Write(value) // Write value
case uint8Str, uint16Str, uint32Str, uint64Str:
value := payloadValue.GetInt64("value") // Get value
switch payloadType { // Handle different int sizes
case uint8Str:
params.WriteByte(byte(value)) // Write value
case uint16Str:
binary.LittleEndian.PutUint16(intBuf[:2], uint16(value)) // Write to buffer
params.Write(intBuf[:2]) // Write value
case uint32Str:
binary.LittleEndian.PutUint32(intBuf[:4], uint32(value)) // Write to buffer
params.Write(intBuf[:4]) // Write value
case uint64Str:
binary.LittleEndian.PutUint64(intBuf[:8], uint64(value)) // Write to buffer
params.Write(intBuf[:8]) // Write value
}
case "hex":
value := string(payloadValue.GetStringBytes("value")) // Get value
buf, err := hex.DecodeString(value) // Decode value
if err != nil { // Check for errors
return nil, err // Return found error
}
params.Write(buf) // Write value
}
}
binary.LittleEndian.PutUint32(intBuf[:4], uint32(len(params.Bytes())))
payload.Write(intBuf[:4]) // Write length of function parameters
payload.Write(params.Bytes()) // Write parameters
}
code, err := ioutil.ReadFile(string(json.GetStringBytes(PayloadParamNameContractCode))) // Read contract code
if err != nil { // Check for errors
return nil, err // Return found error
}
_, err = payload.Write(code) // Write contract code to buffer
if err != nil { // Check for errors
return nil, err // Return found error
}
return payload.Bytes(), nil // Return payload
}
// parseBatch parses a transaction payload with the batch tag.
func parseBatch(data []byte) ([]byte, error) {
payload := bytes.NewBuffer(nil) // Initialize buffer
var p fastjson.Parser // Initialize parser
json, err := p.Parse(string(data)) // Parse json
if err != nil { // Check for errors
return nil, err // Return found error
}
if !json.Exists("payloads") { // Check no value
return nil, ErrNilField // Return nil field error
}
transactions := json.GetArray("payloads") // Get payloads
var batchLength [8]byte // Initialize batch length buffer
binary.LittleEndian.PutUint64(batchLength[:], uint64(len(transactions))) // Write batch length to buffer
payload.Write(batchLength[:4]) // Write batch length
for _, tx := range transactions { // Iterate through transactions
txJSON, err := tx.StringBytes() // Get JSON
if err != nil { // Check for errors
return nil, err // Return found error
}
txPayload, err := ParseJSON(txJSON, string(tx.GetStringBytes("tag"))) // Parse JSON
if err != nil { // Check for errors
return nil, err // Return found error
}
_, err = payload.Write(txPayload) // Write payload
if err != nil { // Check for errors
return nil, err // Return found error
}
}
return payload.Bytes(), nil // Return payload
}
/*
END TAG HANDLERS
*/
/* END INTERNAL METHODS */