-
Notifications
You must be signed in to change notification settings - Fork 671
/
base_tx.go
91 lines (78 loc) · 2.69 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avax
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/types"
)
// MaxMemoSize is the maximum number of bytes in the memo field
const MaxMemoSize = 256
var (
errNilTx = errors.New("nil tx is not valid")
errWrongNetworkID = errors.New("tx has wrong network ID")
errWrongChainID = errors.New("tx has wrong chain ID")
)
// BaseTx is the basis of all standard transactions.
type BaseTx struct {
Metadata
NetworkID uint32 `serialize:"true" json:"networkID"` // ID of the network this chain lives on
BlockchainID ids.ID `serialize:"true" json:"blockchainID"` // ID of the chain on which this transaction exists (prevents replay attacks)
Outs []*TransferableOutput `serialize:"true" json:"outputs"` // The outputs of this transaction
Ins []*TransferableInput `serialize:"true" json:"inputs"` // The inputs to this transaction
Memo types.JSONByteSlice `serialize:"true" json:"memo"` // Memo field contains arbitrary bytes, up to maxMemoSize
}
// InputUTXOs track which UTXOs this transaction is consuming.
func (t *BaseTx) InputUTXOs() []*UTXOID {
utxos := make([]*UTXOID, len(t.Ins))
for i, in := range t.Ins {
utxos[i] = &in.UTXOID
}
return utxos
}
// ConsumedAssetIDs returns the IDs of the assets this transaction consumes
func (t *BaseTx) ConsumedAssetIDs() ids.Set {
assets := ids.Set{}
for _, in := range t.Ins {
assets.Add(in.AssetID())
}
return assets
}
// AssetIDs returns the IDs of the assets this transaction depends on
func (t *BaseTx) AssetIDs() ids.Set { return t.ConsumedAssetIDs() }
// NumCredentials returns the number of expected credentials
func (t *BaseTx) NumCredentials() int { return len(t.Ins) }
// UTXOs returns the UTXOs transaction is producing.
func (t *BaseTx) UTXOs() []*UTXO {
txID := t.ID()
utxos := make([]*UTXO, len(t.Outs))
for i, out := range t.Outs {
utxos[i] = &UTXO{
UTXOID: UTXOID{
TxID: txID,
OutputIndex: uint32(i),
},
Asset: Asset{ID: out.AssetID()},
Out: out.Out,
}
}
return utxos
}
// MetadataVerify ensures that transaction metadata is valid
func (t *BaseTx) MetadataVerify(ctx *snow.Context) error {
switch {
case t == nil:
return errNilTx
case t.NetworkID != ctx.NetworkID:
return errWrongNetworkID
case t.BlockchainID != ctx.ChainID:
return errWrongChainID
case len(t.Memo) > MaxMemoSize:
return fmt.Errorf("memo length, %d, exceeds maximum memo length, %d",
len(t.Memo), MaxMemoSize)
default:
return t.Metadata.Verify()
}
}