From 5d63ff100e94febb247e70763c26db9696c399c8 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 25 Jun 2020 17:25:05 +0300 Subject: [PATCH 1/3] vm/cli: add support for loadbase64 command Neo3 outputs scripts in base64 for transactions and contracts, to quickly parse them add a special `loadbase64` command. --- pkg/vm/cli/cli.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 433917335..6355491c6 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -2,6 +2,7 @@ package cli import ( "bytes" + "encoding/base64" "encoding/hex" "errors" "fmt" @@ -73,6 +74,14 @@ var commands = []*ishell.Cmd{ > load /path/to/script.avm`, Func: handleLoadAVM, }, + { + Name: "loadbase64", + Help: "Load a base64-encoded script string into the VM", + LongHelp: `Usage: loadbase64 + is mandatory parameter, example: +> loadbase64 AwAQpdToAAAADBQV9ehtQR1OrVZVhtHtoUHRfoE+agwUzmFvf3Rhfg/EuAVYOvJgKiON9j8TwAwIdHJhbnNmZXIMFDt9NxHG8Mz5sdypA9G/odiW8SOMQWJ9W1I4`, + Func: handleLoadBase64, + }, { Name: "loadhex", Help: "Load a hex-encoded script string into the VM", @@ -242,6 +251,18 @@ func handleLoadAVM(c *ishell.Context) { changePrompt(c, v) } +func handleLoadBase64(c *ishell.Context) { + v := getVMFromContext(c) + b, err := base64.StdEncoding.DecodeString(c.Args[0]) + if err != nil { + c.Err(err) + return + } + v.Load(b) + c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr()) + changePrompt(c, v) +} + func handleLoadHex(c *ishell.Context) { v := getVMFromContext(c) b, err := hex.DecodeString(c.Args[0]) From 003daf37cfc94687a53726a3acfe07339a2ed275 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 25 Jun 2020 17:32:58 +0300 Subject: [PATCH 2/3] vm/cli: add parameter checks for load* commands Prevent failing like this: NEO-GO-VM > loadhex panic: runtime error: index out of range ... --- pkg/vm/cli/cli.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 6355491c6..29615c58b 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -243,6 +243,10 @@ func handleXStack(c *ishell.Context) { func handleLoadAVM(c *ishell.Context) { v := getVMFromContext(c) + if len(c.Args) < 1 { + c.Err(errors.New("missing parameter ")) + return + } if err := v.LoadFile(c.Args[0]); err != nil { c.Err(err) } else { @@ -253,6 +257,10 @@ func handleLoadAVM(c *ishell.Context) { func handleLoadBase64(c *ishell.Context) { v := getVMFromContext(c) + if len(c.Args) < 1 { + c.Err(errors.New("missing parameter ")) + return + } b, err := base64.StdEncoding.DecodeString(c.Args[0]) if err != nil { c.Err(err) @@ -265,6 +273,10 @@ func handleLoadBase64(c *ishell.Context) { func handleLoadHex(c *ishell.Context) { v := getVMFromContext(c) + if len(c.Args) < 1 { + c.Err(errors.New("missing parameter ")) + return + } b, err := hex.DecodeString(c.Args[0]) if err != nil { c.Err(err) @@ -277,6 +289,10 @@ func handleLoadHex(c *ishell.Context) { func handleLoadGo(c *ishell.Context) { v := getVMFromContext(c) + if len(c.Args) < 1 { + c.Err(errors.New("missing parameter ")) + return + } fb, err := ioutil.ReadFile(c.Args[0]) if err != nil { c.Err(err) From 32b093649962c1a9d1d76c49bf88f12315d01522 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 25 Jun 2020 17:33:38 +0300 Subject: [PATCH 3/3] vm/cli: improve loadhex example It was using Neo 2 opcodes. --- pkg/vm/cli/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 29615c58b..efe793255 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -87,7 +87,7 @@ var commands = []*ishell.Cmd{ Help: "Load a hex-encoded script string into the VM", LongHelp: `Usage: loadhex is mandatory parameter, example: -> load 006166`, +> load 0c0c48656c6c6f20776f726c6421`, Func: handleLoadHex, }, {