2019-12-23 14:05:34 +00:00
|
|
|
package compiler_test
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-28 13:38:00 +00:00
|
|
|
"errors"
|
2018-04-10 09:45:31 +00:00
|
|
|
"fmt"
|
2018-04-02 15:04:42 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-29 08:25:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-07-24 10:40:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-04-02 15:04:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-22 10:44:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-02 15:04:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
src string
|
|
|
|
result interface{}
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:40:54 +00:00
|
|
|
// testMainIdent is a method invoked in tests by default.
|
|
|
|
const testMainIdent = "Main"
|
|
|
|
|
2019-10-18 15:36:54 +00:00
|
|
|
func runTestCases(t *testing.T, tcases []testCase) {
|
2019-08-20 17:37:06 +00:00
|
|
|
for _, tcase := range tcases {
|
2019-09-09 08:23:27 +00:00
|
|
|
t.Run(tcase.name, func(t *testing.T) { eval(t, tcase.src, tcase.result) })
|
2019-08-20 17:37:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func eval(t *testing.T, src string, result interface{}) {
|
2021-12-02 14:44:53 +00:00
|
|
|
vm, _ := vmAndCompileInterop(t, src)
|
2022-01-12 10:11:17 +00:00
|
|
|
runAndCheck(t, vm, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAndCheck(t *testing.T, v *vm.VM, result interface{}) {
|
|
|
|
err := v.Run()
|
2019-10-22 10:44:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-12 10:11:17 +00:00
|
|
|
assert.Equal(t, 1, v.Estack().Len(), "stack contains unexpected items")
|
|
|
|
assertResult(t, v, result)
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func evalWithArgs(t *testing.T, src string, op []byte, args []stackitem.Item, result interface{}) {
|
2018-04-10 09:45:31 +00:00
|
|
|
vm := vmAndCompile(t, src)
|
2021-11-19 19:25:14 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
vm.Estack().PushVal(args)
|
|
|
|
}
|
|
|
|
if op != nil {
|
|
|
|
vm.Estack().PushVal(op)
|
|
|
|
}
|
2022-01-12 10:11:17 +00:00
|
|
|
runAndCheck(t, vm, result)
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertResult(t *testing.T, vm *vm.VM, result interface{}) {
|
|
|
|
assert.Equal(t, result, vm.PopResult())
|
|
|
|
assert.Equal(t, 0, vm.Istack().Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmAndCompile(t *testing.T, src string) *vm.VM {
|
2020-04-05 15:05:40 +00:00
|
|
|
v, _ := vmAndCompileInterop(t, src)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) {
|
2019-10-22 10:44:14 +00:00
|
|
|
vm := vm.New()
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
storePlugin := newStoragePlugin()
|
2020-07-28 13:38:00 +00:00
|
|
|
vm.GasLimit = -1
|
|
|
|
vm.SyscallHandler = storePlugin.syscallHandler
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2021-07-26 12:34:07 +00:00
|
|
|
b, di, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2020-07-29 14:20:00 +00:00
|
|
|
|
2021-05-24 14:03:31 +00:00
|
|
|
storePlugin.info = di
|
2021-12-08 19:33:03 +00:00
|
|
|
invokeMethod(t, testMainIdent, b.Script, vm, di)
|
2020-04-05 15:05:40 +00:00
|
|
|
return vm, storePlugin
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 10:40:54 +00:00
|
|
|
func invokeMethod(t *testing.T, method string, script []byte, v *vm.VM, di *compiler.DebugInfo) {
|
|
|
|
mainOffset := -1
|
|
|
|
initOffset := -1
|
|
|
|
for i := range di.Methods {
|
|
|
|
switch di.Methods[i].ID {
|
|
|
|
case method:
|
|
|
|
mainOffset = int(di.Methods[i].Range.Start)
|
|
|
|
case manifest.MethodInit:
|
|
|
|
initOffset = int(di.Methods[i].Range.Start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, mainOffset >= 0)
|
2020-12-29 10:45:49 +00:00
|
|
|
v.LoadScriptWithFlags(script, callflag.All)
|
2021-11-19 13:46:29 +00:00
|
|
|
v.Context().Jump(mainOffset)
|
2020-07-24 10:40:54 +00:00
|
|
|
if initOffset >= 0 {
|
2021-11-19 14:15:30 +00:00
|
|
|
v.Call(initOffset)
|
2020-07-24 10:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
type storagePlugin struct {
|
2021-05-24 14:03:31 +00:00
|
|
|
info *compiler.DebugInfo
|
2019-12-18 16:49:56 +00:00
|
|
|
mem map[string][]byte
|
2020-07-28 13:38:00 +00:00
|
|
|
interops map[uint32]func(v *vm.VM) error
|
2020-06-29 08:25:32 +00:00
|
|
|
events []state.NotificationEvent
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newStoragePlugin() *storagePlugin {
|
2019-12-18 16:49:56 +00:00
|
|
|
s := &storagePlugin{
|
|
|
|
mem: make(map[string][]byte),
|
2020-07-28 13:38:00 +00:00
|
|
|
interops: make(map[uint32]func(v *vm.VM) error),
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
2020-08-14 10:50:52 +00:00
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemStorageGet))] = s.Get
|
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemStoragePut))] = s.Put
|
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemStorageGetContext))] = s.GetContext
|
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemRuntimeNotify))] = s.Notify
|
2021-03-04 10:26:16 +00:00
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemRuntimeGetTime))] = s.GetTime
|
2019-12-18 16:49:56 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:38:00 +00:00
|
|
|
func (s *storagePlugin) syscallHandler(v *vm.VM, id uint32) error {
|
2019-12-18 16:49:56 +00:00
|
|
|
f := s.interops[id]
|
|
|
|
if f != nil {
|
2020-07-28 13:38:00 +00:00
|
|
|
if !v.AddGas(1) {
|
|
|
|
return errors.New("insufficient amount of gas")
|
|
|
|
}
|
|
|
|
return f(v)
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
2020-07-28 13:38:00 +00:00
|
|
|
return errors.New("syscall not found")
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 15:05:40 +00:00
|
|
|
func (s *storagePlugin) Notify(v *vm.VM) error {
|
2020-07-29 08:18:51 +00:00
|
|
|
name := v.Estack().Pop().String()
|
2020-06-29 08:25:32 +00:00
|
|
|
item := stackitem.NewArray(v.Estack().Pop().Array())
|
|
|
|
s.events = append(s.events, state.NotificationEvent{
|
|
|
|
Name: name,
|
|
|
|
Item: item,
|
|
|
|
})
|
2020-04-05 15:05:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
func (s *storagePlugin) Delete(vm *vm.VM) error {
|
|
|
|
vm.Estack().Pop()
|
|
|
|
key := vm.Estack().Pop().Bytes()
|
|
|
|
delete(s.mem, string(key))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storagePlugin) Put(vm *vm.VM) error {
|
|
|
|
vm.Estack().Pop()
|
|
|
|
key := vm.Estack().Pop().Bytes()
|
|
|
|
value := vm.Estack().Pop().Bytes()
|
|
|
|
s.mem[string(key)] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storagePlugin) Get(vm *vm.VM) error {
|
|
|
|
vm.Estack().Pop()
|
|
|
|
item := vm.Estack().Pop().Bytes()
|
|
|
|
if val, ok := s.mem[string(item)]; ok {
|
|
|
|
vm.Estack().PushVal(val)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("could not find %+v", item)
|
|
|
|
}
|
2018-05-06 06:03:26 +00:00
|
|
|
|
|
|
|
func (s *storagePlugin) GetContext(vm *vm.VM) error {
|
|
|
|
// Pushing anything on the stack here will work. This is just to satisfy
|
|
|
|
// the compiler, thinking it has pushed the context ^^.
|
|
|
|
vm.Estack().PushVal(10)
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-04 10:26:16 +00:00
|
|
|
|
|
|
|
func (s *storagePlugin) GetTime(vm *vm.VM) error {
|
|
|
|
// Pushing anything on the stack here will work. This is just to satisfy
|
|
|
|
// the compiler, thinking it has pushed the context ^^.
|
|
|
|
vm.Estack().PushVal(4)
|
|
|
|
return nil
|
|
|
|
}
|