frostfs-node/pkg/innerring/processors/netmap/process_peers.go
Alex Vanin f2562e8c47 [#486] innerring: Use fee provider and notary disabled flag in processors
Processors that use `invoke` package to make chain invocation should provide
fee config and client with enabled or disabled notary support. If notary
support is disabled, then functions from `invoke` package will perform
ordinary method invocation with extra fee.

Processors that use `morph/client` wrappers should check `notaryDisabled`
flag to call corresponding wrapper function.

Netmap processor omits some actions during validator syncronization
if notary is disabled.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
2021-05-05 12:45:16 +03:00

104 lines
2.8 KiB
Go

package netmap
import (
"encoding/hex"
"sort"
"strings"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/nspcc-dev/neofs-node/pkg/innerring/invoke"
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.
func (np *Processor) processAddPeer(node []byte) {
if !np.alphabetState.IsAlphabet() {
np.log.Info("non alphabet mode, ignore new peer notification")
return
}
// unmarshal node info
nodeInfo := netmap.NewNodeInfo()
if err := nodeInfo.Unmarshal(node); err != nil {
// it will be nice to have tx id at event structure to log it
np.log.Warn("can't parse network map candidate")
return
}
// 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
}
// 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...)
// marshal node info back to binary
node, err = nodeInfo.Marshal()
if err != nil {
np.log.Warn("can't marshal approved network map candidate",
zap.String("error", err.Error()))
return
}
keyString := hex.EncodeToString(nodeInfo.PublicKey())
exists := np.netmapSnapshot.touch(keyString, np.epochState.EpochCounter())
if !exists {
np.log.Info("approving network map candidate",
zap.String("key", keyString))
err := invoke.ApprovePeer(np.morphClient, np.netmapContract, np.feeProvider, node)
if err != nil {
np.log.Error("can't invoke netmap.AddPeer", zap.Error(err))
}
}
}
// Process update peer notification by sending approval tx to the smart contract.
func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) {
if !np.alphabetState.IsAlphabet() {
np.log.Info("non alphabet mode, ignore update peer notification")
return
}
// better use unified enum from neofs-api-go/v2/netmap package
if ev.Status() != netmap.NodeStateOffline {
np.log.Warn("node proposes unknown state",
zap.String("key", hex.EncodeToString(ev.PublicKey().Bytes())),
zap.Stringer("status", ev.Status()),
)
return
}
// flag node to remove from local view, so it can be re-bootstrapped
// again before new epoch will tick
np.netmapSnapshot.flag(hex.EncodeToString(ev.PublicKey().Bytes()))
err := invoke.UpdatePeerState(np.morphClient, np.netmapContract, np.feeProvider,
&invoke.UpdatePeerArgs{
Key: ev.PublicKey(),
Status: ev.Status(),
})
if err != nil {
np.log.Error("can't invoke netmap.UpdatePeer", zap.Error(err))
}
}