From df5fb23ba878b2a4b7e18987aca491d3303b5fa8 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 22 Sep 2020 15:59:09 +0300 Subject: [PATCH] [#36] Move node attribute code into separate file Signed-off-by: Alex Vanin --- cmd/neofs-node/attributes.go | 73 ++++++++++++++++++++++++++++++++++++ cmd/neofs-node/config.go | 23 ++---------- cmd/neofs-node/morph.go | 44 +--------------------- 3 files changed, 77 insertions(+), 63 deletions(-) create mode 100644 cmd/neofs-node/attributes.go diff --git a/cmd/neofs-node/attributes.go b/cmd/neofs-node/attributes.go new file mode 100644 index 000000000..d7e7266e7 --- /dev/null +++ b/cmd/neofs-node/attributes.go @@ -0,0 +1,73 @@ +package main + +import ( + "strconv" + + sdk "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-api-go/v2/netmap" + "github.com/nspcc-dev/neofs-node/pkg/util/attributes" + "github.com/spf13/viper" +) + +const ( + // list of default values for well-known attributes + defaultCapacity = 0 + defaultPrice = 0 +) + +func parseAttributes(v *viper.Viper) []*netmap.Attribute { + stringAttributes := readAttributes(v) + + attrs, err := attributes.ParseV2Attributes(stringAttributes, nil) + if err != nil { + fatalOnErr(err) + } + + return addWellKnownAttributes(attrs) +} + +func readAttributes(v *viper.Viper) (attrs []string) { + const maxAttributes = 100 + + for i := 0; i < maxAttributes; i++ { + attr := v.GetString(cfgNodeAttributePrefix + "_" + strconv.Itoa(i)) + if attr == "" { + return + } else { + attrs = append(attrs, attr) + } + } + + return attrs +} + +func addWellKnownAttributes(attrs []*netmap.Attribute) []*netmap.Attribute { + var hasCapacity, hasPrice bool + + // check if user defined capacity and price attributes + for i := range attrs { + if !hasPrice && attrs[i].GetKey() == sdk.PriceAttr { + hasPrice = true + } else if !hasCapacity && attrs[i].GetKey() == sdk.CapacityAttr { + hasCapacity = true + } + } + + // do not override user defined capacity and price attributes + + if !hasCapacity { + capacity := new(netmap.Attribute) + capacity.SetKey(sdk.CapacityAttr) + capacity.SetValue(strconv.FormatUint(defaultCapacity, 10)) + attrs = append(attrs, capacity) + } + + if !hasPrice { + price := new(netmap.Attribute) + price.SetKey(sdk.PriceAttr) + price.SetValue(strconv.FormatUint(defaultPrice, 10)) + attrs = append(attrs, price) + } + + return attrs +} diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index a83c9784f..b290e018e 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -4,11 +4,11 @@ import ( "context" "crypto/ecdsa" "net" - "strconv" "strings" "sync" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-api-go/v2/netmap" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/misc" "github.com/nspcc-dev/neofs-node/pkg/morph/client" @@ -98,9 +98,7 @@ type BootstrapType uint32 type cfgNodeInfo struct { bootType BootstrapType - attributes []string - capacity uint64 // default: 0 - price uint64 // default: 0 + attributes []*netmap.Attribute } const ( @@ -146,7 +144,7 @@ func initCfg(path string) *cfg { }, cfgNodeInfo: cfgNodeInfo{ bootType: StorageNode, - attributes: readAttributes(viperCfg), + attributes: parseAttributes(viperCfg), }, } } @@ -189,18 +187,3 @@ func defaultConfiguration(v *viper.Viper) { v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3") v.SetDefault(cfgNetmapFee, "1") } - -func readAttributes(v *viper.Viper) (attrs []string) { - const maxAttributes = 100 - - for i := 0; i < maxAttributes; i++ { - attr := v.GetString(cfgNodeAttributePrefix + "_" + strconv.Itoa(i)) - if attr == "" { - return - } else { - attrs = append(attrs, attr) - } - } - - return attrs -} diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index e6b939c15..c9c0df2b6 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -1,15 +1,11 @@ package main import ( - "strconv" - - sdk "github.com/nspcc-dev/neofs-api-go/pkg/netmap" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" - "github.com/nspcc-dev/neofs-node/pkg/util/attributes" "github.com/pkg/errors" ) @@ -35,50 +31,12 @@ func bootstrapNode(c *cfg) { cliWrapper, err := wrapper.New(cli) fatalOnErr(errors.Wrap(err, "bootstrap error")) - attrs, err := attributes.ParseV2Attributes(c.cfgNodeInfo.attributes, nil) - if err != nil { - fatalOnErr(errors.Wrap(err, "bootstrap attribute error")) - } - - attrs = addWellKnownAttributes(c, attrs) - peerInfo := new(v2netmap.NodeInfo) peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress)) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) - peerInfo.SetAttributes(attrs) + peerInfo.SetAttributes(c.cfgNodeInfo.attributes) err = cliWrapper.AddPeer(peerInfo) fatalOnErr(errors.Wrap(err, "bootstrap error")) } } - -func addWellKnownAttributes(c *cfg, attrs []*v2netmap.Attribute) []*v2netmap.Attribute { - var hasCapacity, hasPrice bool - - // check if user defined capacity and price attributes - for i := range attrs { - if !hasPrice && attrs[i].GetKey() == sdk.PriceAttr { - hasPrice = true - } else if !hasCapacity && attrs[i].GetKey() == sdk.CapacityAttr { - hasCapacity = true - } - } - - // do not override user defined capacity and price attributes - - if !hasCapacity { - capacity := new(v2netmap.Attribute) - capacity.SetKey(sdk.CapacityAttr) - capacity.SetValue(strconv.FormatUint(c.cfgNodeInfo.capacity, 10)) - attrs = append(attrs, capacity) - } - - if !hasPrice { - price := new(v2netmap.Attribute) - price.SetKey(sdk.PriceAttr) - price.SetValue(strconv.FormatUint(c.cfgNodeInfo.price, 10)) - attrs = append(attrs, price) - } - - return attrs -}