[#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 <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-22 15:31:13 +03:00
parent 1759ff57f1
commit 8e4416ad4b
2 changed files with 38 additions and 8 deletions

View file

@ -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
}