Aleksey Savchuk
f0c43c8d80
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 3m1s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m29s
Tests and linters / gopls check (pull_request) Successful in 3m50s
Tests and linters / Lint (pull_request) Successful in 4m35s
DCO action / DCO (pull_request) Successful in 5m12s
Tests and linters / Run gofumpt (pull_request) Successful in 5m33s
Build / Build Components (pull_request) Successful in 5m45s
Tests and linters / Tests with -race (pull_request) Successful in 6m37s
Tests and linters / Tests (pull_request) Successful in 7m17s
Tests and linters / Staticcheck (pull_request) Successful in 7m36s
Tests and linters / Run gofumpt (push) Successful in 1m22s
Tests and linters / Staticcheck (push) Successful in 3m19s
Tests and linters / Lint (push) Successful in 4m35s
Vulncheck / Vulncheck (push) Successful in 5m20s
Build / Build Components (push) Successful in 6m16s
Pre-commit hooks / Pre-commit (push) Successful in 6m37s
Tests and linters / Tests (push) Successful in 6m48s
Tests and linters / Tests with -race (push) Successful in 7m15s
Tests and linters / gopls check (push) Successful in 7m27s
Use `zap.Error` instead of `zap.String` for logging errors: change all expressions like `zap.String("error", err.Error())` or `zap.String("err", err.Error())` to `zap.Error(err)`. Leave similar expressions with other messages unchanged, for example, `zap.String("last_error", lastErr.Error())` or `zap.String("reason", ctx.Err().Error())`. This change was made by applying the following patch: ```diff @@ var err expression @@ -zap.String("error", err.Error()) +zap.Error(err) @@ var err expression @@ -zap.String("err", err.Error()) +zap.Error(err) ``` Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package netmap
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Process add peer notification by sanity check of new node
|
|
// local epoch timer.
|
|
func (np *Processor) processAddPeer(ctx context.Context, ev netmapEvent.AddPeer) bool {
|
|
if !np.alphabetState.IsAlphabet(ctx) {
|
|
np.log.Info(ctx, logs.NetmapNonAlphabetModeIgnoreNewPeerNotification)
|
|
return true
|
|
}
|
|
|
|
// check if notary transaction is valid, see #976
|
|
tx := ev.NotaryRequest().MainTransaction
|
|
ok, err := np.netmapClient.MorphIsValidScript(tx.Script, tx.Signers)
|
|
if err != nil || !ok {
|
|
np.log.Warn(ctx, logs.NetmapNonhaltNotaryTransaction,
|
|
zap.String("method", "netmap.AddPeer"),
|
|
zap.String("hash", tx.Hash().StringLE()),
|
|
zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
// 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(ctx, logs.NetmapCantParseNetworkMapCandidate)
|
|
return false
|
|
}
|
|
|
|
// validate and update node info
|
|
err = np.nodeValidator.VerifyAndUpdate(&nodeInfo)
|
|
if err != nil {
|
|
np.log.Warn(ctx, logs.NetmapCouldNotVerifyAndUpdateInformationAboutNetworkMapCandidate,
|
|
zap.Error(err),
|
|
)
|
|
|
|
return false
|
|
}
|
|
|
|
// 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)
|
|
|
|
// `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.Status().IsOnline() {
|
|
np.log.Info(ctx, 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"
|
|
|
|
// create new notary request with the original nonce
|
|
err = np.netmapClient.MorphNotaryInvoke(
|
|
ctx,
|
|
np.netmapClient.ContractAddress(),
|
|
0,
|
|
ev.NotaryRequest().MainTransaction.Nonce,
|
|
nil,
|
|
methodAddPeerNotary,
|
|
nodeInfoBinary,
|
|
)
|
|
if err != nil {
|
|
np.log.Error(ctx, logs.NetmapCantInvokeNetmapAddPeer, zap.Error(err))
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// Process update peer notification by sending approval tx to the smart contract.
|
|
func (np *Processor) processUpdatePeer(ctx context.Context, ev netmapEvent.UpdatePeer) bool {
|
|
if !np.alphabetState.IsAlphabet(ctx) {
|
|
np.log.Info(ctx, logs.NetmapNonAlphabetModeIgnoreUpdatePeerNotification)
|
|
return true
|
|
}
|
|
|
|
// 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(ctx, logs.NetmapPreventSwitchingNodeToMaintenanceState,
|
|
zap.Error(err),
|
|
)
|
|
|
|
return false
|
|
}
|
|
}
|
|
|
|
if err = np.netmapClient.MorphNotarySignAndInvokeTX(ev.NotaryRequest().MainTransaction); err != nil {
|
|
np.log.Error(ctx, logs.NetmapCantInvokeNetmapUpdatePeer, zap.Error(err))
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|