From 4dfb0eb438238d1907a133f17ea816918684c0d4 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 30 Jun 2020 14:21:58 +0300 Subject: [PATCH 1/2] compiler, cli: generate manifest.json and debug.json independently We should be able to generate manifest.json without debug.json and vice versa. --- cli/smartcontract/smart_contract.go | 5 ++-- pkg/compiler/compiler.go | 43 +++++++++++++++++------------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index 1006bf17e..c24d5f32d 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -351,14 +351,15 @@ func contractCompile(ctx *cli.Context) error { } manifestFile := ctx.String("manifest") confFile := ctx.String("config") - if len(manifestFile) != 0 && len(confFile) == 0 { + debugFile := ctx.String("debug") + if len(confFile) == 0 && (len(manifestFile) != 0 || len(debugFile) != 0) { return cli.NewExitError(errNoConfFile, 1) } o := &compiler.Options{ Outfile: ctx.String("out"), - DebugInfo: ctx.String("debug"), + DebugInfo: debugFile, ManifestFile: manifestFile, } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index c17f33e2f..5191d3fce 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -13,6 +13,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" + "github.com/pkg/errors" "golang.org/x/tools/go/loader" ) @@ -112,31 +113,37 @@ func CompileAndSave(src string, o *Options) ([]byte, error) { if err != nil { return b, err } - if o.DebugInfo == "" { + if o.DebugInfo == "" && o.ManifestFile == "" { return b, nil } + p, err := filepath.Abs(src) if err != nil { return b, err } di.Documents = append(di.Documents, p) - data, err := json.Marshal(di) - if err != nil { - return b, err + + if o.DebugInfo != "" { + data, err := json.Marshal(di) + if err != nil { + return b, err + } + if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil { + return b, err + } } - if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil { - return b, err + + if o.ManifestFile != "" { + m, err := di.convertToManifest(o.ContractFeatures) + if err != nil { + return b, errors.Wrap(err, "failed to convert debug info to manifest") + } + mData, err := json.Marshal(m) + if err != nil { + return b, errors.Wrap(err, "failed to marshal manifest") + } + return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm) } - if o.ManifestFile == "" { - return b, err - } - m, err := di.convertToManifest(o.ContractFeatures) - if err != nil { - return b, err - } - mData, err := json.Marshal(m) - if err != nil { - return b, err - } - return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm) + + return b, nil } From 3875c0a923999a95636a137c979cbce261278212 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 30 Jun 2020 14:24:07 +0300 Subject: [PATCH 2/2] cli: deploy contract using manifest.json instead of yaml config 1. We have all necessary information in manifest.json, so don't need config.yaml for deployment anymore. It also allow us to be consistent with C# cli (it uses only .manifest.json and .nef files for contract deployment) 2. Remove EntryPoint and Methods from ProjectConfig as compiler is able to generate this information by itself. --- cli/smartcontract/smart_contract.go | 46 ++++++++++------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index c24d5f32d..292c6649c 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -33,6 +33,7 @@ import ( var ( errNoInput = errors.New("no input file was found, specify an input file with the '--in or -i' flag") errNoConfFile = errors.New("no config file was found, specify a config file with the '--config' or '-c' flag") + errNoManifestFile = errors.New("no manifest file was found, specify a manifest file with the '--manifest' flag") errNoMethod = errors.New("no method specified for function invocation command") errNoWallet = errors.New("no wallet parameter found, specify it with the '--wallet or -w' flag") errNoScriptHash = errors.New("no smart contract hash was provided, specify one as the first argument") @@ -83,8 +84,8 @@ func NewCommands() []cli.Command { Usage: "Input file for the smart contract (*.nef)", }, cli.StringFlag{ - Name: "config, c", - Usage: "configuration input file (*.yml)", + Name: "manifest", + Usage: "Manifest input file (*.manifest.json)", }, walletFlag, addressFlag, @@ -316,16 +317,7 @@ func initSmartContract(ctx *cli.Context) error { return cli.NewExitError(err, 1) } - m := ProjectConfig{ - EntryPoint: manifest.Method{ - Name: "Main", - Parameters: []manifest.Parameter{ - manifest.NewParameter("Method", smartcontract.StringType), - manifest.NewParameter("Arguments", smartcontract.ArrayType), - }, - ReturnType: smartcontract.ByteArrayType, - }, - } + m := ProjectConfig{} b, err := yaml.Marshal(m) if err != nil { return cli.NewExitError(err, 1) @@ -538,8 +530,6 @@ func testInvokeScript(ctx *cli.Context) error { type ProjectConfig struct { HasStorage bool IsPayable bool - EntryPoint manifest.Method - Methods []manifest.Method Events []manifest.Event } @@ -555,17 +545,6 @@ func (p *ProjectConfig) GetFeatures() smartcontract.PropertyState { return fs } -// ToManifest converts project config to the manifest. -func (p *ProjectConfig) ToManifest(file nef.File) *manifest.Manifest { - m := manifest.NewManifest(file.Header.ScriptHash) - m.Features = p.GetFeatures() - m.ABI.Hash = file.Header.ScriptHash - m.ABI.EntryPoint = p.EntryPoint - m.ABI.Methods = p.Methods - m.ABI.Events = p.Events - return m -} - func inspect(ctx *cli.Context) error { in := ctx.String("in") compile := ctx.Bool("compile") @@ -638,9 +617,9 @@ func contractDeploy(ctx *cli.Context) error { if len(in) == 0 { return cli.NewExitError(errNoInput, 1) } - confFile := ctx.String("config") - if len(confFile) == 0 { - return cli.NewExitError(errNoConfFile, 1) + manifestFile := ctx.String("manifest") + if len(manifestFile) == 0 { + return cli.NewExitError(errNoManifestFile, 1) } gas := flags.Fixed8FromContext(ctx, "gas") @@ -656,9 +635,15 @@ func contractDeploy(ctx *cli.Context) error { if err != nil { return cli.NewExitError(errors.Wrapf(err, "failed to restore .nef file"), 1) } - conf, err := parseContractConfig(confFile) + + manifestBytes, err := ioutil.ReadFile(manifestFile) if err != nil { - return err + return cli.NewExitError(errors.Wrapf(err, "failed to read manifest file"), 1) + } + m := &manifest.Manifest{} + err = json.Unmarshal(manifestBytes, m) + if err != nil { + return cli.NewExitError(errors.Wrapf(err, "failed to restore manifest file"), 1) } gctx, cancel := options.GetTimeoutContext(ctx) @@ -669,7 +654,6 @@ func contractDeploy(ctx *cli.Context) error { return err } - m := conf.ToManifest(nefFile) txScript, sysfee, err := request.CreateDeploymentScript(nefFile.Script, m) if err != nil { return cli.NewExitError(fmt.Errorf("failed to create deployment script: %v", err), 1)