Skip to content

Commit

Permalink
Merge PR cosmos#3451: Make tags and responses legible
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzampolin authored Feb 5, 2019
1 parent f15ad04 commit 5e35354
Show file tree
Hide file tree
Showing 23 changed files with 316 additions and 286 deletions.
3 changes: 2 additions & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ BREAKING CHANGES
- [\#3465](https://github.com/cosmos/cosmos-sdk/issues/3465) `gaiacli rest-server` switched back to insecure mode by default:
- `--insecure` flag is removed.
- `--tls` is now used to enable secure layer.
- [\#3451](https://github.com/cosmos/cosmos-sdk/pull/3451) `gaiacli` now returns transactions in plain text including tags.

* Gaia
* [\#3457](https://github.com/cosmos/cosmos-sdk/issues/3457) Changed governance tally validatorGovInfo to use sdk.Int power instead of sdk.Dec
Expand Down Expand Up @@ -86,7 +87,7 @@ BUG FIXES
- [\#3345](https://github.com/cosmos/cosmos-sdk/issues/3345) Upgrade ledger-cosmos-go dependency to v0.9.3 to pull
https://github.com/ZondaX/ledger-cosmos-go/commit/ed9aa39ce8df31bad1448c72d3d226bf2cb1a8d1 in order to fix a derivation path issue that causes `gaiacli keys add --recover`
to malfunction.
- [\#3419](https://github.com/cosmos/cosmos-sdk/pull/3419) Fix `q distr slashes` panic
- [\#3419](https://github.com/cosmos/cosmos-sdk/pull/3419) Fix `q distr slashes` panic
- [\#3453](https://github.com/cosmos/cosmos-sdk/pull/3453) The `rest-server` command didn't respect persistent flags such as `--chain-id` and `--trust-node` if they were
passed on the command line.

Expand Down
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re

// Append Data and Tags
data = append(data, msgResult.Data...)
tags = append(tags, sdk.MakeTag(sdk.TagAction, []byte(msg.Type())))
tags = append(tags, sdk.MakeTag(sdk.TagAction, msg.Type()))
tags = append(tags, msgResult.Tags...)

// Stop execution and return on first failed message.
Expand Down
140 changes: 25 additions & 115 deletions client/context/broadcast.go
Original file line number Diff line number Diff line change
@@ -1,170 +1,80 @@
package context

import (
"fmt"
"io"

"github.com/pkg/errors"

abci "github.com/tendermint/tendermint/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TODO: This should get deleted eventually, and perhaps
// ctypes.ResultBroadcastTx be stripped of unused fields, and
// ctypes.ResultBroadcastTxCommit returned for tendermint RPC BroadcastTxSync.
//
// The motivation is that we want a unified type to return, and the better
// option is the one that can hold CheckTx/DeliverTx responses optionally.
func resultBroadcastTxToCommit(res *ctypes.ResultBroadcastTx) *ctypes.ResultBroadcastTxCommit {
return &ctypes.ResultBroadcastTxCommit{
Hash: res.Hash,
// NOTE: other fields are unused for async.
}
}

// BroadcastTx broadcasts a transactions either synchronously or asynchronously
// 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 CLIContext) BroadcastTx(txBytes []byte) (*ctypes.ResultBroadcastTxCommit, error) {
func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
if ctx.Async {
res, err := ctx.broadcastTxAsync(txBytes)
if err != nil {
return nil, err
if res, err = ctx.BroadcastTxAsync(txBytes); err != nil {
return
}
return
}

resCommit := resultBroadcastTxToCommit(res)
return resCommit, err
if res, err = ctx.BroadcastTxAndAwaitCommit(txBytes); err != nil {
return
}

return ctx.broadcastTxCommit(txBytes)
return
}

// BroadcastTxAndAwaitCommit broadcasts transaction bytes to a Tendermint node
// and waits for a commit.
func (ctx CLIContext) BroadcastTxAndAwaitCommit(tx []byte) (*ctypes.ResultBroadcastTxCommit, error) {
func (ctx CLIContext) BroadcastTxAndAwaitCommit(tx []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxCommit(tx)
if err != nil {
return res, err
return sdk.NewResponseFormatBroadcastTxCommit(res), err
}

if !res.CheckTx.IsOK() {
return res, errors.Errorf(res.CheckTx.Log)
return sdk.NewResponseFormatBroadcastTxCommit(res), errors.Errorf(res.CheckTx.Log)
}

if !res.DeliverTx.IsOK() {
return res, errors.Errorf(res.DeliverTx.Log)
return sdk.NewResponseFormatBroadcastTxCommit(res), errors.Errorf(res.DeliverTx.Log)
}

return res, err
return sdk.NewResponseFormatBroadcastTxCommit(res), err
}

// BroadcastTxSync broadcasts transaction bytes to a Tendermint node
// synchronously.
func (ctx CLIContext) BroadcastTxSync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
// BroadcastTxSync broadcasts transaction bytes to a Tendermint node synchronously.
func (ctx CLIContext) BroadcastTxSync(tx []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxSync(tx)
if err != nil {
return res, err
return sdk.NewResponseFormatBroadcastTx(res), err
}

return res, err
return sdk.NewResponseFormatBroadcastTx(res), err
}

// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
// asynchronously.
func (ctx CLIContext) BroadcastTxAsync(tx []byte) (*ctypes.ResultBroadcastTx, error) {
// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node asynchronously.
func (ctx CLIContext) BroadcastTxAsync(tx []byte) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return nil, err
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxAsync(tx)
if err != nil {
return res, err
}

return res, err
}

func (ctx CLIContext) broadcastTxAsync(txBytes []byte) (*ctypes.ResultBroadcastTx, error) {
res, err := ctx.BroadcastTxAsync(txBytes)
if err != nil {
return res, err
}

if ctx.Output != nil {
if ctx.OutputFormat == "json" {
type toJSON struct {
TxHash string
}

resJSON := toJSON{res.Hash.String()}
bz, err := ctx.Codec.MarshalJSON(resJSON)
if err != nil {
return res, err
}

ctx.Output.Write(bz)
io.WriteString(ctx.Output, "\n")
} else {
io.WriteString(ctx.Output, fmt.Sprintf("async tx sent (tx hash: %s)\n", res.Hash))
}
}

return res, nil
}

func (ctx CLIContext) broadcastTxCommit(txBytes []byte) (*ctypes.ResultBroadcastTxCommit, error) {
res, err := ctx.BroadcastTxAndAwaitCommit(txBytes)
if err != nil {
return res, err
}

if ctx.OutputFormat == "json" {
// Since JSON is intended for automated scripts, always include response in
// JSON mode.
type toJSON struct {
Height int64
TxHash string
Response abci.ResponseDeliverTx
}

if ctx.Output != nil {
resJSON := toJSON{res.Height, res.Hash.String(), res.DeliverTx}
bz, err := ctx.Codec.MarshalJSON(resJSON)
if err != nil {
return res, err
}

ctx.Output.Write(bz)
io.WriteString(ctx.Output, "\n")
}

return res, nil
}

if ctx.Output != nil {
resStr := fmt.Sprintf("Committed at block %d (tx hash: %s)\n", res.Height, res.Hash.String())

if ctx.PrintResponse {
resStr = fmt.Sprintf("Committed at block %d (tx hash: %s, response: %+v)\n",
res.Height, res.Hash.String(), res.DeliverTx,
)
}

io.WriteString(ctx.Output, resStr)
return sdk.NewResponseFormatBroadcastTx(res), err
}

return res, nil
return sdk.NewResponseFormatBroadcastTx(res), err
}
Loading

0 comments on commit 5e35354

Please sign in to comment.