Merge pull request #1137 from nspcc-dev/neo3/compiler/contract_deploy

compiler, cli: update ContractCompile and ContractDeploy methods
This commit is contained in:
Roman Khimov 2020-07-02 10:01:56 +03:00 committed by GitHub
commit d8677844fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 51 deletions

View file

@ -33,6 +33,7 @@ import (
var ( var (
errNoInput = errors.New("no input file was found, specify an input file with the '--in or -i' flag") 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") 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") 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") 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") 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)", Usage: "Input file for the smart contract (*.nef)",
}, },
cli.StringFlag{ cli.StringFlag{
Name: "config, c", Name: "manifest",
Usage: "configuration input file (*.yml)", Usage: "Manifest input file (*.manifest.json)",
}, },
walletFlag, walletFlag,
addressFlag, addressFlag,
@ -316,16 +317,7 @@ func initSmartContract(ctx *cli.Context) error {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
m := ProjectConfig{ m := ProjectConfig{}
EntryPoint: manifest.Method{
Name: "Main",
Parameters: []manifest.Parameter{
manifest.NewParameter("Method", smartcontract.StringType),
manifest.NewParameter("Arguments", smartcontract.ArrayType),
},
ReturnType: smartcontract.ByteArrayType,
},
}
b, err := yaml.Marshal(m) b, err := yaml.Marshal(m)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
@ -351,14 +343,15 @@ func contractCompile(ctx *cli.Context) error {
} }
manifestFile := ctx.String("manifest") manifestFile := ctx.String("manifest")
confFile := ctx.String("config") 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) return cli.NewExitError(errNoConfFile, 1)
} }
o := &compiler.Options{ o := &compiler.Options{
Outfile: ctx.String("out"), Outfile: ctx.String("out"),
DebugInfo: ctx.String("debug"), DebugInfo: debugFile,
ManifestFile: manifestFile, ManifestFile: manifestFile,
} }
@ -537,8 +530,6 @@ func testInvokeScript(ctx *cli.Context) error {
type ProjectConfig struct { type ProjectConfig struct {
HasStorage bool HasStorage bool
IsPayable bool IsPayable bool
EntryPoint manifest.Method
Methods []manifest.Method
Events []manifest.Event Events []manifest.Event
} }
@ -554,17 +545,6 @@ func (p *ProjectConfig) GetFeatures() smartcontract.PropertyState {
return fs 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 { func inspect(ctx *cli.Context) error {
in := ctx.String("in") in := ctx.String("in")
compile := ctx.Bool("compile") compile := ctx.Bool("compile")
@ -637,9 +617,9 @@ func contractDeploy(ctx *cli.Context) error {
if len(in) == 0 { if len(in) == 0 {
return cli.NewExitError(errNoInput, 1) return cli.NewExitError(errNoInput, 1)
} }
confFile := ctx.String("config") manifestFile := ctx.String("manifest")
if len(confFile) == 0 { if len(manifestFile) == 0 {
return cli.NewExitError(errNoConfFile, 1) return cli.NewExitError(errNoManifestFile, 1)
} }
gas := flags.Fixed8FromContext(ctx, "gas") gas := flags.Fixed8FromContext(ctx, "gas")
@ -655,9 +635,15 @@ func contractDeploy(ctx *cli.Context) error {
if err != nil { if err != nil {
return cli.NewExitError(errors.Wrapf(err, "failed to restore .nef file"), 1) return cli.NewExitError(errors.Wrapf(err, "failed to restore .nef file"), 1)
} }
conf, err := parseContractConfig(confFile)
manifestBytes, err := ioutil.ReadFile(manifestFile)
if err != nil { 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) gctx, cancel := options.GetTimeoutContext(ctx)
@ -668,7 +654,6 @@ func contractDeploy(ctx *cli.Context) error {
return err return err
} }
m := conf.ToManifest(nefFile)
txScript, sysfee, err := request.CreateDeploymentScript(nefFile.Script, m) txScript, sysfee, err := request.CreateDeploymentScript(nefFile.Script, m)
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to create deployment script: %v", err), 1) return cli.NewExitError(fmt.Errorf("failed to create deployment script: %v", err), 1)

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef" "github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/pkg/errors"
"golang.org/x/tools/go/loader" "golang.org/x/tools/go/loader"
) )
@ -112,14 +113,17 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
if err != nil { if err != nil {
return b, err return b, err
} }
if o.DebugInfo == "" { if o.DebugInfo == "" && o.ManifestFile == "" {
return b, nil return b, nil
} }
p, err := filepath.Abs(src) p, err := filepath.Abs(src)
if err != nil { if err != nil {
return b, err return b, err
} }
di.Documents = append(di.Documents, p) di.Documents = append(di.Documents, p)
if o.DebugInfo != "" {
data, err := json.Marshal(di) data, err := json.Marshal(di)
if err != nil { if err != nil {
return b, err return b, err
@ -127,16 +131,19 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil { if err := ioutil.WriteFile(o.DebugInfo, data, os.ModePerm); err != nil {
return b, err return b, err
} }
if o.ManifestFile == "" {
return b, err
} }
if o.ManifestFile != "" {
m, err := di.convertToManifest(o.ContractFeatures) m, err := di.convertToManifest(o.ContractFeatures)
if err != nil { if err != nil {
return b, err return b, errors.Wrap(err, "failed to convert debug info to manifest")
} }
mData, err := json.Marshal(m) mData, err := json.Marshal(m)
if err != nil { if err != nil {
return b, err return b, errors.Wrap(err, "failed to marshal manifest")
} }
return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm) return b, ioutil.WriteFile(o.ManifestFile, mData, os.ModePerm)
}
return b, nil
} }