From cf5e3715909ad2a75363088dbac972147a16e1b0 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Jun 2021 17:33:45 +0300 Subject: [PATCH] [#496] cmd/node: Add morph section to config Signed-off-by: Alex Vanin --- cmd/neofs-node/config/morph/config.go | 54 ++++++++++++++++++++++ cmd/neofs-node/config/morph/config_test.go | 47 +++++++++++++++++++ config/example/node.env | 5 ++ config/example/node.json | 11 +++++ config/example/node.yaml | 9 ++++ 5 files changed, 126 insertions(+) create mode 100644 cmd/neofs-node/config/morph/config.go create mode 100644 cmd/neofs-node/config/morph/config_test.go diff --git a/cmd/neofs-node/config/morph/config.go b/cmd/neofs-node/config/morph/config.go new file mode 100644 index 00000000..43784e92 --- /dev/null +++ b/cmd/neofs-node/config/morph/config.go @@ -0,0 +1,54 @@ +package morphconfig + +import ( + "fmt" + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +) + +const ( + subsection = "morph" + + // DialTimeoutDefault is a default dial timeout of morph chain client connection. + DialTimeoutDefault = 5 * time.Second +) + +// RPCEndpoint returns list of values of "rpc_endpoint" config parameter +// from "morph" section. +// +// Throws panic if list is empty. +func RPCEndpoint(c *config.Config) []string { + v := config.StringSliceSafe(c.Sub(subsection), "rpc_endpoint") + if len(v) == 0 { + panic(fmt.Errorf("no morph chain RPC endpoints, see `morph.rpc_endpoint` section")) + } + + return v +} + +// NotificationEndpoint returns list of values of "notification_endpoint" config +// parameter from "morph" section. +// +// Throws panic if list is empty. +func NotificationEndpoint(c *config.Config) []string { + v := config.StringSliceSafe(c.Sub(subsection), "notification_endpoint") + if len(v) == 0 { + panic(fmt.Errorf("no morph chain notification endpoints, see `morph.notification_endpoint` section")) + } + + return v +} + +// DialTimeout returns value of "dial_timeout" config parameter +// from "morph" section. +// +// Returns DialTimeoutDefault if value is not positive duration. +func DialTimeout(c *config.Config) time.Duration { + v := config.DurationSafe(c.Sub(subsection), "dial_timeout") + if v != 0 { + return v + } + + return DialTimeoutDefault +} diff --git a/cmd/neofs-node/config/morph/config_test.go b/cmd/neofs-node/config/morph/config_test.go new file mode 100644 index 00000000..babe4e07 --- /dev/null +++ b/cmd/neofs-node/config/morph/config_test.go @@ -0,0 +1,47 @@ +package morphconfig_test + +import ( + "testing" + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + morphconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/morph" + configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" + "github.com/stretchr/testify/require" +) + +func TestMorphSection(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + empty := configtest.EmptyConfig() + + require.Panics(t, func() { morphconfig.RPCEndpoint(empty) }) + require.Panics(t, func() { morphconfig.NotificationEndpoint(empty) }) + require.Equal(t, morphconfig.DialTimeoutDefault, morphconfig.DialTimeout(empty)) + }) + + const path = "../../../../config/example/node" + + var ( + rpcs = []string{ + "https://rpc1.morph.fs.neo.org:40341", + "https://rpc2.morph.fs.neo.org:40341", + } + + wss = []string{ + "wss://rpc1.morph.fs.neo.org:40341/ws", + "wss://rpc2.morph.fs.neo.org:40341/ws", + } + ) + + var fileConfigTest = func(c *config.Config) { + require.Equal(t, rpcs, morphconfig.RPCEndpoint(c)) + require.Equal(t, wss, morphconfig.NotificationEndpoint(c)) + require.Equal(t, 30*time.Second, morphconfig.DialTimeout(c)) + } + + configtest.ForEachFileType(path, fileConfigTest) + + t.Run("ENV", func(t *testing.T) { + configtest.ForEnvFileType(path, fileConfigTest) + }) +} diff --git a/config/example/node.env b/config/example/node.env index 61688a76..bf5b7399 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -29,6 +29,11 @@ NEOFS_CONTRACTS_CONTAINER=5d084790d7aa36cea7b53fe897380dab11d2cd3c NEOFS_CONTRACTS_NETMAP=0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca NEOFS_CONTRACTS_REPUTATION=441995f631c1da2b133462b71859494a5cd45e90 +# Morph chain section +NEOFS_MORPH_DIAL_TIMEOUT=30s +NEOFS_MORPH_RPC_ENDPOINT=https://rpc1.morph.fs.neo.org:40341 https://rpc2.morph.fs.neo.org:40341 +NEOFS_MORPH_NOTIFICATION_ENDPOINT=wss://rpc1.morph.fs.neo.org:40341/ws wss://rpc2.morph.fs.neo.org:40341/ws + # Storage engine section NEOFS_STORAGE_SHARD_NUM=2 ## 0 shard diff --git a/config/example/node.json b/config/example/node.json index f6515950..0b7cfb8b 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -40,6 +40,17 @@ "netmap": "0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca", "reputation": "441995f631c1da2b133462b71859494a5cd45e90" }, + "morph": { + "dial_timeout": "30s", + "rpc_endpoint": [ + "https://rpc1.morph.fs.neo.org:40341", + "https://rpc2.morph.fs.neo.org:40341" + ], + "notification_endpoint": [ + "wss://rpc1.morph.fs.neo.org:40341/ws", + "wss://rpc2.morph.fs.neo.org:40341/ws" + ] + }, "storage": { "shard_num": 2, "shard": { diff --git a/config/example/node.yaml b/config/example/node.yaml index 3fd0a7ef..5dbc6b76 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -36,6 +36,15 @@ contracts: netmap: 0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca reputation: 441995f631c1da2b133462b71859494a5cd45e90 +morph: + dial_timeout: 30s + rpc_endpoint: + - https://rpc1.morph.fs.neo.org:40341 + - https://rpc2.morph.fs.neo.org:40341 + notification_endpoint: + - wss://rpc1.morph.fs.neo.org:40341/ws + - wss://rpc2.morph.fs.neo.org:40341/ws + storage: shard_num: 2 shard: