Skip to content

Commit

Permalink
Also keep track of bootstrap/static peers for seeder, and any full nd…
Browse files Browse the repository at this point in the history
…oe static peers
  • Loading branch information
cmmarslender committed Nov 21, 2024
1 parent bd5bb99 commit c217c56
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cmd/network/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)}
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions cmd/network/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}

0 comments on commit c217c56

Please sign in to comment.