From 0eea25375eeb7b8ea96ca17dc0611a8d4bfcf500 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Jun 2021 15:51:28 +0300 Subject: [PATCH] [#493] cmd/node: Add contracts section to config Signed-off-by: Alex Vanin --- cmd/neofs-node/config/contracts/config.go | 67 +++++++++++++++++++ .../config/contracts/config_test.go | 54 +++++++++++++++ config/example/node.env | 6 ++ config/example/node.json | 6 ++ config/example/node.yaml | 6 ++ 5 files changed, 139 insertions(+) create mode 100644 cmd/neofs-node/config/contracts/config.go create mode 100644 cmd/neofs-node/config/contracts/config_test.go diff --git a/cmd/neofs-node/config/contracts/config.go b/cmd/neofs-node/config/contracts/config.go new file mode 100644 index 000000000..7fe65e49a --- /dev/null +++ b/cmd/neofs-node/config/contracts/config.go @@ -0,0 +1,67 @@ +package contractsconfig + +import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" +) + +const ( + subsection = "contracts" +) + +// Netmap returns value of "netmap" config parameter +// from "contracts" section. +// +// Throws panic if value is not is not a 20-byte LE hex-encoded string. +func Netmap(c *config.Config) util.Uint160 { + return contractAddress(c, "netmap") +} + +// Balance returns value of "balance" config parameter +// from "contracts" section. +// +// Throws panic if value is not is not a 20-byte LE hex-encoded string. +func Balance(c *config.Config) util.Uint160 { + return contractAddress(c, "balance") +} + +// Container returns value of "container" config parameter +// from "contracts" section. +// +// Throws panic if value is not is not a 20-byte LE hex-encoded string. +func Container(c *config.Config) util.Uint160 { + return contractAddress(c, "container") +} + +// Reputation returns value of "reputation" config parameter +// from "contracts" section. +// +// Throws panic if value is not is not a 20-byte LE hex-encoded string. +func Reputation(c *config.Config) util.Uint160 { + return contractAddress(c, "reputation") +} + +func contractAddress(c *config.Config, name string) util.Uint160 { + v := config.String(c.Sub(subsection), name) + if v == "" { + panic(fmt.Errorf( + "empty %s contract address, see `contracts.%s` section", + name, + name, + )) + } + + addr, err := util.Uint160DecodeStringLE(v) + if err != nil { + panic(fmt.Errorf( + "can't parse %s contract address %s: %w", + name, + v, + err, + )) + } + + return addr +} diff --git a/cmd/neofs-node/config/contracts/config_test.go b/cmd/neofs-node/config/contracts/config_test.go new file mode 100644 index 000000000..ca21e908d --- /dev/null +++ b/cmd/neofs-node/config/contracts/config_test.go @@ -0,0 +1,54 @@ +package contractsconfig_test + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config" + contractsconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/contracts" + configtest "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/test" + "github.com/stretchr/testify/require" +) + +func TestContractsSection(t *testing.T) { + t.Run("defaults", func(t *testing.T) { + empty := configtest.EmptyConfig() + + require.Panics(t, func() { contractsconfig.Balance(empty) }) + require.Panics(t, func() { contractsconfig.Container(empty) }) + require.Panics(t, func() { contractsconfig.Netmap(empty) }) + require.Panics(t, func() { contractsconfig.Reputation(empty) }) + }) + + const path = "../../../../config/example/node" + + expBalance, err := util.Uint160DecodeStringLE("5263abba1abedbf79bb57f3e40b50b4425d2d6cd") + require.NoError(t, err) + + expConatiner, err := util.Uint160DecodeStringLE("5d084790d7aa36cea7b53fe897380dab11d2cd3c") + require.NoError(t, err) + + expNetmap, err := util.Uint160DecodeStringLE("0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca") + require.NoError(t, err) + + expReputation, err := util.Uint160DecodeStringLE("441995f631c1da2b133462b71859494a5cd45e90") + require.NoError(t, err) + + var fileConfigTest = func(c *config.Config) { + balance := contractsconfig.Balance(c) + container := contractsconfig.Container(c) + netmap := contractsconfig.Netmap(c) + reputation := contractsconfig.Reputation(c) + + require.Equal(t, expBalance, balance) + require.Equal(t, expConatiner, container) + require.Equal(t, expNetmap, netmap) + require.Equal(t, expReputation, reputation) + } + + 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 6827679a3..fe40a28b0 100644 --- a/config/example/node.env +++ b/config/example/node.env @@ -19,6 +19,12 @@ NEOFS_GRPC_TLS_ENABLED=true NEOFS_GRPC_TLS_CERTIFICATE=/path/to/cert NEOFS_GRPC_TLS_KEY=/path/to/key +# Contracts section +NEOFS_CONTRACTS_BALANCE=5263abba1abedbf79bb57f3e40b50b4425d2d6cd +NEOFS_CONTRACTS_CONTAINER=5d084790d7aa36cea7b53fe897380dab11d2cd3c +NEOFS_CONTRACTS_NETMAP=0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca +NEOFS_CONTRACTS_REPUTATION=441995f631c1da2b133462b71859494a5cd45e90 + # Storage engine section NEOFS_STORAGE_SHARD_NUM=2 ## 0 shard diff --git a/config/example/node.json b/config/example/node.json index cc80c7551..5d2a97fac 100644 --- a/config/example/node.json +++ b/config/example/node.json @@ -25,6 +25,12 @@ "key": "/path/to/key" } }, + "contracts": { + "balance": "5263abba1abedbf79bb57f3e40b50b4425d2d6cd", + "container": "5d084790d7aa36cea7b53fe897380dab11d2cd3c", + "netmap": "0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca", + "reputation": "441995f631c1da2b133462b71859494a5cd45e90" + }, "storage": { "shard_num": 2, "shard": { diff --git a/config/example/node.yaml b/config/example/node.yaml index b7b7c83a6..ccf1fd274 100644 --- a/config/example/node.yaml +++ b/config/example/node.yaml @@ -23,6 +23,12 @@ grpc: certificate: /path/to/cert key: /path/to/key +contracts: + balance: 5263abba1abedbf79bb57f3e40b50b4425d2d6cd + container: 5d084790d7aa36cea7b53fe897380dab11d2cd3c + netmap: 0cce9e948dca43a6b592efe59ddb4ecb89bdd9ca + reputation: 441995f631c1da2b133462b71859494a5cd45e90 + storage: shard_num: 2 shard: