Skip to content

Commit 373d7c2

Browse files
authored
Update go chia libs to latest to fix bug setting lists directly with … (#42)
1 parent a655e73 commit 373d7c2

File tree

5 files changed

+265
-164
lines changed

5 files changed

+265
-164
lines changed

cmd/network/switch.go

+166-159
Original file line numberDiff line numberDiff line change
@@ -22,56 +22,77 @@ var switchCmd = &cobra.Command{
2222
Args: cobra.ExactArgs(1),
2323
Run: func(cmd *cobra.Command, args []string) {
2424
networkName := args[0]
25-
slogs.Logr.Info("Swapping to network", "network", networkName)
25+
SwitchNetwork(networkName, true)
26+
},
27+
}
2628

27-
chiaRoot, err := config.GetChiaRootPath()
28-
if err != nil {
29-
slogs.Logr.Fatal("error determining chia root", "error", err)
30-
}
31-
slogs.Logr.Debug("Chia root discovered", "CHIA_ROOT", chiaRoot)
29+
func init() {
30+
networkCmd.PersistentFlags().String("introducer", "", "Override the default values for introducer host")
31+
networkCmd.PersistentFlags().String("dns-introducer", "", "Override the default values for dns-introducer host")
32+
networkCmd.PersistentFlags().String("bootstrap-peer", "", "Override the default value for seeder bootstrap peer")
33+
networkCmd.PersistentFlags().Uint16("full-node-port", 0, "Override the default values for the full node port")
3234

33-
cfg, err := config.GetChiaConfig()
34-
if err != nil {
35-
slogs.Logr.Fatal("error loading config", "error", err)
36-
}
37-
slogs.Logr.Debug("Successfully loaded config")
35+
cobra.CheckErr(viper.BindPFlag("switch-introducer", networkCmd.PersistentFlags().Lookup("introducer")))
36+
cobra.CheckErr(viper.BindPFlag("switch-dns-introducer", networkCmd.PersistentFlags().Lookup("dns-introducer")))
37+
cobra.CheckErr(viper.BindPFlag("switch-bootstrap-peer", networkCmd.PersistentFlags().Lookup("bootstrap-peer")))
38+
cobra.CheckErr(viper.BindPFlag("switch-full-node-port", networkCmd.PersistentFlags().Lookup("full-node-port")))
3839

39-
currentNetwork := *cfg.SelectedNetwork
40-
slogs.Logr.Info("discovered current network", "current-network", currentNetwork)
40+
networkCmd.AddCommand(switchCmd)
41+
}
4142

42-
if currentNetwork == networkName {
43-
slogs.Logr.Fatal("current network name and new network name are the same", "current", currentNetwork, "new", networkName)
44-
}
43+
// SwitchNetwork implements the logic to swap networks
44+
func SwitchNetwork(networkName string, checkForRunningNode bool) {
45+
slogs.Logr.Info("Swapping to network", "network", networkName)
4546

46-
// Ensure we have network constants for the network trying to be swapped to
47-
if _, ok := cfg.NetworkOverrides.Constants[networkName]; !ok {
48-
slogs.Logr.Fatal("selected network does not exist in config's network override constants", "network", networkName)
49-
}
50-
var (
51-
netConfig config.NetworkConfig
52-
ok bool
53-
)
54-
if netConfig, ok = cfg.NetworkOverrides.Config[networkName]; !ok {
55-
slogs.Logr.Fatal("selected network does not exist in config's network override config", "network", networkName)
56-
}
47+
chiaRoot, err := config.GetChiaRootPath()
48+
if err != nil {
49+
slogs.Logr.Fatal("error determining chia root", "error", err)
50+
}
51+
slogs.Logr.Debug("Chia root discovered", "CHIA_ROOT", chiaRoot)
5752

58-
// Ensure a folder to store the current network's sub-epoch-summaries and height-to-hash files exists
59-
cacheFileDirOldNetwork := path.Join(chiaRoot, "db", currentNetwork)
60-
cacheFileDirNewNetwork := path.Join(chiaRoot, "db", networkName)
53+
cfg, err := config.GetChiaConfig()
54+
if err != nil {
55+
slogs.Logr.Fatal("error loading config", "error", err)
56+
}
57+
slogs.Logr.Debug("Successfully loaded config")
6158

62-
slogs.Logr.Debug("ensuring directory exists for current network cache files", "directory", cacheFileDirOldNetwork)
63-
err = os.MkdirAll(cacheFileDirOldNetwork, 0755)
64-
if err != nil {
65-
slogs.Logr.Fatal("error creating cache file directory for current network", "error", err, "directory", cacheFileDirOldNetwork)
66-
}
59+
currentNetwork := *cfg.SelectedNetwork
60+
slogs.Logr.Info("discovered current network", "current-network", currentNetwork)
6761

68-
slogs.Logr.Debug("ensuring directory exists for new network cache files", "directory", cacheFileDirNewNetwork)
69-
err = os.MkdirAll(cacheFileDirNewNetwork, 0755)
70-
if err != nil {
71-
slogs.Logr.Fatal("error creating cache file directory for new network", "error", err, "directory", cacheFileDirNewNetwork)
72-
}
62+
if currentNetwork == networkName {
63+
slogs.Logr.Fatal("current network name and new network name are the same", "current", currentNetwork, "new", networkName)
64+
}
65+
66+
// Ensure we have network constants for the network trying to be swapped to
67+
if _, ok := cfg.NetworkOverrides.Constants[networkName]; !ok {
68+
slogs.Logr.Fatal("selected network does not exist in config's network override constants", "network", networkName)
69+
}
70+
var (
71+
netConfig config.NetworkConfig
72+
ok bool
73+
)
74+
if netConfig, ok = cfg.NetworkOverrides.Config[networkName]; !ok {
75+
slogs.Logr.Fatal("selected network does not exist in config's network override config", "network", networkName)
76+
}
77+
78+
// Ensure a folder to store the current network's sub-epoch-summaries and height-to-hash files exists
79+
cacheFileDirOldNetwork := path.Join(chiaRoot, "db", currentNetwork)
80+
cacheFileDirNewNetwork := path.Join(chiaRoot, "db", networkName)
81+
82+
slogs.Logr.Debug("ensuring directory exists for current network cache files", "directory", cacheFileDirOldNetwork)
83+
err = os.MkdirAll(cacheFileDirOldNetwork, 0755)
84+
if err != nil {
85+
slogs.Logr.Fatal("error creating cache file directory for current network", "error", err, "directory", cacheFileDirOldNetwork)
86+
}
7387

74-
// Check if Full Node is running
88+
slogs.Logr.Debug("ensuring directory exists for new network cache files", "directory", cacheFileDirNewNetwork)
89+
err = os.MkdirAll(cacheFileDirNewNetwork, 0755)
90+
if err != nil {
91+
slogs.Logr.Fatal("error creating cache file directory for new network", "error", err, "directory", cacheFileDirNewNetwork)
92+
}
93+
94+
// Check if Full Node is running
95+
if checkForRunningNode {
7596
slogs.Logr.Debug("initializing websocket client to ensure chia is stopped")
7697
rpcClient, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig(), rpc.WithSyncWebsocket())
7798
if err != nil {
@@ -85,138 +106,124 @@ var switchCmd = &cobra.Command{
85106
slogs.Logr.Fatal("error stopping chia services", "error", err)
86107
}
87108
}
109+
}
88110

89-
// Safe to move files now
90-
activeSubEpochSummariesPath := path.Join(chiaRoot, "db", "sub-epoch-summaries")
91-
activeHeightToHashPath := path.Join(chiaRoot, "db", "height-to-hash")
111+
// Safe to move files now
112+
activeSubEpochSummariesPath := path.Join(chiaRoot, "db", "sub-epoch-summaries")
113+
activeHeightToHashPath := path.Join(chiaRoot, "db", "height-to-hash")
92114

93-
// Move current cache files to the network subdir
94-
err = moveAndOverwriteFile(activeSubEpochSummariesPath, path.Join(cacheFileDirOldNetwork, "sub-epoch-summaries"))
95-
if err != nil {
96-
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
97-
}
98-
err = moveAndOverwriteFile(activeHeightToHashPath, path.Join(cacheFileDirOldNetwork, "height-to-hash"))
99-
if err != nil {
100-
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
101-
}
115+
// Move current cache files to the network subdir
116+
err = moveAndOverwriteFile(activeSubEpochSummariesPath, path.Join(cacheFileDirOldNetwork, "sub-epoch-summaries"))
117+
if err != nil {
118+
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
119+
}
120+
err = moveAndOverwriteFile(activeHeightToHashPath, path.Join(cacheFileDirOldNetwork, "height-to-hash"))
121+
if err != nil {
122+
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
123+
}
102124

103-
// Move old cached files to active dir
104-
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "sub-epoch-summaries"), activeSubEpochSummariesPath)
105-
if err != nil {
106-
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
107-
}
108-
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "height-to-hash"), activeHeightToHashPath)
109-
if err != nil {
110-
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
111-
}
125+
// Move old cached files to active dir
126+
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "sub-epoch-summaries"), activeSubEpochSummariesPath)
127+
if err != nil {
128+
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
129+
}
130+
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "height-to-hash"), activeHeightToHashPath)
131+
if err != nil {
132+
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
133+
}
112134

