2020-09-03 14:55:11 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2021-11-26 14:16:03 +00:00
|
|
|
"bytes"
|
2020-09-03 14:55:11 +00:00
|
|
|
"encoding/hex"
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
netmapclient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
2020-09-03 14:55:11 +00:00
|
|
|
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
|
2021-11-26 14:16:03 +00:00
|
|
|
subnetEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/subnet"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2021-11-26 14:16:03 +00:00
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
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.
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:20:47 +00:00
|
|
|
// 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("non-halt notary transaction",
|
|
|
|
zap.String("method", "netmap.AddPeer"),
|
|
|
|
zap.String("hash", tx.Hash().StringLE()),
|
|
|
|
zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
np.log.Warn("can't parse network map candidate")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:36:43 +00:00
|
|
|
// validate and update node info
|
2022-06-08 23:18:26 +00:00
|
|
|
err := np.nodeValidator.VerifyAndUpdate(&nodeInfo)
|
2021-02-09 14:36:43 +00:00
|
|
|
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
|
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)
|
|
|
|
|
|
|
|
if updated {
|
2020-10-29 16:08:36 +00:00
|
|
|
np.log.Info("approving network map candidate",
|
|
|
|
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.
|
|
|
|
// See https://github.com/nspcc-dev/neofs-contract/issues/154.
|
2022-03-22 08:48:00 +00:00
|
|
|
const methodAddPeerNotary = "addPeerIR"
|
2021-12-07 14:04:32 +00:00
|
|
|
|
2021-09-23 18:02:24 +00:00
|
|
|
if nr := ev.NotaryRequest(); nr != nil {
|
|
|
|
// 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,
|
2021-11-10 11:05:51 +00:00
|
|
|
nil,
|
2021-12-07 14:04:32 +00:00
|
|
|
methodAddPeerNotary,
|
2021-09-23 18:02:24 +00:00
|
|
|
nodeInfoBinary,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// notification event case
|
2021-11-10 11:05:51 +00:00
|
|
|
err = np.netmapClient.AddPeer(prm)
|
2021-09-23 18:02:24 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
np.log.Info("prevent switching node to maintenance state",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 11:55:01 +00:00
|
|
|
if nr := ev.NotaryRequest(); nr != nil {
|
|
|
|
err = np.netmapClient.Morph().NotarySignAndInvokeTX(nr.MainTransaction)
|
|
|
|
} else {
|
2021-11-10 11:05:51 +00:00
|
|
|
prm := netmapclient.UpdatePeerPrm{}
|
|
|
|
|
2022-09-19 16:34:53 +00:00
|
|
|
switch {
|
|
|
|
case ev.Online():
|
2022-06-08 23:18:26 +00:00
|
|
|
prm.SetOnline()
|
2022-09-19 16:34:53 +00:00
|
|
|
case ev.Maintenance():
|
|
|
|
prm.SetMaintenance()
|
2022-06-08 23:18:26 +00:00
|
|
|
}
|
2022-09-19 16:34:53 +00:00
|
|
|
|
2021-11-10 11:05:51 +00:00
|
|
|
prm.SetKey(ev.PublicKey().Bytes())
|
|
|
|
|
|
|
|
err = np.netmapClient.UpdatePeerState(prm)
|
2021-09-09 11:55:01 +00:00
|
|
|
}
|
2020-09-03 14:55:11 +00:00
|
|
|
if err != nil {
|
|
|
|
np.log.Error("can't invoke netmap.UpdatePeer", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 14:16:03 +00:00
|
|
|
|
|
|
|
func (np *Processor) processRemoveSubnetNode(ev subnetEvent.RemoveNode) {
|
|
|
|
if !np.alphabetState.IsAlphabet() {
|
|
|
|
np.log.Info("non alphabet mode, ignore remove node from subnet notification")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates, err := np.netmapClient.GetCandidates()
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("could not get network map candidates",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rawSubnet := ev.SubnetworkID()
|
2022-06-15 08:08:10 +00:00
|
|
|
var subnetToRemoveFrom subnetid.ID
|
2021-11-26 14:16:03 +00:00
|
|
|
|
|
|
|
err = subnetToRemoveFrom.Unmarshal(rawSubnet)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("could not unmarshal subnet id",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-15 08:08:10 +00:00
|
|
|
if subnetid.IsZero(subnetToRemoveFrom) {
|
2021-11-26 14:16:03 +00:00
|
|
|
np.log.Warn("got zero subnet in remove node notification")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
for i := range candidates {
|
|
|
|
if !bytes.Equal(candidates[i].PublicKey(), ev.Node()) {
|
2021-11-26 14:16:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
err = candidates[i].IterateSubnets(func(subNetID subnetid.ID) error {
|
2021-11-26 14:16:03 +00:00
|
|
|
if subNetID.Equals(subnetToRemoveFrom) {
|
|
|
|
return netmap.ErrRemoveSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
np.log.Warn("could not iterate over subnetworks of the node", zap.Error(err))
|
|
|
|
np.log.Info("vote to remove node from netmap", 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("could not invoke netmap.UpdateState", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prm := netmapclient.AddPeerPrm{}
|
2022-09-28 11:34:28 +00:00
|
|
|
prm.SetNodeInfo(candidates[i])
|
2021-11-26 14:16:03 +00:00
|
|
|
prm.SetHash(ev.TxHash())
|
|
|
|
|
|
|
|
err = np.netmapClient.AddPeer(prm)
|
|
|
|
if err != nil {
|
|
|
|
np.log.Error("could not invoke netmap.AddPeer", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|