From a86bd2cd0819540a382ba15cfd9735fdcf772fd4 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 10 Nov 2020 15:17:06 +0300 Subject: [PATCH 1/2] cli: set ctl.ErrWriter We did not have it set, however we use it in the several places which results with the following panic: ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4ddf0d] goroutine 1 [running]: fmt.Fprintln(0x0, 0x0, 0xc0001d7c08, 0x1, 0x1, 0x10376c0, 0xc0002051c0, 0x0) fmt/print.go:265 +0x5d github.com/nspcc-dev/neo-go/cli/wallet.transferNEP5(0xc0000a9080, 0x0, 0x0) github.com/nspcc-dev/neo-go/cli/wallet/nep5.go:445 +0x9eb github.com/urfave/cli.HandleAction(0xc837c0, 0xf26198, 0xc0000a9080, 0xc00007f600, 0x0) github.com/urfave/cli@v1.20.0/app.go:490 +0xc8 github.com/urfave/cli.Command.Run(0xdc66a3, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd0b0d, 0x14, 0xdf6c25, ...) github.com/urfave/cli@v1.20.0/command.go:210 +0x9e8 github.com/urfave/cli.(*App).RunAsSubcommand(0xc0001aa340, 0xc0000a8dc0, 0x0, 0x0) github.com/urfave/cli@v1.20.0/app.go:379 +0x88b github.com/urfave/cli.Command.startApp(0xdc23e4, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd546a, 0x18, 0x0, ...) github.com/urfave/cli@v1.20.0/command.go:298 +0x81a github.com/urfave/cli.Command.Run(0xdc23e4, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0xdd546a, 0x18, 0x0, ...) github.com/urfave/cli@v1.20.0/command.go:98 +0x1219 github.com/urfave/cli.(*App).RunAsSubcommand(0xc0001aa1a0, 0xc0000a8c60, 0x0, 0x0) github.com/urfave/cli@v1.20.0/app.go:379 +0x88b github.com/urfave/cli.Command.startApp(0xdc45bf, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0xde1d28, 0x24, 0x0, ...) github.com/urfave/cli@v1.20.0/command.go:298 +0x81a github.com/urfave/cli.Command.Run(0xdc45bf, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0xde1d28, 0x24, 0x0, ...) github.com/urfave/cli@v1.20.0/command.go:98 +0x1219 github.com/urfave/cli.(*App).Run(0xc0001aa000, 0xc000030120, 0x12, 0x12, 0x0, 0x0) github.com/urfave/cli@v1.20.0/app.go:255 +0x741 main.main() command-line-arguments/main.go:18 +0x4b ``` --- cli/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/main.go b/cli/main.go index 5d762a545..9d81b36bf 100644 --- a/cli/main.go +++ b/cli/main.go @@ -25,6 +25,7 @@ func newApp() *cli.App { ctl.Name = "neo-go" ctl.Version = config.Version ctl.Usage = "Official Go client for Neo" + ctl.ErrWriter = os.Stdout ctl.Commands = append(ctl.Commands, server.NewCommands()...) ctl.Commands = append(ctl.Commands, smartcontract.NewCommands()...) From 5e193a34eb132cf21955446227c97d34b68b2d51 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 10 Nov 2020 15:21:57 +0300 Subject: [PATCH 2/2] examples: add an example of binary.Itoa usage --- examples/timer/timer.go | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/examples/timer/timer.go b/examples/timer/timer.go index 2fcabd03c..169506fa0 100644 --- a/examples/timer/timer.go +++ b/examples/timer/timer.go @@ -1,6 +1,7 @@ package timer import ( + "github.com/nspcc-dev/neo-go/pkg/interop/binary" "github.com/nspcc-dev/neo-go/pkg/interop/contract" "github.com/nspcc-dev/neo-go/pkg/interop/engine" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" @@ -31,7 +32,8 @@ func _deploy(isUpdate bool) { return } storage.Put(ctx, ticksKey, defaultTicks) - runtime.Log("Timer set to " + itoa(defaultTicks) + " ticks.") + i := binary.Itoa(defaultTicks, 10) + runtime.Log("Timer set to " + i + " ticks.") } // Migrate migrates the contract. @@ -55,7 +57,8 @@ func Tick() bool { return engine.AppCall(runtime.GetExecutingScriptHash(), "selfDestroy").(bool) } storage.Put(ctx, ticksKey, ticksLeft) - runtime.Log(itoa(ticksLeft.(int)) + " ticks left.") + i := binary.Itoa(ticksLeft.(int), 10) + runtime.Log(i + " ticks left.") return true } @@ -69,28 +72,3 @@ func SelfDestroy() bool { runtime.Log("Destroyed.") return true } - -// itoa converts int to string -func itoa(i int) string { - digits := "0123456789" - var ( - res string - isNegative bool - ) - if i < 0 { - i = -i - isNegative = true - } - for { - r := i % 10 - res = digits[r:r+1] + res - i = i / 10 - if i == 0 { - break - } - } - if isNegative { - res = "-" + res - } - return res -}