From 293ca36ff4cc397f53500a7642c4acbf4b1c8455 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 24 Nov 2021 21:09:13 +0300 Subject: [PATCH] [#174] subnet: Do not panic while doing operation that are already done Signed-off-by: Pavel Karpy --- subnet/subnet_contract.go | 18 +++++++++--------- tests/subnet_test.go | 25 ++++++++++++------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/subnet/subnet_contract.go b/subnet/subnet_contract.go index 30b134a..595da16 100644 --- a/subnet/subnet_contract.go +++ b/subnet/subnet_contract.go @@ -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) diff --git a/tests/subnet_test.go b/tests/subnet_test.go index 50c7124..b44dfc0 100644 --- a/tests/subnet_test.go +++ b/tests/subnet_test.go @@ -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) {