Merge PR #4996: Expose precheck errors from tx
This commit is contained in:
committed by
Alexander Bezobchuk
parent
849e2fb638
commit
5bcab79e8a
@@ -2,6 +2,10 @@ package context
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||
"github.com/tendermint/tendermint/mempool"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -29,6 +33,46 @@ func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error
|
||||
return res, err
|
||||
}
|
||||
|
||||
// CheckTendermintError checks if the error returned from BroadcastTx is a
|
||||
// Tendermint error that is returned before the tx is submitted due to
|
||||
// precondition checks that failed. If an Tendermint error is detected, this
|
||||
// function returns the correct code back in TxResponse.
|
||||
//
|
||||
// TODO: Avoid brittle string matching in favor of error matching. This requires
|
||||
// a change to Tendermint's RPCError type to allow retrieval or matching against
|
||||
// a concrete error type.
|
||||
func CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
errStr := strings.ToLower(err.Error())
|
||||
txHash := fmt.Sprintf("%X", tmhash.Sum(txBytes))
|
||||
|
||||
switch {
|
||||
case strings.Contains(errStr, strings.ToLower(mempool.ErrTxInCache.Error())):
|
||||
return &sdk.TxResponse{
|
||||
Code: uint32(sdk.CodeTxInMempoolCache),
|
||||
TxHash: txHash,
|
||||
}
|
||||
|
||||
case strings.Contains(errStr, "mempool is full"):
|
||||
return &sdk.TxResponse{
|
||||
Code: uint32(sdk.CodeMempoolIsFull),
|
||||
TxHash: txHash,
|
||||
}
|
||||
|
||||
case strings.Contains(errStr, "tx too large"):
|
||||
return &sdk.TxResponse{
|
||||
Code: uint32(sdk.CodeTxTooLarge),
|
||||
TxHash: txHash,
|
||||
}
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastTxCommit broadcasts transaction bytes to a Tendermint node and
|
||||
// waits for a commit. An error is only returned if there is no RPC node
|
||||
// connection or if broadcasting fails.
|
||||
@@ -44,6 +88,10 @@ func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error)
|
||||
|
||||
res, err := node.BroadcastTxCommit(txBytes)
|
||||
if err != nil {
|
||||
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
|
||||
return *errRes, nil
|
||||
}
|
||||
|
||||
return sdk.NewResponseFormatBroadcastTxCommit(res), err
|
||||
}
|
||||
|
||||
@@ -67,6 +115,10 @@ func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
|
||||
}
|
||||
|
||||
res, err := node.BroadcastTxSync(txBytes)
|
||||
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
|
||||
return *errRes, nil
|
||||
}
|
||||
|
||||
return sdk.NewResponseFormatBroadcastTx(res), err
|
||||
}
|
||||
|
||||
@@ -79,5 +131,9 @@ func (ctx CLIContext) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
|
||||
}
|
||||
|
||||
res, err := node.BroadcastTxAsync(txBytes)
|
||||
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
|
||||
return *errRes, nil
|
||||
}
|
||||
|
||||
return sdk.NewResponseFormatBroadcastTx(res), err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tendermint/tendermint/mempool"
|
||||
"github.com/tendermint/tendermint/rpc/client/mock"
|
||||
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
type MockClient struct {
|
||||
mock.Client
|
||||
err error
|
||||
}
|
||||
|
||||
func (c MockClient) BroadcastTxCommit(tx tmtypes.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
|
||||
return nil, c.err
|
||||
}
|
||||
|
||||
func (c MockClient) BroadcastTxAsync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) {
|
||||
return nil, c.err
|
||||
}
|
||||
|
||||
func (c MockClient) BroadcastTxSync(tx tmtypes.Tx) (*ctypes.ResultBroadcastTx, error) {
|
||||
return nil, c.err
|
||||
}
|
||||
|
||||
func CreateContextWithErrorAndMode(err error, mode string) CLIContext {
|
||||
return CLIContext{
|
||||
Client: MockClient{err: err},
|
||||
BroadcastMode: mode,
|
||||
}
|
||||
}
|
||||
|
||||
// Test the correct code is returned when
|
||||
func TestBroadcastError(t *testing.T) {
|
||||
errors := map[error]uint32{
|
||||
mempool.ErrTxInCache: uint32(types.CodeTxInMempoolCache),
|
||||
mempool.ErrTxTooLarge{}: uint32(types.CodeTxTooLarge),
|
||||
mempool.ErrMempoolIsFull{}: uint32(types.CodeMempoolIsFull),
|
||||
}
|
||||
|
||||
modes := []string{
|
||||
flags.BroadcastAsync,
|
||||
flags.BroadcastBlock,
|
||||
flags.BroadcastSync,
|
||||
}
|
||||
|
||||
txBytes := []byte{0xA, 0xB}
|
||||
txHash := fmt.Sprintf("%X", tmhash.Sum(txBytes))
|
||||
|
||||
for _, mode := range modes {
|
||||
for err, code := range errors {
|
||||
ctx := CreateContextWithErrorAndMode(err, mode)
|
||||
resp, returnedErr := ctx.BroadcastTx(txBytes)
|
||||
require.NoError(t, returnedErr)
|
||||
require.Equal(t, code, resp.Code)
|
||||
require.Equal(t, txHash, resp.TxHash)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user