Evgenii Stratonikov
d82f0d1926
All checks were successful
DCO action / DCO (pull_request) Successful in 1m1s
Vulncheck / Vulncheck (pull_request) Successful in 2m15s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m39s
Build / Build Components (pull_request) Successful in 3m0s
Tests and linters / Run gofumpt (pull_request) Successful in 2m53s
Tests and linters / Staticcheck (pull_request) Successful in 2m59s
Tests and linters / Lint (pull_request) Successful in 3m41s
Tests and linters / gopls check (pull_request) Successful in 3m58s
Tests and linters / Tests (pull_request) Successful in 4m23s
Tests and linters / Tests with -race (pull_request) Successful in 4m44s
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package netmap
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
)
|
|
|
|
var errFailedToRemovePeerWithoutNotary = errors.New("peer can be forcefully removed only in notary environment")
|
|
|
|
// AddPeerPrm groups parameters of AddPeer operation.
|
|
type AddPeerPrm struct {
|
|
nodeInfo netmap.NodeInfo
|
|
|
|
client.InvokePrmOptional
|
|
}
|
|
|
|
// SetNodeInfo sets new peer NodeInfo.
|
|
func (a *AddPeerPrm) SetNodeInfo(nodeInfo netmap.NodeInfo) {
|
|
a.nodeInfo = nodeInfo
|
|
}
|
|
|
|
// AddPeer registers peer in FrostFS network through
|
|
// Netmap contract call.
|
|
func (c *Client) AddPeer(ctx context.Context, p AddPeerPrm) error {
|
|
method := addPeerMethod
|
|
|
|
if c.client.WithNotary() && c.client.IsAlpha() {
|
|
// In notary environments Alphabet must calls 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.
|
|
method += "IR"
|
|
}
|
|
|
|
prm := client.InvokePrm{}
|
|
prm.SetMethod(method)
|
|
prm.SetArgs(p.nodeInfo.Marshal())
|
|
prm.InvokePrmOptional = p.InvokePrmOptional
|
|
|
|
if _, err := c.client.Invoke(ctx, prm); err != nil {
|
|
return fmt.Errorf("could not invoke method (%s): %w", method, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ForceRemovePeer marks the given peer as offline via a notary control transaction.
|
|
// If vub > 0, vub will be used as valid until block value.
|
|
func (c *Client) ForceRemovePeer(ctx context.Context, nodeInfo netmap.NodeInfo, vub uint32) (uint32, error) {
|
|
if !c.client.WithNotary() {
|
|
return 0, errFailedToRemovePeerWithoutNotary
|
|
}
|
|
|
|
prm := UpdatePeerPrm{}
|
|
prm.SetKey(nodeInfo.PublicKey())
|
|
prm.SetControlTX(true)
|
|
prm.SetVUB(vub)
|
|
|
|
res, err := c.UpdatePeerState(ctx, prm)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("updating peer state: %v", err)
|
|
}
|
|
return res.VUB, nil
|
|
}
|