-
Notifications
You must be signed in to change notification settings - Fork 659
/
create_chain_tx.go
202 lines (179 loc) · 5.84 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"errors"
"fmt"
"unicode"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
var (
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")
_ UnsignedDecisionTx = &UnsignedCreateChainTx{}
)
const (
maxNameLen = 1 << 7
maxGenesisLen = 1 << 20
)
// UnsignedCreateChainTx is an unsigned CreateChainTx
type UnsignedCreateChainTx 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"`
// Auth that will be allowing this validator into the network
SubnetAuth verify.Verifiable `serialize:"true" json:"subnetAuthorization"`
}
// Verify this transaction is well-formed
func (tx *UnsignedCreateChainTx) Verify(
ctx *snow.Context,
c codec.Manager,
feeAmount uint64,
feeAssetID ids.ID,
) error {
switch {
case tx == nil:
return errNilTx
case tx.syntacticallyVerified: // already passed syntactic verification
return nil
case tx.SubnetID == constants.PrimaryNetworkID:
return errDSCantValidate
case len(tx.ChainName) > maxNameLen:
return errNameTooLong
case tx.VMID == ids.Empty:
return errInvalidVMID
case !ids.IsSortedAndUniqueIDs(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.Verify(ctx, c); err != nil {
return err
}
if err := tx.SubnetAuth.Verify(); err != nil {
return err
}
tx.syntacticallyVerified = true
return nil
}
// SemanticVerify this transaction is valid.
func (tx *UnsignedCreateChainTx) SemanticVerify(
vm *VM,
vs VersionedState,
stx *Tx,
) (
func() error,
TxError,
) {
// Make sure this transaction is well formed.
if len(stx.Creds) == 0 {
return nil, permError{errWrongNumberOfCredentials}
}
if err := tx.Verify(vm.ctx, vm.codec, vm.CreationTxFee, vm.ctx.AVAXAssetID); err != nil {
return nil, permError{err}
}
// Select the credentials for each purpose
baseTxCredsLen := len(stx.Creds) - 1
baseTxCreds := stx.Creds[:baseTxCredsLen]
subnetCred := stx.Creds[baseTxCredsLen]
// Verify the flowcheck
if err := vm.semanticVerifySpend(vs, tx, tx.Ins, tx.Outs, baseTxCreds, vm.CreationTxFee, vm.ctx.AVAXAssetID); err != nil {
return nil, err
}
subnetIntf, _, err := vs.GetTx(tx.SubnetID)
if err == database.ErrNotFound {
return nil, permError{
fmt.Errorf("%s isn't a known subnet", tx.SubnetID),
}
}
if err != nil {
return nil, tempError{err}
}
subnet, ok := subnetIntf.UnsignedTx.(*UnsignedCreateSubnetTx)
if !ok {
return nil, permError{
fmt.Errorf("%s isn't a subnet", tx.SubnetID),
}
}
// Verify that this chain is authorized by the subnet
if err := vm.fx.VerifyPermission(tx, tx.SubnetAuth, subnetCred, subnet.Owner); err != nil {
return nil, permError{err}
}
// Consume the UTXOS
consumeInputs(vs, tx.Ins)
// Produce the UTXOS
txID := tx.ID()
produceOutputs(vs, txID, vm.ctx.AVAXAssetID, tx.Outs)
// Attempt to the new chain to the database
vs.AddChain(stx)
// If this proposal is committed and this node is a member of the
// subnet that validates the blockchain, create the blockchain
onAccept := func() error { return vm.createChain(stx) }
return onAccept, nil
}
// Create a new transaction
func (vm *VM) newCreateChainTx(
subnetID ids.ID, // ID of the subnet that validates the new chain
genesisData []byte, // Byte repr. of genesis state of the new chain
vmID ids.ID, // VM this chain runs
fxIDs []ids.ID, // fxs this chain supports
chainName string, // Name of the chain
keys []*crypto.PrivateKeySECP256K1R, // Keys to sign the tx
changeAddr ids.ShortID, // Address to send change to, if there is any
) (*Tx, error) {
ins, outs, _, signers, err := vm.stake(keys, 0, vm.CreationTxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := vm.authorize(vm.internalState, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
// Sort the provided fxIDs
ids.SortIDs(fxIDs)
// Create the tx
utx := &UnsignedCreateChainTx{
BaseTx: BaseTx{BaseTx: avax.BaseTx{
NetworkID: vm.ctx.NetworkID,
BlockchainID: vm.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
SubnetID: subnetID,
ChainName: chainName,
VMID: vmID,
FxIDs: fxIDs,
GenesisData: genesisData,
SubnetAuth: subnetAuth,
}
tx := &Tx{UnsignedTx: utx}
if err := tx.Sign(vm.codec, signers); err != nil {
return nil, err
}
return tx, utx.Verify(vm.ctx, vm.codec, vm.CreationTxFee, vm.ctx.AVAXAssetID)
}