-
Notifications
You must be signed in to change notification settings - Fork 672
/
operation.go
121 lines (104 loc) · 3.03 KB
/
operation.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-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"bytes"
"errors"
"sort"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var (
ErrNilOperation = errors.New("nil operation is not valid")
ErrNilFxOperation = errors.New("nil fx operation is not valid")
ErrNotSortedAndUniqueUTXOIDs = errors.New("utxo IDs not sorted and unique")
)
type Operation struct {
avax.Asset `serialize:"true"`
UTXOIDs []*avax.UTXOID `serialize:"true" json:"inputIDs"`
FxID ids.ID `serialize:"false" json:"fxID"`
Op fxs.FxOperation `serialize:"true" json:"operation"`
}
func (op *Operation) Verify() error {
switch {
case op == nil:
return ErrNilOperation
case op.Op == nil:
return ErrNilFxOperation
case !utils.IsSortedAndUnique(op.UTXOIDs):
return ErrNotSortedAndUniqueUTXOIDs
default:
return verify.All(&op.Asset, op.Op)
}
}
type operationAndCodec struct {
op *Operation
codec codec.Manager
}
func (o *operationAndCodec) Compare(other *operationAndCodec) int {
oBytes, err := o.codec.Marshal(CodecVersion, o.op)
if err != nil {
return 0
}
otherBytes, err := o.codec.Marshal(CodecVersion, other.op)
if err != nil {
return 0
}
return bytes.Compare(oBytes, otherBytes)
}
func SortOperations(ops []*Operation, c codec.Manager) {
sortableOps := make([]*operationAndCodec, len(ops))
for i, op := range ops {
sortableOps[i] = &operationAndCodec{
op: op,
codec: c,
}
}
utils.Sort(sortableOps)
for i, sortableOp := range sortableOps {
ops[i] = sortableOp.op
}
}
func IsSortedAndUniqueOperations(ops []*Operation, c codec.Manager) bool {
sortableOps := make([]*operationAndCodec, len(ops))
for i, op := range ops {
sortableOps[i] = &operationAndCodec{
op: op,
codec: c,
}
}
return utils.IsSortedAndUnique(sortableOps)
}
type innerSortOperationsWithSigners struct {
ops []*Operation
signers [][]*secp256k1.PrivateKey
codec codec.Manager
}
func (ops *innerSortOperationsWithSigners) Less(i, j int) bool {
iOp := ops.ops[i]
jOp := ops.ops[j]
iBytes, err := ops.codec.Marshal(CodecVersion, iOp)
if err != nil {
return false
}
jBytes, err := ops.codec.Marshal(CodecVersion, jOp)
if err != nil {
return false
}
return bytes.Compare(iBytes, jBytes) == -1
}
func (ops *innerSortOperationsWithSigners) Len() int {
return len(ops.ops)
}
func (ops *innerSortOperationsWithSigners) Swap(i, j int) {
ops.ops[j], ops.ops[i] = ops.ops[i], ops.ops[j]
ops.signers[j], ops.signers[i] = ops.signers[i], ops.signers[j]
}
func SortOperationsWithSigners(ops []*Operation, signers [][]*secp256k1.PrivateKey, codec codec.Manager) {
sort.Sort(&innerSortOperationsWithSigners{ops: ops, signers: signers, codec: codec})
}