/
base_tx.go
74 lines (66 loc) · 2 KB
/
base_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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"fmt"
"github.com/Toinounet21/swapalanchego/ids"
"github.com/Toinounet21/swapalanchego/snow"
"github.com/Toinounet21/swapalanchego/vms/components/avax"
"github.com/Toinounet21/swapalanchego/vms/secp256k1fx"
)
// BaseTx contains fields common to many transaction types. It should be
// embedded in transaction implementations.
type BaseTx struct {
avax.BaseTx `serialize:"true" json:"inputs"`
// true iff this transaction has already passed syntactic verification
syntacticallyVerified bool
}
func (tx *BaseTx) InputIDs() ids.Set {
inputIDs := ids.NewSet(len(tx.Ins))
for _, in := range tx.Ins {
inputIDs.Add(in.InputID())
}
return inputIDs
}
// InitCtx sets the FxID fields in the inputs and outputs of this [BaseTx]. Also
// sets the [ctx] to the given [vm.ctx] so that the addresses can be json
// marshalled into human readable format
func (tx *BaseTx) InitCtx(ctx *snow.Context) {
for _, in := range tx.BaseTx.Ins {
in.FxID = secp256k1fx.ID
}
for _, out := range tx.BaseTx.Outs {
out.FxID = secp256k1fx.ID
out.InitCtx(ctx)
}
}
// SyntacticVerify returns nil iff this tx is well formed
func (tx *BaseTx) SyntacticVerify(ctx *snow.Context) error {
switch {
case tx == nil:
return errNilTx
case tx.syntacticallyVerified: // already passed syntactic verification
return nil
}
if err := tx.MetadataVerify(ctx); err != nil {
return fmt.Errorf("metadata failed verification: %w", err)
}
for _, out := range tx.Outs {
if err := out.Verify(); err != nil {
return fmt.Errorf("output failed verification: %w", err)
}
}
for _, in := range tx.Ins {
if err := in.Verify(); err != nil {
return fmt.Errorf("input failed verification: %w", err)
}
}
switch {
case !avax.IsSortedTransferableOutputs(tx.Outs, Codec):
return errOutputsNotSorted
case !avax.IsSortedAndUniqueTransferableInputs(tx.Ins):
return errInputsNotSortedUnique
default:
return nil
}
}