Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(network): default configs for bootstrap and relay peers #812

Merged
merged 2 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 56 additions & 50 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,58 +340,10 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,
crypto.XPrivateKeyHRP = "txsecret"
}

var defConf *config.Config
switch gen.ChainType() {
case genesis.Mainnet:
panic("not yet implemented!")
case genesis.Testnet:
defConf = config.DefaultConfigTestnet()
case genesis.Localnet:
defConf = config.DefaultConfigLocalnet()
}

confPath := PactusConfigPath(workingDir)
conf, err := config.LoadFromFile(confPath, true, defConf)
conf, err := tryLoadConfig(gen.ChainType(), confPath)
if err != nil {
PrintWarnMsgf("Unable to load the config: %s", err)
PrintInfoMsgf("Attempting to restore the config to the default values...")

// First, try to open the old config file in non-strict mode
confBack, err := config.LoadFromFile(confPath, false, defConf)
if err != nil {
return nil, nil, err
}

// Let's create a backup of the config
confBackupPath := fmt.Sprintf("%v_bak_%s", confPath, time.Now().Format("2006_01_02"))
err = os.Rename(confPath, confBackupPath)
if err != nil {
return nil, nil, err
}

// Now, attempt to restore the config file with the number of validators from the old config.
switch gen.ChainType() {
case genesis.Mainnet:
panic("not yet implemented!")

case genesis.Testnet:
err = config.SaveTestnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, nil, err
}

case genesis.Localnet:
err = config.SaveLocalnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, nil, err
}

default:
return nil, nil, fmt.Errorf("invalid chain type")
}

PrintSuccessMsgf("Config restored to the default values")
conf, _ = config.LoadFromFile(confPath, true, defConf) // This time it should be OK
return nil, nil, err
}

err = conf.BasicCheck()
Expand Down Expand Up @@ -487,3 +439,57 @@ func makeLocalGenesis(w wallet.Wallet) *genesis.Genesis {
gen := genesis.MakeGenesis(util.RoundNow(60), accs, vals, params)
return gen
}

func tryLoadConfig(chainType genesis.ChainType, confPath string) (*config.Config, error) {
var defConf *config.Config
switch chainType {
case genesis.Mainnet:
panic("not yet implemented!")
case genesis.Testnet:
defConf = config.DefaultConfigTestnet()
case genesis.Localnet:
defConf = config.DefaultConfigLocalnet()
}

conf, err := config.LoadFromFile(confPath, true, defConf)
if err != nil {
PrintWarnMsgf("Unable to load the config: %s", err)
PrintInfoMsgf("Attempting to restore the config to the default values...")

// First, try to open the old config file in non-strict mode
confBack, err := config.LoadFromFile(confPath, false, defConf)
if err != nil {
return nil, err
}

// Let's create a backup of the config
confBackupPath := fmt.Sprintf("%v_bak_%s", confPath, time.Now().Format("2006_01_02"))
err = os.Rename(confPath, confBackupPath)
if err != nil {
return nil, err
}

// Now, attempt to restore the config file with the number of validators from the old config.
switch chainType {
case genesis.Mainnet:
panic("not yet implemented!")

case genesis.Testnet:
err = config.SaveTestnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, err
}

case genesis.Localnet:
err = config.SaveLocalnetConfig(confPath, confBack.Node.NumValidators)
if err != nil {
return nil, err
}
}

PrintSuccessMsgf("Config restored to the default values")
conf, _ = config.LoadFromFile(confPath, true, defConf) // This time it should be OK
}

return conf, nil
}
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func DefaultConfigTestnet() *Config {
"/ip4/0.0.0.0/tcp/21777", "/ip4/0.0.0.0/udp/21777/quic-v1",
"/ip6/::/tcp/21777", "/ip6/::/udp/21777/quic-v1",
}
conf.Network.BootstrapAddrStrings = []string{
conf.Network.DefaultBootstrapAddrStrings = []string{
"/ip4/94.101.184.118/tcp/21777/p2p/12D3KooWCwQZt8UriVXobQHPXPR8m83eceXVoeT6brPNiBHomebc",
"/ip4/172.104.46.145/tcp/21777/p2p/12D3KooWNYD4bB82YZRXv6oNyYPwc5ozabx2epv75ATV3D8VD3Mq",
"/ip4/13.115.190.71/tcp/21777/p2p/12D3KooWBGNEH8NqdK1UddSnPV1yRHGLYpaQUcnujC24s7YNWPiq",
Expand All @@ -116,13 +116,14 @@ func DefaultConfigTestnet() *Config {
"/dns/pactus.nodesync.top/tcp/21777/p2p/12D3KooWP25ejVsd7cL5DvWAPwEu4JTUwnPniHBf4w93tgSezVt8", // NodeSync.Top ([email protected])
"/ip4/95.217.89.202/tcp/21777/p2p/12D3KooWMsi5oYkbbpyyXctmPXzF8UZu2pCvKPRZGyvymhN9BzTD", // CodeBlockLabs ([email protected])
}
conf.Network.RelayAddrStrings = []string{
conf.Network.DefaultRelayAddrStrings = []string{
"/ip4/139.162.153.10/tcp/4002/p2p/12D3KooWNR79jqHVVNhNVrqnDbxbJJze4VjbEsBjZhz6mkvinHAN",
"/ip4/188.121.102.178/tcp/4002/p2p/12D3KooWCRHn8vjrKNBEQcut8uVCYX5q77RKidPaE6iMK31qEVHb",
}
conf.Network.MinConns = 16
conf.Network.MaxConns = 32
conf.Network.EnableNAT = false
conf.Network.EnableNATService = false
conf.Network.EnableUPnP = false
conf.Network.EnableRelay = true
conf.Network.NetworkName = "pactus-testnet"
conf.Network.DefaultPort = 21777
Expand All @@ -142,7 +143,8 @@ func DefaultConfigLocalnet() *Config {
conf := defaultConfig()
conf.Network.ListenAddrStrings = []string{}
conf.Network.EnableRelay = false
conf.Network.EnableNAT = false
conf.Network.EnableNATService = false
conf.Network.EnableUPnP = false
conf.Network.BootstrapAddrStrings = []string{}
conf.Network.MinConns = 0
conf.Network.MaxConns = 0
Expand Down
56 changes: 28 additions & 28 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
[store]

# `path` specifies the directory where blockchain data will be stored.
# Default is data
# Default is `"data"`.
## path = "data"

# `network` contains configuration options for the network module, which manages communication between nodes.
[network]

# `network_key` specifies the private key filename to use for node authentication and encryption in the p2p protocol.
# Default is `"network_key"`.
## network_key = "network_key"

# `public_addr` is the the address that is public and accessible to other nodes.
Expand All @@ -39,89 +40,88 @@
## bootstrap_addrs = []

# `min_connections` is the minimum number of connections that the Pactus node should maintain.
# Default is 16
# Default is `16`.
## min_connections = 16

# `max_connections` is the maximum number of connections that the Pactus node should maintain.
# Default is 32
# Default is `32`.
## max_connections = 32

# `enable_nat` indicates whether NAT service should be enabled or not.
# NAT service allows many machines to share a single public address.
# Default is false.
## enable_nat = false
# `enable_nat_service` provides a service to other peer for determining their reachability status.
# Default is `false`.
## enable_nat_service = false

# `enable_upnp` will attempt to open a port in your network's firewall using UPnP.
# Default is `false`.
## enable_upnp = false

# `enable_relay` indicates whether relay service should be enabled or not.
# Relay service is a transport protocol that routes traffic between two peers over a third-party “relay” peer.
# Default is false.
# Default is `false`.
## enable_relay = false

# `enable_mdns` indicates whether MDNS should be enabled or not.
# MDNS is a protocol to discover local peers quickly and efficiently.
# Default is false.
# Default is `false`.
## enable_mdns = false

# `enable_metrics` provides the network metrics for the Prometheus software.
# Default is false.
# Default is `false`.
## enable_metrics = false

# `force_private_network` forces the connection to nodes within a private network.
# A private network is a computer network that uses private addresses.
# Read more about private networks here: https://en.wikipedia.org/wiki/Private_network
# Default is false.
# Default is `false`.
## force_private_network = false

# `bootstrapper` runs the node in bootstrap mode.
# Default is false.
## bootstrapper = false

# `sync` contains configuration of sync module.
[sync]

# `moniker` is a custom human-readable name for this node.
## moniker = ""

# `session_timeout` is a timeout for a session to be opened.
# Default is 10 seconds
# Default is `10s`.
## session_timeout = "10s"

# `node_network` indicates whether the node is capable of serving the complete blockchain.
# Default is true
# Default is `true`.
## node_network = true

# `node_gossip_experimental` indicates whether the node participates in the gossiping protocol.
# Default is false
# Default is `false`.
## node_gossip_experimental = false

# `sync.firewall` contains configuration options for the sync firewall.
[sync.firewall]
# `enable` indicates whether the firewall should be enabled or not.
# Default is false
# Default is `false`.
## enable = false

# `tx_pool` contains configuration options for the transaction pool module.
[tx_pool]

# `max_size` indicates the maximum number of unconfirmed transactions inside the pool.
# Default is 2000
# Default is `2000`.
## max_size = 2000

# `consensus` contains configuration options for the consensus module.
[consensus]

# `change_proposer_timeout` describes how long to wait for a proposal in
# each round before requesting a change of proposer.
# Default is 6 seconds.
# Default is `6s`.
## change_proposer_timeout = "6s"

# `change_proposer_delta` increases `change_proposer_timeout` with each round.
# Default is 2 seconds.
# Default is `2s`.
## change_proposer_delta = "2s"

# `logger` contains configuration options for the logger.
[logger]
# `colorful` indicates whether log can be colorful or not.
# Default is true.
# Default is `true`.
## colorful = true

# `logger.levels` contains the level of logger per module.
Expand All @@ -142,7 +142,7 @@
[grpc]

# `enable` indicates whether gRPC service should be enabled or not.
# Default is false
# Default is `false`.
## enable = false

# `listen` is the address to listen for incoming connections for gRPC server.
Expand All @@ -153,22 +153,22 @@
[grpc.gateway]

# `enable` indicates whether gRPCGateway service should be enabled or not.
# Default is false
# Default is `false`.
## enable = false

# `listen` is the address to listen for incoming connections for gRPCGateway server.
## listen = "8080"

# `enable_cors` indicates whether Cross-Origin Request Sharing (CORS)
# protection should be enabled or not.
# Default is false
# Default is `false`.
## enable_cors = false

# `http` configuration.
[http]

# `enable` indicates whether HTTP service should be enabled or not.
# Default is false
# Default is `false`.
## enable = false

# `listen` is the address address to listen for incoming connections for HTTP server.
Expand All @@ -178,7 +178,7 @@
[nanomsg]

# `enable` indicates whether nanomsg service should be enabled or not.
# Default is false
# Default is `false`.
## enable = false

# `listen` is the address to listen for incoming connections for nanomsg server.
Expand Down
Loading
Loading