Skip to content

Commit

Permalink
refactor auths broadcast cmd in alignment with #6216
Browse files Browse the repository at this point in the history
  • Loading branch information
clevinson committed Jul 14, 2020
1 parent 8a62e1a commit 7a305ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
22 changes: 11 additions & 11 deletions client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// based on the context parameters. The result of the broadcast is parsed into
// an intermediate structure which is logged if the context has a logger
// defined.
func (ctx Context) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
func (ctx Context) BroadcastTx(txBytes []byte) (res *sdk.TxResponse, err error) {
switch ctx.BroadcastMode {
case flags.BroadcastSync:
res, err = ctx.BroadcastTxSync(txBytes)
Expand All @@ -28,7 +28,7 @@ func (ctx Context) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
res, err = ctx.BroadcastTxCommit(txBytes)

default:
return sdk.TxResponse{}, fmt.Errorf("unsupported return type %s; supported types: sync, async, block", ctx.BroadcastMode)
return nil, fmt.Errorf("unsupported return type %s; supported types: sync, async, block", ctx.BroadcastMode)
}

return res, err
Expand Down Expand Up @@ -84,16 +84,16 @@ func CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
// NOTE: This should ideally not be used as the request may timeout but the tx
// may still be included in a block. Use BroadcastTxAsync or BroadcastTxSync
// instead.
func (ctx Context) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxCommit(txBytes []byte) (*sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
return nil, err
}

res, err := node.BroadcastTxCommit(txBytes)
if err != nil {
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
return errRes, nil
}

return sdk.NewResponseFormatBroadcastTxCommit(res), err
Expand All @@ -112,31 +112,31 @@ func (ctx Context) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error) {

// BroadcastTxSync broadcasts transaction bytes to a Tendermint node
// synchronously (i.e. returns after CheckTx execution).
func (ctx Context) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxSync(txBytes []byte) (*sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
return nil, err
}

res, err := node.BroadcastTxSync(txBytes)
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
return errRes, nil
}

return sdk.NewResponseFormatBroadcastTx(res), err
}

// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
// asynchronously (i.e. returns immediately).
func (ctx Context) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx Context) BroadcastTxAsync(txBytes []byte) (*sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
return nil, err
}

res, err := node.BroadcastTxAsync(txBytes)
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
return errRes, nil
}

return sdk.NewResponseFormatBroadcastTx(res), err
Expand Down
23 changes: 12 additions & 11 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ func NewResponseResultTx(res *ctypes.ResultTx, tx Tx, timestamp string) TxRespon

// NewResponseFormatBroadcastTxCommit returns a TxResponse given a
// ResultBroadcastTxCommit from tendermint.
func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) TxResponse {
func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return TxResponse{}
// TODO: Clarify whether or not this should return nil
return &TxResponse{}
}

if !res.CheckTx.IsOK() {
Expand All @@ -122,9 +123,9 @@ func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) TxR
return newTxResponseDeliverTx(res)
}

func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return TxResponse{}
return nil
}

var txHash string
Expand All @@ -134,7 +135,7 @@ func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {

parsedLogs, _ := ParseABCILogs(res.CheckTx.Log)

return TxResponse{
return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.CheckTx.Codespace,
Expand All @@ -148,9 +149,9 @@ func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
}
}

func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return TxResponse{}
return nil
}

var txHash string
Expand All @@ -160,7 +161,7 @@ func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {

parsedLogs, _ := ParseABCILogs(res.DeliverTx.Log)

return TxResponse{
return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.DeliverTx.Codespace,
Expand All @@ -175,14 +176,14 @@ func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
}

// NewResponseFormatBroadcastTx returns a TxResponse given a ResultBroadcastTx from tendermint
func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) TxResponse {
func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) *TxResponse {
if res == nil {
return TxResponse{}
return &TxResponse{}
}

parsedLogs, _ := ParseABCILogs(res.Log)

return TxResponse{
return &TxResponse{
Code: res.Code,
Codespace: res.Codespace,
Data: res.Data.String(),
Expand Down
8 changes: 4 additions & 4 deletions types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,20 @@ func TestResponseResultTx(t *testing.T) {
Log: `[]`,
Hash: bytes.HexBytes([]byte("test")),
}
require.Equal(t, sdk.TxResponse{
require.Equal(t, &sdk.TxResponse{
Code: 1,
Codespace: "codespace",
Data: "64617461",
RawLog: `[]`,
Logs: logs,
TxHash: "74657374",
}, sdk.NewResponseFormatBroadcastTx(resultBroadcastTx))
require.Equal(t, sdk.TxResponse{}, sdk.NewResponseFormatBroadcastTx(nil))
require.Equal(t, &sdk.TxResponse{}, sdk.NewResponseFormatBroadcastTx(nil))
}

func TestResponseFormatBroadcastTxCommit(t *testing.T) {
// test nil
require.Equal(t, sdk.TxResponse{}, sdk.NewResponseFormatBroadcastTxCommit(nil))
require.Equal(t, &sdk.TxResponse{}, sdk.NewResponseFormatBroadcastTxCommit(nil))

logs, err := sdk.ParseABCILogs(`[]`)
require.NoError(t, err)
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestResponseFormatBroadcastTxCommit(t *testing.T) {
},
}

want := sdk.TxResponse{
want := &sdk.TxResponse{
Height: 10,
TxHash: "74657374",
Codespace: "codespace",
Expand Down

0 comments on commit 7a305ab

Please sign in to comment.