Skip to content

Commit e0e7fdf

Browse files
committed
cli: unify canceltx command output
Always return "Target transaction accepted" error if the target transation was either accepted by the moment of command run or during the command handling. Exit with code 1 in both cases. Adjust TestAwaitUtilCancelTx correspondingly, allow both cases in test. Simplify the test along the way, remove useless code. Close #3365. Signed-off-by: Anna Shaleva <[email protected]>
1 parent 3090165 commit e0e7fdf

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

cli/util/cancel.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func cancelTx(ctx *cli.Context) error {
5252

5353
mainTx, _ := c.GetRawTransactionVerbose(txHash)
5454
if mainTx != nil && !mainTx.Blockhash.Equals(util.Uint256{}) {
55-
return cli.NewExitError(fmt.Errorf("transaction %s is already accepted at block %s", txHash, mainTx.Blockhash.StringLE()), 1)
55+
return cli.NewExitError(fmt.Errorf("target transaction %s is accepted at block %s", txHash, mainTx.Blockhash.StringLE()), 1)
5656
}
5757

5858
if mainTx != nil && !mainTx.HasSigner(acc.ScriptHash()) {
@@ -76,10 +76,7 @@ func cancelTx(ctx *cli.Context) error {
7676
if err != nil {
7777
return cli.NewExitError(fmt.Errorf("failed to send conflicting transaction: %w", err), 1)
7878
}
79-
var (
80-
acceptedH = resHash
81-
res *state.AppExecResult
82-
)
79+
var res *state.AppExecResult
8380
if ctx.Bool("await") {
8481
res, err = a.WaitAny(gctx, resVub, txHash, resHash)
8582
if err != nil {
@@ -93,12 +90,14 @@ func cancelTx(ctx *cli.Context) error {
9390
return cli.NewExitError(fmt.Errorf("failed to await target/ conflicting transaction %s/ %s: %w", txHash.StringLE(), resHash.StringLE(), err), 1)
9491
}
9592
if txHash.Equals(res.Container) {
96-
fmt.Fprintln(ctx.App.Writer, "Target transaction accepted")
97-
acceptedH = txHash
98-
} else {
99-
fmt.Fprintln(ctx.App.Writer, "Conflicting transaction accepted")
93+
tx, err := c.GetRawTransactionVerbose(txHash)
94+
if err != nil {
95+
return cli.NewExitError(fmt.Errorf("target transaction %s is accepted", txHash), 1)
96+
}
97+
return cli.NewExitError(fmt.Errorf("target transaction %s is accepted at block %s", txHash, tx.Blockhash.StringLE()), 1)
10098
}
99+
fmt.Fprintln(ctx.App.Writer, "Conflicting transaction accepted")
101100
}
102-
txctx.DumpTransactionInfo(ctx.App.Writer, acceptedH, res)
101+
txctx.DumpTransactionInfo(ctx.App.Writer, resHash, res)
103102
return nil
104103
}

cli/util/util_test.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package util_test
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
6-
"strings"
77
"testing"
88
"time"
99

@@ -168,24 +168,13 @@ func TestAwaitUtilCancelTx(t *testing.T) {
168168
_, ok := e.Chain.GetMemPool().TryGetValue(txHash)
169169
require.True(t, ok)
170170

171+
// Allow both cases: either target or conflicting tx acceptance.
171172
e.In.WriteString("one\r")
172-
e.Run(t, append(args, txHash.StringLE())...)
173-
174-
response := e.GetNextLine(t)
175-
if strings.Contains(response, "Conflicting transaction accepted") {
173+
err = e.RunOrError(t, fmt.Sprintf("target transaction %s is accepted", txHash), append(args, txHash.StringLE())...)
174+
if err == nil {
175+
response := e.GetNextLine(t)
176+
require.Equal(t, "Conflicting transaction accepted", response)
176177
resHash, _ := e.CheckAwaitableTxPersisted(t)
177-
require.Eventually(t, func() bool {
178-
_, aerErr := e.Chain.GetAppExecResults(resHash.Hash(), trigger.Application)
179-
return aerErr == nil
180-
}, time.Second*2, time.Millisecond*50)
181-
} else if strings.Contains(response, "Target transaction accepted") {
182-
require.Eventually(t, func() bool {
183-
_, _, err := e.Chain.GetTransaction(txHash)
184-
require.NoError(t, err, "original transaction should be on chain")
185-
_, aerErr := e.Chain.GetAppExecResults(txHash, trigger.Application)
186-
return aerErr == nil
187-
}, time.Second*2, time.Millisecond*50)
188-
} else {
189-
t.Fatalf("unexpected response: %s", response)
178+
require.NotEqual(t, resHash, txHash)
190179
}
191180
}

internal/testcli/executor.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ func (e *Executor) Run(t *testing.T, args ...string) {
286286
require.NoError(t, e.run(args...))
287287
checkExit(t, ch, 0)
288288
}
289+
290+
// RunOrError runs command and checks that if there was an error, then its text matches the provided one.
291+
func (e *Executor) RunOrError(t *testing.T, errText string, args ...string) error {
292+
ch := setExitFunc()
293+
err := e.run(args...)
294+
if err != nil {
295+
require.True(t, strings.Contains(err.Error(), errText))
296+
checkExit(t, ch, 1)
297+
} else {
298+
checkExit(t, ch, 0)
299+
}
300+
return err
301+
}
302+
289303
func (e *Executor) run(args ...string) error {
290304
e.Out.Reset()
291305
e.Err.Reset()

0 commit comments

Comments
 (0)