From 42fb40e841ca1cd3a69429f8badb2f54b32c77a0 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 19 Sep 2022 17:38:07 +0400 Subject: [PATCH] [#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 --- pkg/morph/client/netmap/update_state.go | 24 ++++++++++++++++---- pkg/morph/event/netmap/update_peer.go | 12 +++++++--- pkg/morph/event/netmap/update_peer_notary.go | 2 +- pkg/morph/event/netmap/update_peer_test.go | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pkg/morph/client/netmap/update_state.go b/pkg/morph/client/netmap/update_state.go index 1b860ffe6..44a404d00 100644 --- a/pkg/morph/client/netmap/update_state.go +++ b/pkg/morph/client/netmap/update_state.go @@ -7,11 +7,17 @@ import ( "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. type UpdatePeerPrm struct { key []byte - online bool + state int8 // state enum value client.InvokePrmOptional } @@ -21,9 +27,11 @@ func (u *UpdatePeerPrm) SetKey(key []byte) { 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() { - u.online = true + u.state = stateOnline } // UpdatePeerState changes peer status through Netmap contract call. @@ -37,8 +45,14 @@ func (c *Client) UpdatePeerState(p UpdatePeerPrm) error { method += "IR" } - state := netmap.OfflineState - if p.online { + state := netmap.OfflineState // pre-assign since type of value is unexported + + 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 } diff --git a/pkg/morph/event/netmap/update_peer.go b/pkg/morph/event/netmap/update_peer.go index b96a932e2..d83da6a50 100644 --- a/pkg/morph/event/netmap/update_peer.go +++ b/pkg/morph/event/netmap/update_peer.go @@ -12,10 +12,16 @@ import ( "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 { publicKey *keys.PublicKey - online bool + state int8 // state enum value // For notary notifications only. // Contains raw transactions of notary request. @@ -26,7 +32,7 @@ type UpdatePeer struct { func (UpdatePeer) MorphEvent() {} func (s UpdatePeer) Online() bool { - return s.online + return s.state == stateOnline } 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) case int64(netmap.OfflineState): case int64(netmap.OnlineState): - ev.online = true + ev.state = stateOnline } return ev, nil diff --git a/pkg/morph/event/netmap/update_peer_notary.go b/pkg/morph/event/netmap/update_peer_notary.go index 8dc22bfc5..1c1b8b361 100644 --- a/pkg/morph/event/netmap/update_peer_notary.go +++ b/pkg/morph/event/netmap/update_peer_notary.go @@ -66,7 +66,7 @@ func ParseUpdatePeerNotary(ne event.NotaryEvent) (event.Event, error) { return nil, fmt.Errorf("unsupported node state %d", err) case v2netmap.Offline: case v2netmap.Online: - ev.online = true + ev.state = stateOnline } fieldNum++ diff --git a/pkg/morph/event/netmap/update_peer_test.go b/pkg/morph/event/netmap/update_peer_test.go index aa397be7e..e45e99cb4 100644 --- a/pkg/morph/event/netmap/update_peer_test.go +++ b/pkg/morph/event/netmap/update_peer_test.go @@ -51,7 +51,7 @@ func TestParseUpdatePeer(t *testing.T) { require.Equal(t, UpdatePeer{ publicKey: publicKey, - online: true, + state: stateOnline, }, ev) }) }