-
Notifications
You must be signed in to change notification settings - Fork 42
/
tx_payload.go
409 lines (301 loc) · 10.3 KB
/
tx_payload.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
package wavelet
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"github.com/perlin-network/wavelet/sys"
"github.com/pkg/errors"
)
type (
Transfer struct {
Recipient AccountID
Amount uint64
// The rest of the fields below are only populated
// should the transaction be made with a recipient
// that is a smart contract address.
GasLimit uint64
GasDeposit uint64
FuncName []byte
FuncParams []byte
}
Stake struct {
Opcode byte
Amount uint64
}
Contract struct {
GasLimit uint64
GasDeposit uint64
Params []byte
Code []byte
}
Batch struct {
Size uint8
Tags []uint8
Payloads [][]byte
}
)
// ParseTransfer parses and performs sanity checks on the payload of a transfer transaction.
func ParseTransfer(payload []byte) (Transfer, error) {
r := bytes.NewReader(payload)
b := make([]byte, 8)
var transfer Transfer
if _, err := io.ReadFull(r, transfer.Recipient[:]); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode recipient")
}
if _, err := io.ReadFull(r, b); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode amount of PERLs to send")
}
transfer.Amount = binary.LittleEndian.Uint64(b)
if r.Len() > 0 {
if _, err := io.ReadFull(r, b); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode gas limit")
}
transfer.GasLimit = binary.LittleEndian.Uint64(b)
}
if r.Len() > 0 {
if _, err := io.ReadFull(r, b[:8]); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode gas deposit")
}
transfer.GasDeposit = binary.LittleEndian.Uint64(b)
}
if r.Len() > 0 {
if _, err := io.ReadFull(r, b[:4]); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode size of smart contract function name to invoke")
}
size := binary.LittleEndian.Uint32(b[:4])
if size > 1024 {
return transfer, errors.New("transfer: smart contract function name exceeds 1024 characters")
}
transfer.FuncName = make([]byte, size)
if _, err := io.ReadFull(r, transfer.FuncName); err != nil {
return transfer, errors.Wrap(err, "transfer: failed to decode smart contract function name to invoke")
}
if string(transfer.FuncName) == "init" {
return transfer, errors.New("transfer: not allowed to call init function for smart contract")
}
}
if r.Len() > 0 {
if _, err := io.ReadFull(r, b[:4]); err != nil {
return transfer, errors.Wrap(
err, "transfer: failed to decode number of smart contract function invocation parameters",
)
}
size := binary.LittleEndian.Uint32(b[:4])
if size > 1*1024*1024 {
return transfer, errors.New("transfer: smart contract payload exceeds 1MB")
}
transfer.FuncParams = make([]byte, size)
if _, err := io.ReadFull(r, transfer.FuncParams); err != nil {
return transfer, errors.Wrap(
err, "transfer: failed to decode smart contract function invocation parameters",
)
}
}
if transfer.GasLimit == 0 && len(transfer.FuncName) > 0 {
return transfer, errors.New(
"transfer: gas limit for invoking smart contract function must be greater than zero",
)
}
return transfer, nil
}
// ParseStake parses and performs sanity checks on the payload of a stake transaction.
func ParseStake(payload []byte) (Stake, error) {
var stake Stake
if len(payload) != 9 {
return stake, errors.New("stake: payload must be exactly 9 bytes")
}
stake.Opcode = payload[0]
if stake.Opcode > sys.WithdrawReward {
return stake, errors.New("stake: opcode must be 0, 1, or 2")
}
stake.Amount = binary.LittleEndian.Uint64(payload[1:9])
if stake.Amount == 0 {
return stake, errors.New("stake: amount must be greater than zero")
}
if stake.Opcode == sys.WithdrawReward && stake.Amount < sys.MinimumRewardWithdraw {
return stake, errors.Errorf(
"stake: must withdraw a reward of a minimum of %d PERLs, but requested to withdraw %d PERLs",
sys.MinimumRewardWithdraw, stake.Amount,
)
}
return stake, nil
}
// ParseContract parses and performs sanity checks on the payload of a contract transaction.
func ParseContract(payload []byte) (Contract, error) {
r := bytes.NewReader(payload)
b := make([]byte, 8)
var contract Contract
if _, err := io.ReadFull(r, b[:8]); err != nil {
return contract, errors.Wrap(err, "contract: failed to decode gas limit")
}
contract.GasLimit = binary.LittleEndian.Uint64(b)
if _, err := io.ReadFull(r, b[:8]); err != nil {
return contract, errors.Wrap(err, "contract: failed to decode gas deposit")
}
if contract.GasLimit == 0 {
return contract, errors.New(
"contract: gas limit for invoking smart contract function must be greater than zero",
)
}
contract.GasDeposit = binary.LittleEndian.Uint64(b)
if _, err := io.ReadFull(r, b[:4]); err != nil {
return contract, errors.Wrap(err, "contract: failed to decode number of smart contract init parameters")
}
size := binary.LittleEndian.Uint32(b[:4])
if size > 1024*1024 {
return contract, errors.New("contract: smart contract payload exceeds 1MB")
}
contract.Params = make([]byte, size)
if _, err := io.ReadFull(r, contract.Params); err != nil {
return contract, errors.Wrap(err, "contract: failed to decode smart contract init parameters")
}
var err error
if contract.Code, err = ioutil.ReadAll(r); err != nil {
return contract, errors.Wrap(err, "contract: failed to decode smart contract code")
}
if len(contract.Code) == 0 {
return contract, errors.New("contract: smart contract must have code of length greater than zero")
}
return contract, nil
}
// ParseBatch parses and performs sanity checks on the payload of a batch transaction.
func ParseBatch(payload []byte) (Batch, error) {
r := bytes.NewReader(payload)
b := make([]byte, 4)
var batch Batch
if _, err := io.ReadFull(r, b[:1]); err != nil {
return batch, errors.Wrap(err, "batch: failed to decode number of transactions in batch")
}
batch.Size = b[0]
if batch.Size == 0 {
return batch, errors.New("batch: size must be greater than zero")
}
batch.Tags = make([]uint8, batch.Size)
batch.Payloads = make([][]byte, batch.Size)
for i := uint8(0); i < batch.Size; i++ {
if _, err := io.ReadFull(r, b[:1]); err != nil {
return batch, errors.Wrap(err, "batch: could not read tag")
}
if sys.Tag(b[0]) == sys.TagBatch {
return batch, errors.New("batch: entries inside batch cannot be batch transactions themselves")
}
batch.Tags[i] = b[0]
if _, err := io.ReadFull(r, b[:4]); err != nil {
return batch, errors.Wrap(err, "batch: could not read payload size")
}
size := binary.BigEndian.Uint32(b[:4])
if size > 2*1024*1024 {
return batch, errors.New("batch: payload size exceeds 2MB")
}
batch.Payloads[i] = make([]byte, size)
if _, err := io.ReadFull(r, batch.Payloads[i]); err != nil {
return batch, errors.Wrap(err, "batch: could not read payload")
}
}
return batch, nil
}
func (t Transfer) Marshal() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, 32+8+8+8+4+4))
buf.Write(t.Recipient[:])
if err := binary.Write(buf, binary.LittleEndian, t.Amount); err != nil {
return nil, errors.Wrap(err, "error marshaling amount")
}
if err := binary.Write(buf, binary.LittleEndian, t.GasLimit); err != nil {
return nil, errors.Wrap(err, "error marshaling gas limit")
}
if err := binary.Write(buf, binary.LittleEndian, t.GasDeposit); err != nil {
return nil, errors.Wrap(err, "error marshaling gas deposit")
}
if t.FuncName != nil && len(t.FuncName) > 0 {
if err := binary.Write(buf, binary.LittleEndian, uint32(len(t.FuncName))); err != nil {
return nil, errors.Wrap(err, "error marshaling func name")
}
buf.Write(t.FuncName)
if t.FuncParams != nil && len(t.FuncParams) > 0 {
if err := binary.Write(buf, binary.LittleEndian, uint32(len(t.FuncParams))); err != nil {
return nil, errors.Wrap(err, "error marshaling func params")
}
buf.Write(t.FuncParams)
}
}
return buf.Bytes(), nil
}
func (s Stake) Marshal() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, 1+8))
buf.WriteByte(s.Opcode)
if err := binary.Write(buf, binary.LittleEndian, s.Amount); err != nil {
return nil, errors.Wrap(err, "error marshaling amount")
}
return buf.Bytes(), nil
}
func (c Contract) Marshal() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, 8+8+4+len(c.Params)+len(c.Code)))
if err := binary.Write(buf, binary.LittleEndian, c.GasLimit); err != nil {
return nil, errors.Wrap(err, "error marshaling gas limit")
}
if err := binary.Write(buf, binary.LittleEndian, c.GasDeposit); err != nil {
return nil, errors.Wrap(err, "error marshaling gas deposit")
}
if err := binary.Write(buf, binary.LittleEndian, uint32(len(c.Params))); err != nil {
return nil, errors.Wrap(err, "error marshaling params")
}
buf.Write(c.Params)
buf.Write(c.Code)
return buf.Bytes(), nil
}
// AddTransfer adds a Transfer payload into a batch.
func (b *Batch) AddTransfer(t Transfer) error {
if b.Size == 255 {
return fmt.Errorf("batch cannot have more than 255 transactions")
}
b.Size++
b.Tags = append(b.Tags, uint8(sys.TagTransfer))
payload, err := t.Marshal()
if err != nil {
return errors.Wrap(err, "error marshaling transfer")
}
b.Payloads = append(b.Payloads, payload)
return nil
}
// AddStake adds a Stake payload into a batch.
func (b *Batch) AddStake(s Stake) error {
if b.Size == 255 {
return fmt.Errorf("batch cannot have more than 255 transactions")
}
b.Size++
b.Tags = append(b.Tags, uint8(sys.TagStake))
payload, err := s.Marshal()
if err != nil {
return errors.Wrap(err, "error marshaling stake")
}
b.Payloads = append(b.Payloads, payload)
return nil
}
// AddContract adds a Contract payload into a batch.
func (b *Batch) AddContract(c Contract) error {
if b.Size == 255 {
return fmt.Errorf("batch cannot have more than 255 transactions")
}
b.Size++
b.Tags = append(b.Tags, uint8(sys.TagContract))
payload, err := c.Marshal()
if err != nil {
return errors.Wrap(err, "error marshaling contract")
}
b.Payloads = append(b.Payloads, payload)
return nil
}
func (b Batch) Marshal() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, 1+(b.Size*(1+4))))
buf.WriteByte(b.Size)
for i := uint8(0); i < b.Size; i++ {
buf.WriteByte(b.Tags[i])
if err := binary.Write(buf, binary.BigEndian, uint32(len(b.Payloads[i]))); err != nil {
return nil, errors.Wrap(err, "error marshaling payload")
}
buf.Write(b.Payloads[i])
}
return buf.Bytes(), nil
}