2020-09-03 17:55:11 +03:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
2023-04-12 17:35:10 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 16:38:26 +03:00
|
|
|
netmapclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
|
|
|
netmapEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2020-09-03 17:55:11 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Process add peer notification by sanity check of new node
|
|
|
|
// local epoch timer.
|
2023-05-26 13:24:41 +03:00
|
|
|
func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) bool {
|
2021-03-23 18:20:44 +03:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Info(logs.NetmapNonAlphabetModeIgnoreNewPeerNotification)
|
2023-05-26 13:24:41 +03:00
|
|
|
return true
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 19:20:47 +03:00
|
|
|
// check if notary transaction is valid, see #976
|
2023-05-17 16:56:47 +03:00
|
|
|
tx := ev.NotaryRequest().MainTransaction
|
|
|
|
ok, err := np.netmapClient.MorphIsValidScript(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))
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2021-11-16 19:20:47 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 13:26:35 +03:00
|
|
|
// unmarshal node info
|
2022-06-09 02:18:26 +03:00
|
|
|
var nodeInfo netmap.NodeInfo
|
2021-09-23 21:02:24 +03:00
|
|
|
if err := nodeInfo.Unmarshal(ev.Node()); err != nil {
|
2020-09-03 17:55:11 +03:00
|
|
|
// it will be nice to have tx id at event structure to log it
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Warn(logs.NetmapCantParseNetworkMapCandidate)
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:36:43 +03:00
|
|
|
// validate and update node info
|
2023-05-17 16:56:47 +03:00
|
|
|
err = np.nodeValidator.VerifyAndUpdate(&nodeInfo)
|
2021-02-09 17:36:43 +03:00
|
|
|
if err != nil {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Warn(logs.NetmapCouldNotVerifyAndUpdateInformationAboutNetworkMapCandidate,
|
2021-02-09 17:36:43 +03:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2021-02-09 17:36:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 13:44:47 +03:00
|
|
|
// sort attributes to make it consistent
|
2022-06-09 02:18:26 +03:00
|
|
|
nodeInfo.SortAttributes()
|
2021-02-11 13:44:47 +03:00
|
|
|
|
2021-11-08 16:21:48 +03:00
|
|
|
// marshal updated node info structure
|
2022-06-09 02:18:26 +03:00
|
|
|
nodeInfoBinary := nodeInfo.Marshal()
|
2021-11-08 16:21:48 +03:00
|
|
|
|
2022-10-11 15:49:34 +03:00
|
|
|
keyString := netmap.StringifyPublicKey(nodeInfo)
|
2020-09-03 17:55:11 +03:00
|
|
|
|
2021-11-08 16:21:48 +03:00
|
|
|
updated := np.netmapSnapshot.touch(keyString, np.epochState.EpochCounter(), nodeInfoBinary)
|
|
|
|
|
|
|
|
if updated {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Info(logs.NetmapApprovingNetworkMapCandidate,
|
2020-10-29 19:08:36 +03:00
|
|
|
zap.String("key", keyString))
|
|
|
|
|
2021-11-10 14:05:51 +03:00
|
|
|
prm := netmapclient.AddPeerPrm{}
|
|
|
|
prm.SetNodeInfo(nodeInfo)
|
|
|
|
|
2022-03-22 11:48:00 +03:00
|
|
|
// In notary environments we call AddPeerIR method instead of AddPeer.
|
2021-12-07 17:04:32 +03:00
|
|
|
// It differs from AddPeer only by name, so we can do this in the same form.
|
2022-12-23 20:35:35 +03:00
|
|
|
// See https://github.com/nspcc-dev/frostfs-contract/issues/154.
|
2022-03-22 11:48:00 +03:00
|
|
|
const methodAddPeerNotary = "addPeerIR"
|
2021-12-07 17:04:32 +03:00
|
|
|
|
2023-05-17 16:56:47 +03:00
|
|
|
// create new notary request with the original nonce
|
|
|
|
err = np.netmapClient.MorphNotaryInvoke(
|
|
|
|
np.netmapClient.ContractAddress(),
|
|
|
|
0,
|
|
|
|
ev.NotaryRequest().MainTransaction.Nonce,
|
|
|
|
nil,
|
|
|
|
methodAddPeerNotary,
|
|
|
|
nodeInfoBinary,
|
|
|
|
)
|
2021-09-23 21:02:24 +03:00
|
|
|
|
2021-04-29 16:40:34 +03:00
|
|
|
if err != nil {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Error(logs.NetmapCantInvokeNetmapAddPeer, zap.Error(err))
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2020-10-29 19:01:35 +03:00
|
|
|
}
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
2023-05-26 13:24:41 +03:00
|
|
|
|
|
|
|
return true
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:04:17 +03:00
|
|
|
// Process update peer notification by sending approval tx to the smart contract.
|
2023-05-26 13:24:41 +03:00
|
|
|
func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) bool {
|
2021-03-23 18:20:44 +03:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Info(logs.NetmapNonAlphabetModeIgnoreUpdatePeerNotification)
|
2023-05-26 13:24:41 +03:00
|
|
|
return true
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:04:17 +03:00
|
|
|
// flag node to remove from local view, so it can be re-bootstrapped
|
|
|
|
// again before new epoch will tick
|
2021-01-18 17:59:51 +03:00
|
|
|
np.netmapSnapshot.flag(hex.EncodeToString(ev.PublicKey().Bytes()))
|
2020-10-29 19:04:17 +03:00
|
|
|
|
2021-09-09 14:55:01 +03:00
|
|
|
var err error
|
|
|
|
|
2022-09-19 20:34:53 +04:00
|
|
|
if ev.Maintenance() {
|
|
|
|
err = np.nodeStateSettings.MaintenanceModeAllowed()
|
|
|
|
if err != nil {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Info(logs.NetmapPreventSwitchingNodeToMaintenanceState,
|
2022-09-19 20:34:53 +04:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2022-09-19 20:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:56:47 +03:00
|
|
|
if err = np.netmapClient.MorphNotarySignAndInvokeTX(ev.NotaryRequest().MainTransaction); err != nil {
|
2023-04-12 17:35:10 +03:00
|
|
|
np.log.Error(logs.NetmapCantInvokeNetmapUpdatePeer, zap.Error(err))
|
2023-05-26 13:24:41 +03:00
|
|
|
return false
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|
2023-05-26 13:24:41 +03:00
|
|
|
|
|
|
|
return true
|
2020-09-03 17:55:11 +03:00
|
|
|
}
|