Skip to content

Commit

Permalink
[#271] netmap: Allow to move node back to Online state
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Stratonikov <[email protected]>
  • Loading branch information
fyrchik committed Sep 21, 2022
1 parent cb68499 commit 9785f9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
14 changes: 9 additions & 5 deletions netmap/netmap_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,13 @@ func UpdateState(state int, publicKey interop.PublicKey) {
common.CheckAlphabetWitness(common.AlphabetAddress())
}

switch NodeState(state) {
st := NodeState(state)
switch st {
case OfflineState:
removeFromNetmap(ctx, publicKey)
runtime.Log("remove storage node from the network map")
case MaintenanceState:
updateNetmapState(ctx, publicKey, MaintenanceState)
case MaintenanceState, OnlineState:
updateNetmapState(ctx, publicKey, st)
runtime.Log("move storage node to a maintenance state")
default:
panic("unsupported state")
Expand All @@ -342,8 +343,8 @@ func UpdateStateIR(state NodeState, publicKey interop.PublicKey) {
switch state {
case OfflineState:
removeFromNetmap(ctx, publicKey)
case MaintenanceState:
updateNetmapState(ctx, publicKey, MaintenanceState)
case MaintenanceState, OnlineState:
updateNetmapState(ctx, publicKey, state)
default:
panic("unsupported state")
}
Expand Down Expand Up @@ -658,6 +659,9 @@ func removeFromNetmap(ctx storage.Context, key interop.PublicKey) {
func updateNetmapState(ctx storage.Context, key interop.PublicKey, state NodeState) {
storageKey := append(candidatePrefix, key...)
raw := storage.Get(ctx, storageKey).([]byte)
if raw == nil {
panic("peer is missing")
}
node := std.Deserialize(raw).(netmapNode)
node.state = state
storage.Put(ctx, storageKey, std.Serialize(node))
Expand Down
36 changes: 26 additions & 10 deletions tests/netmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,14 @@ func TestUpdateStateIR(t *testing.T) {
cNm := newNetmapInvoker(t)

acc := cNm.NewAccount(t)
pub := acc.(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()

t.Run("can't move online, need addPeerIR", func(t *testing.T) {
cNm.InvokeFail(t, "peer is missing", "updateStateIR", int64(netmap.OnlineState), pub)
})

dummyInfo := dummyNodeInfo(acc)
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo.raw)
pub := acc.(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()

acc1 := cNm.NewAccount(t)
dummyInfo1 := dummyNodeInfo(acc1)
Expand All @@ -322,9 +327,6 @@ func TestUpdateStateIR(t *testing.T) {
cAcc := cNm.WithSigners(acc)
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, "updateStateIR", int64(netmap.OfflineState), pub)
})
t.Run("can't move online", func(t *testing.T) {
cNm.InvokeFail(t, "unsupported state", "updateStateIR", int64(netmap.OnlineState), pub)
})
t.Run("invalid state", func(t *testing.T) {
cNm.InvokeFail(t, "unsupported state", "updateStateIR", int64(42), pub)
})
Expand All @@ -335,14 +337,28 @@ func TestUpdateStateIR(t *testing.T) {
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.OfflineState), pub)
checkNetmapCandidates(t, cNm, 1)

checkState := func(expected netmap.NodeState) {
arr := checkNetmapCandidates(t, cNm, 1)
nn := arr[0].Value().([]stackitem.Item)
state, err := nn[1].TryInteger()
require.NoError(t, err)
require.Equal(t, int64(expected), state.Int64())
}

// Move the second node in the maintenance state.
pub1 := acc1.(neotest.SingleSigner).Account().PrivateKey().PublicKey().Bytes()
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.MaintenanceState), pub1)
arr := checkNetmapCandidates(t, cNm, 1)
nn := arr[0].Value().([]stackitem.Item)
state, err := nn[1].TryInteger()
require.NoError(t, err)
require.Equal(t, int64(netmap.MaintenanceState), state.Int64())
t.Run("maintenance -> add peer", func(t *testing.T) {
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.MaintenanceState), pub1)
checkState(netmap.MaintenanceState)
cNm.Invoke(t, stackitem.Null{}, "addPeerIR", dummyInfo1.raw)
checkState(netmap.OnlineState)
})
t.Run("maintenance -> online", func(t *testing.T) {
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.MaintenanceState), pub1)
checkState(netmap.MaintenanceState)
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(netmap.OnlineState), pub1)
checkState(netmap.OnlineState)
})
}

func TestUpdateState(t *testing.T) {
Expand Down

0 comments on commit 9785f9b

Please sign in to comment.