[#36] Move node attribute code into separate file

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-22 15:59:09 +03:00
parent 8e4416ad4b
commit df5fb23ba8
3 changed files with 77 additions and 63 deletions

View file

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

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"net" "net"
"strconv"
"strings" "strings"
"sync" "sync"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/misc" "github.com/nspcc-dev/neofs-node/misc"
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
@ -98,9 +98,7 @@ type BootstrapType uint32
type cfgNodeInfo struct { type cfgNodeInfo struct {
bootType BootstrapType bootType BootstrapType
attributes []string attributes []*netmap.Attribute
capacity uint64 // default: 0
price uint64 // default: 0
} }
const ( const (
@ -146,7 +144,7 @@ func initCfg(path string) *cfg {
}, },
cfgNodeInfo: cfgNodeInfo{ cfgNodeInfo: cfgNodeInfo{
bootType: StorageNode, bootType: StorageNode,
attributes: readAttributes(viperCfg), attributes: parseAttributes(viperCfg),
}, },
} }
} }
@ -189,18 +187,3 @@ func defaultConfiguration(v *viper.Viper) {
v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3") v.SetDefault(cfgNetmapContract, "75194459637323ea8837d2afe8225ec74a5658c3")
v.SetDefault(cfgNetmapFee, "1") 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
}

View file

@ -1,15 +1,11 @@
package main package main
import ( import (
"strconv"
sdk "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
crypto "github.com/nspcc-dev/neofs-crypto" 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"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "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/morph/client/netmap/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -35,50 +31,12 @@ func bootstrapNode(c *cfg) {
cliWrapper, err := wrapper.New(cli) cliWrapper, err := wrapper.New(cli)
fatalOnErr(errors.Wrap(err, "bootstrap error")) 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 := new(v2netmap.NodeInfo)
peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress)) peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress))
peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey))
peerInfo.SetAttributes(attrs) peerInfo.SetAttributes(c.cfgNodeInfo.attributes)
err = cliWrapper.AddPeer(peerInfo) err = cliWrapper.AddPeer(peerInfo)
fatalOnErr(errors.Wrap(err, "bootstrap error")) 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
}