113-
introducerHost := "introducer.chia.net"
114-
dnsIntroducerHost := "dns-introducer.chia.net"
115-
fullNodePort := uint16(8444)
116-
peersFilePath := "peers.dat"
117-
walletPeersFilePath := "wallet/db/wallet_peers.dat"
118-
bootstrapPeers := []string{"node.chia.net"}
119-
if networkName != "mainnet" {
120-
introducerHost = fmt.Sprintf("introducer-%s.chia.net", networkName)
121-
dnsIntroducerHost = fmt.Sprintf("dns-introducer-%s.chia.net", networkName)
122-
fullNodePort = uint16(58444)
123-
peersFilePath = fmt.Sprintf("peers-%s.dat", networkName)
124-
walletPeersFilePath = fmt.Sprintf("wallet/db/wallet_peers-%s.dat", networkName)
125-
bootstrapPeers = []string{fmt.Sprintf("node-%s.chia.net", networkName)}
126-
}
127-
if introFlag := viper.GetString("switch-introducer"); introFlag != "" {
128-
introducerHost = introFlag
129-
}
130-
if dnsIntroFlag := viper.GetString("switch-dns-introducer"); dnsIntroFlag != "" {
131-
dnsIntroducerHost = dnsIntroFlag
132-
}
133-
if bootPeer := viper.GetString("switch-bootstrap-peer"); bootPeer != "" {
134-
bootstrapPeers = []string{bootPeer}
135-
}
136-
// If there is a port in the config, use that, but still allow the flag to be the final say
137-
if netConfig.DefaultFullNodePort != 0 {
138-
fullNodePort = netConfig.DefaultFullNodePort
139-
}
140-
if portFlag := viper.GetUint16("switch-full-node-port"); portFlag != 0 {
141-
fullNodePort = portFlag
142-
}
135+
introducerHost := "introducer.chia.net"
136+
dnsIntroducerHost := "dns-introducer.chia.net"
137+
fullNodePort := uint16(8444)
138+
peersFilePath := "peers.dat"
139+
walletPeersFilePath := "wallet/db/wallet_peers.dat"
140+
bootstrapPeers := []string{"node.chia.net"}
141+
if networkName != "mainnet" {
142+
introducerHost = fmt.Sprintf("introducer-%s.chia.net", networkName)
143+
dnsIntroducerHost = fmt.Sprintf("dns-introducer-%s.chia.net", networkName)
144+
fullNodePort = uint16(58444)
145+
peersFilePath = fmt.Sprintf("peers-%s.dat", networkName)
146+
walletPeersFilePath = fmt.Sprintf("wallet/db/wallet_peers-%s.dat", networkName)
147+
bootstrapPeers = []string{fmt.Sprintf("node-%s.chia.net", networkName)}
148+
}
149+
if introFlag := viper.GetString("switch-introducer"); introFlag != "" {
150+
introducerHost = introFlag
151+
}
152+
if dnsIntroFlag := viper.GetString("switch-dns-introducer"); dnsIntroFlag != "" {
153+
dnsIntroducerHost = dnsIntroFlag
154+
}
155+
if bootPeer := viper.GetString("switch-bootstrap-peer"); bootPeer != "" {
156+
bootstrapPeers = []string{bootPeer}
157+
}
158+
// If there is a port in the config, use that, but still allow the flag to be the final say
159+
if netConfig.DefaultFullNodePort != 0 {
160+
fullNodePort = netConfig.DefaultFullNodePort
161+
}
162+
if portFlag := viper.GetUint16("switch-full-node-port"); portFlag != 0 {
163+
fullNodePort = portFlag
164+
}
143165

