-
Notifications
You must be signed in to change notification settings - Fork 671
/
transferables.go
252 lines (213 loc) · 7.37 KB
/
transferables.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"bytes"
"errors"
"sort"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var (
errNilTransferableOutput = errors.New("nil transferable output is not valid")
errNilTransferableFxOutput = errors.New("nil transferable feature extension output is not valid")
errOutputsNotSorted = errors.New("outputs not sorted")
errNilTransferableInput = errors.New("nil transferable input is not valid")
errNilTransferableFxInput = errors.New("nil transferable feature extension input is not valid")
errInputsNotSortedUnique = errors.New("inputs not sorted and unique")
_ verify.Verifiable = &TransferableOutput{}
_ verify.Verifiable = &TransferableInput{}
)
// Amounter is a data structure that has an amount of something associated with it
type Amounter interface {
snow.ContextInitializable
// Amount returns how much value this element represents of the asset in its
// transaction.
Amount() uint64
}
// Coster is a data structure that has a cost associated with it
type Coster interface {
// Cost returns how much this element costs to be included in its
// transaction.
Cost() (uint64, error)
}
// TransferableIn is the interface a feature extension must provide to transfer
// value between features extensions.
type TransferableIn interface {
verify.Verifiable
Amounter
Coster
}
// TransferableOut is the interface a feature extension must provide to transfer
// value between features extensions.
type TransferableOut interface {
snow.ContextInitializable
verify.State
Amounter
}
type TransferableOutput struct {
Asset `serialize:"true"`
// FxID has serialize false because we don't want this to be encoded in bytes
FxID ids.ID `serialize:"false" json:"fxID"`
Out TransferableOut `serialize:"true" json:"output"`
}
func (out *TransferableOutput) InitCtx(ctx *snow.Context) {
out.Out.InitCtx(ctx)
}
// Output returns the feature extension output that this Output is using.
func (out *TransferableOutput) Output() TransferableOut { return out.Out }
func (out *TransferableOutput) Verify() error {
switch {
case out == nil:
return errNilTransferableOutput
case out.Out == nil:
return errNilTransferableFxOutput
default:
return verify.All(&out.Asset, out.Out)
}
}
type innerSortTransferableOutputs struct {
outs []*TransferableOutput
codec codec.Manager
}
func (outs *innerSortTransferableOutputs) Less(i, j int) bool {
iOut := outs.outs[i]
jOut := outs.outs[j]
iAssetID := iOut.AssetID()
jAssetID := jOut.AssetID()
switch bytes.Compare(iAssetID[:], jAssetID[:]) {
case -1:
return true
case 1:
return false
}
iBytes, err := outs.codec.Marshal(codecVersion, &iOut.Out)
if err != nil {
return false
}
jBytes, err := outs.codec.Marshal(codecVersion, &jOut.Out)
if err != nil {
return false
}
return bytes.Compare(iBytes, jBytes) == -1
}
func (outs *innerSortTransferableOutputs) Len() int { return len(outs.outs) }
func (outs *innerSortTransferableOutputs) Swap(i, j int) { o := outs.outs; o[j], o[i] = o[i], o[j] }
// SortTransferableOutputs sorts output objects
func SortTransferableOutputs(outs []*TransferableOutput, c codec.Manager) {
sort.Sort(&innerSortTransferableOutputs{outs: outs, codec: c})
}
// IsSortedTransferableOutputs returns true if output objects are sorted
func IsSortedTransferableOutputs(outs []*TransferableOutput, c codec.Manager) bool {
return sort.IsSorted(&innerSortTransferableOutputs{outs: outs, codec: c})
}
type TransferableInput struct {
UTXOID `serialize:"true"`
Asset `serialize:"true"`
// FxID has serialize false because we don't want this to be encoded in bytes
FxID ids.ID `serialize:"false" json:"fxID"`
In TransferableIn `serialize:"true" json:"input"`
}
// Input returns the feature extension input that this Input is using.
func (in *TransferableInput) Input() TransferableIn { return in.In }
func (in *TransferableInput) Verify() error {
switch {
case in == nil:
return errNilTransferableInput
case in.In == nil:
return errNilTransferableFxInput
default:
return verify.All(&in.UTXOID, &in.Asset, in.In)
}
}
type innerSortTransferableInputs []*TransferableInput
func (ins innerSortTransferableInputs) Less(i, j int) bool {
iID, iIndex := ins[i].InputSource()
jID, jIndex := ins[j].InputSource()
switch bytes.Compare(iID[:], jID[:]) {
case -1:
return true
case 0:
return iIndex < jIndex
default:
return false
}
}
func (ins innerSortTransferableInputs) Len() int { return len(ins) }
func (ins innerSortTransferableInputs) Swap(i, j int) { ins[j], ins[i] = ins[i], ins[j] }
func SortTransferableInputs(ins []*TransferableInput) { sort.Sort(innerSortTransferableInputs(ins)) }
func IsSortedAndUniqueTransferableInputs(ins []*TransferableInput) bool {
return utils.IsSortedAndUnique(innerSortTransferableInputs(ins))
}
type innerSortTransferableInputsWithSigners struct {
ins []*TransferableInput
signers [][]*crypto.PrivateKeySECP256K1R
}
func (ins *innerSortTransferableInputsWithSigners) Less(i, j int) bool {
iID, iIndex := ins.ins[i].InputSource()
jID, jIndex := ins.ins[j].InputSource()
switch bytes.Compare(iID[:], jID[:]) {
case -1:
return true
case 0:
return iIndex < jIndex
default:
return false
}
}
func (ins *innerSortTransferableInputsWithSigners) Len() int { return len(ins.ins) }
func (ins *innerSortTransferableInputsWithSigners) Swap(i, j int) {
ins.ins[j], ins.ins[i] = ins.ins[i], ins.ins[j]
ins.signers[j], ins.signers[i] = ins.signers[i], ins.signers[j]
}
// SortTransferableInputsWithSigners sorts the inputs and signers based on the
// input's utxo ID
func SortTransferableInputsWithSigners(ins []*TransferableInput, signers [][]*crypto.PrivateKeySECP256K1R) {
sort.Sort(&innerSortTransferableInputsWithSigners{ins: ins, signers: signers})
}
// IsSortedAndUniqueTransferableInputsWithSigners returns true if the inputs are
// sorted and unique
func IsSortedAndUniqueTransferableInputsWithSigners(ins []*TransferableInput, signers [][]*crypto.PrivateKeySECP256K1R) bool {
return utils.IsSortedAndUnique(&innerSortTransferableInputsWithSigners{ins: ins, signers: signers})
}
// VerifyTx verifies that the inputs and outputs flowcheck, including a fee.
// Additionally, this verifies that the inputs and outputs are sorted.
func VerifyTx(
feeAmount uint64,
feeAssetID ids.ID,
allIns [][]*TransferableInput,
allOuts [][]*TransferableOutput,
c codec.Manager,
) error {
fc := NewFlowChecker()
fc.Produce(feeAssetID, feeAmount) // The txFee must be burned
// Add all the outputs to the flow checker and make sure they are sorted
for _, outs := range allOuts {
for _, out := range outs {
if err := out.Verify(); err != nil {
return err
}
fc.Produce(out.AssetID(), out.Output().Amount())
}
if !IsSortedTransferableOutputs(outs, c) {
return errOutputsNotSorted
}
}
// Add all the inputs to the flow checker and make sure they are sorted
for _, ins := range allIns {
for _, in := range ins {
if err := in.Verify(); err != nil {
return err
}
fc.Consume(in.AssetID(), in.Input().Amount())
}
if !IsSortedAndUniqueTransferableInputs(ins) {
return errInputsNotSortedUnique
}
}
return fc.Verify()
}