-
Notifications
You must be signed in to change notification settings - Fork 669
/
operation_tx.go
157 lines (136 loc) · 3.73 KB
/
operation_tx.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"errors"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var (
errOperationsNotSortedUnique = errors.New("operations not sorted and unique")
errNoOperations = errors.New("an operationTx must have at least one operation")
errDoubleSpend = errors.New("inputs attempt to double spend an input")
_ UnsignedTx = &OperationTx{}
)
// OperationTx is a transaction with no credentials.
type OperationTx struct {
BaseTx `serialize:"true"`
Ops []*Operation `serialize:"true" json:"operations"`
}
func (t *OperationTx) Init(vm *VM) error {
for _, op := range t.Ops {
fx, err := vm.getParsedFx(op.Op)
if err != nil {
return err
}
op.FxID = fx.ID
op.Op.InitCtx(vm.ctx)
}
return t.BaseTx.Init(vm)
}
// Operations track which ops this transaction is performing. The returned array
// should not be modified.
func (t *OperationTx) Operations() []*Operation { return t.Ops }
// InputUTXOs track which UTXOs this transaction is consuming.
func (t *OperationTx) InputUTXOs() []*avax.UTXOID {
utxos := t.BaseTx.InputUTXOs()
for _, op := range t.Ops {
utxos = append(utxos, op.UTXOIDs...)
}
return utxos
}
// ConsumedAssetIDs returns the IDs of the assets this transaction consumes
func (t *OperationTx) ConsumedAssetIDs() ids.Set {
assets := t.BaseTx.AssetIDs()
for _, op := range t.Ops {
if len(op.UTXOIDs) > 0 {
assets.Add(op.AssetID())
}
}
return assets
}
// AssetIDs returns the IDs of the assets this transaction depends on
func (t *OperationTx) AssetIDs() ids.Set {
assets := t.BaseTx.AssetIDs()
for _, op := range t.Ops {
assets.Add(op.AssetID())
}
return assets
}
// NumCredentials returns the number of expected credentials
func (t *OperationTx) NumCredentials() int { return t.BaseTx.NumCredentials() + len(t.Ops) }
// UTXOs returns the UTXOs transaction is producing.
func (t *OperationTx) UTXOs() []*avax.UTXO {
txID := t.ID()
utxos := t.BaseTx.UTXOs()
for _, op := range t.Ops {
asset := op.AssetID()
for _, out := range op.Op.Outs() {
utxos = append(utxos, &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(len(utxos)),
},
Asset: avax.Asset{ID: asset},
Out: out,
})
}
}
return utxos
}
// SyntacticVerify that this transaction is well-formed.
func (t *OperationTx) SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
txFee uint64,
_ uint64,
numFxs int,
) error {
switch {
case t == nil:
return errNilTx
case len(t.Ops) == 0:
return errNoOperations
}
if err := t.BaseTx.SyntacticVerify(ctx, c, txFeeAssetID, txFee, txFee, numFxs); err != nil {
return err
}
inputs := ids.NewSet(len(t.Ins))
for _, in := range t.Ins {
inputs.Add(in.InputID())
}
for _, op := range t.Ops {
if err := op.Verify(c); err != nil {
return err
}
for _, utxoID := range op.UTXOIDs {
inputID := utxoID.InputID()
if inputs.Contains(inputID) {
return errDoubleSpend
}
inputs.Add(inputID)
}
}
if !isSortedAndUniqueOperations(t.Ops, c) {
return errOperationsNotSortedUnique
}
return nil
}
// SemanticVerify that this transaction is well-formed.
func (t *OperationTx) SemanticVerify(vm *VM, tx UnsignedTx, creds []verify.Verifiable) error {
if err := t.BaseTx.SemanticVerify(vm, tx, creds); err != nil {
return err
}
offset := t.BaseTx.NumCredentials()
for i, op := range t.Ops {
cred := creds[offset+i]
if err := vm.verifyOperation(tx, op, cred); err != nil {
return err
}
}
return nil
}