-
Notifications
You must be signed in to change notification settings - Fork 658
/
export_tx.go
226 lines (198 loc) · 6.33 KB
/
export_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
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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/platformvm/stakeable"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
var (
errNoExportOutputs = errors.New("no export outputs")
errOutputsNotSorted = errors.New("outputs not sorted")
errOverflowExport = errors.New("overflow when computing export amount + txFee")
_ UnsignedAtomicTx = &UnsignedExportTx{}
)
// UnsignedExportTx is an unsigned ExportTx
type UnsignedExportTx struct {
BaseTx `serialize:"true"`
// Which chain to send the funds to
DestinationChain ids.ID `serialize:"true" json:"destinationChain"`
// Outputs that are exported to the chain
ExportedOutputs []*avax.TransferableOutput `serialize:"true" json:"exportedOutputs"`
}
// InitCtx sets the FxID fields in the inputs and outputs of this
// [UnsignedExportTx]. Also sets the [ctx] to the given [vm.ctx] so that
// the addresses can be json marshalled into human readable format
func (tx *UnsignedExportTx) InitCtx(ctx *snow.Context) {
tx.BaseTx.InitCtx(ctx)
for _, out := range tx.ExportedOutputs {
out.FxID = secp256k1fx.ID
out.InitCtx(ctx)
}
}
// InputUTXOs returns an empty set
func (tx *UnsignedExportTx) InputUTXOs() ids.Set { return nil }
// SyntacticVerify this transaction is well-formed
func (tx *UnsignedExportTx) SyntacticVerify(ctx *snow.Context) error {
switch {
case tx == nil:
return errNilTx
case tx.syntacticallyVerified: // already passed syntactic verification
return nil
case len(tx.ExportedOutputs) == 0:
return errNoExportOutputs
}
if err := tx.BaseTx.SyntacticVerify(ctx); err != nil {
return err
}
for _, out := range tx.ExportedOutputs {
if err := out.Verify(); err != nil {
return fmt.Errorf("output failed verification: %w", err)
}
if _, ok := out.Output().(*stakeable.LockOut); ok {
return errWrongLocktime
}
}
if !avax.IsSortedTransferableOutputs(tx.ExportedOutputs, Codec) {
return errOutputsNotSorted
}
tx.syntacticallyVerified = true
return nil
}
// Attempts to verify this transaction with the provided state.
func (tx *UnsignedExportTx) SemanticVerify(vm *VM, parentState MutableState, stx *Tx) error {
_, err := tx.AtomicExecute(vm, parentState, stx)
return err
}
// Execute this transaction.
func (tx *UnsignedExportTx) Execute(
vm *VM,
vs VersionedState,
stx *Tx,
) (func() error, error) {
if err := tx.SyntacticVerify(vm.ctx); err != nil {
return nil, err
}
outs := make([]*avax.TransferableOutput, len(tx.Outs)+len(tx.ExportedOutputs))
copy(outs, tx.Outs)
copy(outs[len(tx.Outs):], tx.ExportedOutputs)
if vm.bootstrapped.GetValue() {
if err := verify.SameSubnet(vm.ctx, tx.DestinationChain); err != nil {
return nil, err
}
}
// Verify the flowcheck
if err := vm.semanticVerifySpend(vs, tx, tx.Ins, outs, stx.Creds, vm.TxFee, vm.ctx.AVAXAssetID); err != nil {
return nil, fmt.Errorf("failed semanticVerifySpend: %w", err)
}
// Consume the UTXOS
consumeInputs(vs, tx.Ins)
// Produce the UTXOS
txID := tx.ID()
produceOutputs(vs, txID, vm.ctx.AVAXAssetID, tx.Outs)
return nil, nil
}
// AtomicOperations returns the shared memory requests
func (tx *UnsignedExportTx) AtomicOperations() (ids.ID, *atomic.Requests, error) {
txID := tx.ID()
elems := make([]*atomic.Element, len(tx.ExportedOutputs))
for i, out := range tx.ExportedOutputs {
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(len(tx.Outs) + i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
}
utxoBytes, err := Codec.Marshal(CodecVersion, utxo)
if err != nil {
return ids.ID{}, nil, fmt.Errorf("failed to marshal UTXO: %w", err)
}
utxoID := utxo.InputID()
elem := &atomic.Element{
Key: utxoID[:],
Value: utxoBytes,
}
if out, ok := utxo.Out.(avax.Addressable); ok {
elem.Traits = out.Addresses()
}
elems[i] = elem
}
return tx.DestinationChain, &atomic.Requests{PutRequests: elems}, nil
}
// Execute this transaction and return the versioned state.
func (tx *UnsignedExportTx) AtomicExecute(
vm *VM,
parentState MutableState,
stx *Tx,
) (VersionedState, error) {
// Set up the state if this tx is committed
newState := newVersionedState(
parentState,
parentState.CurrentStakerChainState(),
parentState.PendingStakerChainState(),
)
_, err := tx.Execute(vm, newState, stx)
return newState, err
}
// Accept this transaction.
func (tx *UnsignedExportTx) AtomicAccept(ctx *snow.Context, batch database.Batch) error {
chainID, requests, err := tx.AtomicOperations()
if err != nil {
return err
}
return ctx.SharedMemory.Apply(map[ids.ID]*atomic.Requests{chainID: requests}, batch)
}
// Create a new transaction
func (vm *VM) newExportTx(
amount uint64, // Amount of tokens to export
chainID ids.ID, // Chain to send the UTXOs to
to ids.ShortID, // Address of chain recipient
keys []*crypto.PrivateKeySECP256K1R, // Pay the fee and provide the tokens
changeAddr ids.ShortID, // Address to send change to, if there is any
) (*Tx, error) {
toBurn, err := math.Add64(amount, vm.TxFee)
if err != nil {
return nil, errOverflowExport
}
ins, outs, _, signers, err := vm.stake(keys, 0, toBurn, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the transaction
utx := &UnsignedExportTx{
BaseTx: BaseTx{BaseTx: avax.BaseTx{
NetworkID: vm.ctx.NetworkID,
BlockchainID: vm.ctx.ChainID,
Ins: ins,
Outs: outs, // Non-exported outputs
}},
DestinationChain: chainID,
ExportedOutputs: []*avax.TransferableOutput{{ // Exported to X-Chain
Asset: avax.Asset{ID: vm.ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
}},
}
tx := &Tx{UnsignedTx: utx}
if err := tx.Sign(Codec, signers); err != nil {
return nil, err
}
return tx, utx.SyntacticVerify(vm.ctx)
}