-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathhelpers.go
More file actions
133 lines (129 loc) · 3.56 KB
/
helpers.go
File metadata and controls
133 lines (129 loc) · 3.56 KB
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
// Copyright (C) 2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package localnet
import (
"context"
"errors"
"fmt"
"os"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)
// Tracks the given [blockchainName] at network given on [networkDir]
// After P-Chain is bootstrapped, set alias [blockchainName]->blockchainID
// for the network, and persists RPC into sidecar
// Use both for local networks and local clusters
func TrackSubnet(
app *application.Avalanche,
printFunc func(msg string, args ...interface{}),
blockchainName string,
networkDir string,
wallet *primary.Wallet,
) error {
networkModel, err := GetNetworkModel(networkDir)
if err != nil {
return err
}
sc, err := app.LoadSidecar(blockchainName)
if err != nil {
return err
}
if sc.Networks[networkModel.Name()].BlockchainID == ids.Empty {
return fmt.Errorf("blockchain %s has not been deployed to %s", blockchainName, networkModel.Name())
}
blockchainID := sc.Networks[networkModel.Name()].BlockchainID
subnetID := sc.Networks[networkModel.Name()].SubnetID
var (
blockchainConfig []byte
subnetConfig []byte
)
vmBinaryPath, err := SetupVMBinary(app, blockchainName)
if err != nil {
return fmt.Errorf("failed to setup VM binary: %w", err)
}
if app.ChainConfigExists(blockchainName) {
blockchainConfig, err = os.ReadFile(app.GetChainConfigPath(blockchainName))
if err != nil {
return err
}
}
if app.AvagoSubnetConfigExists(blockchainName) {
subnetConfig, err = os.ReadFile(app.GetAvagoSubnetConfigPath(blockchainName))
if err != nil {
return err
}
}
perNodeBlockchainConfig, err := app.GetPerNodeBlockchainConfig(blockchainName)
if err != nil {
return err
}
ctx, cancel := networkModel.BootstrappingContext()
defer cancel()
if err := TmpNetTrackSubnet(
ctx,
app.Log,
printFunc,
networkDir,
blockchainName,
sc.Sovereign,
blockchainID,
subnetID,
vmBinaryPath,
blockchainConfig,
subnetConfig,
perNodeBlockchainConfig,
wallet,
); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
printFunc("")
printFunc("A context timeout has occurred while trying to bootstrap the blockchain.")
printFunc("")
logPaths, _ := GetTmpNetAvailableLogs(networkDir, blockchainID, false)
if len(logPaths) != 0 {
printFunc("Please check this log files for more information on the error cause:")
for _, logPath := range logPaths {
printFunc(" " + logPath)
}
printFunc("")
}
}
return err
}
ux.Logger.GreenCheckmarkToUser("%s successfully tracking %s", networkModel.Name(), blockchainName)
network, err := GetTmpNetNetwork(networkDir)
if err != nil {
return err
}
if networkModel.Kind == models.Local {
if err := TmpNetSetAlias(network.Nodes, blockchainID.String(), blockchainName, subnetID); err != nil {
return err
}
}
nodeURIs, err := GetTmpNetNodeURIsWithFix(networkDir)
if err != nil {
return err
}
_, err = app.AddDefaultBlockchainRPCsToSidecar(
blockchainName,
networkModel,
nodeURIs,
)
return err
}
// Returns the network model for the network at [networkDir]
func GetNetworkModel(
networkDir string,
) (models.Network, error) {
network, err := GetTmpNetNetwork(networkDir)
if err != nil {
return models.UndefinedNetwork, err
}
networkID, err := GetTmpNetNetworkID(network)
if err != nil {
return models.UndefinedNetwork, err
}
return models.NetworkFromNetworkID(networkID), nil
}