2019-12-23 14:05:34 +00:00
|
|
|
package compiler_test
|
2018-04-02 15:04:42 +00:00
|
|
|
|
|
|
|
import (
|
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-06-29 08:25:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-04-15 14:13:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
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{}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 12:11:14 +00:00
|
|
|
func evalWithoutStackChecks(t *testing.T, src string, result interface{}) {
|
|
|
|
v := vmAndCompile(t, src)
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
assertResult(t, v, result)
|
|
|
|
}
|
|
|
|
|
2018-04-02 15:04:42 +00:00
|
|
|
func eval(t *testing.T, src string, result interface{}) {
|
2018-04-10 09:45:31 +00:00
|
|
|
vm := vmAndCompile(t, src)
|
2019-10-22 10:44:14 +00:00
|
|
|
err := vm.Run()
|
|
|
|
require.NoError(t, err)
|
2020-03-06 12:11:14 +00:00
|
|
|
assert.Equal(t, 1, vm.Estack().Len(), "stack contains unexpected items")
|
2018-04-10 09:45:31 +00:00
|
|
|
assertResult(t, vm, result)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
vm.LoadArgs(op, args)
|
2019-10-22 10:44:14 +00:00
|
|
|
err := vm.Run()
|
|
|
|
require.NoError(t, err)
|
2020-03-06 12:11:14 +00:00
|
|
|
assert.Equal(t, 1, vm.Estack().Len(), "stack contains unexpected items")
|
2018-04-10 09:45:31 +00:00
|
|
|
assertResult(t, vm, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertResult(t *testing.T, vm *vm.VM, result interface{}) {
|
|
|
|
assert.Equal(t, result, vm.PopResult())
|
|
|
|
assert.Equal(t, 0, vm.Astack().Len())
|
|
|
|
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()
|
2019-12-18 16:49:56 +00:00
|
|
|
vm.RegisterInteropGetter(storePlugin.getInterop)
|
2018-04-10 09:45:31 +00:00
|
|
|
|
2019-10-29 10:02:54 +00:00
|
|
|
b, err := compiler.Compile(strings.NewReader(src))
|
2020-02-29 15:55:16 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-02 15:04:42 +00:00
|
|
|
vm.Load(b)
|
2020-04-05 15:05:40 +00:00
|
|
|
return vm, storePlugin
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
type storagePlugin struct {
|
2019-12-18 16:49:56 +00:00
|
|
|
mem map[string][]byte
|
|
|
|
interops map[uint32]vm.InteropFunc
|
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),
|
|
|
|
interops: make(map[uint32]vm.InteropFunc),
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
2020-06-10 08:55:56 +00:00
|
|
|
s.interops[emit.InteropNameToID([]byte("System.Storage.Get"))] = s.Get
|
|
|
|
s.interops[emit.InteropNameToID([]byte("System.Storage.Put"))] = s.Put
|
|
|
|
s.interops[emit.InteropNameToID([]byte("System.Storage.GetContext"))] = s.GetContext
|
2020-06-10 08:49:39 +00:00
|
|
|
s.interops[emit.InteropNameToID([]byte("System.Runtime.Notify"))] = s.Notify
|
2019-12-18 16:49:56 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storagePlugin) getInterop(id uint32) *vm.InteropFuncPrice {
|
|
|
|
f := s.interops[id]
|
|
|
|
if f != nil {
|
|
|
|
return &vm.InteropFuncPrice{Func: f, Price: 1}
|
|
|
|
}
|
|
|
|
return nil
|
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-06-29 08:25:32 +00:00
|
|
|
name := string(v.Estack().Pop().Bytes())
|
|
|
|
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
|
|
|
|
}
|