From a9262fdbeec5192a50e7997b412a8a3801a2d06b Mon Sep 17 00:00:00 2001 From: Chris Marslender Date: Thu, 21 Nov 2024 16:19:19 -0600 Subject: [PATCH] Also keep track of bootstrap/static peers for seeder, and any full ndoe static peers --- cmd/network/switch.go | 23 +++++++++++++++++++++-- cmd/network/switch_test.go | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cmd/network/switch.go b/cmd/network/switch.go index 04d633f..8f3d35c 100644 --- a/cmd/network/switch.go +++ b/cmd/network/switch.go @@ -43,7 +43,10 @@ func init() { // retainedSettings are the settings we want to keep track of when switching networks so we can swap back to them in the future type retainedSettings struct { - DNSServers []string `json:"dns_servers"` + DNSServers []string `json:"dns_servers"` + BootstrapPeers []string `json:"bootstrap_peers"` + StaticPeers []string `json:"static_peers"` + FullNodePeers []config.Peer `json:"full_node_peers"` } // SwitchNetwork implements the logic to swap networks @@ -98,7 +101,10 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) { } previousSettings := retainedSettings{ - DNSServers: cfg.FullNode.DNSServers, + DNSServers: cfg.FullNode.DNSServers, + BootstrapPeers: cfg.Seeder.BootstrapPeers, + StaticPeers: cfg.Seeder.StaticPeers, + FullNodePeers: cfg.FullNode.FullNodePeers, } marshalled, err := json.Marshal(previousSettings) if err != nil { @@ -167,9 +173,11 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) { introducerHost := "introducer.chia.net" dnsIntroducerHosts := []string{"dns-introducer.chia.net"} fullNodePort := uint16(8444) + var fullnodePeers []config.Peer peersFilePath := "db/peers.dat" walletPeersFilePath := "wallet/db/wallet_peers.dat" bootstrapPeers := []string{"node.chia.net"} + var staticPeers []string if networkName != "mainnet" { introducerHost = fmt.Sprintf("introducer-%s.chia.net", networkName) dnsIntroducerHosts = []string{fmt.Sprintf("dns-introducer-%s.chia.net", networkName)} @@ -185,6 +193,15 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) { if len(settingsToRestore.DNSServers) > 0 { dnsIntroducerHosts = settingsToRestore.DNSServers } + if len(settingsToRestore.BootstrapPeers) > 0 { + bootstrapPeers = settingsToRestore.BootstrapPeers + } + if len(settingsToRestore.StaticPeers) > 0 { + staticPeers = settingsToRestore.StaticPeers + } + if len(settingsToRestore.FullNodePeers) > 0 { + fullnodePeers = settingsToRestore.FullNodePeers + } } if introFlag := viper.GetString("switch-introducer"); introFlag != "" { @@ -216,12 +233,14 @@ func SwitchNetwork(networkName string, checkForRunningNode bool) { "full_node.dns_servers": dnsIntroducerHosts, "full_node.peers_file_path": peersFilePath, "full_node.port": fullNodePort, + "full_node.full_node_peers": fullnodePeers, "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, + "seeder.static_peers": staticPeers, "timelord.full_node_peers": []config.Peer{ { Host: "localhost", diff --git a/cmd/network/switch_test.go b/cmd/network/switch_test.go index 3c10282..cb807ca 100644 --- a/cmd/network/switch_test.go +++ b/cmd/network/switch_test.go @@ -98,6 +98,9 @@ func TestNetworkSwitch_SettingRetention(t *testing.T) { // Set some custom dns introducers, and ensure they are back when swapping away and back to mainnet cfg.FullNode.DNSServers = []string{"dns-mainnet-1.example.com", "dns-mainnet-2.example.com"} + cfg.Seeder.BootstrapPeers = []string{"bootstrap-mainnet-1.example.com"} + cfg.Seeder.StaticPeers = []string{"static-peer-1.example.com"} + cfg.FullNode.FullNodePeers = []config.Peer{{Host: "fn-peer-1.example.com", Port: 1234}} err = cfg.Save() assert.NoError(t, err) @@ -107,10 +110,21 @@ func TestNetworkSwitch_SettingRetention(t *testing.T) { assert.Equal(t, []string{"dns-mainnet-1.example.com", "dns-mainnet-2.example.com"}, cfg.FullNode.DNSServers) network.SwitchNetwork("unittestnet", false) + // reload config from disk to ensure defaults are in the config now + cfg, err = config.GetChiaConfig() + assert.NoError(t, err) + assert.Equal(t, []string{"dns-introducer-unittestnet.chia.net"}, cfg.FullNode.DNSServers) + assert.Equal(t, []string{"node-unittestnet.chia.net"}, cfg.Seeder.BootstrapPeers) + assert.Equal(t, []string{}, cfg.Seeder.StaticPeers) + assert.Equal(t, []config.Peer{}, cfg.FullNode.FullNodePeers) + network.SwitchNetwork("mainnet", false) // reload config from disk cfg, err = config.GetChiaConfig() assert.NoError(t, err) assert.Equal(t, []string{"dns-mainnet-1.example.com", "dns-mainnet-2.example.com"}, cfg.FullNode.DNSServers) + assert.Equal(t, []string{"bootstrap-mainnet-1.example.com"}, cfg.Seeder.BootstrapPeers) + assert.Equal(t, []string{"static-peer-1.example.com"}, cfg.Seeder.StaticPeers) + assert.Equal(t, []config.Peer{{Host: "fn-peer-1.example.com", Port: 1234}}, cfg.FullNode.FullNodePeers) }