cli: simplify and fix invokeWithArgs logic

Saving into a file can't be successful without signAndPush flag (wallet
present). This situation can't happen in CLI invocations since
testinvokefunction doesn't have `--out` flag, but still it's a logic
error. Everything else can be simplified a bit taking that into account.
This commit is contained in:
Roman Khimov 2022-08-06 16:36:11 +03:00
parent aa2dbe9caf
commit 8385efe4b3

View file

@ -677,13 +677,12 @@ func invokeWithArgs(ctx *cli.Context, acc *wallet.Account, wall *wallet.Wallet,
} }
if resp.State != "HALT" { if resp.State != "HALT" {
errText := fmt.Sprintf("Warning: %s VM state returned from the RPC node: %s", resp.State, resp.FaultException) errText := fmt.Sprintf("Warning: %s VM state returned from the RPC node: %s", resp.State, resp.FaultException)
if out == "" && !signAndPush { if !signAndPush {
return sender, cli.NewExitError(errText, 1) return sender, cli.NewExitError(errText, 1)
} }
action := "save" action := "save"
process := "Saving" process := "Saving"
if signAndPush {
if out != "" { if out != "" {
action += "and send" action += "and send"
process += "and sending" process += "and sending"
@ -691,17 +690,27 @@ func invokeWithArgs(ctx *cli.Context, acc *wallet.Account, wall *wallet.Wallet,
action = "send" action = "send"
process = "Sending" process = "Sending"
} }
}
if !ctx.Bool("force") { if !ctx.Bool("force") {
return sender, cli.NewExitError(errText+".\nUse --force flag to "+action+" the transaction anyway.", 1) return sender, cli.NewExitError(errText+".\nUse --force flag to "+action+" the transaction anyway.", 1)
} }
fmt.Fprintln(ctx.App.Writer, errText+".\n"+process+" transaction...") fmt.Fprintln(ctx.App.Writer, errText+".\n"+process+" transaction...")
} }
if out != "" { if !signAndPush {
b, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return sender, cli.NewExitError(err, 1)
}
fmt.Fprintln(ctx.App.Writer, string(b))
} else {
if len(resp.Script) == 0 {
return sender, cli.NewExitError(errors.New("no script returned from the RPC node"), 1)
}
tx, err := c.CreateTxFromScript(resp.Script, acc, resp.GasConsumed+int64(sysgas), int64(gas), cosignersAccounts) tx, err := c.CreateTxFromScript(resp.Script, acc, resp.GasConsumed+int64(sysgas), int64(gas), cosignersAccounts)
if err != nil { if err != nil {
return sender, cli.NewExitError(fmt.Errorf("failed to create tx: %w", err), 1) return sender, cli.NewExitError(fmt.Errorf("failed to create tx: %w", err), 1)
} }
if out != "" {
m, err := c.GetNetwork() m, err := c.GetNetwork()
if err != nil { if err != nil {
return sender, cli.NewExitError(fmt.Errorf("failed to save tx: %w", err), 1) return sender, cli.NewExitError(fmt.Errorf("failed to save tx: %w", err), 1)
@ -710,16 +719,7 @@ func invokeWithArgs(ctx *cli.Context, acc *wallet.Account, wall *wallet.Wallet,
return sender, cli.NewExitError(err, 1) return sender, cli.NewExitError(err, 1)
} }
fmt.Fprintln(ctx.App.Writer, tx.Hash().StringLE()) fmt.Fprintln(ctx.App.Writer, tx.Hash().StringLE())
return sender, nil } else {
}
if signAndPush {
if len(resp.Script) == 0 {
return sender, cli.NewExitError(errors.New("no script returned from the RPC node"), 1)
}
tx, err := c.CreateTxFromScript(resp.Script, acc, resp.GasConsumed+int64(sysgas), int64(gas), cosignersAccounts)
if err != nil {
return sender, cli.NewExitError(fmt.Errorf("failed to create tx: %w", err), 1)
}
if !ctx.Bool("force") { if !ctx.Bool("force") {
err := input.ConfirmTx(ctx.App.Writer, tx) err := input.ConfirmTx(ctx.App.Writer, tx)
if err != nil { if err != nil {
@ -731,13 +731,7 @@ func invokeWithArgs(ctx *cli.Context, acc *wallet.Account, wall *wallet.Wallet,
return sender, cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1) return sender, cli.NewExitError(fmt.Errorf("failed to push invocation tx: %w", err), 1)
} }
fmt.Fprintf(ctx.App.Writer, "Sent invocation transaction %s\n", txHash.StringLE()) fmt.Fprintf(ctx.App.Writer, "Sent invocation transaction %s\n", txHash.StringLE())
} else {
b, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return sender, cli.NewExitError(err, 1)
} }
fmt.Fprintln(ctx.App.Writer, string(b))
} }
return sender, nil return sender, nil