Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor auths broadcast cmd in alignment with #6216 #6713

Merged
merged 10 commits into from
Jul 17, 2020
Merged
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
15 changes: 15 additions & 0 deletions codec/types/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ func (any *Any) Pack(x proto.Message) error {
return nil
}

// UnsafePackAny packs the value x in the Any and instead of returning the error
// in the case of a packing failure, keeps the cached value. This should only
// be used in situations where compatibility is needed with amino. Amino-only
// values can safely be packed using this method when they will only be
// marshaled with amino and not protobuf
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing puncuation.

func UnsafePackAny(x interface{}) *Any {
if msg, ok := x.(proto.Message); ok {
any, err := NewAnyWithValue(msg)
if err != nil {
return any
}
}
return &Any{cachedValue: x}
}

// GetCachedValue returns the cached value from the Any if present
func (any *Any) GetCachedValue() interface{} {
return any.cachedValue
Expand Down
48 changes: 48 additions & 0 deletions proto/cosmos/cosmos.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cosmos;

import "gogoproto/gogo.proto";
import "tendermint/abci/types/types.proto";
import "google/protobuf/any.proto";

option go_package = "github.com/cosmos/cosmos-sdk/types";
option (gogoproto.goproto_stringer_all) = false;
Expand Down Expand Up @@ -94,3 +95,50 @@ message TxData {

repeated MsgData data = 1;
}

// TxResponse defines a structure containing relevant tx data and metadata. The
// tags are stringified and the log is JSON decoded.
message TxResponse {
option (gogoproto.goproto_getters) = false;

int64 height = 1;
string txhash = 2 [(gogoproto.customname) = "TxHash"];
string codespace = 3;
uint32 code = 4;
string data = 5;
string raw_log = 6;
repeated ABCIMessageLog logs = 7 [(gogoproto.castrepeated) = "ABCIMessageLogs", (gogoproto.nullable) = false];
string info = 8;
int64 gas_wanted = 9;
int64 gas_used = 10;
google.protobuf.Any tx = 11;
string timestamp = 12;
}
Comment on lines +101 to +116
Copy link
Member

@aaronc aaronc Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do want to note that I do think this stuff should live in the root cosmos proto package. I'm not sure where it should go but I don't feel comfortable with it here. Any thoughts?

I'd also like to take out the other Result, SimulationResponse, etc. stuff and leave just Coin, DecCoin & Int/DecProto preferably before 0.40 is done.

Copy link
Contributor Author

@clevinson clevinson Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. I can tackle this as another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍


// ABCIMessageLog defines a structure containing an indexed tx ABCI message log.
message ABCIMessageLog {
option (gogoproto.stringer) = true;

uint32 msg_index = 1;
string log = 2;

// Events contains a slice of Event objects that were emitted during some
// execution.
repeated StringEvent events = 3 [(gogoproto.castrepeated) = "StringEvents", (gogoproto.nullable) = false];
}

// StringAttribute defines en Event object wrapper where all the attributes
// contain key/value pairs that are strings instead of raw bytes.
message StringEvent {
option (gogoproto.stringer) = true;

string type = 1;
repeated Attribute attributes = 2 [(gogoproto.nullable) = false];
}

// Attribute defines an attribute wrapper where the key and value are
// strings instead of raw bytes.
message Attribute {
string key = 1;
string value = 2;
}
Loading