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

Add tx broadcast gRPC endpoint #7852

Merged
merged 40 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4d22fdf
WIP tx/broadcast grpc endpoint
aleem1314 Nov 9, 2020
6674921
fix lint
aleem1314 Nov 9, 2020
6134a9b
fix proto lint
aleem1314 Nov 9, 2020
0f09eb9
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 9, 2020
437b1bf
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 11, 2020
4871d32
Update service.proto
aleem1314 Nov 12, 2020
ccf6f0c
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 12, 2020
d65d3ee
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 12, 2020
abc49b8
resolve conflicts
aleem1314 Nov 12, 2020
05f6282
update service.proto
aleem1314 Nov 16, 2020
a0e5111
Update service.proto
aleem1314 Nov 16, 2020
dc8e1f3
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 16, 2020
c1d323e
review changes
aleem1314 Nov 18, 2020
1401b00
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into 73…
aleem1314 Nov 18, 2020
3ed9110
proto lint
aleem1314 Nov 18, 2020
75d7f8b
Merge branch 'master' into 7355-grpc-broadcast
aleem1314 Nov 23, 2020
c7031a6
Merge branch 'master' into 7355-grpc-broadcast
aleem1314 Nov 27, 2020
24030a4
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into 7355…
amaury1093 Nov 27, 2020
846946f
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into 7355…
amaury1093 Nov 30, 2020
52a9860
Switch to txraw
amaury1093 Nov 30, 2020
152022e
Add check breaking at the end
amaury1093 Nov 30, 2020
6e2c50d
Fix broadcast
amaury1093 Nov 30, 2020
c70daf7
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into 7355…
amaury1093 Nov 30, 2020
d2ec8f9
Send Msg on SetupSuite
amaury1093 Nov 30, 2020
4b323c4
Remove proto-check-breaking
amaury1093 Nov 30, 2020
48b8e4c
1 validator in test
amaury1093 Nov 30, 2020
41039b0
Add grpc server tests for broadcast
amaury1093 Nov 30, 2020
ce8f657
Fix grpc server tests
amaury1093 Nov 30, 2020
04cdc23
Merge branch 'master' into 7355-grpc-broadcast
amaury1093 Nov 30, 2020
4fb6f7f
Add some changes
amaury1093 Dec 1, 2020
d5152d8
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into 7355…
amaury1093 Dec 1, 2020
e2c75f1
Add ress comments
amaury1093 Dec 1, 2020
aade13d
Add table tests for tx service
amaury1093 Dec 1, 2020
00683e5
Add test for mode
amaury1093 Dec 1, 2020
b37c04c
Add simulate tests
amaury1093 Dec 1, 2020
8803107
Add build flag back
amaury1093 Dec 1, 2020
57cc0e8
Revert custom stringer for enum
amaury1093 Dec 2, 2020
3113060
Remove stray logs
amaury1093 Dec 2, 2020
64d4719
Merge branch 'master' into 7355-grpc-broadcast
alexanderbez Dec 2, 2020
7c3ad41
Use /txs/{hash}
amaury1093 Dec 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ devdoc-update:
### Protobuf ###
###############################################################################

proto-all: proto-format proto-lint proto-check-breaking proto-gen
proto-all: proto-format proto-lint proto-gen
Copy link
Contributor

Choose a reason for hiding this comment

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

ref: #8021


proto-gen:
@echo "Generating Protobuf files"
Expand Down
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")
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}

clientCtx = clientCtx.WithBroadcastMode(normalizeBoradcastMode((req.Mode)))
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
txBytes, err := proto.Marshal(req.TxRaw)
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
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 {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
switch mode {
case tx.BroadcastMode_async:
return "async"
case tx.BroadcastMode_sync:
return "sync"
case tx.BroadcastMode_block:
return "block"
default:
return "sync"
}
}
31 changes: 28 additions & 3 deletions client/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,43 @@ 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" {
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
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)
if err != nil {
return err
}
*res = *broadcastRes

return err
}

// Case 2. Querying state.
reqBz, err := protoCodec.Marshal(args)
if err != nil {
return err
Expand Down
37 changes: 36 additions & 1 deletion proto/cosmos/tx/v1beta1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package cosmos.tx.v1beta1;
import "google/api/annotations.proto";
import "cosmos/base/abci/v1beta1/abci.proto";
import "cosmos/tx/v1beta1/tx.proto";
import "gogoproto/gogo.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos/tx/v1beta1/tx.proto";

option go_package = "github.com/cosmos/cosmos-sdk/types/tx";

Expand All @@ -18,7 +20,10 @@ service Service {
rpc GetTx(GetTxRequest) returns (GetTxResponse) {
option (google.api.http).get = "/cosmos/tx/v1beta1/tx/{hash}";
Copy link
Contributor

Choose a reason for hiding this comment

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

not related to this PR, but I also noticed this, we could also:

- "/cosmos/tx/v1beta1/tx/{hash}"
+ "/cosmos/tx/v1beta1/txs/{hash}"

this matches what we do in modules, e.g. "/cosmos/staking/v1beta1/validators/{validator_addr}", wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

ACK.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done in 7c3ad41

}

// BroadcastTx broadcast transaction.
rpc BroadcastTx(BroadcastTxRequest) returns (BroadcastTxResponse) {
option (google.api.http).post = "/cosmos/tx/v1beta1/txs";
}
// GetTxsEvent fetches txs by event.
rpc GetTxsEvent(GetTxsEventRequest) returns (GetTxsEventResponse) {
option (google.api.http).get = "/cosmos/tx/v1beta1/txs";
Expand All @@ -45,6 +50,36 @@ message GetTxsEventResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 3;
}

// BroadcastTxRequest is the request type for the Service.BroadcastTxRequest
// RPC method.
message BroadcastTxRequest {
// tx_raw is the raw transaction.
cosmos.tx.v1beta1.TxRaw tx_raw = 1;
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
BroadcastMode mode = 2;
}

// BroadcastMode specifies the broadcast mode for the TxService.Broadcast RPC method.
enum BroadcastMode {
// zero-value for mode ordering
BROADCAST_MODE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "none"];
// BROADCAST_MODE_BLOCK defines a tx broadcasting mode where the client waits for
// the tx to be committed in a block.
BROADCAST_MODE_BLOCK = 1 [(gogoproto.enumvalue_customname) = "block"];
// BROADCAST_MODE_SYNC defines a tx broadcasting mode where the client waits for
// a CheckTx execution response only.
BROADCAST_MODE_SYNC = 2 [(gogoproto.enumvalue_customname) = "sync"];
// BROADCAST_MODE_ASYNC defines a tx broadcasting mode where the client returns
// immediately.
BROADCAST_MODE_ASYNC = 3 [(gogoproto.enumvalue_customname) = "async"];
}

// BroadcastTxResponse is the response type for the
// Service.BroadcastTx method.
message BroadcastTxResponse {
// tx_response is the queried TxResponses.
cosmos.base.abci.v1beta1.TxResponse tx_response = 1;
}

// SimulateRequest is the request type for the Service.Simulate
// RPC method.
message SimulateRequest {
Expand Down
Loading