@@ -24,109 +24,101 @@ var _ gogogrpc.ClientConn = Context{}
2424var protoCodec = encoding .GetCodec (proto .Name )
2525
2626// Invoke implements the grpc ClientConn.Invoke method
27- func (ctx Context ) Invoke (grpcCtx gocontext.Context , method string , req , reply interface {}, opts ... grpc.CallOption ) (err error ) {
27+ func (ctx Context ) Invoke (grpcCtx gocontext.Context , method string , args , reply interface {}, opts ... grpc.CallOption ) (err error ) {
2828 // Two things can happen here:
2929 // 1. either we're broadcasting a Tx, in which call we call Tendermint's broadcast endpoint directly,
3030 // 2. or we are querying for state, in which case we call ABCI's Query.
3131
32- // In both cases, we don't allow empty request req (it will panic unexpectedly).
33- if reflect .ValueOf (req ).IsNil () {
32+ // In both cases, we don't allow empty request args (it will panic unexpectedly).
33+ if reflect .ValueOf (args ).IsNil () {
3434 return sdkerrors .Wrap (sdkerrors .ErrInvalidRequest , "request cannot be nil" )
3535 }
3636
3737 // Case 1. Broadcasting a Tx.
38- if reqProto , ok := req .(* tx.BroadcastTxRequest ); ok {
38+ if isBroadcast (method ) {
39+ req , ok := args .(* tx.BroadcastTxRequest )
3940 if ! ok {
40- return sdkerrors .Wrapf (sdkerrors .ErrInvalidRequest , "expected %T, got %T" , (* tx .BroadcastTxRequest )(nil ), req )
41+ return sdkerrors .Wrapf (sdkerrors .ErrInvalidRequest , "expected %T, got %T" , (* tx .BroadcastTxRequest )(nil ), args )
4142 }
42- resProto , ok := reply .(* tx.BroadcastTxResponse )
43+ res , ok := reply .(* tx.BroadcastTxResponse )
4344 if ! ok {
44- return sdkerrors .Wrapf (sdkerrors .ErrInvalidRequest , "expected %T, got %T" , (* tx .BroadcastTxResponse )(nil ), req )
45+ return sdkerrors .Wrapf (sdkerrors .ErrInvalidRequest , "expected %T, got %T" , (* tx .BroadcastTxResponse )(nil ), args )
4546 }
4647
47- broadcastRes , err := TxServiceBroadcast (grpcCtx , ctx , reqProto )
48+ broadcastRes , err := TxServiceBroadcast (grpcCtx , ctx , req )
4849 if err != nil {
4950 return err
5051 }
51- * resProto = * broadcastRes
52+ * res = * broadcastRes
5253
5354 return err
5455 }
5556
5657 // Case 2. Querying state.
57- inMd , _ := metadata .FromOutgoingContext (grpcCtx )
58- abciRes , outMd , err := RunGRPCQuery (ctx , grpcCtx , method , req , inMd )
58+ reqBz , err := protoCodec .Marshal (args )
5959 if err != nil {
6060 return err
6161 }
6262
63- err = protoCodec .Unmarshal (abciRes .Value , reply )
64- if err != nil {
65- return err
66- }
67-
68- for _ , callOpt := range opts {
69- header , ok := callOpt .(grpc.HeaderCallOption )
70- if ! ok {
71- continue
72- }
73-
74- * header .HeaderAddr = outMd
75- }
76-
77- if ctx .InterfaceRegistry != nil {
78- return types .UnpackInterfaces (reply , ctx .InterfaceRegistry )
79- }
80-
81- return nil
82- }
83-
84- // NewStream implements the grpc ClientConn.NewStream method
85- func (Context ) NewStream (gocontext.Context , * grpc.StreamDesc , string , ... grpc.CallOption ) (grpc.ClientStream , error ) {
86- return nil , fmt .Errorf ("streaming rpc not supported" )
87- }
88-
89- // RunGRPCQuery runs a gRPC query from the clientCtx, given all necessary
90- // arguments for the gRPC method, and returns the ABCI response. It is used
91- // to factorize code between client (Invoke) and server (RegisterGRPCServer)
92- // gRPC handlers.
93- func RunGRPCQuery (ctx Context , grpcCtx gocontext.Context , method string , req interface {}, md metadata.MD ) (abci.ResponseQuery , metadata.MD , error ) {
94- reqBz , err := protoCodec .Marshal (req )
95- if err != nil {
96- return abci.ResponseQuery {}, nil , err
97- }
98-
9963 // parse height header
64+ md , _ := metadata .FromOutgoingContext (grpcCtx )
10065 if heights := md .Get (grpctypes .GRPCBlockHeightHeader ); len (heights ) > 0 {
10166 height , err := strconv .ParseInt (heights [0 ], 10 , 64 )
10267 if err != nil {
103- return abci. ResponseQuery {}, nil , err
68+ return err
10469 }
10570 if height < 0 {
106- return abci. ResponseQuery {}, nil , sdkerrors .Wrapf (
71+ return sdkerrors .Wrapf (
10772 sdkerrors .ErrInvalidRequest ,
10873 "client.Context.Invoke: height (%d) from %q must be >= 0" , height , grpctypes .GRPCBlockHeightHeader )
10974 }
11075
11176 ctx = ctx .WithHeight (height )
11277 }
11378
114- abciReq := abci.RequestQuery {
115- Path : method ,
116- Data : reqBz ,
79+ req := abci.RequestQuery {
80+ Path : method ,
81+ Data : reqBz ,
82+ Height : ctx .Height ,
11783 }
11884
119- abciRes , err := ctx .QueryABCI (abciReq )
85+ res , err := ctx .QueryABCI (req )
12086 if err != nil {
121- return abci.ResponseQuery {}, nil , err
87+ return err
88+ }
89+
90+ err = protoCodec .Unmarshal (res .Value , reply )
91+ if err != nil {
92+ return err
12293 }
12394
12495 // Create header metadata. For now the headers contain:
12596 // - block height
12697 // We then parse all the call options, if the call option is a
12798 // HeaderCallOption, then we manually set the value of that header to the
12899 // metadata.
129- md = metadata .Pairs (grpctypes .GRPCBlockHeightHeader , strconv .FormatInt (abciRes .Height , 10 ))
100+ md = metadata .Pairs (grpctypes .GRPCBlockHeightHeader , strconv .FormatInt (res .Height , 10 ))
101+ for _ , callOpt := range opts {
102+ header , ok := callOpt .(grpc.HeaderCallOption )
103+ if ! ok {
104+ continue
105+ }
106+
107+ * header .HeaderAddr = md
108+ }
109+
110+ if ctx .InterfaceRegistry != nil {
111+ return types .UnpackInterfaces (reply , ctx .InterfaceRegistry )
112+ }
113+
114+ return nil
115+ }
116+
117+ // NewStream implements the grpc ClientConn.NewStream method
118+ func (Context ) NewStream (gocontext.Context , * grpc.StreamDesc , string , ... grpc.CallOption ) (grpc.ClientStream , error ) {
119+ return nil , fmt .Errorf ("streaming rpc not supported" )
120+ }
130121
131- return abciRes , md , nil
122+ func isBroadcast (method string ) bool {
123+ return method == "/cosmos.tx.v1beta1.Service/BroadcastTx"
132124}
0 commit comments