From 895a8d9ebc24b1e00270d06b1505a9119c32d886 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 10 Feb 2020 10:53:55 +0300 Subject: [PATCH] compiler: reverse args in AppCall Invoked contract is expecting first argument to be on top of the stack. Change test to use non-commutative operation to catch this behaviour. --- pkg/compiler/codegen.go | 3 +++ pkg/compiler/interop_test.go | 25 ++++++++++++------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 182e5b308..8836cc342 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -786,6 +786,9 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { case "VerifySignature": emit.Opcode(c.prog.BinWriter, opcode.VERIFY) case "AppCall": + numArgs := len(expr.Args) - 1 + c.emitReverse(numArgs) + emit.Opcode(c.prog.BinWriter, opcode.APPCALL) buf := c.getByteArray(expr.Args[0]) if len(buf) != 20 { diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index 4a3a65484..2ebc71c29 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -2,7 +2,6 @@ package compiler_test import ( "fmt" - "math/big" "strings" "testing" @@ -55,8 +54,8 @@ func TestFromAddress(t *testing.T) { func TestAppCall(t *testing.T) { srcInner := ` package foo - func Main(a int, b int) int { - return a + b + func Main(a []byte, b []byte) []byte { + return append(a, b...) } ` @@ -78,7 +77,7 @@ func TestAppCall(t *testing.T) { require.NoError(t, v.Run()) - assertResult(t, v, big.NewInt(42)) + assertResult(t, v, []byte{1, 2, 3, 4}) }) t.Run("missing script", func(t *testing.T) { @@ -104,11 +103,11 @@ func TestAppCall(t *testing.T) { package foo import "github.com/CityOfZion/neo-go/pkg/interop/engine" const scriptHash = ` + fmt.Sprintf("%#v", string(ih.BytesBE())) + ` - func Main() int { - x := 13 - y := 29 + func Main() []byte { + x := []byte{1, 2} + y := []byte{3, 4} result := engine.AppCall([]byte(scriptHash), x, y) - return result.(int) + return result.([]byte) } ` @@ -117,7 +116,7 @@ func TestAppCall(t *testing.T) { require.NoError(t, v.Run()) - assertResult(t, v, big.NewInt(42)) + assertResult(t, v, []byte{1, 2, 3, 4}) }) } @@ -125,11 +124,11 @@ func getAppCallScript(h string) string { return ` package foo import "github.com/CityOfZion/neo-go/pkg/interop/engine" - func Main() int { - x := 13 - y := 29 + func Main() []byte { + x := []byte{1, 2} + y := []byte{3, 4} result := engine.AppCall(` + h + `, x, y) - return result.(int) + return result.([]byte) } ` }