[#154] netmap: implement `Register` method

For notary-disabled environment it makes sense to split node
registration and actual candidate update into separate methods.
This way we have less complicate logic in `AddPeer` and overall
registration flow is more understandable.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
enable-notary-in-public-chains
Evgenii Stratonikov 2021-12-02 18:59:29 +03:00 committed by Alex Vanin
parent bf83ed9a4f
commit b104a2ccbc
2 changed files with 24 additions and 9 deletions

View File

@ -188,6 +188,21 @@ func UpdateInnerRing(keys []interop.PublicKey) {
common.SetSerialized(ctx, innerRingKey, irList)
}
// Register method tries to add new candidate to the network map by
// emitting AddPeer notification. Should be invoked by the registree.
func Register(nodeInfo []byte) {
ctx := storage.GetContext()
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
if notaryDisabled {
panic("Register should only be called in notary-enabled environment")
}
common.CheckAlphabetWitness(common.AlphabetAddress())
addToNetmap(ctx, storageNode{info: nodeInfo})
return
}
// AddPeer method adds new candidate to the next network map if it was invoked
// by Alphabet node. If it was invoked by node candidate, it produces AddPeer
// notification. Otherwise method throws panic.
@ -200,25 +215,22 @@ func AddPeer(nodeInfo []byte) {
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
var ( // for invocation collection without notary
alphabet []common.IRNode
nodeKey []byte
alphabetCall bool
alphabet []common.IRNode
nodeKey []byte
)
if notaryDisabled {
alphabet = common.AlphabetNodes()
nodeKey = common.InnerRingInvoker(alphabet)
alphabetCall = len(nodeKey) != 0
} else {
multiaddr := common.AlphabetAddress()
alphabetCall = runtime.CheckWitness(multiaddr)
}
if !alphabetCall {
// If notary is enabled or caller is not an alphabet node,
// just emit the notification for alphabet.
if !notaryDisabled || len(nodeKey) == 0 {
// V2 format
publicKey := nodeInfo[2:35] // offset:2, len:33
common.CheckWitness(publicKey)
common.CheckWitness(publicKey)
runtime.Notify("AddPeer", nodeInfo)
return
}

View File

@ -90,6 +90,9 @@ func TestAddPeer(t *testing.T) {
require.Equal(t, "AddPeer", aer.Events[0].Name)
require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo)}),
aer.Events[0].Item)
c.InvokeFail(t, common.ErrWitnessFailed, "addPeer", dummyInfo)
c.Invoke(t, stackitem.Null{}, "register", dummyInfo)
}
func TestUpdateState(t *testing.T) {