2020-08-22 15:20:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-22 12:31:13 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
sdk "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-22 11:02:32 +00:00
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2020-08-31 15:20:02 +00:00
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-08-22 15:20:47 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2020-08-31 15:20:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
2020-09-08 13:14:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper"
|
2020-09-22 11:02:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
|
2020-09-16 08:24:10 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-08-22 15:20:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initMorphComponents(c *cfg) {
|
|
|
|
var err error
|
|
|
|
|
2020-09-16 07:45:08 +00:00
|
|
|
c.cfgMorph.client, err = client.New(c.key, c.viper.GetString(cfgMorphRPCAddress))
|
2020-08-22 15:20:47 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
}
|
2020-08-31 15:20:02 +00:00
|
|
|
|
|
|
|
func bootstrapNode(c *cfg) {
|
|
|
|
if c.cfgNodeInfo.bootType == StorageNode {
|
2020-09-16 07:45:08 +00:00
|
|
|
staticClient, err := client.NewStatic(
|
|
|
|
c.cfgMorph.client,
|
|
|
|
c.cfgNetmap.scriptHash,
|
|
|
|
c.cfgContainer.fee,
|
|
|
|
)
|
2020-08-31 15:20:02 +00:00
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
cli, err := netmap.New(staticClient)
|
2020-09-16 08:24:10 +00:00
|
|
|
fatalOnErr(errors.Wrap(err, "bootstrap error"))
|
2020-08-31 15:20:02 +00:00
|
|
|
|
2020-09-08 13:14:22 +00:00
|
|
|
cliWrapper, err := wrapper.New(cli)
|
2020-09-16 08:24:10 +00:00
|
|
|
fatalOnErr(errors.Wrap(err, "bootstrap error"))
|
2020-09-08 13:14:22 +00:00
|
|
|
|
2020-09-22 11:02:32 +00:00
|
|
|
attrs, err := attributes.ParseV2Attributes(c.cfgNodeInfo.attributes, nil)
|
|
|
|
if err != nil {
|
|
|
|
fatalOnErr(errors.Wrap(err, "bootstrap attribute error"))
|
|
|
|
}
|
|
|
|
|
2020-09-22 12:31:13 +00:00
|
|
|
attrs = addWellKnownAttributes(c, attrs)
|
|
|
|
|
2020-09-22 11:02:32 +00:00
|
|
|
peerInfo := new(v2netmap.NodeInfo)
|
2020-09-16 07:45:08 +00:00
|
|
|
peerInfo.SetAddress(c.viper.GetString(cfgBootstrapAddress))
|
2020-08-31 15:20:02 +00:00
|
|
|
peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey))
|
2020-09-22 11:02:32 +00:00
|
|
|
peerInfo.SetAttributes(attrs)
|
2020-08-31 15:20:02 +00:00
|
|
|
|
2020-09-08 13:14:22 +00:00
|
|
|
err = cliWrapper.AddPeer(peerInfo)
|
2020-09-16 08:24:10 +00:00
|
|
|
fatalOnErr(errors.Wrap(err, "bootstrap error"))
|
2020-08-31 15:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 12:31:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|