forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx_init.go
142 lines (121 loc) · 2.61 KB
/
tx_init.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"reflect"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/avm/txs"
)
var _ txs.Visitor = &txInit{}
// Init initializes FxID where required
type txInit struct {
tx *txs.Tx
ctx *snow.Context
typeToFxIndex map[reflect.Type]int
fxs []*fxs.ParsedFx
}
func (t *txInit) getFx(val interface{}) (int, error) {
valType := reflect.TypeOf(val)
fx, exists := t.typeToFxIndex[valType]
if !exists {
return 0, errUnknownFx
}
return fx, nil
}
// getParsedFx returns the parsedFx object for a given TransferableInput
// or TransferableOutput object
func (t *txInit) getParsedFx(val interface{}) (*fxs.ParsedFx, error) {
idx, err := t.getFx(val)
if err != nil {
return nil, err
}
return t.fxs[idx], nil
}
func (t *txInit) init() error {
t.tx.Unsigned.InitCtx(t.ctx)
for _, cred := range t.tx.Creds {
fx, err := t.getParsedFx(cred.Verifiable)
if err != nil {
return err
}
cred.FxID = fx.ID
}
return nil
}
func (t *txInit) BaseTx(tx *txs.BaseTx) error {
if err := t.init(); err != nil {
return err
}
for _, in := range tx.Ins {
fx, err := t.getParsedFx(in.In)
if err != nil {
return err
}
in.FxID = fx.ID
}
for _, out := range tx.Outs {
fx, err := t.getParsedFx(out.Out)
if err != nil {
return err
}
out.FxID = fx.ID
}
return nil
}
func (t *txInit) CreateAssetTx(tx *txs.CreateAssetTx) error {
if err := t.init(); err != nil {
return err
}
for _, state := range tx.States {
fx := t.fxs[state.FxIndex]
state.FxID = fx.ID
}
for _, in := range tx.Ins {
fx, err := t.getParsedFx(in.In)
if err != nil {
return err
}
in.FxID = fx.ID
}
return t.BaseTx(&tx.BaseTx)
}
func (t *txInit) ImportTx(tx *txs.ImportTx) error {
if err := t.init(); err != nil {
return err
}
for _, in := range tx.ImportedIns {
fx, err := t.getParsedFx(in.In)
if err != nil {
return err
}
in.FxID = fx.ID
}
return t.BaseTx(&tx.BaseTx)
}
func (t *txInit) ExportTx(tx *txs.ExportTx) error {
if err := t.init(); err != nil {
return err
}
for _, out := range tx.ExportedOuts {
fx, err := t.getParsedFx(out.Out)
if err != nil {
return err
}
out.FxID = fx.ID
}
return t.BaseTx(&tx.BaseTx)
}
func (t *txInit) OperationTx(tx *txs.OperationTx) error {
if err := t.init(); err != nil {
return err
}
for _, op := range tx.Ops {
fx, err := t.getParsedFx(op.Op)
if err != nil {
return err
}
op.FxID = fx.ID
}
return t.BaseTx(&tx.BaseTx)
}