Skip to content

Commit

Permalink
feat: add codespace to broadcast response (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongwoo Lee authored May 14, 2020
1 parent 432b26d commit c4a62a7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
15 changes: 9 additions & 6 deletions client/context/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@ func CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
switch {
case strings.Contains(errStr, strings.ToLower(mempool.ErrTxInCache.Error())):
return &sdk.TxResponse{
Code: sdkerrors.ErrTxInMempoolCache.ABCICode(),
TxHash: txHash,
Code: sdkerrors.ErrTxInMempoolCache.ABCICode(),
Codespace: sdkerrors.ErrTxInMempoolCache.Codespace(),
TxHash: txHash,
}

case strings.Contains(errStr, "mempool is full"):
return &sdk.TxResponse{
Code: sdkerrors.ErrMempoolIsFull.ABCICode(),
TxHash: txHash,
Code: sdkerrors.ErrMempoolIsFull.ABCICode(),
Codespace: sdkerrors.ErrMempoolIsFull.Codespace(),
TxHash: txHash,
}

case strings.Contains(errStr, "tx too large"):
return &sdk.TxResponse{
Code: sdkerrors.ErrTxTooLarge.ABCICode(),
TxHash: txHash,
Code: sdkerrors.ErrTxTooLarge.ABCICode(),
Codespace: sdkerrors.ErrTxTooLarge.Codespace(),
TxHash: txHash,
}

default:
Expand Down
1 change: 1 addition & 0 deletions client/context/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestBroadcastError(t *testing.T) {
resp, returnedErr := ctx.BroadcastTx(txBytes)
require.NoError(t, returnedErr)
require.Equal(t, code, resp.Code)
require.NotEmpty(t, resp.Codespace)
require.Equal(t, txHash, resp.TxHash)
}
}
Expand Down
11 changes: 6 additions & 5 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) TxResponse {
parsedLogs, _ := ParseABCILogs(res.Log)

return TxResponse{
Code: res.Code,
Data: res.Data.String(),
RawLog: res.Log,
Logs: parsedLogs,
TxHash: res.Hash.String(),
Code: res.Code,
Codespace: res.Codespace,
Data: res.Data.String(),
RawLog: res.Log,
Logs: parsedLogs,
TxHash: res.Hash.String(),
}
}

Expand Down
24 changes: 24 additions & 0 deletions types/result_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package types

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/require"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/codec"
)
Expand All @@ -27,3 +29,25 @@ func TestABCIMessageLog(t *testing.T) {
require.NoError(t, err)
require.Equal(t, string(bz), msgLogs.String())
}

func TestNewResponseFormatBroadcastTx(t *testing.T) {
hash, err := hex.DecodeString("00000000000000000000000000000000")
require.NoError(t, err)
result := ctypes.ResultBroadcastTx{
Code: 1,
Data: []byte("some data"),
Log: `[{"log":"","msg_index":1,"success":true}]`,
Codespace: "codespace",
Hash: hash,
}

txResponse := NewResponseFormatBroadcastTx(&result)

require.NoError(t, err)
require.Equal(t, result.Code, txResponse.Code)
require.Equal(t, result.Data.String(), txResponse.Data)
require.NotEmpty(t, txResponse.Logs)
require.Equal(t, result.Log, txResponse.RawLog)
require.Equal(t, result.Codespace, txResponse.Codespace)
require.Equal(t, result.Hash.String(), txResponse.TxHash)
}

0 comments on commit c4a62a7

Please sign in to comment.