forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
operation_tx.go
121 lines (103 loc) · 2.88 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
)
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)(nil)
_ secp256k1fx.UnsignedTx = (*OperationTx)(nil)
)
// OperationTx is a transaction with no credentials.
type OperationTx struct {
BaseTx `serialize:"true"`
Ops []*Operation `serialize:"true" json:"operations"`
}
func (t *OperationTx) InitCtx(ctx *snow.Context) {
for _, op := range t.Ops {
op.Op.InitCtx(ctx)
}
t.BaseTx.InitCtx(ctx)
}
// Operations track which ops this transaction is performing. The returned array
// should not be modified.
func (t *OperationTx) Operations() []*Operation { return t.Ops }
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.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)
}
// 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
}
func (t *OperationTx) Visit(v Visitor) error {
return v.OperationTx(t)
}