From 4ef968aa069cdb241d5309f9753b717723321988 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Jun 2021 17:47:19 +0300 Subject: [PATCH] [#496] cmd/node: Add mainchain section to config Signed-off-by: Alex Vanin --- cmd/neofs-node/config/mainchain/config.go | 35 +++++++++++++++++ .../config/mainchain/config_test.go | 38 +++++++++++++++++++ config/example/node.env | 4 ++ config/example/node.json | 7 ++++ config/example/node.yaml | 6 +++ 5 files changed, 90 insertions(+) create mode 100644 cmd/neofs-node/config/mainchain/config.go create mode 100644 cmd/neofs-node/config/mainchain/config_test.go diff --git a/cmd/neofs-node/config/mainchain/config.go b/cmd/neofs-node/config/mainchain/config.go new file mode 100644 index 00000000..3fa38d82 --- /dev/null +++ b/cmd/neofs-node/config/mainchain/config.go @@ -0,0 +1,35 @@ +package mainchainconfig + +import ( + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +) + +const ( + subsection = "mainchain" + + // DialTimeoutDefault is a default dial timeout of main chain client connection. + DialTimeoutDefault = 5 * time.Second +) + +// RPCEndpoint returns list of values of "rpc_endpoint" config parameter +// from "mainchain" section. +// +// Returns empty list if value is not a non-empty string array. +func RPCEndpoint(c *config.Config) []string { + return config.StringSliceSafe(c.Sub(subsection), "rpc_endpoint") +} + +// DialTimeout returns value of "dial_timeout" config parameter +// from "mainchain" 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/mainchain/config_test.go b/cmd/neofs-node/config/mainchain/config_test.go new file mode 100644 index 00000000..3335ac13 --- /dev/null +++ b/cmd/neofs-node/config/mainchain/config_test.go @@ -0,0 +1,38 @@ +package mainchainconfig_test + +import ( + "testing" + "time" + + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + mainchainconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/mainchain" + configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" + "github.com/stretchr/testify/require" +) + +func TestMainchainSection(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + empty := configtest.EmptyConfig() + + require.Empty(t, mainchainconfig.RPCEndpoint(empty)) + require.Equal(t, mainchainconfig.DialTimeoutDefault, mainchainconfig.DialTimeout(empty)) + }) + + const path = "../../../../config/example/node" + + var rpcs = []string{ + "https://rpc1.n3.nspcc.ru:30341", + "https://rpc2.n3.nspcc.ru:30341", + } + + var fileConfigTest = func(c *config.Config) { + require.Equal(t, rpcs, mainchainconfig.RPCEndpoint(c)) + require.Equal(t, 30*time.Second, mainchainconfig.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 bf5b7399..21e1fbbf 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -34,6 +34,10 @@ 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 +# Main chain section (optional) +NEOFS_MAINCHAIN_DIAL_TIMEOUT=30s +NEOFS_MAINCHAIN_RPC_ENDPOINT=https://rpc1.n3.nspcc.ru:30341 https://rpc2.n3.nspcc.ru:30341 + # Storage engine section NEOFS_STORAGE_SHARD_NUM=2 ## 0 shard diff --git a/config/example/node.json b/config/example/node.json index 0b7cfb8b..1ac54255 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -51,6 +51,13 @@ "wss://rpc2.morph.fs.neo.org:40341/ws" ] }, + "mainchain": { + "dial_timeout": "30s", + "rpc_endpoint": [ + "https://rpc1.n3.nspcc.ru:30341", + "https://rpc2.n3.nspcc.ru:30341" + ] + }, "storage": { "shard_num": 2, "shard": { diff --git a/config/example/node.yaml b/config/example/node.yaml index 5dbc6b76..b96aaca0 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -45,6 +45,12 @@ morph: - wss://rpc1.morph.fs.neo.org:40341/ws - wss://rpc2.morph.fs.neo.org:40341/ws +mainchain: + dial_timeout: 30s + rpc_endpoint: + - https://rpc1.n3.nspcc.ru:30341 + - https://rpc2.n3.nspcc.ru:30341 + storage: shard_num: 2 shard: