Skip to content

Commit

Permalink
config/migrate: Automatically configure external P2P addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Oct 19, 2023
1 parent 1b654de commit 1f4f23f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changelog/5410.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config/migrate: Automatically configure external P2P addresses for validators
33 changes: 32 additions & 1 deletion go/oasis-node/cmd/config/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"fmt"
"io"
"net/url"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -724,13 +725,43 @@ func doMigrateConfig(cmd *cobra.Command, args []string) {

// If a node has `consensus.validator` set to true and it does not have any runtimes configured,
// the new `mode` should be set to `validator`.
var isValidator bool
if newValidator, ok := m(newCfg["consensus"])["validator"]; ok {
isValidator, _ := newValidator.(bool)
isValidator, _ = newValidator.(bool)
if _, hasRuntimes := m(newCfg["runtime"])["paths"]; !hasRuntimes && isValidator {
nodeMode = "validator"
delete(m(newCfg["consensus"]), "validator")
}
}
// If node is a validator, it now requires external P2P addresses to have set.
if isValidator {
// Set P2P registration addresses based on consensus.external_address.
ea := m(newCfg["consensus"])["external_address"]
if eaStr, ok := ea.(string); !ok {
logger.Warn("consensus.external_address missing, not configuring p2p registration parameters", "address", ea)
} else {
// Parse host from the external address.
url, err := url.Parse(eaStr)
if err != nil {
logger.Error("failed to parse URI from consensus.external_address", "err", err)
os.Exit(1)
}
// Set P2P port if not set.
if _, ok := m(newCfg["p2p"])["port"]; !ok {
m(newCfg["p2p"])["port"] = 9200
}
// Set P2P registration addresses if not set.
if _, ok := m(m(newCfg["p2p"])["registration"])["addresses"]; !ok {
mkSubMap(m(newCfg["p2p"]), "registration")
m(m(newCfg["p2p"])["registration"])["addresses"] = []string{url.Hostname() + ":9200"}
} else {
logger.Warn("p2p.registration.addresses is already set, make sure the address port matches p2p.port")
}
}
}
if isValidator && (m(newCfg["p2p"])["port"] == nil || m(m(newCfg["p2p"])["registration"])["addresses"] == nil) {
logger.Warn("node is a validator but p2p.port or p2p.registration.addresses are not set")
}

// Check for options that are only available on the command-line.
if _, ok = oldCfg["debug"]; ok {
Expand Down
56 changes: 55 additions & 1 deletion go/oasis-node/cmd/config/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ genesis:
file: /storage/node/genesis.json
# Worker configuration.
worker:
worker:
p2p:
port: 9002
peer_outbound_queue_size: 42
Expand Down Expand Up @@ -261,6 +261,39 @@ worker:
entity: /node/entity/entity.json
`

// Validator node with external address set and no P2P address.
const testValidatorConfig2Raw = `
datadir: /node/data
log:
level:
default: info
tendermint: info
tendermint/context: error
format: JSON
genesis:
file: /node/etc/genesis.json
consensus:
validator: true
tendermint:
p2p:
# List of seed nodes to connect to.
# NOTE: You can add additional seed nodes to this list if you want.
seed:
- "[email protected]:26656"
core:
external_address: tcp://4.3.2.1:26656
worker:
registration:
# In order for the node to register itself, the entity.json of the entity
# used to provision the node must be available on the node.
entity: /node/entity/entity.json
`

// Non-validator node from docs test configuration file.
const testDocsNonValidatorConfigRaw = `
datadir: /node/data
Expand Down Expand Up @@ -612,6 +645,27 @@ func TestConfigMigrationValidator(t *testing.T) {
require.Equal(newConfig.Consensus.Validator, false)
}

func TestConfigMigrationValidator2(t *testing.T) {
require := require.New(t)
newConfig := prepareTest(require, testValidatorConfig2Raw)

// Now check if the config struct fields actually match the original state.
require.Equal(newConfig.Mode, config.ModeValidator)
require.Equal(newConfig.Common.DataDir, "/node/data")
require.Equal(newConfig.Common.Log.Format, "JSON")
require.Equal(newConfig.Common.Log.Level["default"], "info")
require.Equal(newConfig.Common.Log.Level["cometbft"], "info")
require.Equal(newConfig.Common.Log.Level["cometbft/context"], "error")
require.Equal(newConfig.Genesis.File, "/node/etc/genesis.json")
require.Equal(newConfig.P2P.Seeds[0], "H6u9MtuoWRKn5DKSgarj/[email protected]:26656")
require.Equal(newConfig.P2P.Seeds[1], "H6u9MtuoWRKn5DKSgarj/[email protected]:9200")
require.Equal(newConfig.Consensus.Validator, false)
require.Equal(newConfig.Consensus.ExternalAddress, "tcp://4.3.2.1:26656")
require.Equal(newConfig.P2P.Port, uint16(9200))
require.Equal(len(newConfig.P2P.Registration.Addresses), 1)
require.Equal(newConfig.P2P.Registration.Addresses[0], "4.3.2.1:9200")
}

func TestConfigMigrationDocsNonValidator(t *testing.T) {
require := require.New(t)
newConfig := prepareTest(require, testDocsNonValidatorConfigRaw)
Expand Down

0 comments on commit 1f4f23f

Please sign in to comment.