-
Notifications
You must be signed in to change notification settings - Fork 660
/
export_tx.go
126 lines (107 loc) · 3.01 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"errors"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var errNoExportOutputs = errors.New("no export outputs")
// ExportTx is a transaction that exports an asset to another blockchain.
type ExportTx struct {
BaseTx `serialize:"true"`
// Which chain to send the funds to
DestinationChain ids.ID `serialize:"true" json:"destinationChain"`
// The outputs this transaction is sending to the other chain
ExportedOuts []*avax.TransferableOutput `serialize:"true" json:"exportedOutputs"`
}
// SyntacticVerify that this transaction is well-formed.
func (t *ExportTx) SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
txFee uint64,
_ uint64,
_ int,
) error {
switch {
case t == nil:
return errNilTx
case len(t.ExportedOuts) == 0:
return errNoExportOutputs
}
if err := t.MetadataVerify(ctx); err != nil {
return err
}
return avax.VerifyTx(
txFee,
txFeeAssetID,
[][]*avax.TransferableInput{t.Ins},
[][]*avax.TransferableOutput{
t.Outs,
t.ExportedOuts,
},
c,
)
}
// SemanticVerify that this transaction is valid to be spent.
func (t *ExportTx) SemanticVerify(vm *VM, tx UnsignedTx, creds []verify.Verifiable) error {
if vm.bootstrapped {
subnetID, err := vm.ctx.SNLookup.SubnetID(t.DestinationChain)
if err != nil {
return err
}
if vm.ctx.SubnetID != subnetID || t.DestinationChain == vm.ctx.ChainID {
return errWrongBlockchainID
}
}
for _, out := range t.ExportedOuts {
fxIndex, err := vm.getFx(out.Out)
if err != nil {
return err
}
assetID := out.AssetID()
if out.AssetID() != vm.ctx.AVAXAssetID && t.DestinationChain == constants.PlatformChainID {
return errWrongAssetID
}
if !vm.verifyFxUsage(fxIndex, assetID) {
return errIncompatibleFx
}
}
return t.BaseTx.SemanticVerify(vm, tx, creds)
}
// ExecuteWithSideEffects writes the batch with any additional side effects
func (t *ExportTx) ExecuteWithSideEffects(vm *VM, batch database.Batch) error {
txID := t.ID()
elems := make([]*atomic.Element, len(t.ExportedOuts))
for i, out := range t.ExportedOuts {
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: txID,
OutputIndex: uint32(len(t.Outs) + i),
},
Asset: avax.Asset{ID: out.AssetID()},
Out: out.Out,
}
utxoBytes, err := vm.codec.Marshal(codecVersion, utxo)
if err != nil {
return err
}
inputID := utxo.InputID()
elem := &atomic.Element{
Key: inputID[:],
Value: utxoBytes,
}
if out, ok := utxo.Out.(avax.Addressable); ok {
elem.Traits = out.Addresses()
}
elems[i] = elem
}
return vm.ctx.SharedMemory.Put(t.DestinationChain, elems, batch)
}