2020-10-08 13:17:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-16 10:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-10-08 13:17:50 +00:00
|
|
|
netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
|
|
|
|
crypto "github.com/nspcc-dev/neofs-crypto"
|
2020-10-21 09:26:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2020-10-21 15:12:31 +00:00
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
2020-10-08 13:17:50 +00:00
|
|
|
netmapTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/netmap/grpc"
|
2021-01-13 13:46:39 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
2020-10-08 13:17:50 +00:00
|
|
|
netmapService "github.com/nspcc-dev/neofs-node/pkg/services/netmap"
|
|
|
|
"github.com/pkg/errors"
|
2020-10-21 15:12:31 +00:00
|
|
|
"go.uber.org/atomic"
|
2020-10-21 16:26:38 +00:00
|
|
|
"go.uber.org/zap"
|
2020-10-08 13:17:50 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 15:12:31 +00:00
|
|
|
// primary solution of local network state dump.
|
|
|
|
type networkState struct {
|
|
|
|
epoch *atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNetworkState() *networkState {
|
|
|
|
return &networkState{
|
|
|
|
epoch: atomic.NewUint64(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *networkState) CurrentEpoch() uint64 {
|
|
|
|
return s.epoch.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *networkState) setCurrentEpoch(v uint64) {
|
|
|
|
s.epoch.Store(v)
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:17:50 +00:00
|
|
|
func initNetmapService(c *cfg) {
|
|
|
|
peerInfo := new(netmap.NodeInfo)
|
|
|
|
peerInfo.SetAddress(c.localAddr.String())
|
|
|
|
peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey))
|
2020-11-16 10:26:35 +00:00
|
|
|
peerInfo.SetAttributes(c.cfgNodeInfo.attributes...)
|
2020-10-08 13:17:50 +00:00
|
|
|
|
|
|
|
c.cfgNodeInfo.info = peerInfo
|
|
|
|
|
|
|
|
netmapGRPC.RegisterNetmapServiceServer(c.cfgGRPC.server,
|
|
|
|
netmapTransportGRPC.New(
|
|
|
|
netmapService.NewSignService(
|
|
|
|
c.key,
|
2020-10-22 11:07:06 +00:00
|
|
|
netmapService.NewResponseService(
|
|
|
|
netmapService.NewExecutionService(
|
2020-11-16 10:26:35 +00:00
|
|
|
c.cfgNodeInfo.info.ToV2(),
|
2020-10-22 11:07:06 +00:00
|
|
|
c.apiVersion,
|
|
|
|
),
|
|
|
|
c.respSvc,
|
2020-10-08 13:17:50 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
2020-10-21 15:12:31 +00:00
|
|
|
|
|
|
|
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
|
|
|
c.cfgNetmap.state.setCurrentEpoch(ev.(netmapEvent.NewEpoch).EpochNumber())
|
|
|
|
})
|
2020-10-30 12:57:49 +00:00
|
|
|
|
|
|
|
if c.cfgNetmap.reBootstrapEnabled {
|
|
|
|
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
|
|
|
n := ev.(netmapEvent.NewEpoch).EpochNumber()
|
|
|
|
|
|
|
|
if n%c.cfgNetmap.reBootstrapInterval == 0 {
|
|
|
|
err := c.cfgNetmap.wrapper.AddPeer(c.cfgNodeInfo.info)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't send re-bootstrap tx", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-10-08 13:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bootstrapNode(c *cfg) {
|
2020-10-21 16:26:38 +00:00
|
|
|
initState(c)
|
|
|
|
|
2020-10-08 13:17:50 +00:00
|
|
|
err := c.cfgNetmap.wrapper.AddPeer(c.cfgNodeInfo.info)
|
|
|
|
fatalOnErr(errors.Wrap(err, "bootstrap error"))
|
|
|
|
}
|
2020-10-21 09:26:16 +00:00
|
|
|
|
|
|
|
func addNetmapNotificationHandler(c *cfg, sTyp string, h event.Handler) {
|
|
|
|
typ := event.TypeFromString(sTyp)
|
|
|
|
|
|
|
|
if c.cfgNetmap.subscribers == nil {
|
|
|
|
c.cfgNetmap.subscribers = make(map[event.Type][]event.Handler, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgNetmap.subscribers[typ] = append(c.cfgNetmap.subscribers[typ], h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setNetmapNotificationParser(c *cfg, sTyp string, p event.Parser) {
|
|
|
|
typ := event.TypeFromString(sTyp)
|
|
|
|
|
|
|
|
if c.cfgNetmap.parsers == nil {
|
|
|
|
c.cfgNetmap.parsers = make(map[event.Type]event.Parser, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cfgNetmap.parsers[typ] = p
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:26:38 +00:00
|
|
|
func initState(c *cfg) {
|
|
|
|
epoch, err := c.cfgNetmap.wrapper.Epoch()
|
|
|
|
fatalOnErr(errors.Wrap(err, "could not initialize current epoch number"))
|
|
|
|
|
|
|
|
c.log.Info("initial epoch number",
|
|
|
|
zap.Uint64("value", epoch),
|
|
|
|
)
|
|
|
|
|
|
|
|
c.cfgNetmap.state.setCurrentEpoch(epoch)
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:26:16 +00:00
|
|
|
func addNewEpochNotificationHandler(c *cfg, h event.Handler) {
|
|
|
|
addNetmapNotificationHandler(c, newEpochNotification, h)
|
|
|
|
}
|
2021-01-11 11:57:01 +00:00
|
|
|
|
|
|
|
func goOffline(c *cfg) {
|
2021-01-13 13:46:39 +00:00
|
|
|
c.setHealthStatus(control.HealthStatus_OFFLINE)
|
2021-01-13 13:36:41 +00:00
|
|
|
|
2021-01-11 11:57:01 +00:00
|
|
|
err := c.cfgNetmap.wrapper.UpdatePeerState(
|
|
|
|
crypto.MarshalPublicKey(&c.key.PublicKey),
|
2021-01-11 14:36:55 +00:00
|
|
|
netmap.NodeStateOffline,
|
2021-01-11 11:57:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("could not go offline",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
c.log.Info("request to go offline successfully sent")
|
|
|
|
}
|
|
|
|
}
|