From b041f6fc10138597a0e31269c4832189da204e30 Mon Sep 17 00:00:00 2001 From: ptrus Date: Thu, 19 Oct 2023 17:09:35 +0200 Subject: [PATCH] config/migrate: Automatically configure external P2P addresses --- .changelog/5410.bugfix.md | 1 + go/oasis-node/cmd/config/migrate/migrate.go | 31 +++++++++- .../cmd/config/migrate/migrate_test.go | 56 ++++++++++++++++++- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 .changelog/5410.bugfix.md diff --git a/.changelog/5410.bugfix.md b/.changelog/5410.bugfix.md new file mode 100644 index 00000000000..9f75de96fe5 --- /dev/null +++ b/.changelog/5410.bugfix.md @@ -0,0 +1 @@ +config/migrate: Automatically configure external P2P addresses for validators diff --git a/go/oasis-node/cmd/config/migrate/migrate.go b/go/oasis-node/cmd/config/migrate/migrate.go index 8c7a2aaeef4..59f2196d609 100644 --- a/go/oasis-node/cmd/config/migrate/migrate.go +++ b/go/oasis-node/cmd/config/migrate/migrate.go @@ -6,6 +6,7 @@ import ( "bytes" "fmt" "io" + "net/url" "os" "runtime" "strings" @@ -724,13 +725,41 @@ 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"} + } + } + } + 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 { diff --git a/go/oasis-node/cmd/config/migrate/migrate_test.go b/go/oasis-node/cmd/config/migrate/migrate_test.go index a55bdcc581e..102386e3f09 100644 --- a/go/oasis-node/cmd/config/migrate/migrate_test.go +++ b/go/oasis-node/cmd/config/migrate/migrate_test.go @@ -67,7 +67,7 @@ genesis: file: /storage/node/genesis.json # Worker configuration. -worker: +worker: p2p: port: 9002 peer_outbound_queue_size: 42 @@ -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: + - "E27F6B7A350B4CC2B48A6CBE94B0A02B0DCB0BF3@35.199.49.168: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 @@ -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/dzr2Z9BsjuRHgRAoXITOcU=@35.199.49.168:26656") + require.Equal(newConfig.P2P.Seeds[1], "H6u9MtuoWRKn5DKSgarj/dzr2Z9BsjuRHgRAoXITOcU=@35.199.49.168: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)