forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subnet.go
346 lines (297 loc) · 8.93 KB
/
subnet.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package tmpnet
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/constants"
"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
"github.com/MetalBlockchain/metalgo/utils/perms"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/utils/units"
"github.com/MetalBlockchain/metalgo/vms/platformvm"
"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
"github.com/MetalBlockchain/metalgo/wallet/subnet/primary"
"github.com/MetalBlockchain/metalgo/wallet/subnet/primary/common"
)
const defaultSubnetDirName = "subnets"
type Chain struct {
// Set statically
VMID ids.ID
Config string
Genesis []byte
// Set at runtime
ChainID ids.ID
PreFundedKey *secp256k1.PrivateKey
}
// Write the chain configuration to the specified directory.
func (c *Chain) WriteConfig(chainDir string) error {
if len(c.Config) == 0 {
return nil
}
chainConfigDir := filepath.Join(chainDir, c.ChainID.String())
if err := os.MkdirAll(chainConfigDir, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to create chain config dir: %w", err)
}
path := filepath.Join(chainConfigDir, defaultConfigFilename)
if err := os.WriteFile(path, []byte(c.Config), perms.ReadWrite); err != nil {
return fmt.Errorf("failed to write chain config: %w", err)
}
return nil
}
type Subnet struct {
// A unique string that can be used to refer to the subnet across different temporary
// networks (since the SubnetID will be different every time the subnet is created)
Name string
// The ID of the transaction that created the subnet
SubnetID ids.ID
// The private key that owns the subnet
OwningKey *secp256k1.PrivateKey
// IDs of the nodes responsible for validating the subnet
ValidatorIDs []ids.NodeID
Chains []*Chain
}
// Retrieves a wallet configured for use with the subnet
func (s *Subnet) GetWallet(ctx context.Context, uri string) (primary.Wallet, error) {
keychain := secp256k1fx.NewKeychain(s.OwningKey)
// Only fetch the subnet transaction if a subnet ID is present. This won't be true when
// the wallet is first used to create the subnet.
txIDs := set.Set[ids.ID]{}
if s.SubnetID != ids.Empty {
txIDs.Add(s.SubnetID)
}
return primary.MakeWallet(ctx, &primary.WalletConfig{
URI: uri,
AVAXKeychain: keychain,
EthKeychain: keychain,
PChainTxsToFetch: txIDs,
})
}
// Issues the subnet creation transaction and retains the result. The URI of a node is
// required to issue the transaction.
func (s *Subnet) Create(ctx context.Context, uri string) error {
wallet, err := s.GetWallet(ctx, uri)
if err != nil {
return err
}
pWallet := wallet.P()
subnetTx, err := pWallet.IssueCreateSubnetTx(
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
s.OwningKey.Address(),
},
},
common.WithContext(ctx),
)
if err != nil {
return fmt.Errorf("failed to create subnet %s: %w", s.Name, err)
}
s.SubnetID = subnetTx.ID()
return nil
}
func (s *Subnet) CreateChains(ctx context.Context, w io.Writer, uri string) error {
wallet, err := s.GetWallet(ctx, uri)
if err != nil {
return err
}
pWallet := wallet.P()
if _, err := fmt.Fprintf(w, "Creating chains for subnet %q\n", s.Name); err != nil {
return err
}
for _, chain := range s.Chains {
createChainTx, err := pWallet.IssueCreateChainTx(
s.SubnetID,
chain.Genesis,
chain.VMID,
nil,
"",
common.WithContext(ctx),
)
if err != nil {
return fmt.Errorf("failed to create chain: %w", err)
}
chain.ChainID = createChainTx.ID()
if _, err := fmt.Fprintf(w, " created chain %q for VM %q on subnet %q\n", chain.ChainID, chain.VMID, s.Name); err != nil {
return err
}
}
return nil
}
// Add validators to the subnet
func (s *Subnet) AddValidators(ctx context.Context, w io.Writer, nodes []*Node) error {
apiURI := nodes[0].URI
wallet, err := s.GetWallet(ctx, apiURI)
if err != nil {
return err
}
pWallet := wallet.P()
// Collect the end times for current validators to reuse for subnet validators
pvmClient := platformvm.NewClient(apiURI)
validators, err := pvmClient.GetCurrentValidators(ctx, constants.PrimaryNetworkID, nil)
if err != nil {
return err
}
endTimes := make(map[ids.NodeID]uint64)
for _, validator := range validators {
endTimes[validator.NodeID] = validator.EndTime
}
startTime := time.Now().Add(DefaultValidatorStartTimeDiff)
for _, node := range nodes {
endTime, ok := endTimes[node.NodeID]
if !ok {
return fmt.Errorf("failed to find end time for %s", node.NodeID)
}
_, err := pWallet.IssueAddSubnetValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: node.NodeID,
Start: uint64(startTime.Unix()),
End: endTime,
Wght: units.Schmeckle,
},
Subnet: s.SubnetID,
},
common.WithContext(ctx),
)
if err != nil {
return err
}
if _, err := fmt.Fprintf(w, " added %s as validator for subnet `%s`\n", node.NodeID, s.Name); err != nil {
return err
}
s.ValidatorIDs = append(s.ValidatorIDs, node.NodeID)
}
return nil
}
// Write the subnet configuration to disk
func (s *Subnet) Write(subnetDir string, chainDir string) error {
if err := os.MkdirAll(subnetDir, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to create subnet dir: %w", err)
}
path := filepath.Join(subnetDir, s.Name+".json")
// Since subnets are expected to be serialized for the first time
// without their chains having been created (i.e. chains will have
// empty IDs), use the absence of chain IDs as a prompt for a
// subnet name uniquness check.
if len(s.Chains) > 0 && s.Chains[0].ChainID == ids.Empty {
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
return fmt.Errorf("a subnet with name %s already exists", s.Name)
}
}
bytes, err := DefaultJSONMarshal(s)
if err != nil {
return fmt.Errorf("failed to marshal subnet %s: %w", s.Name, err)
}
if err := os.WriteFile(path, bytes, perms.ReadWrite); err != nil {
return fmt.Errorf("failed to write subnet %s: %w", s.Name, err)
}
for _, chain := range s.Chains {
if err := chain.WriteConfig(chainDir); err != nil {
return err
}
}
return nil
}
// HasChainConfig indicates whether at least one of the subnet's
// chains have explicit configuration. This can be used to determine
// whether validator restart is required after chain creation to
// ensure that chains are configured correctly.
func (s *Subnet) HasChainConfig() bool {
for _, chain := range s.Chains {
if len(chain.Config) > 0 {
return true
}
}
return false
}
func waitForActiveValidators(
ctx context.Context,
w io.Writer,
pChainClient platformvm.Client,
subnet *Subnet,
) error {
ticker := time.NewTicker(DefaultPollingInterval)
defer ticker.Stop()
if _, err := fmt.Fprintf(w, "Waiting for validators of subnet %q to become active\n", subnet.Name); err != nil {
return err
}
if _, err := fmt.Fprintf(w, " "); err != nil {
return err
}
for {
if _, err := fmt.Fprintf(w, "."); err != nil {
return err
}
validators, err := pChainClient.GetCurrentValidators(ctx, subnet.SubnetID, nil)
if err != nil {
return err
}
validatorSet := set.NewSet[ids.NodeID](len(validators))
for _, validator := range validators {
validatorSet.Add(validator.NodeID)
}
allActive := true
for _, validatorID := range subnet.ValidatorIDs {
if !validatorSet.Contains(validatorID) {
allActive = false
}
}
if allActive {
if _, err := fmt.Fprintf(w, "\n saw the expected active validators of subnet %q\n", subnet.Name); err != nil {
return err
}
return nil
}
select {
case <-ctx.Done():
return fmt.Errorf("failed to see the expected active validators of subnet %q before timeout", subnet.Name)
case <-ticker.C:
}
}
}
// Reads subnets from [network dir]/subnets/[subnet name].json
func readSubnets(subnetDir string) ([]*Subnet, error) {
if _, err := os.Stat(subnetDir); os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
entries, err := os.ReadDir(subnetDir)
if err != nil {
return nil, fmt.Errorf("failed to read subnet dir: %w", err)
}
subnets := []*Subnet{}
for _, entry := range entries {
if entry.IsDir() {
// Looking only for files
continue
}
if filepath.Ext(entry.Name()) != ".json" {
// Subnet files should have a .json extension
continue
}
subnetPath := filepath.Join(subnetDir, entry.Name())
bytes, err := os.ReadFile(subnetPath)
if err != nil {
return nil, fmt.Errorf("failed to read subnet file %s: %w", subnetPath, err)
}
subnet := &Subnet{}
if err := json.Unmarshal(bytes, subnet); err != nil {
return nil, fmt.Errorf("failed to unmarshal subnet from %s: %w", subnetPath, err)
}
subnets = append(subnets, subnet)
}
return subnets, nil
}