2020-07-24 13:54:03 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2024-10-21 13:27:28 +00:00
|
|
|
"context"
|
2024-03-11 14:55:50 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2024-03-11 14:55:50 +00:00
|
|
|
var errFailedToRemovePeerWithoutNotary = errors.New("peer can be forcefully removed only in notary environment")
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
// AddPeerPrm groups parameters of AddPeer operation.
|
|
|
|
type AddPeerPrm struct {
|
2022-06-08 23:18:26 +00:00
|
|
|
nodeInfo netmap.NodeInfo
|
2021-11-10 10:44:19 +00:00
|
|
|
|
|
|
|
client.InvokePrmOptional
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
// SetNodeInfo sets new peer NodeInfo.
|
2022-06-08 23:18:26 +00:00
|
|
|
func (a *AddPeerPrm) SetNodeInfo(nodeInfo netmap.NodeInfo) {
|
2022-01-31 11:58:55 +00:00
|
|
|
a.nodeInfo = nodeInfo
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// AddPeer registers peer in FrostFS network through
|
2022-01-31 11:58:55 +00:00
|
|
|
// Netmap contract call.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) AddPeer(ctx context.Context, p AddPeerPrm) error {
|
2023-10-31 11:56:55 +00:00
|
|
|
method := addPeerMethod
|
2022-05-13 18:35:37 +00:00
|
|
|
|
|
|
|
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.
|
2022-12-23 17:35:35 +00:00
|
|
|
// See https://github.com/nspcc-dev/frostfs-contract/issues/154.
|
2022-05-13 18:35:37 +00:00
|
|
|
method += "IR"
|
|
|
|
}
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
prm := client.InvokePrm{}
|
2022-05-13 18:35:37 +00:00
|
|
|
prm.SetMethod(method)
|
2022-06-08 23:18:26 +00:00
|
|
|
prm.SetArgs(p.nodeInfo.Marshal())
|
2022-01-31 11:58:55 +00:00
|
|
|
prm.InvokePrmOptional = p.InvokePrmOptional
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
if _, err := c.client.Invoke(ctx, prm); err != nil {
|
2022-05-13 18:35:37 +00:00
|
|
|
return fmt.Errorf("could not invoke method (%s): %w", method, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
2023-05-03 13:19:46 +00:00
|
|
|
|
|
|
|
// ForceRemovePeer marks the given peer as offline via a notary control transaction.
|
2023-11-08 09:05:03 +00:00
|
|
|
// If vub > 0, vub will be used as valid until block value.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) ForceRemovePeer(ctx context.Context, nodeInfo netmap.NodeInfo, vub uint32) (uint32, error) {
|
2023-05-03 13:19:46 +00:00
|
|
|
if !c.client.WithNotary() {
|
2024-03-11 14:55:50 +00:00
|
|
|
return 0, errFailedToRemovePeerWithoutNotary
|
2023-05-03 13:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm := UpdatePeerPrm{}
|
|
|
|
prm.SetKey(nodeInfo.PublicKey())
|
|
|
|
prm.SetControlTX(true)
|
2023-11-08 09:05:03 +00:00
|
|
|
prm.SetVUB(vub)
|
2023-05-03 13:19:46 +00:00
|
|
|
|
2024-11-14 12:09:42 +00:00
|
|
|
res, err := c.UpdatePeerState(ctx, prm)
|
2023-11-08 09:05:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("updating peer state: %v", err)
|
2023-05-03 13:19:46 +00:00
|
|
|
}
|
2024-11-14 12:09:42 +00:00
|
|
|
return res.VUB, nil
|
2023-05-03 13:19:46 +00:00
|
|
|
}
|