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.
This commit is contained in:
Evgenii Stratonikov 2020-02-10 10:53:55 +03:00
parent 52d8d58593
commit 895a8d9ebc
2 changed files with 15 additions and 13 deletions

View file

@ -786,6 +786,9 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
case "VerifySignature": case "VerifySignature":
emit.Opcode(c.prog.BinWriter, opcode.VERIFY) emit.Opcode(c.prog.BinWriter, opcode.VERIFY)
case "AppCall": case "AppCall":
numArgs := len(expr.Args) - 1
c.emitReverse(numArgs)
emit.Opcode(c.prog.BinWriter, opcode.APPCALL) emit.Opcode(c.prog.BinWriter, opcode.APPCALL)
buf := c.getByteArray(expr.Args[0]) buf := c.getByteArray(expr.Args[0])
if len(buf) != 20 { if len(buf) != 20 {

View file

@ -2,7 +2,6 @@ package compiler_test
import ( import (
"fmt" "fmt"
"math/big"
"strings" "strings"
"testing" "testing"
@ -55,8 +54,8 @@ func TestFromAddress(t *testing.T) {
func TestAppCall(t *testing.T) { func TestAppCall(t *testing.T) {
srcInner := ` srcInner := `
package foo package foo
func Main(a int, b int) int { func Main(a []byte, b []byte) []byte {
return a + b return append(a, b...)
} }
` `
@ -78,7 +77,7 @@ func TestAppCall(t *testing.T) {
require.NoError(t, v.Run()) 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) { t.Run("missing script", func(t *testing.T) {
@ -104,11 +103,11 @@ func TestAppCall(t *testing.T) {
package foo package foo
import "github.com/CityOfZion/neo-go/pkg/interop/engine" import "github.com/CityOfZion/neo-go/pkg/interop/engine"
const scriptHash = ` + fmt.Sprintf("%#v", string(ih.BytesBE())) + ` const scriptHash = ` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `
func Main() int { func Main() []byte {
x := 13 x := []byte{1, 2}
y := 29 y := []byte{3, 4}
result := engine.AppCall([]byte(scriptHash), x, y) 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()) 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 ` return `
package foo package foo
import "github.com/CityOfZion/neo-go/pkg/interop/engine" import "github.com/CityOfZion/neo-go/pkg/interop/engine"
func Main() int { func Main() []byte {
x := 13 x := []byte{1, 2}
y := 29 y := []byte{3, 4}
result := engine.AppCall(` + h + `, x, y) result := engine.AppCall(` + h + `, x, y)
return result.(int) return result.([]byte)
} }
` `
} }