Skip to content

Commit

Permalink
Switch to txraw
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Nov 30, 2020
1 parent 846946f commit 52a9860
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 119 deletions.
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

proto-gen:
@echo "Generating Protobuf files"
Expand Down
70 changes: 35 additions & 35 deletions proto/cosmos/tx/v1beta1/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ 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";

// Service defines a gRPC service for interacting with transactions.
service Service {
// Simulate simulates executing a transaction for estimating gas usage.
rpc Simulate(SimulateRequest) returns (SimulateResponse) {
option (google.api.http).post = "/cosmos/tx/v1beta1/simulate";
}
// GetTx fetches a tx by hash.
rpc GetTx(GetTxRequest) returns (GetTxResponse) {
option (google.api.http).get = "/cosmos/tx/v1beta1/tx/{hash}";
}
// 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";
}
// Simulate simulates executing a transaction for estimating gas usage.
rpc Simulate(SimulateRequest) returns (SimulateResponse) {
option (google.api.http).post = "/cosmos/tx/v1beta1/simulate";
}
// GetTx fetches a tx by hash.
rpc GetTx(GetTxRequest) returns (GetTxResponse) {
option (google.api.http).get = "/cosmos/tx/v1beta1/tx/{hash}";
}
// 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";
}
}

// GetTxsEventRequest is the request type for the Service.TxsByEvents
Expand All @@ -52,36 +53,35 @@ message GetTxsEventResponse {
// BroadcastTxRequest is the request type for the Service.BroadcastTxRequest
// RPC method.
message BroadcastTxRequest {
// tx_raw is the raw transaction.
bytes tx_raw = 1;
BroadcastMode mode = 2;
// tx_raw is the raw transaction.
cosmos.tx.v1beta1.TxRaw tx_raw = 1;
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"];
// 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 is the queried transaction.
cosmos.tx.v1beta1.Tx tx = 1;
// tx_response is the queried TxResponses.
cosmos.base.abci.v1beta1.TxResponse tx_response = 2;
// 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;
}


// SimulateRequest is the request type for the Service.Simulate
// RPC method.
message SimulateRequest {
Expand Down
2 changes: 1 addition & 1 deletion types/query/query.pb.go

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

133 changes: 70 additions & 63 deletions types/tx/service.pb.go

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

25 changes: 9 additions & 16 deletions x/auth/tx/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -204,11 +205,15 @@ func (s txServer) BroadcastTx(ctx context.Context, req *txtypes.BroadcastTxReque
}

clientCtx := s.clientCtx.WithBroadcastMode(normalizeBoradcastMode((req.Mode)))
resp, err := clientCtx.BroadcastTx(req.TxRaw)

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
Expand All @@ -221,20 +226,8 @@ func (s txServer) BroadcastTx(ctx context.Context, req *txtypes.BroadcastTxReque
return nil, err
}
return &txtypes.BroadcastTxResponse{
Tx: &protoTx,
TxResponse: &sdk.TxResponse{
Code: resp.Code,
Codespace: resp.Codespace,
Data: resp.Data,
GasUsed: resp.GasUsed,
GasWanted: resp.GasWanted,
Height: resp.Height,
Info: resp.Info,
RawLog: resp.RawLog,
Timestamp: resp.Timestamp,
TxHash: resp.TxHash,
Logs: resp.Logs,
},
Tx: &protoTx,
TxResponse: resp,
}, nil

}
Expand Down
14 changes: 11 additions & 3 deletions x/auth/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
clienttx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
Expand Down Expand Up @@ -222,19 +223,26 @@ func (s IntegrationTestSuite) TestBroadcastTx() {
err := authclient.SignTx(txFactory, val.ClientCtx, val.Moniker, txBuilder, false)
s.Require().NoError(err)

// To get the TxRaw to be broadcasted, we unforunately need to first encode
// the tx into bytes, then decode it into TxRaw.
txBytes, err := val.ClientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
fmt.Println(string(txBytes))
s.Require().NoError(err)
// Create a proto codec, we need it to unmarshal the tx bytes.
cdc := codec.NewProtoCodec(val.ClientCtx.InterfaceRegistry)
var txRaw tx.TxRaw
err = cdc.UnmarshalBinaryBare(txBytes, &txRaw)
s.Require().NoError(err)

// Query the tx via gRPC.
grpcRes, err := s.queryClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_async,
TxRaw: txBytes,
Mode: tx.BroadcastMode_sync,
TxRaw: &txRaw,
},
)
s.Require().NoError(err)

s.Require().Equal("foobar", grpcRes.Tx.Body.Memo)
}

Expand Down

0 comments on commit 52a9860

Please sign in to comment.