[#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>
This commit is contained in:
parent
bf83ed9a4f
commit
b104a2ccbc
2 changed files with 24 additions and 9 deletions
|
@ -188,6 +188,21 @@ 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
|
||||||
|
// 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
|
// 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
|
// by Alphabet node. If it was invoked by node candidate, it produces AddPeer
|
||||||
// notification. Otherwise method throws panic.
|
// notification. Otherwise method throws panic.
|
||||||
|
@ -202,23 +217,20 @@ func AddPeer(nodeInfo []byte) {
|
||||||
var ( // for invocation collection without notary
|
var ( // for invocation collection without notary
|
||||||
alphabet []common.IRNode
|
alphabet []common.IRNode
|
||||||
nodeKey []byte
|
nodeKey []byte
|
||||||
alphabetCall bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if notaryDisabled {
|
if notaryDisabled {
|
||||||
alphabet = common.AlphabetNodes()
|
alphabet = common.AlphabetNodes()
|
||||||
nodeKey = common.InnerRingInvoker(alphabet)
|
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
|
// V2 format
|
||||||
publicKey := nodeInfo[2:35] // offset:2, len:33
|
publicKey := nodeInfo[2:35] // offset:2, len:33
|
||||||
common.CheckWitness(publicKey)
|
|
||||||
|
|
||||||
|
common.CheckWitness(publicKey)
|
||||||
runtime.Notify("AddPeer", nodeInfo)
|
runtime.Notify("AddPeer", nodeInfo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,9 @@ func TestAddPeer(t *testing.T) {
|
||||||
require.Equal(t, "AddPeer", aer.Events[0].Name)
|
require.Equal(t, "AddPeer", aer.Events[0].Name)
|
||||||
require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo)}),
|
require.Equal(t, stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(dummyInfo)}),
|
||||||
aer.Events[0].Item)
|
aer.Events[0].Item)
|
||||||
|
|
||||||
|
c.InvokeFail(t, common.ErrWitnessFailed, "addPeer", dummyInfo)
|
||||||
|
c.Invoke(t, stackitem.Null{}, "register", dummyInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateState(t *testing.T) {
|
func TestUpdateState(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue