forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
import_tx.go
91 lines (75 loc) · 2.31 KB
/
import_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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/utils"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
)
var (
_ UnsignedTx = (*ImportTx)(nil)
errNoImportInputs = errors.New("tx has no imported inputs")
)
// ImportTx is an unsigned importTx
type ImportTx struct {
BaseTx `serialize:"true"`
// Which chain to consume the funds from
SourceChain ids.ID `serialize:"true" json:"sourceChain"`
// Inputs that consume UTXOs produced on the chain
ImportedInputs []*avax.TransferableInput `serialize:"true" json:"importedInputs"`
}
// InitCtx sets the FxID fields in the inputs and outputs of this
// [ImportTx]. Also sets the [ctx] to the given [vm.ctx] so that
// the addresses can be json marshalled into human readable format
func (tx *ImportTx) InitCtx(ctx *snow.Context) {
tx.BaseTx.InitCtx(ctx)
for _, in := range tx.ImportedInputs {
in.FxID = secp256k1fx.ID
}
}
// InputUTXOs returns the UTXOIDs of the imported funds
func (tx *ImportTx) InputUTXOs() set.Set[ids.ID] {
set := set.NewSet[ids.ID](len(tx.ImportedInputs))
for _, in := range tx.ImportedInputs {
set.Add(in.InputID())
}
return set
}
func (tx *ImportTx) InputIDs() set.Set[ids.ID] {
inputs := tx.BaseTx.InputIDs()
atomicInputs := tx.InputUTXOs()
inputs.Union(atomicInputs)
return inputs
}
// SyntacticVerify this transaction is well-formed
func (tx *ImportTx) SyntacticVerify(ctx *snow.Context) error {
switch {
case tx == nil:
return ErrNilTx
case tx.SyntacticallyVerified: // already passed syntactic verification
return nil
case len(tx.ImportedInputs) == 0:
return errNoImportInputs
}
if err := tx.BaseTx.SyntacticVerify(ctx); err != nil {
return err
}
for _, in := range tx.ImportedInputs {
if err := in.Verify(); err != nil {
return fmt.Errorf("input failed verification: %w", err)
}
}
if !utils.IsSortedAndUniqueSortable(tx.ImportedInputs) {
return errInputsNotSortedUnique
}
tx.SyntacticallyVerified = true
return nil
}
func (tx *ImportTx) Visit(visitor Visitor) error {
return visitor.ImportTx(tx)
}