Skip to content

Commit

Permalink
Update go chia libs to latest to fix bug setting lists directly with … (
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Nov 21, 2024
1 parent a655e73 commit 373d7c2
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 164 deletions.
325 changes: 166 additions & 159 deletions cmd/network/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,77 @@ var switchCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
networkName := args[0]
slogs.Logr.Info("Swapping to network", "network", networkName)
SwitchNetwork(networkName, true)
},
}

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

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

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

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

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

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

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

slogs.Logr.Debug("ensuring directory exists for new network cache files", "directory", cacheFileDirNewNetwork)
err = os.MkdirAll(cacheFileDirNewNetwork, 0755)
if err != nil {
slogs.Logr.Fatal("error creating cache file directory for new network", "error", err, "directory", cacheFileDirNewNetwork)
}
if currentNetwork == networkName {
slogs.Logr.Fatal("current network name and new network name are the same", "current", currentNetwork, "new", networkName)
}

// Ensure we have network constants for the network trying to be swapped to
if _, ok := cfg.NetworkOverrides.Constants[networkName]; !ok {
slogs.Logr.Fatal("selected network does not exist in config's network override constants", "network", networkName)
}
var (
netConfig config.NetworkConfig
ok bool
)
if netConfig, ok = cfg.NetworkOverrides.Config[networkName]; !ok {
slogs.Logr.Fatal("selected network does not exist in config's network override config", "network", networkName)
}

// Ensure a folder to store the current network's sub-epoch-summaries and height-to-hash files exists
cacheFileDirOldNetwork := path.Join(chiaRoot, "db", currentNetwork)
cacheFileDirNewNetwork := path.Join(chiaRoot, "db", networkName)

slogs.Logr.Debug("ensuring directory exists for current network cache files", "directory", cacheFileDirOldNetwork)
err = os.MkdirAll(cacheFileDirOldNetwork, 0755)
if err != nil {
slogs.Logr.Fatal("error creating cache file directory for current network", "error", err, "directory", cacheFileDirOldNetwork)
}

// Check if Full Node is running
slogs.Logr.Debug("ensuring directory exists for new network cache files", "directory", cacheFileDirNewNetwork)
err = os.MkdirAll(cacheFileDirNewNetwork, 0755)
if err != nil {
slogs.Logr.Fatal("error creating cache file directory for new network", "error", err, "directory", cacheFileDirNewNetwork)
}

// Check if Full Node is running
if checkForRunningNode {
slogs.Logr.Debug("initializing websocket client to ensure chia is stopped")
rpcClient, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig(), rpc.WithSyncWebsocket())
if err != nil {
Expand All @@ -85,138 +106,124 @@ var switchCmd = &cobra.Command{
slogs.Logr.Fatal("error stopping chia services", "error", err)
}
}
}

// Safe to move files now
activeSubEpochSummariesPath := path.Join(chiaRoot, "db", "sub-epoch-summaries")
activeHeightToHashPath := path.Join(chiaRoot, "db", "height-to-hash")
// Safe to move files now
activeSubEpochSummariesPath := path.Join(chiaRoot, "db", "sub-epoch-summaries")
activeHeightToHashPath := path.Join(chiaRoot, "db", "height-to-hash")

// Move current cache files to the network subdir
err = moveAndOverwriteFile(activeSubEpochSummariesPath, path.Join(cacheFileDirOldNetwork, "sub-epoch-summaries"))
if err != nil {
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
}
err = moveAndOverwriteFile(activeHeightToHashPath, path.Join(cacheFileDirOldNetwork, "height-to-hash"))
if err != nil {
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
}
// Move current cache files to the network subdir
err = moveAndOverwriteFile(activeSubEpochSummariesPath, path.Join(cacheFileDirOldNetwork, "sub-epoch-summaries"))
if err != nil {
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
}
err = moveAndOverwriteFile(activeHeightToHashPath, path.Join(cacheFileDirOldNetwork, "height-to-hash"))
if err != nil {
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
}

