-
Notifications
You must be signed in to change notification settings - Fork 671
/
export_tx.go
79 lines (65 loc) · 2.05 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/stakeable"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
var (
_ UnsignedTx = (*ExportTx)(nil)
ErrWrongLocktime = errors.New("wrong locktime reported")
errNoExportOutputs = errors.New("no export outputs")
)
// ExportTx is an unsigned exportTx
type ExportTx 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 *ExportTx) InitCtx(ctx *snow.Context) {
tx.BaseTx.InitCtx(ctx)
for _, out := range tx.ExportedOutputs {
out.FxID = secp256k1fx.ID
out.InitCtx(ctx)
}
}
// SyntacticVerify this transaction is well-formed
func (tx *ExportTx) 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
}
func (tx *ExportTx) Visit(visitor Visitor) error {
return visitor.ExportTx(tx)
}