2020-07-24 13:54:03 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2024-10-21 13:27:28 +00:00
|
|
|
"context"
|
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-contract/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
// UpdatePeerPrm groups parameters of UpdatePeerState operation.
|
|
|
|
type UpdatePeerPrm struct {
|
2022-06-08 23:18:26 +00:00
|
|
|
key []byte
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
state netmap.NodeState
|
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
|
|
|
// SetKey sets public key.
|
|
|
|
func (u *UpdatePeerPrm) SetKey(key []byte) {
|
|
|
|
u.key = key
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 13:38:07 +00:00
|
|
|
// SetOnline marks node to be switched into "online" state.
|
|
|
|
//
|
|
|
|
// Zero UpdatePeerPrm marks node as "offline".
|
2022-06-08 23:18:26 +00:00
|
|
|
func (u *UpdatePeerPrm) SetOnline() {
|
2022-09-28 11:34:28 +00:00
|
|
|
u.state = netmap.NodeStateOnline
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 16:12:20 +00:00
|
|
|
// SetMaintenance marks node to be switched into "maintenance" state.
|
|
|
|
//
|
|
|
|
// Zero UpdatePeerPrm marks node as "offline".
|
|
|
|
func (u *UpdatePeerPrm) SetMaintenance() {
|
2022-09-28 11:34:28 +00:00
|
|
|
u.state = netmap.NodeStateMaintenance
|
2022-09-19 16:12:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
// UpdatePeerState changes peer status through Netmap contract call.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (c *Client) UpdatePeerState(ctx context.Context, p UpdatePeerPrm) (uint32, error) {
|
2022-05-13 18:35:37 +00:00
|
|
|
method := updateStateMethod
|
|
|
|
|
|
|
|
if c.client.WithNotary() && c.client.IsAlpha() {
|
|
|
|
// In notary environments Alphabet must calls UpdateStateIR method instead of UpdateState.
|
|
|
|
// It differs from UpdateState 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/225.
|
2022-05-13 18:35:37 +00:00
|
|
|
method += "IR"
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:51:22 +00:00
|
|
|
if p.state == 0 {
|
|
|
|
p.state = netmap.NodeStateOffline
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
prm := client.InvokePrm{}
|
2022-05-13 18:35:37 +00:00
|
|
|
prm.SetMethod(method)
|
2022-09-28 11:34:28 +00:00
|
|
|
prm.SetArgs(int64(p.state), p.key)
|
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
|
|
|
res, err := c.client.Invoke(ctx, prm)
|
2023-11-08 09:05:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("could not invoke smart contract: %w", err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
2023-11-08 09:05:03 +00:00
|
|
|
return res.VUB, nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|