// Move old cached files to active dir
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "sub-epoch-summaries"), activeSubEpochSummariesPath)
if err != nil {
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
}
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "height-to-hash"), activeHeightToHashPath)
if err != nil {
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
}
// Move old cached files to active dir
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "sub-epoch-summaries"), activeSubEpochSummariesPath)
if err != nil {
slogs.Logr.Fatal("error moving sub-epoch-summaries file", "error", err)
}
err = moveAndOverwriteFile(path.Join(cacheFileDirNewNetwork, "height-to-hash"), activeHeightToHashPath)
if err != nil {
slogs.Logr.Fatal("error moving height-to-hash file", "error", err)
}

introducerHost := "introducer.chia.net"
dnsIntroducerHost := "dns-introducer.chia.net"
fullNodePort := uint16(8444)
peersFilePath := "peers.dat"
walletPeersFilePath := "wallet/db/wallet_peers.dat"
bootstrapPeers := []string{"node.chia.net"}
if networkName != "mainnet" {
introducerHost = fmt.Sprintf("introducer-%s.chia.net", networkName)
dnsIntroducerHost = fmt.Sprintf("dns-introducer-%s.chia.net", networkName)
fullNodePort = uint16(58444)
peersFilePath = fmt.Sprintf("peers-%s.dat", networkName)
walletPeersFilePath = fmt.Sprintf("wallet/db/wallet_peers-%s.dat", networkName)
bootstrapPeers = []string{fmt.Sprintf("node-%s.chia.net", networkName)}
}
if introFlag := viper.GetString("switch-introducer"); introFlag != "" {
introducerHost = introFlag
}
if dnsIntroFlag := viper.GetString("switch-dns-introducer"); dnsIntroFlag != "" {
dnsIntroducerHost = dnsIntroFlag
}
if bootPeer := viper.GetString("switch-bootstrap-peer"); bootPeer != "" {
bootstrapPeers = []string{bootPeer}
}
// If there is a port in the config, use that, but still allow the flag to be the final say
if netConfig.DefaultFullNodePort != 0 {
fullNodePort = netConfig.DefaultFullNodePort
}
if portFlag := viper.GetUint16("switch-full-node-port"); portFlag != 0 {
fullNodePort = portFlag
}
introducerHost := "introducer.chia.net"
dnsIntroducerHost := "dns-introducer.chia.net"
fullNodePort := uint16(8444)
peersFilePath := "peers.dat"
walletPeersFilePath := "wallet/db/wallet_peers.dat"
bootstrapPeers := []string{"node.chia.net"}
if networkName != "mainnet" {
introducerHost = fmt.Sprintf("introducer-%s.chia.net", networkName)
dnsIntroducerHost = fmt.Sprintf("dns-introducer-%s.chia.net", networkName)
fullNodePort = uint16(58444)
peersFilePath = fmt.Sprintf("peers-%s.dat", networkName)
walletPeersFilePath = fmt.Sprintf("wallet/db/wallet_peers-%s.dat", networkName)
bootstrapPeers = []string{fmt.Sprintf("node-%s.chia.net", networkName)}
}
if introFlag := viper.GetString("switch-introducer"); introFlag != "" {
introducerHost = introFlag
}
if dnsIntroFlag := viper.GetString("switch-dns-introducer"); dnsIntroFlag != "" {
dnsIntroducerHost = dnsIntroFlag
}
if bootPeer := viper.GetString("switch-bootstrap-peer"); bootPeer != "" {
bootstrapPeers = []string{bootPeer}
}
// If there is a port in the config, use that, but still allow the flag to be the final say
if netConfig.DefaultFullNodePort != 0 {
fullNodePort = netConfig.DefaultFullNodePort
}
if portFlag := viper.GetUint16("switch-full-node-port"); portFlag != 0 {
fullNodePort = portFlag
}

