-
Notifications
You must be signed in to change notification settings - Fork 110
/
transaction.go
591 lines (527 loc) · 15.2 KB
/
transaction.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"context"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/fees"
"github.com/ava-labs/hypersdk/internal/emap"
"github.com/ava-labs/hypersdk/internal/math"
"github.com/ava-labs/hypersdk/internal/mempool"
"github.com/ava-labs/hypersdk/keys"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/state/tstate"
"github.com/ava-labs/hypersdk/utils"
internalfees "github.com/ava-labs/hypersdk/internal/fees"
)
var (
_ emap.Item = (*Transaction)(nil)
_ mempool.Item = (*Transaction)(nil)
)
type TransactionData struct {
Base *Base `json:"base"`
Actions Actions `json:"actions"`
unsignedBytes []byte
}
func NewTxData(base *Base, actions Actions) *TransactionData {
return &TransactionData{
Base: base,
Actions: actions,
}
}
// UnsignedBytes returns the byte slice representation of the tx
func (t *TransactionData) UnsignedBytes() ([]byte, error) {
if len(t.unsignedBytes) > 0 {
return t.unsignedBytes, nil
}
size := t.Base.Size() + consts.Uint8Len
actionsSize, err := t.Actions.Size()
if err != nil {
return nil, err
}
size += actionsSize
p := codec.NewWriter(size, consts.NetworkSizeLimit)
if err := t.marshal(p); err != nil {
return nil, err
}
t.unsignedBytes = p.Bytes()
return t.unsignedBytes, p.Err()
}
// Sign returns a new signed transaction with the unsigned tx copied from
// the original and a signature provided by the authFactory
func (t *TransactionData) Sign(
factory AuthFactory,
actionCodec *codec.TypeParser[Action],
authCodec *codec.TypeParser[Auth],
) (*Transaction, error) {
msg, err := t.UnsignedBytes()
if err != nil {
return nil, err
}
auth, err := factory.Sign(msg)
if err != nil {
return nil, err
}
signedTransaction := Transaction{
TransactionData: TransactionData{
Base: t.Base,
Actions: t.Actions,
},
Auth: auth,
}
// Ensure transaction is fully initialized and correct by reloading it from
// bytes
size := len(msg) + consts.ByteLen + auth.Size()
p := codec.NewWriter(size, consts.NetworkSizeLimit)
if err := signedTransaction.Marshal(p); err != nil {
return nil, err
}
if err := p.Err(); err != nil {
return nil, err
}
p = codec.NewReader(p.Bytes(), consts.MaxInt)
return UnmarshalTx(p, actionCodec, authCodec)
}
func SignRawActionBytesTx(
base *Base,
rawActionsBytes []byte,
authFactory AuthFactory,
) ([]byte, error) {
p := codec.NewWriter(base.Size(), consts.NetworkSizeLimit)
base.Marshal(p)
p.PackFixedBytes(rawActionsBytes)
auth, err := authFactory.Sign(p.Bytes())
if err != nil {
return nil, err
}
p.PackByte(auth.GetTypeID())
auth.Marshal(p)
return p.Bytes(), p.Err()
}
func (t *TransactionData) Expiry() int64 { return t.Base.Timestamp }
func (t *TransactionData) MaxFee() uint64 { return t.Base.MaxFee }
func (t *TransactionData) Marshal(p *codec.Packer) error {
if len(t.unsignedBytes) > 0 {
p.PackFixedBytes(t.unsignedBytes)
return p.Err()
}
return t.marshal(p)
}
func (t *TransactionData) marshal(p *codec.Packer) error {
t.Base.Marshal(p)
return t.Actions.MarshalInto(p)
}
type Actions []Action
func (a Actions) Size() (int, error) {
var size int
for _, action := range a {
actionSize, err := GetSize(action)
if err != nil {
return 0, err
}
size += consts.ByteLen + actionSize
}
return size, nil
}
func (a Actions) MarshalInto(p *codec.Packer) error {
p.PackByte(uint8(len(a)))
for _, action := range a {
p.PackByte(action.GetTypeID())
err := marshalInto(action, p)
if err != nil {
return err
}
}
return nil
}
type Transaction struct {
TransactionData
Auth Auth `json:"auth"`
bytes []byte
size int
id ids.ID
stateKeys state.Keys
}
func (t *Transaction) Bytes() []byte { return t.bytes }
func (t *Transaction) Size() int { return t.size }
func (t *Transaction) ID() ids.ID { return t.id }
func (t *Transaction) StateKeys(bh BalanceHandler) (state.Keys, error) {
if t.stateKeys != nil {
return t.stateKeys, nil
}
stateKeys := make(state.Keys)
// Verify the formatting of state keys passed by the controller
for i, action := range t.Actions {
for k, v := range action.StateKeys(t.Auth.Actor(), CreateActionID(t.ID(), uint8(i))) {
if !stateKeys.Add(k, v) {
return nil, ErrInvalidKeyValue
}
}
}
for k, v := range bh.SponsorStateKeys(t.Auth.Sponsor()) {
if !stateKeys.Add(k, v) {
return nil, ErrInvalidKeyValue
}
}
// Cache keys if called again
t.stateKeys = stateKeys
return stateKeys, nil
}
// Units is charged whether or not a transaction is successful.
func (t *Transaction) Units(bh BalanceHandler, r Rules) (fees.Dimensions, error) {
// Calculate compute usage
computeOp := math.NewUint64Operator(r.GetBaseComputeUnits())
for _, action := range t.Actions {
computeOp.Add(action.ComputeUnits(r))
}
computeOp.Add(t.Auth.ComputeUnits(r))
maxComputeUnits, err := computeOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
// Calculate storage usage
stateKeys, err := t.StateKeys(bh)
if err != nil {
return fees.Dimensions{}, err
}
readsOp := math.NewUint64Operator(0)
allocatesOp := math.NewUint64Operator(0)
writesOp := math.NewUint64Operator(0)
for k := range stateKeys {
// Compute key costs
readsOp.Add(r.GetStorageKeyReadUnits())
allocatesOp.Add(r.GetStorageKeyAllocateUnits())
writesOp.Add(r.GetStorageKeyWriteUnits())
// Compute value costs
maxChunks, ok := keys.MaxChunks([]byte(k))
if !ok {
return fees.Dimensions{}, ErrInvalidKeyValue
}
readsOp.MulAdd(uint64(maxChunks), r.GetStorageValueReadUnits())
allocatesOp.MulAdd(uint64(maxChunks), r.GetStorageValueAllocateUnits())
writesOp.MulAdd(uint64(maxChunks), r.GetStorageValueWriteUnits())
}
reads, err := readsOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
allocates, err := allocatesOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
writes, err := writesOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
return fees.Dimensions{uint64(t.Size()), maxComputeUnits, reads, allocates, writes}, nil
}
func (t *Transaction) PreExecute(
ctx context.Context,
feeManager *internalfees.Manager,
bh BalanceHandler,
r Rules,
im state.Immutable,
timestamp int64,
) error {
if err := t.Base.Execute(r, timestamp); err != nil {
return err
}
if len(t.Actions) > int(r.GetMaxActionsPerTx()) {
return ErrTooManyActions
}
for i, action := range t.Actions {
start, end := action.ValidRange(r)
if start >= 0 && timestamp < start {
return fmt.Errorf("%w: action type %d at index %d", ErrActionNotActivated, action.GetTypeID(), i)
}
if end >= 0 && timestamp > end {
return fmt.Errorf("%w: action type %d at index %d", ErrActionNotActivated, action.GetTypeID(), i)
}
}
start, end := t.Auth.ValidRange(r)
if start >= 0 && timestamp < start {
return ErrAuthNotActivated
}
if end >= 0 && timestamp > end {
return ErrAuthNotActivated
}
units, err := t.Units(bh, r)
if err != nil {
return err
}
fee, err := feeManager.Fee(units)
if err != nil {
return err
}
return bh.CanDeduct(ctx, t.Auth.Sponsor(), im, fee)
}
// Execute after knowing a transaction can pay a fee. Attempt
// to charge the fee in as many cases as possible.
//
// Invariant: [PreExecute] is called just before [Execute]
func (t *Transaction) Execute(
ctx context.Context,
feeManager *internalfees.Manager,
bh BalanceHandler,
r Rules,
ts *tstate.TStateView,
timestamp int64,
) (*Result, error) {
// Always charge fee first
units, err := t.Units(bh, r)
if err != nil {
// Should never happen
return nil, err
}
fee, err := feeManager.Fee(units)
if err != nil {
// Should never happen
return nil, err
}
if err := bh.Deduct(ctx, t.Auth.Sponsor(), ts, fee); err != nil {
// This should never fail for low balance (as we check [CanDeductFee]
// immediately before).
return nil, err
}
// We create a temp state checkpoint to ensure we don't commit failed actions to state.
//
// We should favor reverting over returning an error because the caller won't be charged
// for a transaction that returns an error.
var (
actionStart = ts.OpIndex()
actionOutputs = [][]byte{}
)
for i, action := range t.Actions {
actionOutput, err := action.Execute(ctx, r, ts, timestamp, t.Auth.Actor(), CreateActionID(t.ID(), uint8(i)))
if err != nil {
ts.Rollback(ctx, actionStart)
return &Result{false, utils.ErrBytes(err), actionOutputs, units, fee}, nil
}
var encodedOutput []byte
if actionOutput == nil {
// Ensure output standardization (match form we will
// unmarshal)
encodedOutput = []byte{}
} else {
encodedOutput, err = MarshalTyped(actionOutput)
if err != nil {
return nil, err
}
}
actionOutputs = append(actionOutputs, encodedOutput)
}
return &Result{
Success: true,
Error: []byte{},
Outputs: actionOutputs,
Units: units,
Fee: fee,
}, nil
}
// Sponsor is the [codec.Address] that pays fees for this transaction.
func (t *Transaction) Sponsor() codec.Address { return t.Auth.Sponsor() }
func (t *Transaction) Marshal(p *codec.Packer) error {
if len(t.bytes) > 0 {
p.PackFixedBytes(t.bytes)
return p.Err()
}
return t.marshal(p)
}
func (t *Transaction) marshal(p *codec.Packer) error {
if err := t.TransactionData.marshal(p); err != nil {
return err
}
authID := t.Auth.GetTypeID()
p.PackByte(authID)
t.Auth.Marshal(p)
return p.Err()
}
// VerifyAuth verifies that the transaction was signed correctly.
func (t *Transaction) VerifyAuth(ctx context.Context) error {
msg, err := t.UnsignedBytes()
if err != nil {
// Should never occur because populated during unmarshal
return err
}
return t.Auth.Verify(ctx, msg)
}
func UnmarshalTxData(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
) (*TransactionData, error) {
start := p.Offset()
base, err := UnmarshalBase(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal base", err)
}
actions, err := UnmarshalActions(p, actionRegistry)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal actions", err)
}
var tx TransactionData
tx.Base = base
tx.Actions = actions
if err := p.Err(); err != nil {
return nil, p.Err()
}
codecBytes := p.Bytes()
tx.unsignedBytes = codecBytes[start:p.Offset()] // ensure errors handled before grabbing memory
return &tx, nil
}
func UnmarshalActions(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
) (Actions, error) {
actionCount := p.UnpackByte()
if actionCount == 0 {
return nil, fmt.Errorf("%w: no actions", ErrInvalidObject)
}
actions := Actions{}
for i := uint8(0); i < actionCount; i++ {
action, err := actionRegistry.Unmarshal(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal action", err)
}
actions = append(actions, action)
}
return actions, nil
}
func UnmarshalTxs(
raw []byte,
initialCapacity int,
actionRegistry *codec.TypeParser[Action],
authRegistry *codec.TypeParser[Auth],
) (map[uint8]int, []*Transaction, error) {
p := codec.NewReader(raw, consts.NetworkSizeLimit)
txCount := p.UnpackInt(true)
authCounts := map[uint8]int{}
txs := make([]*Transaction, 0, initialCapacity) // DoS to set size to txCount
for i := uint32(0); i < txCount; i++ {
tx, err := UnmarshalTx(p, actionRegistry, authRegistry)
if err != nil {
return nil, nil, err
}
txs = append(txs, tx)
authCounts[tx.Auth.GetTypeID()]++
}
if !p.Empty() {
// Ensure no leftover bytes
return nil, nil, ErrInvalidObject
}
return authCounts, txs, p.Err()
}
func UnmarshalTx(
p *codec.Packer,
actionRegistry *codec.TypeParser[Action],
authRegistry *codec.TypeParser[Auth],
) (*Transaction, error) {
start := p.Offset()
unsignedTransaction, err := UnmarshalTxData(p, actionRegistry)
if err != nil {
return nil, err
}
auth, err := authRegistry.Unmarshal(p)
if err != nil {
return nil, fmt.Errorf("%w: could not unmarshal auth", err)
}
authType := auth.GetTypeID()
if actorType := auth.Actor()[0]; actorType != authType {
return nil, fmt.Errorf("%w: actorType (%d) did not match authType (%d)", ErrInvalidActor, actorType, authType)
}
if sponsorType := auth.Sponsor()[0]; sponsorType != authType {
return nil, fmt.Errorf("%w: sponsorType (%d) did not match authType (%d)", ErrInvalidSponsor, sponsorType, authType)
}
var tx Transaction
tx.TransactionData = *unsignedTransaction
tx.Auth = auth
if err := p.Err(); err != nil {
return nil, p.Err()
}
codecBytes := p.Bytes()
tx.bytes = codecBytes[start:p.Offset()] // ensure errors handled before grabbing memory
tx.size = len(tx.bytes)
tx.id = utils.ToID(tx.bytes)
return &tx, nil
}
func MarshalTxs(txs []*Transaction) ([]byte, error) {
if len(txs) == 0 {
return nil, ErrNoTxs
}
size := consts.IntLen + codec.CummSize(txs)
p := codec.NewWriter(size, consts.NetworkSizeLimit)
p.PackInt(uint32(len(txs)))
for _, tx := range txs {
if err := tx.Marshal(p); err != nil {
return nil, err
}
}
return p.Bytes(), p.Err()
}
// EstimateUnits provides a pessimistic estimate (some key accesses may be duplicates) of the cost
// to execute a transaction.
//
// This is typically used during transaction construction.
func EstimateUnits(r Rules, actions Actions, authFactory AuthFactory) (fees.Dimensions, error) {
var (
bandwidth = uint64(BaseSize)
stateKeysMaxChunks = []uint16{} // TODO: preallocate
computeOp = math.NewUint64Operator(r.GetBaseComputeUnits())
readsOp = math.NewUint64Operator(0)
allocatesOp = math.NewUint64Operator(0)
writesOp = math.NewUint64Operator(0)
)
// Calculate over action/auth
bandwidth += consts.Uint8Len
for i, action := range actions {
actionSize, err := GetSize(action)
if err != nil {
return fees.Dimensions{}, err
}
actor := authFactory.Address()
stateKeys := action.StateKeys(actor, CreateActionID(ids.Empty, uint8(i)))
actionStateKeysMaxChunks, ok := stateKeys.ChunkSizes()
if !ok {
return fees.Dimensions{}, ErrInvalidKeyValue
}
bandwidth += consts.ByteLen + uint64(actionSize)
stateKeysMaxChunks = append(stateKeysMaxChunks, actionStateKeysMaxChunks...)
computeOp.Add(action.ComputeUnits(r))
}
authBandwidth, authCompute := authFactory.MaxUnits()
bandwidth += consts.ByteLen + authBandwidth
sponsorStateKeyMaxChunks := r.GetSponsorStateKeysMaxChunks()
stateKeysMaxChunks = append(stateKeysMaxChunks, sponsorStateKeyMaxChunks...)
computeOp.Add(authCompute)
// Estimate compute costs
compute, err := computeOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
// Estimate storage costs
for _, maxChunks := range stateKeysMaxChunks {
// Compute key costs
readsOp.Add(r.GetStorageKeyReadUnits())
allocatesOp.Add(r.GetStorageKeyAllocateUnits())
writesOp.Add(r.GetStorageKeyWriteUnits())
// Compute value costs
readsOp.MulAdd(uint64(maxChunks), r.GetStorageValueReadUnits())
allocatesOp.MulAdd(uint64(maxChunks), r.GetStorageValueAllocateUnits())
writesOp.MulAdd(uint64(maxChunks), r.GetStorageValueWriteUnits())
}
reads, err := readsOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
allocates, err := allocatesOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
writes, err := writesOp.Value()
if err != nil {
return fees.Dimensions{}, err
}
return fees.Dimensions{bandwidth, compute, reads, allocates, writes}, nil
}