[#174] subnet: Do not panic while doing operation that are already done

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-11-24 21:09:13 +03:00 committed by LeL
parent 0e84900187
commit 293ca36ff4
2 changed files with 21 additions and 22 deletions

View file

@ -161,7 +161,7 @@ func Delete(id []byte) {
key := append([]byte{ownerPrefix}, id...)
raw := storage.Get(ctx, key)
if raw == nil {
panic("delete:" + ErrNotExist)
return
}
owner := raw.([]byte)
@ -217,7 +217,7 @@ func AddNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
stKey[0] = nodeAdminPrefix
if keyInList(ctx, adminKey, stKey) {
panic("addNodeAdmin: node admin has already been added")
return
}
putKeyInList(ctx, adminKey, stKey)
@ -252,7 +252,7 @@ func RemoveNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
stKey[0] = nodeAdminPrefix
if !keyInList(ctx, adminKey, stKey) {
panic("removeNodeAdmin: " + ErrNodeAdmNotExist)
return
}
deleteKeyFromList(ctx, adminKey, stKey)
@ -291,7 +291,7 @@ func AddNode(subnetID []byte, node interop.PublicKey) {
stKey[0] = nodePrefix
if keyInList(ctx, node, stKey) {
panic("addNode: node has already been added")
return
}
putKeyInList(ctx, node, stKey)
@ -330,7 +330,7 @@ func RemoveNode(subnetID []byte, node interop.PublicKey) {
stKey[0] = nodePrefix
if !keyInList(ctx, node, stKey) {
panic("removeNode: " + ErrNodeNotExist)
return
}
storage.Delete(ctx, append(stKey, node...))
@ -399,7 +399,7 @@ func AddClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.Publ
stKey = append(stKey, groupID...)
if keyInList(ctx, adminPublicKey, stKey) {
panic("addClientAdmin: client admin has already been added")
return
}
putKeyInList(ctx, adminPublicKey, stKey)
@ -441,7 +441,7 @@ func RemoveClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.P
stKey = append(stKey, groupID...)
if !keyInList(ctx, adminPublicKey, stKey) {
panic("removeClientAdmin: " + ErrClientAdmNotExist)
return
}
deleteKeyFromList(ctx, adminPublicKey, stKey)
@ -486,7 +486,7 @@ func AddUser(subnetID []byte, groupID []byte, userID []byte) {
stKey[0] = userPrefix
if keyInList(ctx, userID, stKey) {
panic("addUser: user has already been added")
return
}
putKeyInList(ctx, userID, stKey)
@ -531,7 +531,7 @@ func RemoveUser(subnetID []byte, groupID []byte, userID []byte) {
stKey[0] = userPrefix
if !keyInList(ctx, userID, stKey) {
panic("removeUser: " + ErrUserNotExist)
return
}
deleteKeyFromList(ctx, userID, stKey)

View file

@ -67,10 +67,9 @@ func TestSubnet_Delete(t *testing.T) {
cAcc := e.WithSigners(owner)
cAcc.InvokeFail(t, subnet.ErrInvalidSubnetID, "delete", []byte{1, 1, 1, 1})
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", []byte{1, 1, 1, 1, 1})
cAcc.Invoke(t, stackitem.Null{}, "delete", []byte{1, 1, 1, 1, 1})
cAcc.Invoke(t, stackitem.Null{}, "delete", id)
cAcc.InvokeFail(t, subnet.ErrNotExist, "get", id)
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", id)
}
func TestSubnet_AddNodeAdmin(t *testing.T) {
@ -94,7 +93,7 @@ func TestSubnet_AddNodeAdmin(t *testing.T) {
cOwner := e.WithSigners(owner)
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
cOwner.InvokeFail(t, "node admin has already been added", method, id, admPub)
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
}
func TestSubnet_RemoveNodeAdmin(t *testing.T) {
@ -117,10 +116,10 @@ func TestSubnet_RemoveNodeAdmin(t *testing.T) {
cOwner := e.WithSigners(owner)
cOwner.InvokeFail(t, subnet.ErrNodeAdmNotExist, method, id, admPub)
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
cOwner.Invoke(t, stackitem.Null{}, "addNodeAdmin", id, admPub)
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
cOwner.InvokeFail(t, subnet.ErrNodeAdmNotExist, method, id, admPub)
cOwner.Invoke(t, stackitem.Null{}, method, id, admPub)
}
func TestSubnet_AddNode(t *testing.T) {
@ -140,7 +139,7 @@ func TestSubnet_AddNode(t *testing.T) {
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
cOwn.InvokeFail(t, "node has already been added", method, id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
}
func TestSubnet_RemoveNode(t *testing.T) {
@ -162,7 +161,7 @@ func TestSubnet_RemoveNode(t *testing.T) {
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, subnet.ErrInvalidNode, method, id, nodePub[1:])
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, nodePub)
cOwn.InvokeFail(t, subnet.ErrNodeNotExist, method, id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, "addNode", id, nodePub)
cOwn.Invoke(t, stackitem.Null{}, method, id, nodePub)
@ -170,7 +169,7 @@ func TestSubnet_RemoveNode(t *testing.T) {
cAdm := cOwn.WithSigners(adm)
cOwn.Invoke(t, stackitem.Null{}, "addNodeAdmin", id, admPub)
cAdm.InvokeFail(t, subnet.ErrNodeNotExist, method, id, nodePub)
cAdm.Invoke(t, stackitem.Null{}, method, id, nodePub)
}
func TestSubnet_NodeAllowed(t *testing.T) {
@ -212,7 +211,7 @@ func TestSubnet_AddClientAdmin(t *testing.T) {
cOwn.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
cOwn.InvokeFail(t, "client admin has already been added", method, id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
}
func TestSubnet_RemoveClientAdmin(t *testing.T) {
@ -232,7 +231,7 @@ func TestSubnet_RemoveClientAdmin(t *testing.T) {
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, admPub)
cOwn.InvokeFail(t, subnet.ErrInvalidAdmin, method, id, groupId, admPub[1:])
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, admPub)
cOwn.InvokeFail(t, subnet.ErrClientAdmNotExist, method, id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, admPub)
}
@ -260,7 +259,7 @@ func TestSubnet_AddUser(t *testing.T) {
cAdm := e.WithSigners(adm)
cAdm.Invoke(t, stackitem.Null{}, method, id, groupId, user)
cOwn.InvokeFail(t, "user has already been added", method, id, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
}
func TestSubnet_RemoveUser(t *testing.T) {
@ -281,14 +280,14 @@ func TestSubnet_RemoveUser(t *testing.T) {
cOwn.InvokeFail(t, subnet.ErrInvalidSubnetID, method, []byte{0, 0, 0, 0}, groupId, user)
cOwn.InvokeFail(t, subnet.ErrNotExist, method, []byte{0, 0, 0, 0, 0}, groupId, user)
cOwn.InvokeFail(t, subnet.ErrUserNotExist, method, id, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, "addUser", id, groupId, user)
cOwn.Invoke(t, stackitem.Null{}, method, id, groupId, user)
cAdm := cOwn.WithSigners(adm)
cOwn.Invoke(t, stackitem.Null{}, "addClientAdmin", id, groupId, admPub)
cAdm.InvokeFail(t, subnet.ErrUserNotExist, method, id, groupId, user)
cAdm.Invoke(t, stackitem.Null{}, method, id, groupId, user)
}
func TestSubnet_UserAllowed(t *testing.T) {