forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_asset_tx.go
115 lines (100 loc) · 3.18 KB
/
create_asset_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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"errors"
"fmt"
"strings"
"unicode"
"github.com/ava-labs/gecko/snow"
"github.com/ava-labs/gecko/vms/components/ava"
"github.com/ava-labs/gecko/vms/components/codec"
)
const (
maxNameLen = 128
maxSymbolLen = 4
maxDenomination = 32
)
var (
errInitialStatesNotSortedUnique = errors.New("initial states not sorted and unique")
errNameTooLong = fmt.Errorf("name is too long, maximum size is %d", maxNameLen)
errSymbolTooLong = fmt.Errorf("symbol is too long, maximum size is %d", maxSymbolLen)
errNoFxs = errors.New("assets must support at least one Fx")
errUnprintableASCIICharacter = errors.New("unprintable ascii character was provided")
errUnexpectedWhitespace = errors.New("unexpected whitespace provided")
errDenominationTooLarge = errors.New("denomination is too large")
)
// CreateAssetTx is a transaction that creates a new asset.
type CreateAssetTx struct {
BaseTx `serialize:"true"`
Name string `serialize:"true" json:"name"`
Symbol string `serialize:"true" json:"symbol"`
Denomination byte `serialize:"true" json:"denomination"`
States []*InitialState `serialize:"true" json:"initialStates"`
}
// InitialStates track which virtual machines, and the initial state of these
// machines, this asset uses. The returned array should not be modified.
func (t *CreateAssetTx) InitialStates() []*InitialState { return t.States }
// UTXOs returns the UTXOs transaction is producing.
func (t *CreateAssetTx) UTXOs() []*ava.UTXO {
txID := t.ID()
utxos := t.BaseTx.UTXOs()
for _, state := range t.States {
for _, out := range state.Outs {
utxos = append(utxos, &ava.UTXO{
UTXOID: ava.UTXOID{
TxID: txID,
OutputIndex: uint32(len(utxos)),
},
Asset: ava.Asset{
ID: txID,
},
Out: out,
})
}
}
return utxos
}
// SyntacticVerify that this transaction is well-formed.
func (t *CreateAssetTx) SyntacticVerify(ctx *snow.Context, c codec.Codec, numFxs int) error {
switch {
case t == nil:
return errNilTx
case len(t.Name) > maxNameLen:
return errNameTooLong
case len(t.Symbol) > maxSymbolLen:
return errSymbolTooLong
case len(t.States) == 0:
return errNoFxs
case t.Denomination > maxDenomination:
return errDenominationTooLarge
case strings.TrimSpace(t.Name) != t.Name:
return errUnexpectedWhitespace
case strings.TrimSpace(t.Symbol) != t.Symbol:
return errUnexpectedWhitespace
}
for _, r := range t.Name {
if r > unicode.MaxASCII || !unicode.IsPrint(r) {
return errUnprintableASCIICharacter
}
}
for _, r := range t.Symbol {
if r > unicode.MaxASCII || !unicode.IsPrint(r) {
return errUnprintableASCIICharacter
}
}
if err := t.BaseTx.SyntacticVerify(ctx, c, numFxs); err != nil {
return err
}
for _, state := range t.States {
if err := state.Verify(c, numFxs); err != nil {
return err
}
}
if !isSortedAndUniqueInitialStates(t.States) {
return errInitialStatesNotSortedUnique
}
return nil
}
// Sort ...
func (t *CreateAssetTx) Sort() { sortInitialStates(t.States) }