144-
pathUpdates := map[string]any{
145-
"selected_network": networkName,
146-
"farmer.full_node_peers": []config.Peer{
147-
{
148-
Host: "localhost",
149-
Port: fullNodePort,
150-
},
166+
pathUpdates := map[string]any{
167+
"selected_network": networkName,
168+
"farmer.full_node_peers": []config.Peer{
169+
{
170+
Host: "localhost",
171+
Port: fullNodePort,
151172
},
152-
"full_node.database_path": fmt.Sprintf("db/blockchain_v2_%s.sqlite", networkName),
153-
"full_node.dns_servers": []string{dnsIntroducerHost},
154-
"full_node.peers_file_path": peersFilePath,
155-
"full_node.port": fullNodePort,
156-
"full_node.introducer_peer.host": introducerHost,
157-
"full_node.introducer_peer.port": fullNodePort,
158-
"introducer.port": fullNodePort,
159-
"seeder.port": fullNodePort,
160-
"seeder.other_peers_port": fullNodePort,
161-
"seeder.bootstrap_peers": bootstrapPeers,
162-
"timelord.full_node_peers": []config.Peer{
163-
{
164-
Host: "localhost",
165-
Port: fullNodePort,
166-
},
173+
},
174+
"full_node.database_path": fmt.Sprintf("db/blockchain_v2_%s.sqlite", networkName),
175+
"full_node.dns_servers": []string{dnsIntroducerHost},
176+
"full_node.peers_file_path": peersFilePath,
177+
"full_node.port": fullNodePort,
178+
"full_node.introducer_peer.host": introducerHost,
179+
"full_node.introducer_peer.port": fullNodePort,
180+
"introducer.port": fullNodePort,
181+
"seeder.port": fullNodePort,
182+
"seeder.other_peers_port": fullNodePort,
183+
"seeder.bootstrap_peers": bootstrapPeers,
184+
"timelord.full_node_peers": []config.Peer{
185+
{
186+
Host: "localhost",
187+
Port: fullNodePort,
167188
},
168-
"wallet.dns_servers": []string{dnsIntroducerHost},
169-
"wallet.full_node_peers": []config.Peer{
170-
{
171-
Host: "localhost",
172-
Port: fullNodePort,
173-
},
189+
},
190+
"wallet.dns_servers": []string{dnsIntroducerHost},
191+
"wallet.full_node_peers": []config.Peer{
192+
{
193+
Host: "localhost",
194+
Port: fullNodePort,
174195
},
175-
"wallet.introducer_peer.host": introducerHost,
176-
"wallet.introducer_peer.port": fullNodePort,
177-
"wallet.wallet_peers_file_path": walletPeersFilePath,
178-
}
179-
for path, value := range pathUpdates {
180-
pathMap := config.ParsePathsFromStrings([]string{path}, false)
181-
var key string
182-
var pathSlice []string
183-
for key, pathSlice = range pathMap {
184-
break
185-
}
186-
slogs.Logr.Debug("setting config path", "path", path, "value", value)
187-
err = cfg.SetFieldByPath(pathSlice, value)
188-
if err != nil {
189-
slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err)
190-
}
191-
}
192-
193-
slogs.Logr.Debug("saving config")
194-
err = cfg.Save()
195-
if err != nil {
196-
slogs.Logr.Fatal("error saving chia config", "error", err)
196+
},
197+
"wallet.introducer_peer.host": introducerHost,
198+
"wallet.introducer_peer.port": fullNodePort,
199+
"wallet.wallet_peers_file_path": walletPeersFilePath,
200+
}
201+
for path, value := range pathUpdates {
202+
pathMap := config.ParsePathsFromStrings([]string{path}, false)
203+
var key string
204+
var pathSlice []string
205+
for key, pathSlice = range pathMap {
206+
break
197207
}
198-
199-
err = removeFileIfExists(path.Join(chiaRoot, "db", peersFilePath))
208+
slogs.Logr.Debug("setting config path", "path", path, "value", value)
209+
err = cfg.SetFieldByPath(pathSlice, value)
200210
if err != nil {
201-
slogs.Logr.Error("error removing old peers.dat file", "path", peersFilePath, "error", err)
211+
slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err)
202212
}
213+
}
203214

204-
slogs.Logr.Info("Complete")
205-
},
206-
}
207-
208-
func init() {
209-
networkCmd.PersistentFlags().String("introducer", "", "Override the default values for introducer host")
210-
networkCmd.PersistentFlags().String("dns-introducer", "", "Override the default values for dns-introducer host")
211-
networkCmd.PersistentFlags().String("bootstrap-peer", "", "Override the default value for seeder bootstrap peer")
212-
networkCmd.PersistentFlags().Uint16("full-node-port", 0, "Override the default values for the full node port")
215+
slogs.Logr.Debug("saving config")
216+
err = cfg.Save()
217+
if err != nil {
218+
slogs.Logr.Fatal("error saving chia config", "error", err)
219+
}
213220

214-
cobra.CheckErr(viper.BindPFlag("switch-introducer", networkCmd.PersistentFlags().Lookup("introducer")))
215-
cobra.CheckErr(viper.BindPFlag("switch-dns-introducer", networkCmd.PersistentFlags().Lookup("dns-introducer")))
216-
cobra.CheckErr(viper.BindPFlag("switch-bootstrap-peer", networkCmd.PersistentFlags().Lookup("bootstrap-peer")))
217-
cobra.CheckErr(viper.BindPFlag("switch-full-node-port", networkCmd.PersistentFlags().Lookup("full-node-port")))
221+
err = removeFileIfExists(path.Join(chiaRoot, "db", peersFilePath))
222+
if err != nil {
223+
slogs.Logr.Error("error removing old peers.dat file", "path", peersFilePath, "error", err)
224+
}
218225

219-
networkCmd.AddCommand(switchCmd)
226+
slogs.Logr.Info("Complete")
220227
}
221228

222229
func isConnectionRefused(err error) bool {

0 commit comments

Comments
 (0)