From e319c6c6381196d09e20862a955343bd0dc37233 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 29 Oct 2019 12:56:44 +0300 Subject: [PATCH] cli: move avm inspect from vm to contract command Make inspect work with avms by default and with go files if told so. In the end this makes our CLI interface more consistent and usable. Drop useless CompileAndInspect() compiler method along the way. --- cli/smartcontract/smart_contract.go | 25 ++++++++++++++++++---- cli/vm/vm.go | 32 ----------------------------- pkg/vm/compiler/compiler.go | 18 ---------------- 3 files changed, 21 insertions(+), 54 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index 86bc1af41..9328a8f4d 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -6,14 +6,15 @@ import ( "context" "encoding/hex" "encoding/json" - "errors" "fmt" "io/ioutil" "os" "path/filepath" "github.com/CityOfZion/neo-go/pkg/rpc" + "github.com/CityOfZion/neo-go/pkg/vm" "github.com/CityOfZion/neo-go/pkg/vm/compiler" + "github.com/pkg/errors" "github.com/urfave/cli" ) @@ -91,6 +92,10 @@ func NewCommands() []cli.Command { Usage: "creates a user readable dump of the program instructions", Action: inspect, Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "compile, c", + Usage: "compile input file (it should be go code then)", + }, cli.StringFlag{ Name: "in, i", Usage: "input file of the program", @@ -251,12 +256,24 @@ func parseContractDetails() ContractDetails { } func inspect(ctx *cli.Context) error { - src := ctx.String("in") - if len(src) == 0 { + in := ctx.String("in") + compile := ctx.Bool("compile") + if len(in) == 0 { return cli.NewExitError(errNoInput, 1) } - if err := compiler.CompileAndInspect(src); err != nil { + b, err := ioutil.ReadFile(in) + if err != nil { return cli.NewExitError(err, 1) } + if compile { + b, err = compiler.Compile(bytes.NewReader(b), &compiler.Options{}) + if err != nil { + return cli.NewExitError(errors.Wrap(err, "failed to compile"), 1) + } + } + v := vm.New() + v.LoadScript(b) + v.PrintOps() + return nil } diff --git a/cli/vm/vm.go b/cli/vm/vm.go index 6e0876723..3dab28638 100644 --- a/cli/vm/vm.go +++ b/cli/vm/vm.go @@ -1,10 +1,6 @@ package vm import ( - "errors" - "io/ioutil" - - "github.com/CityOfZion/neo-go/pkg/vm" vmcli "github.com/CityOfZion/neo-go/pkg/vm/cli" "github.com/urfave/cli" ) @@ -18,19 +14,6 @@ func NewCommands() []cli.Command { Flags: []cli.Flag{ cli.BoolFlag{Name: "debug, d"}, }, - Subcommands: []cli.Command{ - { - Name: "inspect", - Usage: "dump instructions of the avm file given", - Action: inspect, - Flags: []cli.Flag{ - cli.StringFlag{ - Name: "in, i", - Usage: "input file of the program (AVM)", - }, - }, - }, - }, }} } @@ -38,18 +21,3 @@ func startVMPrompt(ctx *cli.Context) error { p := vmcli.New() return p.Run() } - -func inspect(ctx *cli.Context) error { - avm := ctx.String("in") - if len(avm) == 0 { - return cli.NewExitError(errors.New("no input file given"), 1) - } - b, err := ioutil.ReadFile(avm) - if err != nil { - return cli.NewExitError(err, 1) - } - v := vm.New() - v.LoadScript(b) - v.PrintOps() - return nil -} diff --git a/pkg/vm/compiler/compiler.go b/pkg/vm/compiler/compiler.go index 7a264650f..7973efa8a 100644 --- a/pkg/vm/compiler/compiler.go +++ b/pkg/vm/compiler/compiler.go @@ -14,7 +14,6 @@ import ( "os" "strings" - "github.com/CityOfZion/neo-go/pkg/vm" "golang.org/x/tools/go/loader" ) @@ -96,23 +95,6 @@ func CompileAndSave(src string, o *Options) error { return ioutil.WriteFile(out, b, os.ModePerm) } -// CompileAndInspect compiles the program and dumps the opcode in a user friendly format. -func CompileAndInspect(src string) error { - b, err := ioutil.ReadFile(src) - if err != nil { - return err - } - b, err = Compile(bytes.NewReader(b), &Options{}) - if err != nil { - return err - } - - v := vm.New() - v.LoadScript(b) - v.PrintOps() - return nil -} - func gopath() string { gopath := os.Getenv("GOPATH") if len(gopath) == 0 {