-
Notifications
You must be signed in to change notification settings - Fork 670
/
create_chain_tx.go
90 lines (77 loc) · 2.71 KB
/
create_chain_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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"unicode"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
const (
MaxNameLen = 128
MaxGenesisLen = units.MiB
)
var (
_ UnsignedTx = (*CreateChainTx)(nil)
ErrCantValidatePrimaryNetwork = errors.New("new blockchain can't be validated by primary network")
errInvalidVMID = errors.New("invalid VM ID")
errFxIDsNotSortedAndUnique = errors.New("feature extensions IDs must be sorted and unique")
errNameTooLong = errors.New("name too long")
errGenesisTooLong = errors.New("genesis too long")
errIllegalNameCharacter = errors.New("illegal name character")
)
// CreateChainTx is an unsigned createChainTx
type CreateChainTx struct {
// Metadata, inputs and outputs
BaseTx `serialize:"true"`
// ID of the Subnet that validates this blockchain
SubnetID ids.ID `serialize:"true" json:"subnetID"`
// A human readable name for the chain; need not be unique
ChainName string `serialize:"true" json:"chainName"`
// ID of the VM running on the new chain
VMID ids.ID `serialize:"true" json:"vmID"`
// IDs of the feature extensions running on the new chain
FxIDs []ids.ID `serialize:"true" json:"fxIDs"`
// Byte representation of genesis state of the new chain
GenesisData []byte `serialize:"true" json:"genesisData"`
// Authorizes this blockchain to be added to this subnet
SubnetAuth verify.Verifiable `serialize:"true" json:"subnetAuthorization"`
}
func (tx *CreateChainTx) SyntacticVerify(ctx *snow.Context) error {
switch {
case tx == nil:
return ErrNilTx
case tx.SyntacticallyVerified: // already passed syntactic verification
return nil
case tx.SubnetID == constants.PrimaryNetworkID:
return ErrCantValidatePrimaryNetwork
case len(tx.ChainName) > MaxNameLen:
return errNameTooLong
case tx.VMID == ids.Empty:
return errInvalidVMID
case !utils.IsSortedAndUnique(tx.FxIDs):
return errFxIDsNotSortedAndUnique
case len(tx.GenesisData) > MaxGenesisLen:
return errGenesisTooLong
}
for _, r := range tx.ChainName {
if r > unicode.MaxASCII || !(unicode.IsLetter(r) || unicode.IsNumber(r) || r == ' ') {
return errIllegalNameCharacter
}
}
if err := tx.BaseTx.SyntacticVerify(ctx); err != nil {
return err
}
if err := tx.SubnetAuth.Verify(); err != nil {
return err
}
tx.SyntacticallyVerified = true
return nil
}
func (tx *CreateChainTx) Visit(visitor Visitor) error {
return visitor.CreateChainTx(tx)
}