2020-09-03 14:55:11 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00: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 14:55:11 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Process add peer notification by sanity check of new node
|
|
|
|
// local epoch timer.
|
2023-05-26 10:24:41 +00:00
|
|
|
func (np *Processor) processAddPeer(ev netmapEvent.AddPeer) bool {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.NetmapNonAlphabetModeIgnoreNewPeerNotification)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:20:47 +00:00
|
|
|
// check if notary transaction is valid, see #976
|
2023-05-17 13:56:47 +00: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 10:24:41 +00:00
|
|
|
return false
|
2021-11-16 16:20:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
// unmarshal node info
|
2022-06-08 23:18:26 +00:00
|
|
|
var nodeInfo netmap.NodeInfo
|
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
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Warn(logs.NetmapCantParseNetworkMapCandidate)
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
// validate and update node info
|
2023-05-17 13:56:47 +00:00
|
|
|
err = np.nodeValidator.VerifyAndUpdate(&nodeInfo)
|
2021-02-09 14:36:43 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Warn(logs.NetmapCouldNotVerifyAndUpdateInformationAboutNetworkMapCandidate,
|
2021-02-09 14:36:43 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2021-02-09 14:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 10:44:47 +00:00
|
|
|
// sort attributes to make it consistent
|
2022-06-08 23:18:26 +00:00
|
|
|
nodeInfo.SortAttributes()
|
2021-02-11 10:44:47 +00:00
|
|
|
|
2021-11-08 13:21:48 +00:00
|
|
|
// marshal updated node info structure
|
2022-06-08 23:18:26 +00:00
|
|
|
nodeInfoBinary := nodeInfo.Marshal()
|
2021-11-08 13:21:48 +00:00
|
|
|
|
2022-10-11 12:49:34 +00:00
|
|
|
keyString := netmap.StringifyPublicKey(nodeInfo)
|
2020-09-03 14:55:11 +00:00
|
|
|
|
2021-11-08 13:21:48 +00:00
|
|
|
updated := np.netmapSnapshot.touch(keyString, np.epochState.EpochCounter(), nodeInfoBinary)
|
|
|
|
|
2023-12-05 08:16:44 +00:00
|
|
|
// `processAddPeer` reacts on `AddPeer` notification, `processNewEpoch` - on `NewEpoch`.
|
|
|
|
// This two notification produces in order - `NewEpoch` -> `AddPeer`.
|
|
|
|
// But there is no guarantee that code will be executed in the same order.
|
|
|
|
// That is why we need to perform `addPeerIR` only in case when node is online,
|
|
|
|
// because in scope of this method, contract set state `ONLINE` for the node.
|
|
|
|
if updated && nodeInfo.IsOnline() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.NetmapApprovingNetworkMapCandidate,
|
2020-10-29 16:08:36 +00:00
|
|
|
zap.String("key", keyString))
|
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
prm := netmapclient.AddPeerPrm{}
|
|
|
|
prm.SetNodeInfo(nodeInfo)
|
|
|
|
|
2022-03-22 08:48:00 +00:00
|
|
|
// In notary environments we call AddPeerIR method instead of AddPeer.
|
2021-12-07 14:04:32 +00:00
|
|
|
// It differs from AddPeer only by name, so we can do this in the same form.
|
2022-12-23 17:35:35 +00:00
|
|
|
// See https://github.com/nspcc-dev/frostfs-contract/issues/154.
|
2022-03-22 08:48:00 +00:00
|
|
|
const methodAddPeerNotary = "addPeerIR"
|
2021-12-07 14:04:32 +00:00
|
|
|
|
2023-05-17 13:56:47 +00: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 18:02:24 +00:00
|
|
|
|
2021-04-29 13:40:34 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.NetmapCantInvokeNetmapAddPeer, zap.Error(err))
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-10-29 16:01:35 +00:00
|
|
|
}
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
2023-05-26 10:24:41 +00:00
|
|
|
|
|
|
|
return true
|
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.
|
2023-05-26 10:24:41 +00:00
|
|
|
func (np *Processor) processUpdatePeer(ev netmapEvent.UpdatePeer) bool {
|
2021-03-23 15:20:44 +00:00
|
|
|
if !np.alphabetState.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.NetmapNonAlphabetModeIgnoreUpdatePeerNotification)
|
2023-05-26 10:24:41 +00:00
|
|
|
return true
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-09-19 16:34:53 +00:00
|
|
|
if ev.Maintenance() {
|
|
|
|
err = np.nodeStateSettings.MaintenanceModeAllowed()
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Info(logs.NetmapPreventSwitchingNodeToMaintenanceState,
|
2022-09-19 16:34:53 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2022-09-19 16:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 13:56:47 +00:00
|
|
|
if err = np.netmapClient.MorphNotarySignAndInvokeTX(ev.NotaryRequest().MainTransaction); err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
np.log.Error(logs.NetmapCantInvokeNetmapUpdatePeer, zap.Error(err))
|
2023-05-26 10:24:41 +00:00
|
|
|
return false
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|
2023-05-26 10:24:41 +00:00
|
|
|
|
|
|
|
return true
|
2020-09-03 14:55:11 +00:00
|
|
|
}
|