1
1
package network
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"net"
@@ -40,6 +41,14 @@ func init() {
40
41
networkCmd .AddCommand (switchCmd )
41
42
}
42
43
44
+ // retainedSettings are the settings we want to keep track of when switching networks so we can swap back to them in the future
45
+ type retainedSettings struct {
46
+ DNSServers []string `json:"dns_servers"`
47
+ BootstrapPeers []string `json:"bootstrap_peers"`
48
+ StaticPeers []string `json:"static_peers"`
49
+ FullNodePeers []config.Peer `json:"full_node_peers"`
50
+ }
51
+
43
52
// SwitchNetwork implements the logic to swap networks
44
53
func SwitchNetwork (networkName string , checkForRunningNode bool ) {
45
54
slogs .Logr .Info ("Swapping to network" , "network" , networkName )
@@ -91,6 +100,35 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) {
91
100
slogs .Logr .Fatal ("error creating cache file directory for new network" , "error" , err , "directory" , cacheFileDirNewNetwork )
92
101
}
93
102
103
+ previousSettings := retainedSettings {
104
+ DNSServers : cfg .FullNode .DNSServers ,
105
+ BootstrapPeers : cfg .Seeder .BootstrapPeers ,
106
+ StaticPeers : cfg .Seeder .StaticPeers ,
107
+ FullNodePeers : cfg .FullNode .FullNodePeers ,
108
+ }
109
+ marshalled , err := json .Marshal (previousSettings )
110
+ if err != nil {
111
+ slogs .Logr .Fatal ("error marshalling retained settings to json" , "error" , err )
112
+ }
113
+ err = os .WriteFile (path .Join (cacheFileDirOldNetwork , "settings.json" ), marshalled , 0644 )
114
+ if err != nil {
115
+ slogs .Logr .Fatal ("error writing settings for old network" , "error" , err )
116
+ }
117
+
118
+ var settingsToRestore * retainedSettings
119
+ newSettingsPath := path .Join (cacheFileDirNewNetwork , "settings.json" )
120
+ if _ , err := os .Stat (newSettingsPath ); err == nil {
121
+ settings , err := os .ReadFile (newSettingsPath )
122
+ if err != nil {
123
+ slogs .Logr .Fatal ("error reading stored settings for the new network" , "error" , err )
124
+ }
125
+ settingsToRestore = & retainedSettings {}
126
+ err = json .Unmarshal (settings , settingsToRestore )
127
+ if err != nil {
128
+ slogs .Logr .Fatal ("error unmarshalling stored settings for the new network" , "error" , err )
129
+ }
130
+ }
131
+
94
132
// Check if Full Node is running
95
133
if checkForRunningNode {
96
134
slogs .Logr .Debug ("initializing websocket client to ensure chia is stopped" )
@@ -133,24 +171,44 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) {
133
171
}
134
172
135
173
introducerHost := "introducer.chia.net"
136
- dnsIntroducerHost := "dns-introducer.chia.net"
174
+ dnsIntroducerHosts := [] string { "dns-introducer.chia.net" }
137
175
fullNodePort := uint16 (8444 )
138
- peersFilePath := "peers.dat"
176
+ var fullnodePeers []config.Peer
177
+ peersFilePath := "db/peers.dat"
139
178
walletPeersFilePath := "wallet/db/wallet_peers.dat"
140
179
bootstrapPeers := []string {"node.chia.net" }
180
+ var staticPeers []string
141
181
if networkName != "mainnet" {
142
182
introducerHost = fmt .Sprintf ("introducer-%s.chia.net" , networkName )
143
- dnsIntroducerHost = fmt .Sprintf ("dns-introducer-%s.chia.net" , networkName )
183
+ dnsIntroducerHosts = [] string { fmt .Sprintf ("dns-introducer-%s.chia.net" , networkName )}
144
184
fullNodePort = uint16 (58444 )
145
- peersFilePath = fmt .Sprintf ("peers-%s.dat" , networkName )
185
+ peersFilePath = fmt .Sprintf ("db/ peers-%s.dat" , networkName )
146
186
walletPeersFilePath = fmt .Sprintf ("wallet/db/wallet_peers-%s.dat" , networkName )
147
187
bootstrapPeers = []string {fmt .Sprintf ("node-%s.chia.net" , networkName )}
148
188
}
189
+
190
+ // Any stored settings for the new network should be applied here, before any flags override them
191
+ if settingsToRestore != nil {
192
+ slogs .Logr .Info ("restoring stored settings for this network" )
193
+ if len (settingsToRestore .DNSServers ) > 0 {
194
+ dnsIntroducerHosts = settingsToRestore .DNSServers
195
+ }
196
+ if len (settingsToRestore .BootstrapPeers ) > 0 {
197
+ bootstrapPeers = settingsToRestore .BootstrapPeers
198
+ }
199
+ if len (settingsToRestore .StaticPeers ) > 0 {
200
+ staticPeers = settingsToRestore .StaticPeers
201
+ }
202
+ if len (settingsToRestore .FullNodePeers ) > 0 {
203
+ fullnodePeers = settingsToRestore .FullNodePeers
204
+ }
205
+ }
206
+
149
207
if introFlag := viper .GetString ("switch-introducer" ); introFlag != "" {
150
208
introducerHost = introFlag
151
209
}
152
210
if dnsIntroFlag := viper .GetString ("switch-dns-introducer" ); dnsIntroFlag != "" {
153
- dnsIntroducerHost = dnsIntroFlag
211
+ dnsIntroducerHosts = [] string { dnsIntroFlag }
154
212
}
155
213
if bootPeer := viper .GetString ("switch-bootstrap-peer" ); bootPeer != "" {
156
214
bootstrapPeers = []string {bootPeer }
@@ -172,22 +230,24 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) {
172
230
},
173
231
},
174
232
"full_node.database_path" : fmt .Sprintf ("db/blockchain_v2_%s.sqlite" , networkName ),
175
- "full_node.dns_servers" : [] string { dnsIntroducerHost } ,
233
+ "full_node.dns_servers" : dnsIntroducerHosts ,
176
234
"full_node.peers_file_path" : peersFilePath ,
177
235
"full_node.port" : fullNodePort ,
236
+ "full_node.full_node_peers" : fullnodePeers ,
178
237
"full_node.introducer_peer.host" : introducerHost ,
179
238
"full_node.introducer_peer.port" : fullNodePort ,
180
239
"introducer.port" : fullNodePort ,
181
240
"seeder.port" : fullNodePort ,
182
241
"seeder.other_peers_port" : fullNodePort ,
183
242
"seeder.bootstrap_peers" : bootstrapPeers ,
243
+ "seeder.static_peers" : staticPeers ,
184
244
"timelord.full_node_peers" : []config.Peer {
185
245
{
186
246
Host : "localhost" ,
187
247
Port : fullNodePort ,
188
248
},
189
249
},
190
- "wallet.dns_servers" : [] string { dnsIntroducerHost } ,
250
+ "wallet.dns_servers" : dnsIntroducerHosts ,
191
251
"wallet.full_node_peers" : []config.Peer {
192
252
{
193
253
Host : "localhost" ,
@@ -198,14 +258,14 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) {
198
258
"wallet.introducer_peer.port" : fullNodePort ,
199
259
"wallet.wallet_peers_file_path" : walletPeersFilePath ,
200
260
}
201
- for path , value := range pathUpdates {
202
- pathMap := config .ParsePathsFromStrings ([]string {path }, false )
261
+ for configPath , value := range pathUpdates {
262
+ pathMap := config .ParsePathsFromStrings ([]string {configPath }, false )
203
263
var key string
204
264
var pathSlice []string
205
265
for key , pathSlice = range pathMap {
206
266
break
207
267
}
208
- slogs .Logr .Debug ("setting config path" , "path" , path , "value" , value )
268
+ slogs .Logr .Debug ("setting config path" , "path" , configPath , "value" , value )
209
269
err = cfg .SetFieldByPath (pathSlice , value )
210
270
if err != nil {
211
271
slogs .Logr .Fatal ("error setting path in config" , "key" , key , "value" , value , "error" , err )
0 commit comments