2019-12-23 14:05:34 +00:00
|
|
|
package compiler_test
|
2018-04-10 09:45:31 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-16 12:58:51 +00:00
|
|
|
"bytes"
|
2021-03-04 15:09:13 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-04-16 12:28:34 +00:00
|
|
|
"math/big"
|
2021-03-04 15:09:13 +00:00
|
|
|
"strings"
|
2018-04-10 09:45:31 +00:00
|
|
|
"testing"
|
2020-04-05 15:05:40 +00:00
|
|
|
|
2021-03-04 15:09:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/compiler"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
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"
|
2022-10-05 07:46:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2022-06-24 14:01:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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)
|
2023-03-24 16:32:06 +00:00
|
|
|
require.EqualValues(t, storage.Backwards, istorage.FindBackwards)
|
2021-01-12 10:39:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 15:09:13 +00:00
|
|
|
type syscallTestCase struct {
|
|
|
|
method string
|
|
|
|
params []string
|
|
|
|
isVoid bool
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// This test ensures that our wrappers have the necessary number of parameters
|
|
|
|
// and execute the appropriate syscall. Because of lack of typing (compared to native contracts),
|
2021-03-04 15:09:13 +00:00
|
|
|
// parameter types can't be checked.
|
|
|
|
func TestSyscallExecution(t *testing.T) {
|
|
|
|
b := `[]byte{1}`
|
|
|
|
u160 := `interop.Hash160("aaaaaaaaaaaaaaaaaaaa")`
|
|
|
|
pub := `interop.PublicKey("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")`
|
|
|
|
pubs := "[]interop.PublicKey{ " + pub + "}"
|
|
|
|
sig := `interop.Signature("aaaaaa")`
|
|
|
|
sigs := "[]interop.Signature{" + sig + "}"
|
|
|
|
sctx := "storage.Context{}"
|
|
|
|
interops := map[string]syscallTestCase{
|
2021-03-09 15:11:21 +00:00
|
|
|
"contract.Call": {interopnames.SystemContractCall, []string{u160, `"m"`, "1", "3"}, false},
|
|
|
|
"contract.CreateMultisigAccount": {interopnames.SystemContractCreateMultisigAccount, []string{"1", pubs}, false},
|
|
|
|
"contract.CreateStandardAccount": {interopnames.SystemContractCreateStandardAccount, []string{pub}, false},
|
|
|
|
"contract.GetCallFlags": {interopnames.SystemContractGetCallFlags, nil, false},
|
|
|
|
"iterator.Next": {interopnames.SystemIteratorNext, []string{"iterator.Iterator{}"}, false},
|
|
|
|
"iterator.Value": {interopnames.SystemIteratorValue, []string{"iterator.Iterator{}"}, false},
|
2021-04-29 08:33:21 +00:00
|
|
|
"runtime.BurnGas": {interopnames.SystemRuntimeBurnGas, []string{"1"}, true},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.CheckWitness": {interopnames.SystemRuntimeCheckWitness, []string{b}, false},
|
2023-07-08 11:38:00 +00:00
|
|
|
"runtime.CurrentSigners": {interopnames.SystemRuntimeCurrentSigners, nil, false},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.GasLeft": {interopnames.SystemRuntimeGasLeft, nil, false},
|
2022-04-21 14:53:11 +00:00
|
|
|
"runtime.GetAddressVersion": {interopnames.SystemRuntimeGetAddressVersion, nil, false},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.GetCallingScriptHash": {interopnames.SystemRuntimeGetCallingScriptHash, nil, false},
|
|
|
|
"runtime.GetEntryScriptHash": {interopnames.SystemRuntimeGetEntryScriptHash, nil, false},
|
|
|
|
"runtime.GetExecutingScriptHash": {interopnames.SystemRuntimeGetExecutingScriptHash, nil, false},
|
|
|
|
"runtime.GetInvocationCounter": {interopnames.SystemRuntimeGetInvocationCounter, nil, false},
|
2021-07-06 11:20:25 +00:00
|
|
|
"runtime.GetNetwork": {interopnames.SystemRuntimeGetNetwork, nil, false},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.GetNotifications": {interopnames.SystemRuntimeGetNotifications, []string{u160}, false},
|
2021-07-14 12:05:28 +00:00
|
|
|
"runtime.GetRandom": {interopnames.SystemRuntimeGetRandom, nil, false},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.GetScriptContainer": {interopnames.SystemRuntimeGetScriptContainer, nil, false},
|
|
|
|
"runtime.GetTime": {interopnames.SystemRuntimeGetTime, nil, false},
|
|
|
|
"runtime.GetTrigger": {interopnames.SystemRuntimeGetTrigger, nil, false},
|
2022-09-29 21:05:50 +00:00
|
|
|
"runtime.LoadScript": {interopnames.SystemRuntimeLoadScript, []string{b, "0", b}, false},
|
2021-03-09 15:11:21 +00:00
|
|
|
"runtime.Log": {interopnames.SystemRuntimeLog, []string{`"msg"`}, true},
|
|
|
|
"runtime.Notify": {interopnames.SystemRuntimeNotify, []string{`"ev"`, "1"}, true},
|
|
|
|
"runtime.Platform": {interopnames.SystemRuntimePlatform, nil, false},
|
|
|
|
"storage.Delete": {interopnames.SystemStorageDelete, []string{sctx, b}, true},
|
|
|
|
"storage.Find": {interopnames.SystemStorageFind, []string{sctx, b, "storage.None"}, false},
|
|
|
|
"storage.Get": {interopnames.SystemStorageGet, []string{sctx, b}, false},
|
|
|
|
"storage.GetContext": {interopnames.SystemStorageGetContext, nil, false},
|
|
|
|
"storage.GetReadOnlyContext": {interopnames.SystemStorageGetReadOnlyContext, nil, false},
|
|
|
|
"storage.Put": {interopnames.SystemStoragePut, []string{sctx, b, b}, true},
|
|
|
|
"storage.ConvertContextToReadOnly": {interopnames.SystemStorageAsReadOnly, []string{sctx}, false},
|
2021-05-11 14:13:33 +00:00
|
|
|
"crypto.CheckMultisig": {interopnames.SystemCryptoCheckMultisig, []string{pubs, sigs}, false},
|
2021-05-11 13:32:09 +00:00
|
|
|
"crypto.CheckSig": {interopnames.SystemCryptoCheckSig, []string{pub, sig}, false},
|
2021-03-04 15:09:13 +00:00
|
|
|
}
|
|
|
|
ic := &interop.Context{}
|
|
|
|
core.SpawnVM(ic) // set Functions field
|
|
|
|
for _, fs := range ic.Functions {
|
2021-05-11 14:40:03 +00:00
|
|
|
// It will be set in test and we want to fail if calling invalid syscall.
|
|
|
|
fs.Func = nil
|
2021-03-04 15:09:13 +00:00
|
|
|
}
|
2022-01-16 12:58:51 +00:00
|
|
|
|
|
|
|
srcBuilder := bytes.NewBuffer(nil)
|
|
|
|
srcBuilder.WriteString(`package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
func unused() { var _ interop.Hash160 }
|
|
|
|
`)
|
|
|
|
for goName, tc := range interops {
|
|
|
|
realName := strings.ToTitle(strings.Replace(goName, ".", "", 1))
|
|
|
|
var tmpl string
|
|
|
|
if tc.isVoid {
|
|
|
|
tmpl = "func %s() { %s(%s) }\n"
|
|
|
|
} else {
|
2023-04-03 10:34:24 +00:00
|
|
|
tmpl = "func %s() any { return %s(%s) }\n"
|
2022-01-16 12:58:51 +00:00
|
|
|
}
|
|
|
|
srcBuilder.WriteString(fmt.Sprintf(tmpl, realName, goName, strings.Join(tc.params, ", ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
nf, di, err := compiler.CompileWithOptions("foo.go", srcBuilder, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-04 15:09:13 +00:00
|
|
|
for goName, tc := range interops {
|
|
|
|
t.Run(goName, func(t *testing.T) {
|
2022-01-16 12:58:51 +00:00
|
|
|
name := strings.ToTitle(strings.Replace(goName, ".", "", 1))
|
|
|
|
runSyscallTestCase(t, ic, name, nf.Script, di, tc)
|
2021-03-04 15:09:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
func runSyscallTestCase(t *testing.T, ic *interop.Context, realName string,
|
|
|
|
script []byte, debugInfo *compiler.DebugInfo, tc syscallTestCase) {
|
2021-03-04 15:09:13 +00:00
|
|
|
syscallID := interopnames.ToID([]byte(tc.method))
|
|
|
|
f := ic.GetFunction(syscallID)
|
|
|
|
require.NotNil(t, f)
|
|
|
|
require.Equal(t, f.ParamCount, len(tc.params))
|
|
|
|
called := false
|
|
|
|
f.Func = func(ic *interop.Context) error {
|
|
|
|
called = true
|
|
|
|
if ic.VM.Estack().Len() < f.ParamCount {
|
|
|
|
return errors.New("not enough parameters")
|
|
|
|
}
|
|
|
|
for i := 0; i < f.ParamCount; i++ {
|
|
|
|
ic.VM.Estack().Pop()
|
|
|
|
}
|
|
|
|
if !tc.isVoid {
|
|
|
|
ic.VM.Estack().PushVal(42)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer func() { f.Func = nil }()
|
|
|
|
|
2022-01-16 12:58:51 +00:00
|
|
|
invokeMethod(t, realName, script, ic.VM, debugInfo)
|
|
|
|
require.NoError(t, ic.VM.Run())
|
2021-03-04 15:09:13 +00:00
|
|
|
require.True(t, called)
|
|
|
|
if tc.isVoid {
|
2022-01-16 12:58:51 +00:00
|
|
|
require.Equal(t, 0, ic.VM.Estack().Len())
|
2021-03-04 15:09:13 +00:00
|
|
|
} else {
|
2022-01-16 12:58:51 +00:00
|
|
|
require.Equal(t, 1, ic.VM.Estack().Len())
|
|
|
|
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Value())
|
2021-03-04 15:09:13 +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")
|
|
|
|
}`
|
|
|
|
|
2022-08-19 11:54:42 +00:00
|
|
|
v, s, _ := vmAndCompileInterop(t, src)
|
2020-04-05 15:05:40 +00:00
|
|
|
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())
|
2021-06-02 08:46:54 +00:00
|
|
|
|
|
|
|
t.Run("long event name", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
func Main(arg int) {
|
|
|
|
runtime.Notify("long event12345678901234567890123")
|
|
|
|
}`
|
|
|
|
|
2021-07-26 12:34:07 +00:00
|
|
|
_, _, err := compiler.CompileWithOptions("foo.go", strings.NewReader(src), nil)
|
2021-06-02 08:46:54 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-04-05 15:05:40 +00:00
|
|
|
}
|
2021-02-25 12:12:16 +00:00
|
|
|
|
|
|
|
func TestSyscallInGlobalInit(t *testing.T) {
|
|
|
|
src := `package foo
|
2021-03-04 10:29:35 +00:00
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
var a = runtime.CheckWitness([]byte("5T"))
|
|
|
|
func Main() bool {
|
2021-02-25 12:12:16 +00:00
|
|
|
return a
|
|
|
|
}`
|
2022-08-19 11:54:42 +00:00
|
|
|
v, s, _ := vmAndCompileInterop(t, src)
|
2021-03-04 10:29:35 +00:00
|
|
|
s.interops[interopnames.ToID([]byte(interopnames.SystemRuntimeCheckWitness))] = func(v *vm.VM) error {
|
2021-02-25 12:12:16 +00:00
|
|
|
s := v.Estack().Pop().Value().([]byte)
|
|
|
|
require.Equal(t, "5T", string(s))
|
2021-03-04 10:29:35 +00:00
|
|
|
v.Estack().PushVal(true)
|
2021-02-25 12:12:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
require.NoError(t, v.Run())
|
2021-03-04 10:29:35 +00:00
|
|
|
require.Equal(t, true, v.Estack().Pop().Value())
|
2021-02-25 12:12:16 +00:00
|
|
|
}
|
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),
|
|
|
|
})
|
|
|
|
})
|
2022-05-12 11:23:22 +00:00
|
|
|
t.Run("MODMUL", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() []int {
|
|
|
|
r := make([]int, 5)
|
|
|
|
r[0] = math.ModMul(3, 4, 5)
|
|
|
|
r[1] = math.ModMul(-3, 4, 5)
|
|
|
|
r[2] = math.ModMul(3, 4, -5)
|
|
|
|
r[3] = math.ModMul(-3, 4, -5)
|
|
|
|
r[4] = math.ModMul(0, 4, 5)
|
|
|
|
return r
|
|
|
|
}`
|
|
|
|
eval(t, src, []stackitem.Item{
|
|
|
|
stackitem.Make(2),
|
|
|
|
stackitem.Make(3),
|
|
|
|
stackitem.Make(2),
|
|
|
|
stackitem.Make(3),
|
|
|
|
stackitem.Make(0),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("MODPOW", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop/math"
|
|
|
|
func Main() []int {
|
|
|
|
r := make([]int, 5)
|
|
|
|
r[0] = math.ModPow(3, 4, 5)
|
|
|
|
r[1] = math.ModPow(-3, 5, 5)
|
|
|
|
r[2] = math.ModPow(3, 4, -5)
|
|
|
|
r[3] = math.ModPow(-3, 5, -5)
|
|
|
|
r[4] = math.ModPow(19, -1, 141)
|
|
|
|
return r
|
|
|
|
}`
|
|
|
|
eval(t, src, []stackitem.Item{
|
|
|
|
stackitem.Make(1),
|
|
|
|
stackitem.Make(2),
|
|
|
|
stackitem.Make(1),
|
|
|
|
stackitem.Make(2),
|
|
|
|
stackitem.Make(52),
|
|
|
|
})
|
|
|
|
})
|
2021-03-02 11:15:25 +00:00
|
|
|
}
|
2022-06-24 14:01:47 +00:00
|
|
|
|
|
|
|
func TestInteropTypesComparison(t *testing.T) {
|
|
|
|
typeCheck := func(t *testing.T, typeName string, typeLen int) {
|
|
|
|
t.Run(typeName, func(t *testing.T) {
|
|
|
|
var ha, hb string
|
|
|
|
for i := 0; i < typeLen; i++ {
|
|
|
|
if i == typeLen-1 {
|
|
|
|
ha += "2"
|
|
|
|
hb += "3"
|
|
|
|
} else {
|
|
|
|
ha += "1, "
|
|
|
|
hb += "1, "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
check := func(t *testing.T, a, b string, expected bool) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + a + `}
|
|
|
|
b := interop.` + typeName + `{` + b + `}
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, expected)
|
|
|
|
}
|
|
|
|
t.Run("same type", func(t *testing.T) {
|
|
|
|
check(t, ha, ha, true)
|
|
|
|
check(t, ha, hb, false)
|
|
|
|
})
|
|
|
|
t.Run("a is nil", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
var a interop.` + typeName + `
|
|
|
|
b := interop.` + typeName + `{` + hb + `}
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, false)
|
|
|
|
})
|
|
|
|
t.Run("b is nil", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + ha + `}
|
|
|
|
var b interop.` + typeName + `
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, false)
|
|
|
|
})
|
|
|
|
t.Run("both nil", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
var a interop.` + typeName + `
|
|
|
|
var b interop.` + typeName + `
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, true)
|
|
|
|
})
|
|
|
|
t.Run("different types", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + ha + `}
|
|
|
|
b := 123
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, false)
|
|
|
|
})
|
|
|
|
t.Run("b is Buffer", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + ha + `}
|
|
|
|
b := []byte{` + ha + `}
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, true)
|
|
|
|
})
|
|
|
|
t.Run("b is ByteString", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + ha + `}
|
|
|
|
b := string([]byte{` + ha + `})
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
|
|
|
eval(t, src, true)
|
|
|
|
})
|
|
|
|
t.Run("b is compound type", func(t *testing.T) {
|
|
|
|
src := `package foo
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
|
|
|
func Main() bool {
|
|
|
|
a := interop.` + typeName + `{` + ha + `}
|
|
|
|
b := struct{}{}
|
|
|
|
return a.Equals(b)
|
|
|
|
}`
|
2022-08-19 11:54:42 +00:00
|
|
|
vm, _, _ := vmAndCompileInterop(t, src)
|
2022-06-24 14:01:47 +00:00
|
|
|
err := vm.Run()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, strings.Contains(err.Error(), "invalid conversion: Struct/ByteString"), err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
typeCheck(t, "Hash160", util.Uint160Size)
|
|
|
|
typeCheck(t, "Hash256", util.Uint256Size)
|
2022-10-05 07:46:21 +00:00
|
|
|
typeCheck(t, "Signature", smartcontract.SignatureLen)
|
|
|
|
typeCheck(t, "PublicKey", smartcontract.PublicKeyLen)
|
2022-06-24 14:01:47 +00:00
|
|
|
}
|