From 870db4a81a3298db530e46caf9d1b60faa2b7b55 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 18 Nov 2021 15:24:32 +0300 Subject: [PATCH] [#122] subnet: implement `delete` method Signed-off-by: Evgenii Stratonikov --- subnet/subnet_contract.go | 20 ++++++++++++++++++++ tests/subnet_test.go | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/subnet/subnet_contract.go b/subnet/subnet_contract.go index 5d5224b..e3ca891 100644 --- a/subnet/subnet_contract.go +++ b/subnet/subnet_contract.go @@ -100,6 +100,26 @@ func Get(id []byte) []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 // only by committee. func Update(script []byte, manifest []byte, data interface{}) { diff --git a/tests/subnet_test.go b/tests/subnet_test.go index 2862db0..c42d5ac 100644 --- a/tests/subnet_test.go +++ b/tests/subnet_test.go @@ -57,3 +57,26 @@ func TestSubnet_Put(t *testing.T) { cAcc.Invoke(t, stackitem.NewBuffer(info), "get", id) 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) +}