pathUpdates := map[string]any{
"selected_network": networkName,
"farmer.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
pathUpdates := map[string]any{
"selected_network": networkName,
"farmer.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
"full_node.database_path": fmt.Sprintf("db/blockchain_v2_%s.sqlite", networkName),
"full_node.dns_servers": []string{dnsIntroducerHost},
"full_node.peers_file_path": peersFilePath,
"full_node.port": fullNodePort,
"full_node.introducer_peer.host": introducerHost,
"full_node.introducer_peer.port": fullNodePort,
"introducer.port": fullNodePort,
"seeder.port": fullNodePort,
"seeder.other_peers_port": fullNodePort,
"seeder.bootstrap_peers": bootstrapPeers,
"timelord.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
},
"full_node.database_path": fmt.Sprintf("db/blockchain_v2_%s.sqlite", networkName),
"full_node.dns_servers": []string{dnsIntroducerHost},
"full_node.peers_file_path": peersFilePath,
"full_node.port": fullNodePort,
"full_node.introducer_peer.host": introducerHost,
"full_node.introducer_peer.port": fullNodePort,
"introducer.port": fullNodePort,
"seeder.port": fullNodePort,
"seeder.other_peers_port": fullNodePort,
"seeder.bootstrap_peers": bootstrapPeers,
"timelord.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
"wallet.dns_servers": []string{dnsIntroducerHost},
"wallet.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
},
"wallet.dns_servers": []string{dnsIntroducerHost},
"wallet.full_node_peers": []config.Peer{
{
Host: "localhost",
Port: fullNodePort,
},
"wallet.introducer_peer.host": introducerHost,
"wallet.introducer_peer.port": fullNodePort,
"wallet.wallet_peers_file_path": walletPeersFilePath,
}
for path, value := range pathUpdates {
pathMap := config.ParsePathsFromStrings([]string{path}, false)
var key string
var pathSlice []string
for key, pathSlice = range pathMap {
break
}
slogs.Logr.Debug("setting config path", "path", path, "value", value)
err = cfg.SetFieldByPath(pathSlice, value)
if err != nil {
slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err)
}
}

slogs.Logr.Debug("saving config")
err = cfg.Save()
if err != nil {
slogs.Logr.Fatal("error saving chia config", "error", err)
},
"wallet.introducer_peer.host": introducerHost,
"wallet.introducer_peer.port": fullNodePort,
"wallet.wallet_peers_file_path": walletPeersFilePath,
}
for path, value := range pathUpdates {
pathMap := config.ParsePathsFromStrings([]string{path}, false)
var key string
var pathSlice []string
for key, pathSlice = range pathMap {
break
}

err = removeFileIfExists(path.Join(chiaRoot, "db", peersFilePath))
slogs.Logr.Debug("setting config path", "path", path, "value", value)
err = cfg.SetFieldByPath(pathSlice, value)
if err != nil {
slogs.Logr.Error("error removing old peers.dat file", "path", peersFilePath, "error", err)
slogs.Logr.Fatal("error setting path in config", "key", key, "value", value, "error", err)
}
}

slogs.Logr.Info("Complete")
},
}

func init() {
networkCmd.PersistentFlags().String("introducer", "", "Override the default values for introducer host")
networkCmd.PersistentFlags().String("dns-introducer", "", "Override the default values for dns-introducer host")
networkCmd.PersistentFlags().String("bootstrap-peer", "", "Override the default value for seeder bootstrap peer")
networkCmd.PersistentFlags().Uint16("full-node-port", 0, "Override the default values for the full node port")
slogs.Logr.Debug("saving config")
err = cfg.Save()
if err != nil {
slogs.Logr.Fatal("error saving chia config", "error", err)
}

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

networkCmd.AddCommand(switchCmd)
slogs.Logr.Info("Complete")
}

func isConnectionRefused(err error) bool {
Expand Down
Loading

0 comments on commit 373d7c2

Please sign in to comment.