-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathtransaction.go
616 lines (554 loc) · 17.6 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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package chain
import (
"context"
"encoding/json"
"fmt"
"reflect"
"github.com/StephenButtolph/canoto"
"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)
_ canoto.Field = (*Transaction)(nil)
)
// TransactionData represents an unsigned transaction
type TransactionData struct {
Base Base
Actions []Action
// unsignedBytes is the byte slice representation of the unsigned tx
// This field is always populated either by the constructor or via signed transaction
// parsing.
unsignedBytes []byte
}
func NewTxData(base Base, actions []Action) TransactionData {
txData := TransactionData{
Base: base,
Actions: actions,
}
// Call CalculateCanotoCache so that equivalent blocks pass an equals check.
// Without calling this function, canoto's required internal field will cause equals
// checks to fail on otherwise identical blocks.
txData.Base.CalculateCanotoCache()
actionBytes := make([]codec.Bytes, len(txData.Actions))
for i, action := range txData.Actions {
actionBytes[i] = action.Bytes()
}
// Serialize the unsigned transaction
// Note: canoto does not serialize empty fields, which allows us to
// re-use the SerializeTx intermediate type directly. This produces an
// identical serialization to creating a separate SerializeUnsignedTx
// type that omitted the Auth field.
serializeTxData := &SerializeTx{
Base: txData.Base,
Actions: actionBytes,
}
txData.unsignedBytes = serializeTxData.MarshalCanoto()
return txData
}
// UnsignedBytes returns the byte slice representation of the tx
func (t *TransactionData) UnsignedBytes() []byte {
return t.unsignedBytes
}
// 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,
) (*Transaction, error) {
auth, err := factory.Sign(t.UnsignedBytes())
if err != nil {
return nil, err
}
return NewTransaction(t.Base, t.Actions, auth)
}
func SignRawActionBytesTx(
base Base,
rawActionsBytes [][]byte,
authFactory AuthFactory,
) ([]byte, error) {
codecBytes := make([]codec.Bytes, len(rawActionsBytes))
for i, actionBytes := range rawActionsBytes {
codecBytes[i] = actionBytes
}
tx := &SerializeTx{
Base: base,
Actions: codecBytes,
}
unsignedTxBytes := tx.MarshalCanoto()
auth, err := authFactory.Sign(unsignedTxBytes)
if err != nil {
return nil, err
}
tx.Auth = auth.Bytes()
return tx.MarshalCanoto(), nil
}
func (t *TransactionData) GetExpiry() int64 { return t.Base.Timestamp }
func (t *TransactionData) MaxFee() uint64 { return t.Base.MaxFee }
// Transaction is a signed transaction that can be executed on chain.
// Transaction must be treated as immutable.
//
// Transaction implements [canoto.Field] using the [SerializeTx] field
// as an intermediate representation, so that it can convert from the
// Action/Auth types to corresponding raw byte slices.
// This additionally allows the transaction type to cache the pre-calculated
// bytes, size, and id fields, so that they never need to be re-computed.
type Transaction struct {
TransactionData
Auth Auth `json:"auth"`
bytes []byte
size int
id ids.ID
stateKeys state.Keys
}
// NewTransaction creates a Transaction and initializes the private fields.
func NewTransaction(base Base, actions []Action, auth Auth) (*Transaction, error) {
txData := NewTxData(base, actions)
actionBytes := make([]codec.Bytes, len(actions))
for i, action := range actions {
actionBytes[i] = action.Bytes()
}
serializeSignedTx := &SerializeTx{
Base: base,
Actions: actionBytes,
Auth: auth.Bytes(),
}
signedTxBytes := serializeSignedTx.MarshalCanoto()
return &Transaction{
TransactionData: txData,
Auth: auth,
bytes: signedTxBytes,
size: len(signedTxBytes),
id: utils.ToID(signedTxBytes),
}, nil
}
func (t *Transaction) Bytes() []byte { return t.bytes }
func (t *Transaction) Size() int { return t.size }
func (t *Transaction) GetID() ids.ID { return t.id }
// StateKeys calculates the set of state keys pre-declared by the transaction.
// This function caches the state keys internally, which makes it unsafe to call in parallel.
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.GetID(), 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 returns the multi-dimensional fee units required by the transaction. The corresponding
// fee will be charged in full regardless of the transaction's execution result.
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, fmt.Errorf("failed to calculate tx units: %w", err)
}
fee, err := feeManager.Fee(units)
if err != nil {
// Should never happen
return nil, fmt.Errorf("failed to calculate tx fee: %w", 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, fmt.Errorf("failed to deduct tx fee: %w", 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.GetID(), uint8(i)))
if err != nil {
ts.Rollback(ctx, actionStart)
return &Result{
Success: false,
Error: utils.ErrBytes(err),
Outputs: actionOutputs,
Units: units,
Fee: fee,
}, nil
}
actionOutputs = append(actionOutputs, actionOutput)
}
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) GetSponsor() codec.Address { return t.Auth.Sponsor() }
type txJSON struct {
ID ids.ID `json:"id"`
Actions []codec.Bytes `json:"actions"`
Auth codec.Bytes `json:"auth"`
Base Base `json:"base"`
}
func (t *Transaction) MarshalJSON() ([]byte, error) {
actionBytes := make([]codec.Bytes, len(t.Actions))
for i, action := range t.Actions {
actionBytes[i] = action.Bytes()
}
return json.Marshal(txJSON{
ID: t.GetID(),
Actions: actionBytes,
Auth: t.Auth.Bytes(),
Base: t.Base,
})
}
func (t *Transaction) UnmarshalJSON(data []byte, parser Parser) error {
var tx txJSON
err := json.Unmarshal(data, &tx)
if err != nil {
return err
}
actions := make([]Action, len(tx.Actions))
for i, actionBytes := range tx.Actions {
action, err := parser.ParseAction(actionBytes)
if err != nil {
return fmt.Errorf("failed to parse action %x at index %d: %w", actionBytes, i, err)
}
actions[i] = action
}
auth, err := parser.ParseAuth(tx.Auth)
if err != nil {
return fmt.Errorf("failed to parse auth %x: %w", tx.Auth, err)
}
unmarshalledTx, err := NewTransaction(tx.Base, actions, auth)
if err != nil {
return err
}
*t = *unmarshalledTx
return nil
}
// VerifyAuth verifies that the transaction was signed correctly.
func (t *Transaction) VerifyAuth(ctx context.Context) error {
msg := t.UnsignedBytes()
return t.Auth.Verify(ctx, msg)
}
func UnmarshalTx(
bytes []byte,
parser Parser,
) (*Transaction, error) {
reader := canoto.Reader{
B: bytes,
Context: parser,
}
tx := &Transaction{}
if err := tx.UnmarshalCanotoFrom(reader); err != nil {
return nil, err
}
return tx, nil
}
// 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 []Action, authFactory AuthFactory) (fees.Dimensions, error) {
var (
bandwidth = uint64(MaxBaseSize)
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 {
actionBytes := action.Bytes()
actionSize := len(actionBytes)
actor := authFactory.Address()
stateKeys := action.StateKeys(actor, CreateActionID(ids.Empty, uint8(i)))
actionStateKeysMaxChunks, ok := stateKeys.ChunkSizes()
if !ok {
return fees.Dimensions{}, ErrInvalidKeyValue
}
bandwidth += uint64(actionSize)
stateKeysMaxChunks = append(stateKeysMaxChunks, actionStateKeysMaxChunks...)
computeOp.Add(action.ComputeUnits(r))
}
authBandwidth, authCompute := authFactory.MaxUnits()
bandwidth += 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
}
func (t *Transaction) MarshalCanotoInto(w canoto.Writer) canoto.Writer {
canoto.Append(&w, t.bytes)
return w
}
// CalculateCanotoCache is a no-op for [Transaction] because it is immutable
// and already cached in the internal bytes field.
func (*Transaction) CalculateCanotoCache() {}
func (t *Transaction) CachedCanotoSize() uint64 { return uint64(t.size) }
func (t *Transaction) UnmarshalCanotoFrom(r canoto.Reader) error {
serializeTx := &SerializeTx{}
if err := serializeTx.UnmarshalCanotoFrom(r); err != nil {
return err
}
parser, ok := r.Context.(Parser)
if !ok {
return fmt.Errorf("failed to extract Parser from canoto context of type: %T", r.Context)
}
actions := make([]Action, len(serializeTx.Actions))
for i, actionBytes := range serializeTx.Actions {
action, err := parser.ParseAction(actionBytes)
if err != nil {
return fmt.Errorf("failed to parse action %x at index %d: %w", actionBytes, i, err)
}
actions[i] = action
}
auth, err := parser.ParseAuth(serializeTx.Auth)
if err != nil {
return fmt.Errorf("failed to parse auth %x: %w", serializeTx.Auth, err)
}
// We do not assume that the auth field is non-zero le
var unsignedTxBytes []byte
// If the auth field is zero-length, then the unsigned tx bytes are identical to the
// bytes of the transaction. This is an unexpected case because it's unlikely an auth
// parser would allow a zero-length byte slice to be parsed as a valid auth and should
// error above. However, we do not assume a zero-length auth field is invalid, so we
// handle the case here.
if len(serializeTx.Auth) == 0 {
unsignedTxBytes = r.B
} else {
authSuffixSize := len(canoto__SerializeTx__Auth__tag) + int(canoto.SizeBytes(serializeTx.Auth))
unsignedTxBytesLimit := len(r.B) - authSuffixSize
// Defensive: check to ensure the calculated auth suffix size is within expected bounds
// and return an error rather than panic on index out of bounds if not.
if unsignedTxBytesLimit < 0 || unsignedTxBytesLimit > len(r.B) {
return fmt.Errorf("failed to extract unsigned tx bytes due to invalid offset: %d, tx size: %d auth suffix size: %d",
unsignedTxBytesLimit,
len(r.B),
authSuffixSize,
)
}
unsignedTxBytes = r.B[:len(r.B)-authSuffixSize]
}
tx := &Transaction{
TransactionData: TransactionData{
Base: serializeTx.Base,
Actions: actions,
unsignedBytes: unsignedTxBytes,
},
Auth: auth,
}
tx.bytes = r.B
tx.size = len(tx.bytes)
tx.id = utils.ToID(tx.bytes)
*t = *tx
return nil
}
func (*Transaction) ValidCanoto() bool { return true }
// CanotoSpec returns the specification of this canoto message.
// Required for canoto.Field interface implementation.
// Delegates to SerializeTx since Transaction uses it for serialization.
func (*Transaction) CanotoSpec(types ...reflect.Type) *canoto.Spec {
serializeTx := &SerializeTx{}
return serializeTx.CanotoSpec(types...)
}
func GenerateTransaction(
ruleFactory RuleFactory,
unitPrices fees.Dimensions,
timestamp int64,
actions []Action,
authFactory AuthFactory,
) (*Transaction, error) {
rules := ruleFactory.GetRules(timestamp)
units, err := EstimateUnits(rules, actions, authFactory)
if err != nil {
return nil, err
}
maxFee, err := fees.MulSum(unitPrices, units)
if err != nil {
return nil, err
}
tx, err := GenerateTransactionManual(rules, timestamp, actions, authFactory, maxFee)
if err != nil {
return nil, err
}
return tx, nil
}
func GenerateTransactionManual(
rules Rules,
timestamp int64,
actions []Action,
authFactory AuthFactory,
maxFee uint64,
) (*Transaction, error) {
unsignedTx := NewTxData(
Base{
Timestamp: utils.UnixRMilli(timestamp, rules.GetValidityWindow()),
ChainID: rules.GetChainID(),
MaxFee: maxFee,
},
actions,
)
tx, err := unsignedTx.Sign(authFactory)
if err != nil {
return nil, fmt.Errorf("%w: failed to sign transaction", err)
}
return tx, nil
}