From 2341ae0c5314706c7a2489e674071dd7507a9b2f Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 10 Dec 2020 17:55:52 +0300 Subject: [PATCH] compiler: specify safe methods in config --- cli/smartcontract/smart_contract.go | 3 +++ cli/smartcontract/smart_contract_test.go | 1 + pkg/compiler/compiler.go | 3 +++ pkg/compiler/debug.go | 6 ++++++ pkg/compiler/debug_test.go | 4 +++- 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index 270a4014b..dc9205953 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -363,6 +363,7 @@ func initSmartContract(ctx *cli.Context) error { m := ProjectConfig{ Name: contractName, SupportedStandards: []string{}, + SafeMethods: []string{}, Events: []manifest.Event{ { Name: "Hello world!", @@ -423,6 +424,7 @@ func contractCompile(ctx *cli.Context) error { o.Name = conf.Name o.ContractEvents = conf.Events o.ContractSupportedStandards = conf.SupportedStandards + o.SafeMethods = conf.SafeMethods } result, err := compiler.CompileAndSave(src, o) @@ -646,6 +648,7 @@ func testInvokeScript(ctx *cli.Context) error { // ProjectConfig contains project metadata. type ProjectConfig struct { Name string + SafeMethods []string SupportedStandards []string Events []manifest.Event } diff --git a/cli/smartcontract/smart_contract_test.go b/cli/smartcontract/smart_contract_test.go index 8264cb858..eba92c021 100644 --- a/cli/smartcontract/smart_contract_test.go +++ b/cli/smartcontract/smart_contract_test.go @@ -59,6 +59,7 @@ func RuntimeNotify(args []interface{}) { require.NoError(t, err) require.Equal(t, `name: testContract +safemethods: [] supportedstandards: [] events: - name: Hello world! diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 0d4b5b970..02987acb7 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -51,6 +51,9 @@ type Options struct { // The list of standards supported by the contract. ContractSupportedStandards []string + + // SafeMethods contains list of methods which will be marked as safe in manifest. + SafeMethods []string } type buildInfo struct { diff --git a/pkg/compiler/debug.go b/pkg/compiler/debug.go index 978241130..11e08db58 100644 --- a/pkg/compiler/debug.go +++ b/pkg/compiler/debug.go @@ -433,6 +433,12 @@ func (di *DebugInfo) ConvertToManifest(o *Options) (*manifest.Manifest, error) { if err != nil { return nil, err } + for i := range o.SafeMethods { + if mMethod.Name == o.SafeMethods[i] { + mMethod.Safe = true + break + } + } methods = append(methods, mMethod) } } diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 3915c6b63..fcced5851 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -150,7 +150,7 @@ func _deploy(isUpdate bool) {} } t.Run("convert to Manifest", func(t *testing.T) { - actual, err := d.ConvertToManifest(&Options{Name: "MyCTR"}) + actual, err := d.ConvertToManifest(&Options{Name: "MyCTR", SafeMethods: []string{"methodInt", "methodString"}}) require.NoError(t, err) // note: offsets are hard to predict, so we just take them from the output expected := &manifest.Manifest{ @@ -183,12 +183,14 @@ func _deploy(isUpdate bool) {} }, }, ReturnType: smartcontract.IntegerType, + Safe: true, }, { Name: "methodString", Offset: 101, Parameters: []manifest.Parameter{}, ReturnType: smartcontract.StringType, + Safe: true, }, { Name: "methodByteArray",