2020-09-03 14:55:11 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2021-02-11 10:44:47 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-09-03 14:55:11 +00:00
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-03 14:55:11 +00:00
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Process add peer notification by sanity check of new node
|
|
|
|
// local epoch timer.
|
2021-09-23 18:02:24 +00:00
|
|
|
func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore new peer notification")
|
2020-09-03 14:55:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
// unmarshal node info
|
|
|
|
nodeInfo := netmap.NewNodeInfo()
|
2021-09-23 18:02:24 +00:00
|
|
|
if err := nodeInfo.Unmarshal(ev.Node()); err != nil {
|
2020-09-03 14:55:11 +00:00
|
|
|
// it will be nice to have tx id at event structure to log it
|
|
|
|
np.log.Warn("can't parse network map candidate")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
// validate and update node info
|
|
|
|
err := np.nodeValidator.VerifyAndUpdate(nodeInfo)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("could not verify and update information about network map candidate",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:44:47 +00:00
|
|
|
// sort attributes to make it consistent
|
|
|
|
a := nodeInfo.Attributes()
|
|
|
|
sort.Slice(a, func(i, j int) bool {
|
|
|
|
switch strings.Compare(a[i].Key(), a[j].Key()) {
|
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return a[i].Value() < a[j].Value()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
nodeInfo.SetAttributes(a...)
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
keyString := hex.EncodeToString(nodeInfo.PublicKey())
|
2020-09-03 14:55:11 +00:00
|
|
|
|
2020-10-29 16:01:35 +00:00
|
|
|
exists := np.netmapSnapshot.touch(keyString, np.epochState.EpochCounter())
|
|
|
|
if !exists {
|
2020-10-29 16:08:36 +00:00
|
|
|
np.log.Info("approving network map candidate",
|
|
|
|
zap.String("key", keyString))
|
|
|
|
|
2021-09-23 18:02:24 +00:00
|
|
|
if nr := ev.NotaryRequest(); nr != nil {
|
|
|
|
// notary event case
|
|
|
|
|
|
|
|
var nodeInfoBinary []byte
|
|
|
|
|
|
|
|
nodeInfoBinary, err = nodeInfo.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("could not marshal updated network map candidate",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new notary request with the original nonce
|
|
|
|
err = np.netmapClient.Morph().NotaryInvoke(
|
2021-09-30 14:36:56 +00:00
|
|
|
np.netmapClient.ContractAddress(),
|
2021-09-23 18:02:24 +00:00
|
|
|
0,
|
|
|
|
nr.MainTransaction.Nonce,
|
|
|
|
netmapEvent.AddPeerNotaryEvent,
|
|
|
|
nodeInfoBinary,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// notification event case
|
|
|
|
err = np.netmapClient.AddPeer(nodeInfo)
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:40:34 +00:00
|
|
|
if err != nil {
|
2020-10-29 16:01:35 +00:00
|
|
|
np.log.Error("can't invoke netmap.AddPeer", zap.Error(err))
|
|
|
|
}
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:04:17 +00:00
|
|
|
// Process update peer notification by sending approval tx to the smart contract.
|
2020-09-03 14:55:11 +00:00
|
|
|
func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore update peer notification")
|
2020-09-03 14:55:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// better use unified enum from neofs-api-go/v2/netmap package
|
2020-11-16 10:26:35 +00:00
|
|
|
if ev.Status() != netmap.NodeStateOffline {
|
2020-09-03 14:55:11 +00:00
|
|
|
np.log.Warn("node proposes unknown state",
|
|
|
|
zap.String("key", hex.EncodeToString(ev.PublicKey().Bytes())),
|
2020-11-16 10:26:35 +00:00
|
|
|
zap.Stringer("status", ev.Status()),
|
2020-09-03 14:55:11 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-29 16:04:17 +00:00
|
|
|
// flag node to remove from local view, so it can be re-bootstrapped
|
|
|
|
// again before new epoch will tick
|
2021-01-18 14:59:51 +00:00
|
|
|
np.netmapSnapshot.flag(hex.EncodeToString(ev.PublicKey().Bytes()))
|
2020-10-29 16:04:17 +00:00
|
|
|
|
2021-09-09 11:55:01 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if nr := ev.NotaryRequest(); nr != nil {
|
|
|
|
err = np.netmapClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
|
|
|
err = np.netmapClient.UpdatePeerState(ev.PublicKey().Bytes(), ev.Status())
|
|
|
|
}
|
2020-09-03 14:55:11 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't invoke netmap.UpdatePeer", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|