From 8e4416ad4b5c80a5e8dfb3fc46b63b966092fe1a Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 22 Sep 2020 15:31:13 +0300 Subject: [PATCH] [#36] Add default capacity and price attributes Default values for capacity and price will be 0, so storage node won't be present in placement. Add these attributes if they are not explicitly set up by user config. Signed-off-by: Alex Vanin --- cmd/neofs-node/config.go | 10 ++-------- cmd/neofs-node/morph.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index bebe0164b..a83c9784f 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -22,8 +22,6 @@ const ( cfgNodeKey = "node.key" cfgBootstrapAddress = "node.address" cfgNodeAttributePrefix = "node.attribute" - cfgNodeCapacity = "node.capacity" - cfgNodePrice = "node.price" // config keys for cfgGRPC cfgListenAddress = "grpc.endpoint" @@ -101,8 +99,8 @@ type BootstrapType uint32 type cfgNodeInfo struct { bootType BootstrapType attributes []string - capacity uint64 - price uint64 + capacity uint64 // default: 0 + price uint64 // default: 0 } const ( @@ -149,8 +147,6 @@ func initCfg(path string) *cfg { cfgNodeInfo: cfgNodeInfo{ bootType: StorageNode, attributes: readAttributes(viperCfg), - capacity: viperCfg.GetUint64(cfgNodeCapacity), - price: viperCfg.GetUint64(cfgNodePrice), }, } } @@ -179,8 +175,6 @@ func initViper(path string) *viper.Viper { func defaultConfiguration(v *viper.Viper) { // fixme: all hardcoded private keys must be removed v.SetDefault(cfgNodeKey, "Kwk6k2eC3L3QuPvD8aiaNyoSXgQ2YL1bwS5CP1oKoA9waeAze97s") - v.SetDefault(cfgNodeCapacity, "10") - v.SetDefault(cfgNodePrice, "10") v.SetDefault(cfgBootstrapAddress, "") // address to bootstrap with v.SetDefault(cfgMorphRPCAddress, "http://morph_chain.localtest.nspcc.ru:30333/") diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 50396e3cb..e6b939c15 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -1,6 +1,9 @@ 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" @@ -37,6 +40,8 @@ func bootstrapNode(c *cfg) { 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)) @@ -46,3 +51,34 @@ func bootstrapNode(c *cfg) { 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 +}