Skip to content

Commit acc627e

Browse files
author
Tural Devrishev
committed
core: add tests for local extension of System.Storage.* interops
A part of #3979. Signed-off-by: Tural Devrishev <[email protected]>
1 parent 24cf029 commit acc627e

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

pkg/compiler/syscall_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"testing"
1010

1111
"github.com/nspcc-dev/neo-go/pkg/compiler"
12+
"github.com/nspcc-dev/neo-go/pkg/config"
1213
"github.com/nspcc-dev/neo-go/pkg/core"
14+
"github.com/nspcc-dev/neo-go/pkg/core/block"
1315
"github.com/nspcc-dev/neo-go/pkg/core/interop"
1416
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
1517
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
@@ -95,12 +97,21 @@ func TestSyscallExecution(t *testing.T) {
9597
"storage.Get": {interopnames.SystemStorageGet, []string{sctx, b}, false},
9698
"storage.GetContext": {interopnames.SystemStorageGetContext, nil, false},
9799
"storage.GetReadOnlyContext": {interopnames.SystemStorageGetReadOnlyContext, nil, false},
100+
"storage.LocalDelete": {interopnames.SystemStorageLocalDelete, []string{b}, true},
101+
"storage.LocalFind": {interopnames.SystemStorageLocalFind, []string{b, "storage.None"}, false},
102+
"storage.LocalGet": {interopnames.SystemStorageLocalGet, []string{b}, false},
103+
"storage.LocalPut": {interopnames.SystemStorageLocalPut, []string{b, b}, true},
98104
"storage.Put": {interopnames.SystemStoragePut, []string{sctx, b, b}, true},
99105
"storage.ConvertContextToReadOnly": {interopnames.SystemStorageAsReadOnly, []string{sctx}, false},
100106
"crypto.CheckMultisig": {interopnames.SystemCryptoCheckMultisig, []string{pubs, sigs}, false},
101107
"crypto.CheckSig": {interopnames.SystemCryptoCheckSig, []string{pub, sig}, false},
102108
}
103-
ic := &interop.Context{}
109+
ic := &interop.Context{
110+
Hardforks: map[string]uint32{
111+
config.HFFaun.String(): 0,
112+
},
113+
Block: &block.Block{},
114+
}
104115
core.SpawnVM(ic) // set Functions field
105116
for _, fs := range ic.Functions {
106117
// It will be set in test and we want to fail if calling invalid syscall.

pkg/core/interop/storage/storage_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package storage_test
22

33
import (
44
"math/big"
5+
"strings"
56
"testing"
67

8+
"github.com/nspcc-dev/neo-go/pkg/compiler"
9+
"github.com/nspcc-dev/neo-go/pkg/config"
710
"github.com/nspcc-dev/neo-go/pkg/config/limits"
811
"github.com/nspcc-dev/neo-go/pkg/core"
912
"github.com/nspcc-dev/neo-go/pkg/core/block"
@@ -14,6 +17,7 @@ import (
1417
"github.com/nspcc-dev/neo-go/pkg/core/state"
1518
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
1619
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
20+
"github.com/nspcc-dev/neo-go/pkg/neotest"
1721
"github.com/nspcc-dev/neo-go/pkg/neotest/chain"
1822
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
1923
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
@@ -334,6 +338,52 @@ func createVMAndContractState(t testing.TB) (*vm.VM, *state.Contract, *interop.C
334338
return v, contractState, context, chain
335339
}
336340

341+
func TestStorage_LocalInteropAPI(t *testing.T) {
342+
bc, acc := chain.NewSingleWithCustomConfig(t, func(c *config.Blockchain) {
343+
c.Hardforks = map[string]uint32{
344+
config.HFFaun.String(): 6,
345+
}
346+
})
347+
e := neotest.NewExecutor(t, bc, acc, acc)
348+
349+
src := `package foo
350+
import (
351+
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
352+
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
353+
)
354+
355+
func LocalPut() {
356+
storage.LocalPut([]byte("key"), []byte("val"))
357+
}
358+
359+
func LocalGet() any {
360+
return storage.LocalGet([]byte("key"))
361+
}
362+
363+
func LocalDelete() {
364+
storage.LocalDelete([]byte("key"))
365+
}
366+
367+
func LocalFind() bool {
368+
return iterator.Next(storage.LocalFind([]byte("key"), storage.None))
369+
}`
370+
371+
ctr := neotest.CompileSource(t, e.Validator.ScriptHash(), strings.NewReader(src), &compiler.Options{
372+
Name: "testpolicy_contract",
373+
})
374+
e.DeployContract(t, ctr, nil)
375+
376+
ctrInvoker := e.NewInvoker(ctr.Hash, e.Committee)
377+
ctrInvoker.InvokeFail(t, "System.Storage.Local.Put failed: syscall not found", "localPut")
378+
ctrInvoker.InvokeFail(t, "System.Storage.Local.Get failed: syscall not found", "localGet")
379+
ctrInvoker.InvokeFail(t, "System.Storage.Local.Delete failed: syscall not found", "localDelete")
380+
ctrInvoker.InvokeFail(t, "System.Storage.Local.Find failed: syscall not found", "localFind")
381+
ctrInvoker.Invoke(t, nil, "localPut")
382+
ctrInvoker.Invoke(t, stackitem.Make([]byte("val")), "localGet")
383+
ctrInvoker.Invoke(t, nil, "localDelete")
384+
ctrInvoker.Invoke(t, stackitem.Make(false), "localFind")
385+
}
386+
337387
func TestStorage_LocalErrors(t *testing.T) {
338388
_, ic, _ := createVM(t)
339389
for _, f := range []func(*interop.Context) error{istorage.LocalDelete, istorage.LocalFind, istorage.LocalGet, istorage.LocalPut} {

0 commit comments

Comments
 (0)