[#225] netmap: Split UpdateState method for notary-enabled environment

Split methods similar to `AddPeer`/`Register` pair from b104a2ccbc .

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-10 15:01:10 +03:00 committed by Alex Vanin
parent 5758dadaa9
commit 9662f9f4db
2 changed files with 57 additions and 19 deletions

View file

@ -190,8 +190,8 @@ func UpdateInnerRing(keys []interop.PublicKey) {
common.SetSerialized(ctx, innerRingKey, irList) common.SetSerialized(ctx, innerRingKey, irList)
} }
// Register method tries to add new candidate to the network map by // Register method tries to add new candidate to the network map.
// emitting AddPeer notification. Should be invoked by the registree. // Should be invoked in notary-enabled environment by the alphabet.
func Register(nodeInfo []byte) { func Register(nodeInfo []byte) {
ctx := storage.GetContext() ctx := storage.GetContext()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool) notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
@ -281,13 +281,17 @@ func UpdateState(state int, publicKey interop.PublicKey) {
if notaryDisabled { if notaryDisabled {
alphabet = common.AlphabetNodes() alphabet = common.AlphabetNodes()
nodeKey = common.InnerRingInvoker(alphabet) nodeKey = common.InnerRingInvoker(alphabet)
if len(nodeKey) == 0 { }
common.CheckWitness(publicKey)
runtime.Notify("UpdateState", state, publicKey) // If notary is enabled or caller is not an alphabet node,
return // just emit the notification for alphabet.
} if !notaryDisabled || len(nodeKey) == 0 {
common.CheckWitness(publicKey)
runtime.Notify("UpdateState", state, publicKey)
return
}
if notaryDisabled {
threshold := len(alphabet)*2/3 + 1 threshold := len(alphabet)*2/3 + 1
id := common.InvokeID([]interface{}{state, publicKey}, []byte("update")) id := common.InvokeID([]interface{}{state, publicKey}, []byte("update"))
@ -297,10 +301,6 @@ func UpdateState(state int, publicKey interop.PublicKey) {
} }
common.RemoveVotes(ctx, id) common.RemoveVotes(ctx, id)
} else {
multiaddr := common.AlphabetAddress()
common.CheckWitness(publicKey)
common.CheckAlphabetWitness(multiaddr)
} }
switch nodeState(state) { switch nodeState(state) {
@ -312,6 +312,25 @@ func UpdateState(state int, publicKey interop.PublicKey) {
} }
} }
// UpdateStateIR method tries to change node state in the network map.
// Should only be invoked in notary-enabled environment by the alphabet.
func UpdateStateIR(state nodeState, publicKey interop.PublicKey) {
ctx := storage.GetContext()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
if notaryDisabled {
panic("UpdateStateIR should only be called in notary-enabled environment")
}
common.CheckAlphabetWitness(common.AlphabetAddress())
switch state {
case offlineState:
removeFromNetmap(ctx, publicKey)
default:
panic("unsupported state")
}
}
// NewEpoch method changes epoch number up to provided epochNum argument. Can // NewEpoch method changes epoch number up to provided epochNum argument. Can
// be invoked only by Alphabet nodes. If provided epoch number is less or equal // be invoked only by Alphabet nodes. If provided epoch number is less or equal
// current epoch number, method throws panic. // current epoch number, method throws panic.

View file

@ -111,25 +111,44 @@ func TestAddPeer(t *testing.T) {
} }
func TestUpdateState(t *testing.T) { func TestUpdateState(t *testing.T) {
e := newNetmapInvoker(t) cNm := newNetmapInvoker(t)
acc := e.NewAccount(t) acc := cNm.NewAccount(t)
cAcc := e.WithSigners(acc) cAcc := cNm.WithSigners(acc)
cBoth := e.WithSigners(e.Committee, acc)
dummyInfo := dummyNodeInfo(acc) dummyInfo := dummyNodeInfo(acc)
cBoth.Invoke(t, stackitem.Null{}, "addPeer", dummyInfo.raw) cAcc.Invoke(t, stackitem.Null{}, "addPeer", dummyInfo.raw)
cNm.Invoke(t, stackitem.Null{}, "register", dummyInfo.raw)
pub, ok := vm.ParseSignatureContract(acc.Script()) pub, ok := vm.ParseSignatureContract(acc.Script())
require.True(t, ok) require.True(t, ok)
t.Run("missing witness", func(t *testing.T) { t.Run("missing witness", func(t *testing.T) {
cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed, cAcc.InvokeFail(t, common.ErrAlphabetWitnessFailed,
"updateState", int64(2), pub) "updateStateIR", int64(2), pub)
e.InvokeFail(t, common.ErrWitnessFailed, cNm.InvokeFail(t, common.ErrWitnessFailed,
"updateState", int64(2), pub) "updateState", int64(2), pub)
}) })
cBoth.Invoke(t, stackitem.Null{}, "updateState", int64(2), pub) h := cAcc.Invoke(t, stackitem.Null{}, "updateState", int64(2), pub)
aer := cAcc.CheckHalt(t, h)
require.Equal(t, 1, len(aer.Events))
require.Equal(t, "UpdateState", aer.Events[0].Name)
require.Equal(t, stackitem.NewArray([]stackitem.Item{
stackitem.NewBigInteger(big.NewInt(2)),
stackitem.NewByteArray(pub),
}), aer.Events[0].Item)
// Check that updating happens only after `updateState` is called by the alphabet.
s, err := cAcc.TestInvoke(t, "netmapCandidates")
require.NoError(t, err)
require.Equal(t, 1, s.Len())
arr, ok := s.Pop().Value().([]stackitem.Item)
require.True(t, ok)
require.Equal(t, 1, len(arr))
cNm.Invoke(t, stackitem.Null{}, "updateStateIR", int64(2), pub)
cAcc.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "netmapCandidates") cAcc.Invoke(t, stackitem.NewArray([]stackitem.Item{}), "netmapCandidates")
} }