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

config/migrate: Automatically configure external P2P addresses for validators #5410

Merged
merged 1 commit into from
Oct 19, 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
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
34 changes: 33 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 @@
"bytes"
"fmt"
"io"
"net/url"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -724,13 +725,44 @@

// 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 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")

// Parse host from consensus.external_address.
ea := m(newCfg["consensus"])["external_address"]
if eaStr, ok := ea.(string); ok {
url, err := url.Parse(eaStr)
if err != nil {
logger.Error("failed to parse URI from consensus.external_address", "err", err)
os.Exit(1)

Check warning on line 752 in go/oasis-node/cmd/config/migrate/migrate.go

View check run for this annotation

Codecov / codecov/patch

go/oasis-node/cmd/config/migrate/migrate.go#L751-L752

Added lines #L751 - L752 were not covered by tests
}
m(m(newCfg["p2p"])["registration"])["addresses"] = []string{url.Hostname() + ":" + fmt.Sprintf("%d", m(newCfg["p2p"])["port"])}
} else {
logger.Warn("consensus.external_address missing, not configuring p2p.registration.addresses parameter", "address", ea)
}
} else {
logger.Warn("p2p.registration.addresses is already set, make sure the address port matches p2p.port")

Check warning on line 759 in go/oasis-node/cmd/config/migrate/migrate.go

View check run for this annotation

Codecov / codecov/patch

go/oasis-node/cmd/config/migrate/migrate.go#L758-L759

Added lines #L758 - L759 were not covered by tests
}

}
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:
- "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
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/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)
Expand Down
Loading