[#1680] morph/netmap: Pre-refactor processing of node states

New network status of storage nodes is going to be introduced. To
simplify the addition, it would be useful to prepare the code for this.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-09-19 17:38:07 +04:00 committed by fyrchik
parent 4c94faac67
commit 42fb40e841
4 changed files with 30 additions and 10 deletions

View file

@ -7,11 +7,17 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
) )
// TODO: enum can become redundant after neofs-contract#270
const (
stateOffline int8 = iota
stateOnline
)
// UpdatePeerPrm groups parameters of UpdatePeerState operation. // UpdatePeerPrm groups parameters of UpdatePeerState operation.
type UpdatePeerPrm struct { type UpdatePeerPrm struct {
key []byte key []byte
online bool state int8 // state enum value
client.InvokePrmOptional client.InvokePrmOptional
} }
@ -21,9 +27,11 @@ func (u *UpdatePeerPrm) SetKey(key []byte) {
u.key = key u.key = key
} }
// SetState sets node state. // SetOnline marks node to be switched into "online" state.
//
// Zero UpdatePeerPrm marks node as "offline".
func (u *UpdatePeerPrm) SetOnline() { func (u *UpdatePeerPrm) SetOnline() {
u.online = true u.state = stateOnline
} }
// UpdatePeerState changes peer status through Netmap contract call. // UpdatePeerState changes peer status through Netmap contract call.
@ -37,8 +45,14 @@ func (c *Client) UpdatePeerState(p UpdatePeerPrm) error {
method += "IR" method += "IR"
} }
state := netmap.OfflineState state := netmap.OfflineState // pre-assign since type of value is unexported
if p.online {
switch p.state {
default:
panic(fmt.Sprintf("unexpected node's state value %v", p.state))
case stateOffline:
// already set above
case stateOnline:
state = netmap.OnlineState state = netmap.OnlineState
} }

View file

@ -12,10 +12,16 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
) )
// TODO: enum can become redundant after neofs-contract#270
const (
_ int8 = iota
stateOnline
)
type UpdatePeer struct { type UpdatePeer struct {
publicKey *keys.PublicKey publicKey *keys.PublicKey
online bool state int8 // state enum value
// For notary notifications only. // For notary notifications only.
// Contains raw transactions of notary request. // Contains raw transactions of notary request.
@ -26,7 +32,7 @@ type UpdatePeer struct {
func (UpdatePeer) MorphEvent() {} func (UpdatePeer) MorphEvent() {}
func (s UpdatePeer) Online() bool { func (s UpdatePeer) Online() bool {
return s.online return s.state == stateOnline
} }
func (s UpdatePeer) PublicKey() *keys.PublicKey { func (s UpdatePeer) PublicKey() *keys.PublicKey {
@ -78,7 +84,7 @@ func ParseUpdatePeer(e *state.ContainedNotificationEvent) (event.Event, error) {
return nil, fmt.Errorf("unsupported node state %d", st) return nil, fmt.Errorf("unsupported node state %d", st)
case int64(netmap.OfflineState): case int64(netmap.OfflineState):
case int64(netmap.OnlineState): case int64(netmap.OnlineState):
ev.online = true ev.state = stateOnline
} }
return ev, nil return ev, nil

View file

@ -66,7 +66,7 @@ func ParseUpdatePeerNotary(ne event.NotaryEvent) (event.Event, error) {
return nil, fmt.Errorf("unsupported node state %d", err) return nil, fmt.Errorf("unsupported node state %d", err)
case v2netmap.Offline: case v2netmap.Offline:
case v2netmap.Online: case v2netmap.Online:
ev.online = true ev.state = stateOnline
} }
fieldNum++ fieldNum++

View file

@ -51,7 +51,7 @@ func TestParseUpdatePeer(t *testing.T) {
require.Equal(t, UpdatePeer{ require.Equal(t, UpdatePeer{
publicKey: publicKey, publicKey: publicKey,
online: true, state: stateOnline,
}, ev) }, ev)
}) })
} }