Skip to content

Commit

Permalink
Fix broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Nov 30, 2020
1 parent 152022e commit 6e2c50d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 165 deletions.
39 changes: 39 additions & 0 deletions client/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"fmt"
"strings"

"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/mempool"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx"
)

// BroadcastTx broadcasts a transactions either synchronously or asynchronously
Expand Down Expand Up @@ -142,3 +146,38 @@ func (ctx Context) BroadcastTxAsync(txBytes []byte) (*sdk.TxResponse, error) {

return sdk.NewResponseFormatBroadcastTx(res), err
}

// TxServiceBroadcast is a helper function to broadcast a Tx with the correct gRPC types
// from the tx service. Calls `clientCtx.BroadcastTx` under the hood.
func TxServiceBroadcast(grpcCtx context.Context, clientCtx Context, req *tx.BroadcastTxRequest) (*tx.BroadcastTxResponse, error) {
if req == nil || req.TxRaw == nil {
return nil, status.Error(codes.InvalidArgument, "invalid empty tx")
}

clientCtx = clientCtx.WithBroadcastMode(normalizeBoradcastMode((req.Mode)))
txBytes, err := proto.Marshal(req.TxRaw)
if err != nil {
return nil, err
}
resp, err := clientCtx.BroadcastTx(txBytes)
if err != nil {
return nil, err
}

return &tx.BroadcastTxResponse{
TxResponse: resp,
}, nil
}

func normalizeBoradcastMode(mode tx.BroadcastMode) string {
switch mode {
case tx.BroadcastMode_async:
return "async"
case tx.BroadcastMode_sync:
return "sync"
case tx.BroadcastMode_block:
return "block"
default:
return "sync"
}
}
28 changes: 25 additions & 3 deletions client/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,40 @@ import (
"google.golang.org/grpc/encoding/proto"
"google.golang.org/grpc/metadata"

grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"

"github.com/cosmos/cosmos-sdk/codec/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
"github.com/cosmos/cosmos-sdk/types/tx"
)

var _ gogogrpc.ClientConn = Context{}

var protoCodec = encoding.GetCodec(proto.Name)

// Invoke implements the grpc ClientConn.Invoke method
func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, args, reply interface{}, opts ...grpc.CallOption) error {
func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, args, reply interface{}, opts ...grpc.CallOption) (err error) {
// Two things can happen here:
// 1. either we're broadcasting a Tx, in which call we call Tendermint's broadcast endpoint directly,
// 2. or we are querying for state, in which case we call ABCI's Query.

// Case 1. Broadcasting a Tx.
if method == "/cosmos.tx.v1beta1.Service/BroadcastTx" {
req, ok := args.(*tx.BroadcastTxRequest)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected %T, got %T", (*tx.BroadcastTxRequest)(nil), args)
}
res, ok := reply.(*tx.BroadcastTxResponse)
if !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "expected %T, got %T", (*tx.BroadcastTxResponse)(nil), args)
}

broadcastRes, err := TxServiceBroadcast(grpcCtx, ctx, req)
*res = *broadcastRes

return err
}

// Case 2. Querying state.
reqBz, err := protoCodec.Marshal(args)
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions proto/cosmos/tx/v1beta1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ enum BroadcastMode {
// BroadcastTxResponse is the response type for the
// Service.BroadcastTx method.
message BroadcastTxResponse {
// tx is the queried transaction.
cosmos.tx.v1beta1.Tx tx = 1;
// tx_response is the queried TxResponses.
cosmos.base.abci.v1beta1.TxResponse tx_response = 2;
cosmos.base.abci.v1beta1.TxResponse tx_response = 1;
}

// SimulateRequest is the request type for the Service.Simulate
Expand Down
161 changes: 50 additions & 111 deletions types/tx/service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 1 addition & 44 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

gogogrpc "github.com/gogo/protobuf/grpc"
"github.com/gogo/protobuf/proto"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -186,50 +185,8 @@ func (s txServer) GetTx(ctx context.Context, req *txtypes.GetTxRequest) (*txtype
}, nil
}

func normalizeBoradcastMode(mode txtypes.BroadcastMode) string {
switch mode {
case txtypes.BroadcastMode_async:
return "async"
case txtypes.BroadcastMode_sync:
return "sync"
case txtypes.BroadcastMode_block:
return "block"
default:
return "sync"
}
}

func (s txServer) BroadcastTx(ctx context.Context, req *txtypes.BroadcastTxRequest) (*txtypes.BroadcastTxResponse, error) {
if req.TxRaw == nil {
return nil, status.Error(codes.InvalidArgument, "invalid empty tx")
}

clientCtx := s.clientCtx.WithBroadcastMode(normalizeBoradcastMode((req.Mode)))
txBytes, err := proto.Marshal(req.TxRaw)
if err != nil {
return nil, err
}
resp, err := clientCtx.BroadcastTx(txBytes)
if err != nil {
return nil, err
}

// Create a proto codec, we need it to unmarshal the tx bytes.
cdc := codec.NewProtoCodec(s.clientCtx.InterfaceRegistry)
var protoTx txtypes.Tx

bz, err := resp.Tx.Marshal()
if err != nil {
return nil, err
}
if err := cdc.UnmarshalBinaryBare(bz, &protoTx); err != nil {
return nil, err
}
return &txtypes.BroadcastTxResponse{
Tx: &protoTx,
TxResponse: resp,
}, nil

return client.TxServiceBroadcast(ctx, s.clientCtx, req)
}

// RegisterTxService registers the tx service on the gRPC router.
Expand Down
Loading

0 comments on commit 6e2c50d

Please sign in to comment.