From 330db3616881da1ea251f357357e9f3fb3cc3b01 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 27 Jan 2020 10:59:57 +0300 Subject: [PATCH] compiler: implement engine.AppCall interop --- pkg/compiler/analysis.go | 7 +++- pkg/compiler/codegen.go | 43 +++++++++++++++++++- pkg/compiler/interop_test.go | 76 ++++++++++++++++++++++++++++++++++++ pkg/interop/engine/engine.go | 5 +++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 pkg/compiler/interop_test.go diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index da05ad2f6..d29348b8d 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -14,7 +14,7 @@ var ( builtinFuncs = []string{ "len", "append", "SHA256", "SHA1", "Hash256", "Hash160", - "VerifySignature", + "VerifySignature", "AppCall", "FromAddress", "Equals", } ) @@ -180,6 +180,11 @@ func isBuiltin(expr ast.Expr) bool { return false } +func isAppCall(expr ast.Expr) bool { + t, ok := expr.(*ast.SelectorExpr) + return ok && t.Sel.Name == "AppCall" +} + func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool { if len(lit.Elts) == 0 { return false diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 1d5e144eb..95695e98b 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -2,6 +2,7 @@ package compiler import ( "encoding/binary" + "errors" "fmt" "go/ast" "go/constant" @@ -488,8 +489,16 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil } + args := n.Args + isAppCall := isAppCall(n.Fun) + // When using APPCALL, script hash is a part of the instruction so + // script hash should be emitted after APPCALL. + if isAppCall { + args = n.Args[1:] + } + // Handle the arguments - for _, arg := range n.Args { + for _, arg := range args { ast.Walk(c, arg) } // Do not swap for builtin functions. @@ -513,6 +522,16 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { // Use the ident to check, builtins are not in func scopes. // We can be sure builtins are of type *ast.Ident. c.convertBuiltin(n) + + if isAppCall { + buf := c.getByteArray(n.Args[0]) + if len(buf) != 20 { + c.prog.Err = errors.New("invalid script hash") + return nil + } + + c.prog.WriteBytes(buf) + } case isSyscall(f): c.convertSyscall(f.selector.Name, f.name) default: @@ -634,6 +653,26 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return c } +// getByteArray returns byte array value from constant expr. +// Only literals are supported. +func (c *codegen) getByteArray(expr ast.Expr) []byte { + switch t := expr.(type) { + case *ast.CompositeLit: + if !isByteArray(t, c.typeInfo) { + return nil + } + buf := make([]byte, len(t.Elts)) + for i := 0; i < len(t.Elts); i++ { + t := c.typeInfo.Types[t.Elts[i]] + val, _ := constant.Int64Val(t.Value) + buf[i] = byte(val) + } + return buf + default: + return nil + } +} + func (c *codegen) convertSyscall(api, name string) { api, ok := syscalls[api][name] if !ok { @@ -687,6 +726,8 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { emitOpcode(c.prog.BinWriter, opcode.HASH160) case "VerifySignature": emitOpcode(c.prog.BinWriter, opcode.VERIFY) + case "AppCall": + emitOpcode(c.prog.BinWriter, opcode.APPCALL) case "Equals": emitOpcode(c.prog.BinWriter, opcode.EQUAL) case "FromAddress": diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go new file mode 100644 index 000000000..bd86c4e4f --- /dev/null +++ b/pkg/compiler/interop_test.go @@ -0,0 +1,76 @@ +package compiler_test + +import ( + "fmt" + "math/big" + "strings" + "testing" + + "github.com/CityOfZion/neo-go/pkg/compiler" + "github.com/CityOfZion/neo-go/pkg/crypto/hash" + "github.com/CityOfZion/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +func TestAppCall(t *testing.T) { + srcInner := ` + package foo + func Main(args []interface{}) int { + a := args[0].(int) + b := args[1].(int) + return a + b + } + ` + + inner, err := compiler.Compile(strings.NewReader(srcInner)) + require.NoError(t, err) + + ih := hash.Hash160(inner) + getScript := func(u util.Uint160) []byte { + if u.Equals(ih) { + return inner + } + return nil + } + + t.Run("valid script", func(t *testing.T) { + src := getAppCallScript(fmt.Sprintf("%#v", ih.BytesBE())) + v := vmAndCompile(t, src) + v.SetScriptGetter(getScript) + + require.NoError(t, v.Run()) + + assertResult(t, v, big.NewInt(42)) + }) + + t.Run("missing script", func(t *testing.T) { + h := ih + h[0] = ^h[0] + + src := getAppCallScript(fmt.Sprintf("%#v", h.BytesBE())) + v := vmAndCompile(t, src) + v.SetScriptGetter(getScript) + + require.Error(t, v.Run()) + }) + + t.Run("invalid script address", func(t *testing.T) { + src := getAppCallScript("[]byte{1, 2, 3}") + + _, err := compiler.Compile(strings.NewReader(src)) + require.Error(t, err) + }) +} + +func getAppCallScript(h string) string { + return ` + package foo + import "github.com/CityOfZion/neo-go/pkg/interop/engine" + func Main() int { + x := 13 + y := 29 + result := engine.AppCall(` + h + `, []interface{}{x, y}) + return result.(int) + } + ` +} diff --git a/pkg/interop/engine/engine.go b/pkg/interop/engine/engine.go index 057d42728..3230d4a09 100644 --- a/pkg/interop/engine/engine.go +++ b/pkg/interop/engine/engine.go @@ -27,3 +27,8 @@ func GetCallingScriptHash() []byte { func GetEntryScriptHash() []byte { return nil } + +// AppCall executes script with specified hash using provided arguments. +func AppCall(scriptHash []byte, args []interface{}) interface{} { + return nil +}