frostfs-node/pkg/innerring/processors/netmap/process_peers.go
Alejandro Lopez 41eb3129ae
All checks were successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/push/pre-commit Pipeline was successful
[#139] Refactor blobovnicza exist test to not use chmod
Signed-off-by: Alejandro Lopez <a.lopez@yadro.com>
2023-04-14 12:16:14 +03:00

212 lines
5.6 KiB
Go

package netmap
import (
"bytes"
"encoding/hex"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
netmapclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
netmapEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/netmap"
subnetEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/subnet"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
subnetid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/subnet/id"
"go.uber.org/zap"
)
// Process add peer notification by sanity check of new node
// local epoch timer.
func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) {
if !np.alphabetState.IsAlphabet() {
np.log.Info(logs.NetmapNonAlphabetModeIgnoreNewPeerNotification)
return
}
// check if notary transaction is valid, see #976
if originalRequest := ev.NotaryRequest(); originalRequest != nil {
tx := originalRequest.MainTransaction
ok, err := np.netmapClient.Morph().IsValidScript(tx.Script, tx.Signers)
if err != nil || !ok {
np.log.Warn(logs.NetmapNonhaltNotaryTransaction,
zap.String("method", "netmap.AddPeer"),
zap.String("hash", tx.Hash().StringLE()),
zap.Error(err))
return
}
}
// unmarshal node info
var nodeInfo netmap.NodeInfo
if err := nodeInfo.Unmarshal(ev.Node()); err != nil {
// it will be nice to have tx id at event structure to log it
np.log.Warn(logs.NetmapCantParseNetworkMapCandidate)
return
}
// validate and update node info
err := np.nodeValidator.VerifyAndUpdate(&nodeInfo)
if err != nil {
np.log.Warn(logs.NetmapCouldNotVerifyAndUpdateInformationAboutNetworkMapCandidate,
zap.String("error", err.Error()),
)
return
}
// sort attributes to make it consistent
nodeInfo.SortAttributes()
// marshal updated node info structure
nodeInfoBinary := nodeInfo.Marshal()
keyString := netmap.StringifyPublicKey(nodeInfo)
updated := np.netmapSnapshot.touch(keyString, np.epochState.EpochCounter(), nodeInfoBinary)
if updated {
np.log.Info(logs.NetmapApprovingNetworkMapCandidate,
zap.String("key", keyString))
prm := netmapclient.AddPeerPrm{}
prm.SetNodeInfo(nodeInfo)
// In notary environments we call AddPeerIR method instead of AddPeer.
// It differs from AddPeer only by name, so we can do this in the same form.
// See https://github.com/nspcc-dev/frostfs-contract/issues/154.
const methodAddPeerNotary = "addPeerIR"
if nr := ev.NotaryRequest(); nr != nil {
// create new notary request with the original nonce
err = np.netmapClient.Morph().NotaryInvoke(
np.netmapClient.ContractAddress(),
0,
nr.MainTransaction.Nonce,
nil,
methodAddPeerNotary,
nodeInfoBinary,
)
} else {
// notification event case
err = np.netmapClient.AddPeer(prm)
}
if err != nil {
np.log.Error(logs.NetmapCantInvokeNetmapAddPeer, 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(logs.NetmapNonAlphabetModeIgnoreUpdatePeerNotification)
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()))
var err error
if ev.Maintenance() {
err = np.nodeStateSettings.MaintenanceModeAllowed()
if err != nil {
np.log.Info(logs.NetmapPreventSwitchingNodeToMaintenanceState,
zap.Error(err),
)
return
}
}
if nr := ev.NotaryRequest(); nr != nil {
err = np.netmapClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
} else {
prm := netmapclient.UpdatePeerPrm{}
switch {
case ev.Online():
prm.SetOnline()
case ev.Maintenance():
prm.SetMaintenance()
}
prm.SetKey(ev.PublicKey().Bytes())
err = np.netmapClient.UpdatePeerState(prm)
}
if err != nil {
np.log.Error(logs.NetmapCantInvokeNetmapUpdatePeer, zap.Error(err))
}
}
func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) {
if !np.alphabetState.IsAlphabet() {
np.log.Info(logs.NetmapNonAlphabetModeIgnoreRemoveNodeFromSubnetNotification)
return
}
candidates, err := np.netmapClient.GetCandidates()
if err != nil {
np.log.Warn(logs.NetmapCouldNotGetNetworkMapCandidates,
zap.Error(err),
)
return
}
rawSubnet := ev.SubnetworkID()
var subnetToRemoveFrom subnetid.ID
err = subnetToRemoveFrom.Unmarshal(rawSubnet)
if err != nil {
np.log.Warn(logs.NetmapCouldNotUnmarshalSubnetID,
zap.Error(err),
)
return
}
if subnetid.IsZero(subnetToRemoveFrom) {
np.log.Warn(logs.NetmapGotZeroSubnetInRemoveNodeNotification)
return
}
for i := range candidates {
if !bytes.Equal(candidates[i].PublicKey(), ev.Node()) {
continue
}
err = candidates[i].IterateSubnets(func(subNetID subnetid.ID) error {
if subNetID.Equals(subnetToRemoveFrom) {
return netmap.ErrRemoveSubnet
}
return nil
})
if err != nil {
np.log.Warn(logs.NetmapCouldNotIterateOverSubnetworksOfTheNode, zap.Error(err))
np.log.Info(logs.NetmapVoteToRemoveNodeFromNetmap, zap.String("key", hex.EncodeToString(ev.Node())))
prm := netmapclient.UpdatePeerPrm{}
prm.SetKey(ev.Node())
prm.SetHash(ev.TxHash())
err = np.netmapClient.UpdatePeerState(prm)
if err != nil {
np.log.Error(logs.NetmapCouldNotInvokeNetmapUpdateState, zap.Error(err))
return
}
} else {
prm := netmapclient.AddPeerPrm{}
prm.SetNodeInfo(candidates[i])
prm.SetHash(ev.TxHash())
err = np.netmapClient.AddPeer(prm)
if err != nil {
np.log.Error(logs.NetmapCouldNotInvokeNetmapAddPeer, zap.Error(err))
return
}
}
break
}
}