[#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:
parent
4c94faac67
commit
42fb40e841
4 changed files with 30 additions and 10 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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++
|
||||
|
|
|
@ -51,7 +51,7 @@ func TestParseUpdatePeer(t *testing.T) {
|
|||
|
||||
require.Equal(t, UpdatePeer{
|
||||
publicKey: publicKey,
|
||||
online: true,
|
||||
state: stateOnline,
|
||||
}, ev)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue