[#122] subnet: implement delete method

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-18 15:24:32 +03:00 committed by Alex Vanin
parent ed6f90c180
commit 870db4a81a
2 changed files with 43 additions and 0 deletions

View file

@ -100,6 +100,26 @@ func Get(id []byte) []byte {
return raw.([]byte) return raw.([]byte)
} }
// Delete deletes subnet with the specified id.
func Delete(id []byte) {
ctx := storage.GetContext()
key := append([]byte{ownerPrefix}, id...)
raw := storage.Get(ctx, key)
if raw == nil {
panic("delete:" + ErrNotExist)
}
owner := raw.([]byte)
if !runtime.CheckWitness(owner) {
panic("delete: owner witness check failed")
}
storage.Delete(ctx, key)
key[0] = infoPrefix
storage.Delete(ctx, key)
}
// Update method updates contract source code and manifest. Can be invoked // Update method updates contract source code and manifest. Can be invoked
// only by committee. // only by committee.
func Update(script []byte, manifest []byte, data interface{}) { func Update(script []byte, manifest []byte, data interface{}) {

View file

@ -57,3 +57,26 @@ func TestSubnet_Put(t *testing.T) {
cAcc.Invoke(t, stackitem.NewBuffer(info), "get", id) cAcc.Invoke(t, stackitem.NewBuffer(info), "get", id)
cBoth.InvokeFail(t, subnet.ErrAlreadyExists, "put", id, pub, info) cBoth.InvokeFail(t, subnet.ErrAlreadyExists, "put", id, pub, info)
} }
func TestSubnet_Delete(t *testing.T) {
e := newSubnetInvoker(t)
acc := e.NewAccount(t)
pub, ok := vm.ParseSignatureContract(acc.Script())
require.True(t, ok)
id := make([]byte, 4)
binary.LittleEndian.PutUint32(id, 123)
info := randomBytes(10)
cBoth := e.WithSigners(e.Committee, acc)
cBoth.Invoke(t, stackitem.Null{}, "put", id, pub, info)
e.InvokeFail(t, "witness check failed", "delete", id)
cAcc := e.WithSigners(acc)
cAcc.InvokeFail(t, subnet.ErrNotExist, "delete", []byte{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)
}