From 7d8a3a7065d39a8d2db0a20a640eda0e1657435f Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Fri, 3 Jul 2020 12:58:41 +0300 Subject: [PATCH] compiler: exclude unexported methods from manifest We should be able to invoke exported methods only. --- pkg/compiler/debug.go | 7 ++-- pkg/compiler/debug_test.go | 68 +++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 25 deletions(-) diff --git a/pkg/compiler/debug.go b/pkg/compiler/debug.go index 7747cae4f..cbc5390c7 100644 --- a/pkg/compiler/debug.go +++ b/pkg/compiler/debug.go @@ -28,6 +28,8 @@ type MethodDebugInfo struct { ID string `json:"id"` // Name is the name of the method together with the namespace it belongs to. Name DebugMethodName `json:"name"` + // IsExported defines whether method is exported. + IsExported bool `json:"-"` // Range is the range of smart-contract's opcodes corresponding to the method. Range DebugRange `json:"range"` // Parameters is a list of method's parameters. @@ -136,6 +138,7 @@ func (c *codegen) methodInfoFromScope(name string, scope *funcScope) *MethodDebu return &MethodDebugInfo{ ID: name, Name: DebugMethodName{Name: name}, + IsExported: scope.decl.Name.IsExported(), Range: scope.rng, Parameters: params, ReturnType: c.scReturnTypeFromScope(scope), @@ -350,9 +353,9 @@ func (di *DebugInfo) convertToManifest(fs smartcontract.PropertyState) (*manifes if entryPoint.Name == "" { return nil, errors.New("no Main method was found") } - methods := make([]manifest.Method, 0, len(di.Methods)-1) + methods := make([]manifest.Method, 0) for _, method := range di.Methods { - if method.Name.Name != mainIdent { + if method.Name.Name != mainIdent && method.IsExported { mMethod, err := method.ToManifestMethod() if err != nil { return nil, err diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index c614a670a..800f38b94 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -18,27 +18,30 @@ func TestCodeGen_DebugInfo(t *testing.T) { func Main(op string) bool { var s string _ = s - res := methodInt(op) - _ = methodString() - _ = methodByteArray() - _ = methodArray() - _ = methodStruct() + res := MethodInt(op) + _ = MethodString() + _ = MethodByteArray() + _ = MethodArray() + _ = MethodStruct() + _ = MethodConcat("a", "b", "c") + _ = unexportedMethod() return res == 42 } -func methodInt(a string) int { +func MethodInt(a string) int { if a == "get42" { return 42 } return 3 } -func methodConcat(a, b string, c string) string{ +func MethodConcat(a, b string, c string) string{ return a + b + c } -func methodString() string { return "" } -func methodByteArray() []byte { return nil } -func methodArray() []bool { return nil } -func methodStruct() struct{} { return struct{}{} } +func MethodString() string { return "" } +func MethodByteArray() []byte { return nil } +func MethodArray() []bool { return nil } +func MethodStruct() struct{} { return struct{}{} } +func unexportedMethod() int { return 1 } ` info, err := getBuildInfo(src) @@ -58,11 +61,12 @@ func methodStruct() struct{} { return struct{}{} } t.Run("return types", func(t *testing.T) { returnTypes := map[string]string{ - "methodInt": "Integer", - "methodConcat": "String", - "methodString": "String", "methodByteArray": "ByteArray", - "methodArray": "Array", "methodStruct": "Struct", - "Main": "Boolean", + "MethodInt": "Integer", + "MethodConcat": "String", + "MethodString": "String", "MethodByteArray": "ByteArray", + "MethodArray": "Array", "MethodStruct": "Struct", + "Main": "Boolean", + "unexportedMethod": "Integer", } for i := range d.Methods { name := d.Methods[i].Name.Name @@ -84,11 +88,11 @@ func methodStruct() struct{} { return struct{}{} } t.Run("param types", func(t *testing.T) { paramTypes := map[string][]DebugParam{ - "methodInt": {{ + "MethodInt": {{ Name: "a", Type: "String", }}, - "methodConcat": { + "MethodConcat": { { Name: "a", Type: "String", @@ -140,7 +144,7 @@ func methodStruct() struct{} { return struct{}{} } }, Methods: []manifest.Method{ { - Name: "methodInt", + Name: "MethodInt", Parameters: []manifest.Parameter{ { Name: "a", @@ -150,25 +154,43 @@ func methodStruct() struct{} { return struct{}{} } ReturnType: smartcontract.IntegerType, }, { - Name: "methodString", + Name: "MethodString", Parameters: []manifest.Parameter{}, ReturnType: smartcontract.StringType, }, { - Name: "methodByteArray", + Name: "MethodByteArray", Parameters: []manifest.Parameter{}, ReturnType: smartcontract.ByteArrayType, }, { - Name: "methodArray", + Name: "MethodArray", Parameters: []manifest.Parameter{}, ReturnType: smartcontract.ArrayType, }, { - Name: "methodStruct", + Name: "MethodStruct", Parameters: []manifest.Parameter{}, ReturnType: smartcontract.ArrayType, }, + { + Name: "MethodConcat", + Parameters: []manifest.Parameter{ + { + Name: "a", + Type: smartcontract.StringType, + }, + { + Name: "b", + Type: smartcontract.StringType, + }, + { + Name: "c", + Type: smartcontract.StringType, + }, + }, + ReturnType: smartcontract.StringType, + }, }, Events: []manifest.Event{}, },