2019-12-23 14:05:34 +00:00
|
|
|
package compiler_test
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-16 12:28:34 +00:00
|
|
|
"math/big"
|
2018-04-10 09:45:31 +00:00
|
|
|
"testing"
|
2020-04-05 15:05:40 +00:00
|
|
|
|
2021-02-25 12:12:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2021-01-12 10:39:31 +00:00
|
|
|
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2020-12-08 12:37:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-01-12 10:39:31 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2021-02-25 12:12:16 +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"
|
2020-04-05 15:05:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-10 09:45:31 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 12:37:03 +00:00
|
|
|
// Checks that changes in `smartcontract` are reflected in compiler interop package.
|
|
|
|
func TestCallFlags(t *testing.T) {
|
2020-12-29 10:45:49 +00:00
|
|
|
require.EqualValues(t, contract.ReadStates, callflag.ReadStates)
|
|
|
|
require.EqualValues(t, contract.WriteStates, callflag.WriteStates)
|
|
|
|
require.EqualValues(t, contract.AllowCall, callflag.AllowCall)
|
|
|
|
require.EqualValues(t, contract.AllowNotify, callflag.AllowNotify)
|
|
|
|
require.EqualValues(t, contract.States, callflag.States)
|
|
|
|
require.EqualValues(t, contract.ReadOnly, callflag.ReadOnly)
|
|
|
|
require.EqualValues(t, contract.All, callflag.All)
|
|
|
|
require.EqualValues(t, contract.NoneFlag, callflag.NoneFlag)
|
2020-12-08 12:37:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 10:39:31 +00:00
|
|
|
func TestFindFlags(t *testing.T) {
|
|
|
|
require.EqualValues(t, storage.None, istorage.FindDefault)
|
|
|
|
require.EqualValues(t, storage.KeysOnly, istorage.FindKeysOnly)
|
|
|
|
require.EqualValues(t, storage.RemovePrefix, istorage.FindRemovePrefix)
|
|
|
|
require.EqualValues(t, storage.ValuesOnly, istorage.FindValuesOnly)
|
2021-01-12 12:09:05 +00:00
|
|
|
require.EqualValues(t, storage.DeserializeValues, istorage.FindDeserialize)
|
2021-01-12 12:32:27 +00:00
|
|
|
require.EqualValues(t, storage.PickField0, istorage.FindPick0)
|
|
|
|
require.EqualValues(t, storage.PickField1, istorage.FindPick1)
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 09:45:31 +00:00
|
|
|
func TestStoragePutGet(t *testing.T) {
|
|
|
|
src := `
|
|
|
|
package foo
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
func Main() string {
|
2018-05-06 06:03:26 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
key := []byte("token")
|
|
|
|
storage.Put(ctx, key, []byte("foo"))
|
2018-04-10 09:45:31 +00:00
|
|
|
x := storage.Get(ctx, key)
|
|
|
|
return x.(string)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
eval(t, src, []byte("foo"))
|
|
|
|
}
|
2020-04-05 15:05:40 +00:00
|
|
|
|
|
|
|
func TestNotify(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
func Main(arg int) {
|
2020-06-29 08:25:32 +00:00
|
|
|
runtime.Notify("Event1", arg, "sum", arg+1)
|
2020-04-05 15:05:40 +00:00
|
|
|
runtime.Notify("single")
|
|
|
|
}`
|
|
|
|
|
|
|
|
v, s := vmAndCompileInterop(t, src)
|
|
|
|
v.Estack().PushVal(11)
|
|
|
|
|
|
|
|
require.NoError(t, v.Run())
|
2020-06-29 08:25:32 +00:00
|
|
|
require.Equal(t, 2, len(s.events))
|
2020-04-05 15:05:40 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
exp0 := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(11)), stackitem.NewByteArray([]byte("sum")), stackitem.NewBigInteger(big.NewInt(12))}
|
2020-06-29 08:25:32 +00:00
|
|
|
assert.Equal(t, "Event1", s.events[0].Name)
|
|
|
|
assert.Equal(t, exp0, s.events[0].Item.Value())
|
|
|
|
assert.Equal(t, "single", s.events[1].Name)
|
|
|
|
assert.Equal(t, []stackitem.Item{}, s.events[1].Item.Value())
|
2020-04-05 15:05:40 +00:00
|
|
|
}
|
2021-02-25 12:12:16 +00:00
|
|
|
|
|
|
|
func TestSyscallInGlobalInit(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
|
|
|
var a = binary.Base58Decode([]byte("5T"))
|
|
|
|
func Main() []byte {
|
|
|
|
return a
|
|
|
|
}`
|
|
|
|
v, s := vmAndCompileInterop(t, src)
|
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemBinaryBase58Decode))] = func(v *vm.VM) error {
|
|
|
|
s := v.Estack().Pop().Value().([]byte)
|
|
|
|
require.Equal(t, "5T", string(s))
|
|
|
|
v.Estack().PushVal([]byte{1, 2})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
require.NoError(t, v.Run())
|
|
|
|
require.Equal(t, []byte{1, 2}, v.Estack().Pop().Value())
|
|
|
|
}
|
2021-03-02 11:15:25 +00:00
|
|
|
|
|
|
|
func TestOpcode(t *testing.T) {
|
|
|
|
t.Run("1 argument", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
func abs(a int) int {
|
|
|
|
return neogointernal.Opcode1("ABS", a).(int)
|
|
|
|
}
|
|
|
|
func Main() int {
|
|
|
|
return abs(-42)
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(42))
|
|
|
|
})
|
|
|
|
t.Run("2 arguments", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
|
|
|
|
func add3(a, b, c int) int {
|
|
|
|
return neogointernal.Opcode2("SUB", a,
|
|
|
|
neogointernal.Opcode2("SUB", b, c).(int)).(int)
|
|
|
|
}
|
|
|
|
func Main() int {
|
|
|
|
return add3(53, 12, 1)
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(42))
|
|
|
|
})
|
2021-03-02 11:19:13 +00:00
|
|
|
t.Run("POW", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() int {
|
|
|
|
return math.Pow(2, math.Pow(3, 2))
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(512))
|
|
|
|
})
|
|
|
|
t.Run("SRQT", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() int {
|
|
|
|
return math.Sqrt(math.Sqrt(101)) // == sqrt(10) == 3
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(3))
|
|
|
|
})
|
2021-03-02 11:56:32 +00:00
|
|
|
t.Run("SIGN", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() []int {
|
|
|
|
signs := make([]int, 3)
|
|
|
|
signs[0] = math.Sign(-123)
|
|
|
|
signs[1] = math.Sign(0)
|
|
|
|
signs[2] = math.Sign(42)
|
|
|
|
return signs
|
|
|
|
}`
|
|
|
|
eval(t, src, []stackitem.Item{
|
|
|
|
stackitem.Make(-1),
|
|
|
|
stackitem.Make(0),
|
|
|
|
stackitem.Make(1),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("ABS", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() int {
|
|
|
|
return math.Abs(-3)
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(3))
|
|
|
|
})
|
|
|
|
t.Run("MAX", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() int {
|
|
|
|
return math.Max(1, 2) + math.Max(8, 3)
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(10))
|
|
|
|
})
|
|
|
|
t.Run("MIN", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() int {
|
|
|
|
return math.Min(1, 2) + math.Min(8, 3)
|
|
|
|
}`
|
|
|
|
eval(t, src, big.NewInt(4))
|
|
|
|
})
|
|
|
|
t.Run("WITHIN", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() []bool {
|
|
|
|
r := make([]bool, 5)
|
|
|
|
r[0] = math.Within(2, 3, 5)
|
|
|
|
r[1] = math.Within(3, 3, 5)
|
|
|
|
r[2] = math.Within(4, 3, 5)
|
|
|
|
r[3] = math.Within(5, 3, 5)
|
|
|
|
r[4] = math.Within(6, 3, 5)
|
|
|
|
return r
|
|
|
|
}`
|
|
|
|
eval(t, src, []stackitem.Item{
|
|
|
|
stackitem.Make(false),
|
|
|
|
stackitem.Make(true),
|
|
|
|
stackitem.Make(true),
|
|
|
|
stackitem.Make(false),
|
|
|
|
stackitem.Make(false),
|
|
|
|
})
|
|
|
|
})
|
2021-03-02 11:15:25 +00:00
|
|
|
}
|