You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
16 KiB

build(deps): Bump google.golang.org/grpc from 1.42.0 to 1.43.0 (#7455) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.42.0 to 1.43.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/grpc/grpc-go/releases">google.golang.org/grpc's releases</a>.</em></p> <blockquote> <h2>Release 1.43.0</h2> <h1>API Changes</h1> <ul> <li>grpc: stabilize <code>WithConnectParams</code> <code>DialOption</code> (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4915">#4915</a>) <ul> <li>Special Thanks: <a href="https://github.com/hypnoglow"><code>@​hypnoglow</code></a></li> </ul> </li> </ul> <h1>Behavior Changes</h1> <ul> <li>status: support wrapped errors in <code>FromContextError</code> (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4977">#4977</a>) <ul> <li>Special Thanks: <a href="https://github.com/bestbeforetoday"><code>@​bestbeforetoday</code></a></li> </ul> </li> <li>config: remove the environment variable to disable retry support (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4922">#4922</a>)</li> </ul> <h1>New Features</h1> <ul> <li>balancer: new field <code>Authority</code> in <code>BuildOptions</code> for server name to use in the authentication handshake with a remote load balancer (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4969">#4969</a>)</li> </ul> <h1>Bug Fixes</h1> <ul> <li>xds/resolver: fix possible <code>ClientConn</code> leak upon resolver initialization failure (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4900">#4900</a>)</li> <li>client: fix <code>nil</code> panic in rare race conditions with the pick first LB policy (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4971">#4971</a>)</li> <li>xds: improve RPC error messages when xDS connection errors occur (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5032">#5032</a>, <a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5054">#5054</a>)</li> <li>transport: do not create stream object in the face of illegal stream IDs (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4873">#4873</a>) <ul> <li>Special Thanks: <a href="https://github.com/uds5501"><code>@​uds5501</code></a></li> </ul> </li> </ul> <h1>Documentation</h1> <ul> <li>client: clarify errors to indicate whether compressed or uncompressed messages exceeded size limits (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4918">#4918</a>) <ul> <li>Special Thanks: <a href="https://github.com/uds5501"><code>@​uds5501</code></a></li> </ul> </li> </ul> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/grpc/grpc-go/commit/14c11384b76b67f7b1b32a5d18f865762634c0ae"><code>14c1138</code></a> Change version to 1.43.0 (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5039">#5039</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/ae29ac3e1e62a7b47fb672dec21de2dd83e4f027"><code>ae29ac3</code></a> xds/client: send NewStream errors to the watchers (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5032">#5032</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/296afc2e579c14228edadb3debcb040204a0870f"><code>296afc2</code></a> transport: better error message when per-RPC creds fail (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5033">#5033</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/e15d978c827661f8e7cfe807cb9fd8fd8f15a262"><code>e15d978</code></a> xds/client: send connection errors to all watchers (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5054">#5054</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/46e883a9ab006a3000afe0a510aea62b75495b8f"><code>46e883a</code></a> Backport &quot;xds/c2p: replace C2P resolver env var with experimental scheme suff...</li> <li><a href="https://github.com/grpc/grpc-go/commit/3786ae1778f5bfcc49da81cbd925b7175ec81839"><code>3786ae1</code></a> xds/resolver: Add support for cluster specifier plugins (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4987">#4987</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/512e89474bfbd567806e026e0afdec468b1ee1ea"><code>512e894</code></a> rls: support extra_keys and constant_keys (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4995">#4995</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/f3bbd12084380987ae0c9fcccdd7c4d405294a68"><code>f3bbd12</code></a> xds/bootstrap_config: add a string function to server config (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5031">#5031</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/46935b96506deef74e73192e70cc44e3795b1452"><code>46935b9</code></a> fix possible nil before casting (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5017">#5017</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/c2bccd0b1594416da57a74d15f09f8eb0a3d727b"><code>c2bccd0</code></a> xds/kokoro: install go 1.17, and retry go build (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5015">#5015</a>)</li> <li>Additional commits viewable in <a href="https://github.com/grpc/grpc-go/compare/v1.42.0...v1.43.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=google.golang.org/grpc&package-manager=go_modules&previous-version=1.42.0&new-version=1.43.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details>
3 years ago
build(deps): Bump google.golang.org/grpc from 1.42.0 to 1.43.0 (#7455) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.42.0 to 1.43.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github.com/grpc/grpc-go/releases">google.golang.org/grpc's releases</a>.</em></p> <blockquote> <h2>Release 1.43.0</h2> <h1>API Changes</h1> <ul> <li>grpc: stabilize <code>WithConnectParams</code> <code>DialOption</code> (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4915">#4915</a>) <ul> <li>Special Thanks: <a href="https://github.com/hypnoglow"><code>@​hypnoglow</code></a></li> </ul> </li> </ul> <h1>Behavior Changes</h1> <ul> <li>status: support wrapped errors in <code>FromContextError</code> (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4977">#4977</a>) <ul> <li>Special Thanks: <a href="https://github.com/bestbeforetoday"><code>@​bestbeforetoday</code></a></li> </ul> </li> <li>config: remove the environment variable to disable retry support (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4922">#4922</a>)</li> </ul> <h1>New Features</h1> <ul> <li>balancer: new field <code>Authority</code> in <code>BuildOptions</code> for server name to use in the authentication handshake with a remote load balancer (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4969">#4969</a>)</li> </ul> <h1>Bug Fixes</h1> <ul> <li>xds/resolver: fix possible <code>ClientConn</code> leak upon resolver initialization failure (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4900">#4900</a>)</li> <li>client: fix <code>nil</code> panic in rare race conditions with the pick first LB policy (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4971">#4971</a>)</li> <li>xds: improve RPC error messages when xDS connection errors occur (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5032">#5032</a>, <a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5054">#5054</a>)</li> <li>transport: do not create stream object in the face of illegal stream IDs (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4873">#4873</a>) <ul> <li>Special Thanks: <a href="https://github.com/uds5501"><code>@​uds5501</code></a></li> </ul> </li> </ul> <h1>Documentation</h1> <ul> <li>client: clarify errors to indicate whether compressed or uncompressed messages exceeded size limits (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4918">#4918</a>) <ul> <li>Special Thanks: <a href="https://github.com/uds5501"><code>@​uds5501</code></a></li> </ul> </li> </ul> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/grpc/grpc-go/commit/14c11384b76b67f7b1b32a5d18f865762634c0ae"><code>14c1138</code></a> Change version to 1.43.0 (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5039">#5039</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/ae29ac3e1e62a7b47fb672dec21de2dd83e4f027"><code>ae29ac3</code></a> xds/client: send NewStream errors to the watchers (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5032">#5032</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/296afc2e579c14228edadb3debcb040204a0870f"><code>296afc2</code></a> transport: better error message when per-RPC creds fail (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5033">#5033</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/e15d978c827661f8e7cfe807cb9fd8fd8f15a262"><code>e15d978</code></a> xds/client: send connection errors to all watchers (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5054">#5054</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/46e883a9ab006a3000afe0a510aea62b75495b8f"><code>46e883a</code></a> Backport &quot;xds/c2p: replace C2P resolver env var with experimental scheme suff...</li> <li><a href="https://github.com/grpc/grpc-go/commit/3786ae1778f5bfcc49da81cbd925b7175ec81839"><code>3786ae1</code></a> xds/resolver: Add support for cluster specifier plugins (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4987">#4987</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/512e89474bfbd567806e026e0afdec468b1ee1ea"><code>512e894</code></a> rls: support extra_keys and constant_keys (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/4995">#4995</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/f3bbd12084380987ae0c9fcccdd7c4d405294a68"><code>f3bbd12</code></a> xds/bootstrap_config: add a string function to server config (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5031">#5031</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/46935b96506deef74e73192e70cc44e3795b1452"><code>46935b9</code></a> fix possible nil before casting (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5017">#5017</a>)</li> <li><a href="https://github.com/grpc/grpc-go/commit/c2bccd0b1594416da57a74d15f09f8eb0a3d727b"><code>c2bccd0</code></a> xds/kokoro: install go 1.17, and retry go build (<a href="https://github-redirect.dependabot.com/grpc/grpc-go/issues/5015">#5015</a>)</li> <li>Additional commits viewable in <a href="https://github.com/grpc/grpc-go/compare/v1.42.0...v1.43.0">compare view</a></li> </ul> </details> <br /> [![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=google.golang.org/grpc&package-manager=go_modules&previous-version=1.42.0&new-version=1.43.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details>
3 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package abciclient
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "sync"
  7. "time"
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/credentials/insecure"
  10. "github.com/tendermint/tendermint/abci/types"
  11. "github.com/tendermint/tendermint/libs/log"
  12. tmnet "github.com/tendermint/tendermint/libs/net"
  13. "github.com/tendermint/tendermint/libs/service"
  14. )
  15. // A gRPC client.
  16. type grpcClient struct {
  17. service.BaseService
  18. logger log.Logger
  19. mustConnect bool
  20. client types.ABCIApplicationClient
  21. conn *grpc.ClientConn
  22. chReqRes chan *ReqRes // dispatches "async" responses to callbacks *in order*, needed by mempool
  23. mtx sync.Mutex
  24. addr string
  25. err error
  26. resCb func(*types.Request, *types.Response) // listens to all callbacks
  27. }
  28. var _ Client = (*grpcClient)(nil)
  29. // NewGRPCClient creates a gRPC client, which will connect to addr upon the
  30. // start. Note Client#Start returns an error if connection is unsuccessful and
  31. // mustConnect is true.
  32. //
  33. // GRPC calls are synchronous, but some callbacks expect to be called
  34. // asynchronously (eg. the mempool expects to be able to lock to remove bad txs
  35. // from cache). To accommodate, we finish each call in its own go-routine,
  36. // which is expensive, but easy - if you want something better, use the socket
  37. // protocol! maybe one day, if people really want it, we use grpc streams, but
  38. // hopefully not :D
  39. func NewGRPCClient(logger log.Logger, addr string, mustConnect bool) Client {
  40. cli := &grpcClient{
  41. logger: logger,
  42. addr: addr,
  43. mustConnect: mustConnect,
  44. // Buffering the channel is needed to make calls appear asynchronous,
  45. // which is required when the caller makes multiple async calls before
  46. // processing callbacks (e.g. due to holding locks). 64 means that a
  47. // caller can make up to 64 async calls before a callback must be
  48. // processed (otherwise it deadlocks). It also means that we can make 64
  49. // gRPC calls while processing a slow callback at the channel head.
  50. chReqRes: make(chan *ReqRes, 64),
  51. }
  52. cli.BaseService = *service.NewBaseService(logger, "grpcClient", cli)
  53. return cli
  54. }
  55. func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
  56. return tmnet.Connect(addr)
  57. }
  58. func (cli *grpcClient) OnStart(ctx context.Context) error {
  59. // This processes asynchronous request/response messages and dispatches
  60. // them to callbacks.
  61. go func() {
  62. // Use a separate function to use defer for mutex unlocks (this handles panics)
  63. callCb := func(reqres *ReqRes) {
  64. cli.mtx.Lock()
  65. defer cli.mtx.Unlock()
  66. reqres.SetDone()
  67. reqres.Done()
  68. // Notify client listener if set
  69. if cli.resCb != nil {
  70. cli.resCb(reqres.Request, reqres.Response)
  71. }
  72. // Notify reqRes listener if set
  73. if cb := reqres.GetCallback(); cb != nil {
  74. cb(reqres.Response)
  75. }
  76. }
  77. for {
  78. select {
  79. case reqres := <-cli.chReqRes:
  80. if reqres != nil {
  81. callCb(reqres)
  82. } else {
  83. cli.logger.Error("Received nil reqres")
  84. }
  85. case <-ctx.Done():
  86. return
  87. }
  88. }
  89. }()
  90. RETRY_LOOP:
  91. for {
  92. conn, err := grpc.Dial(cli.addr,
  93. grpc.WithTransportCredentials(insecure.NewCredentials()),
  94. grpc.WithContextDialer(dialerFunc),
  95. )
  96. if err != nil {
  97. if cli.mustConnect {
  98. return err
  99. }
  100. cli.logger.Error(fmt.Sprintf("abci.grpcClient failed to connect to %v. Retrying...\n", cli.addr), "err", err)
  101. time.Sleep(time.Second * dialRetryIntervalSeconds)
  102. continue RETRY_LOOP
  103. }
  104. cli.logger.Info("Dialed server. Waiting for echo.", "addr", cli.addr)
  105. client := types.NewABCIApplicationClient(conn)
  106. cli.conn = conn
  107. ENSURE_CONNECTED:
  108. for {
  109. _, err := client.Echo(context.Background(), &types.RequestEcho{Message: "hello"}, grpc.WaitForReady(true))
  110. if err == nil {
  111. break ENSURE_CONNECTED
  112. }
  113. cli.logger.Error("Echo failed", "err", err)
  114. time.Sleep(time.Second * echoRetryIntervalSeconds)
  115. }
  116. cli.client = client
  117. return nil
  118. }
  119. }
  120. func (cli *grpcClient) OnStop() {
  121. if cli.conn != nil {
  122. cli.conn.Close()
  123. }
  124. close(cli.chReqRes)
  125. }
  126. func (cli *grpcClient) StopForError(err error) {
  127. if !cli.IsRunning() {
  128. return
  129. }
  130. cli.mtx.Lock()
  131. if cli.err == nil {
  132. cli.err = err
  133. }
  134. cli.mtx.Unlock()
  135. cli.logger.Error(fmt.Sprintf("Stopping abci.grpcClient for error: %v", err.Error()))
  136. if err := cli.Stop(); err != nil {
  137. cli.logger.Error("Error stopping abci.grpcClient", "err", err)
  138. }
  139. }
  140. func (cli *grpcClient) Error() error {
  141. cli.mtx.Lock()
  142. defer cli.mtx.Unlock()
  143. return cli.err
  144. }
  145. // Set listener for all responses
  146. // NOTE: callback may get internally generated flush responses.
  147. func (cli *grpcClient) SetResponseCallback(resCb Callback) {
  148. cli.mtx.Lock()
  149. cli.resCb = resCb
  150. cli.mtx.Unlock()
  151. }
  152. //----------------------------------------
  153. // NOTE: call is synchronous, use ctx to break early if needed
  154. func (cli *grpcClient) EchoAsync(ctx context.Context, msg string) (*ReqRes, error) {
  155. req := types.ToRequestEcho(msg)
  156. res, err := cli.client.Echo(ctx, req.GetEcho(), grpc.WaitForReady(true))
  157. if err != nil {
  158. return nil, err
  159. }
  160. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_Echo{Echo: res}})
  161. }
  162. // NOTE: call is synchronous, use ctx to break early if needed
  163. func (cli *grpcClient) FlushAsync(ctx context.Context) (*ReqRes, error) {
  164. req := types.ToRequestFlush()
  165. res, err := cli.client.Flush(ctx, req.GetFlush(), grpc.WaitForReady(true))
  166. if err != nil {
  167. return nil, err
  168. }
  169. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_Flush{Flush: res}})
  170. }
  171. // NOTE: call is synchronous, use ctx to break early if needed
  172. func (cli *grpcClient) InfoAsync(ctx context.Context, params types.RequestInfo) (*ReqRes, error) {
  173. req := types.ToRequestInfo(params)
  174. res, err := cli.client.Info(ctx, req.GetInfo(), grpc.WaitForReady(true))
  175. if err != nil {
  176. return nil, err
  177. }
  178. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_Info{Info: res}})
  179. }
  180. // NOTE: call is synchronous, use ctx to break early if needed
  181. func (cli *grpcClient) DeliverTxAsync(ctx context.Context, params types.RequestDeliverTx) (*ReqRes, error) {
  182. req := types.ToRequestDeliverTx(params)
  183. res, err := cli.client.DeliverTx(ctx, req.GetDeliverTx(), grpc.WaitForReady(true))
  184. if err != nil {
  185. return nil, err
  186. }
  187. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_DeliverTx{DeliverTx: res}})
  188. }
  189. // NOTE: call is synchronous, use ctx to break early if needed
  190. func (cli *grpcClient) CheckTxAsync(ctx context.Context, params types.RequestCheckTx) (*ReqRes, error) {
  191. req := types.ToRequestCheckTx(params)
  192. res, err := cli.client.CheckTx(ctx, req.GetCheckTx(), grpc.WaitForReady(true))
  193. if err != nil {
  194. return nil, err
  195. }
  196. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_CheckTx{CheckTx: res}})
  197. }
  198. // NOTE: call is synchronous, use ctx to break early if needed
  199. func (cli *grpcClient) QueryAsync(ctx context.Context, params types.RequestQuery) (*ReqRes, error) {
  200. req := types.ToRequestQuery(params)
  201. res, err := cli.client.Query(ctx, req.GetQuery(), grpc.WaitForReady(true))
  202. if err != nil {
  203. return nil, err
  204. }
  205. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_Query{Query: res}})
  206. }
  207. // NOTE: call is synchronous, use ctx to break early if needed
  208. func (cli *grpcClient) CommitAsync(ctx context.Context) (*ReqRes, error) {
  209. req := types.ToRequestCommit()
  210. res, err := cli.client.Commit(ctx, req.GetCommit(), grpc.WaitForReady(true))
  211. if err != nil {
  212. return nil, err
  213. }
  214. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_Commit{Commit: res}})
  215. }
  216. // NOTE: call is synchronous, use ctx to break early if needed
  217. func (cli *grpcClient) InitChainAsync(ctx context.Context, params types.RequestInitChain) (*ReqRes, error) {
  218. req := types.ToRequestInitChain(params)
  219. res, err := cli.client.InitChain(ctx, req.GetInitChain(), grpc.WaitForReady(true))
  220. if err != nil {
  221. return nil, err
  222. }
  223. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_InitChain{InitChain: res}})
  224. }
  225. // NOTE: call is synchronous, use ctx to break early if needed
  226. func (cli *grpcClient) BeginBlockAsync(ctx context.Context, params types.RequestBeginBlock) (*ReqRes, error) {
  227. req := types.ToRequestBeginBlock(params)
  228. res, err := cli.client.BeginBlock(ctx, req.GetBeginBlock(), grpc.WaitForReady(true))
  229. if err != nil {
  230. return nil, err
  231. }
  232. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_BeginBlock{BeginBlock: res}})
  233. }
  234. // NOTE: call is synchronous, use ctx to break early if needed
  235. func (cli *grpcClient) EndBlockAsync(ctx context.Context, params types.RequestEndBlock) (*ReqRes, error) {
  236. req := types.ToRequestEndBlock(params)
  237. res, err := cli.client.EndBlock(ctx, req.GetEndBlock(), grpc.WaitForReady(true))
  238. if err != nil {
  239. return nil, err
  240. }
  241. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_EndBlock{EndBlock: res}})
  242. }
  243. // NOTE: call is synchronous, use ctx to break early if needed
  244. func (cli *grpcClient) ListSnapshotsAsync(ctx context.Context, params types.RequestListSnapshots) (*ReqRes, error) {
  245. req := types.ToRequestListSnapshots(params)
  246. res, err := cli.client.ListSnapshots(ctx, req.GetListSnapshots(), grpc.WaitForReady(true))
  247. if err != nil {
  248. return nil, err
  249. }
  250. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_ListSnapshots{ListSnapshots: res}})
  251. }
  252. // NOTE: call is synchronous, use ctx to break early if needed
  253. func (cli *grpcClient) OfferSnapshotAsync(ctx context.Context, params types.RequestOfferSnapshot) (*ReqRes, error) {
  254. req := types.ToRequestOfferSnapshot(params)
  255. res, err := cli.client.OfferSnapshot(ctx, req.GetOfferSnapshot(), grpc.WaitForReady(true))
  256. if err != nil {
  257. return nil, err
  258. }
  259. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_OfferSnapshot{OfferSnapshot: res}})
  260. }
  261. // NOTE: call is synchronous, use ctx to break early if needed
  262. func (cli *grpcClient) LoadSnapshotChunkAsync(
  263. ctx context.Context,
  264. params types.RequestLoadSnapshotChunk,
  265. ) (*ReqRes, error) {
  266. req := types.ToRequestLoadSnapshotChunk(params)
  267. res, err := cli.client.LoadSnapshotChunk(ctx, req.GetLoadSnapshotChunk(), grpc.WaitForReady(true))
  268. if err != nil {
  269. return nil, err
  270. }
  271. return cli.finishAsyncCall(ctx, req, &types.Response{Value: &types.Response_LoadSnapshotChunk{LoadSnapshotChunk: res}})
  272. }
  273. // NOTE: call is synchronous, use ctx to break early if needed
  274. func (cli *grpcClient) ApplySnapshotChunkAsync(
  275. ctx context.Context,
  276. params types.RequestApplySnapshotChunk,
  277. ) (*ReqRes, error) {
  278. req := types.ToRequestApplySnapshotChunk(params)
  279. res, err := cli.client.ApplySnapshotChunk(ctx, req.GetApplySnapshotChunk(), grpc.WaitForReady(true))
  280. if err != nil {
  281. return nil, err
  282. }
  283. return cli.finishAsyncCall(
  284. ctx,
  285. req,
  286. &types.Response{Value: &types.Response_ApplySnapshotChunk{ApplySnapshotChunk: res}},
  287. )
  288. }
  289. // finishAsyncCall creates a ReqRes for an async call, and immediately populates it
  290. // with the response. We don't complete it until it's been ordered via the channel.
  291. func (cli *grpcClient) finishAsyncCall(ctx context.Context, req *types.Request, res *types.Response) (*ReqRes, error) {
  292. reqres := NewReqRes(req)
  293. reqres.Response = res
  294. select {
  295. case cli.chReqRes <- reqres: // use channel for async responses, since they must be ordered
  296. return reqres, nil
  297. case <-ctx.Done():
  298. return nil, ctx.Err()
  299. }
  300. }
  301. // finishSyncCall waits for an async call to complete. It is necessary to call all
  302. // sync calls asynchronously as well, to maintain call and response ordering via
  303. // the channel, and this method will wait until the async call completes.
  304. func (cli *grpcClient) finishSyncCall(reqres *ReqRes) *types.Response {
  305. // It's possible that the callback is called twice, since the callback can
  306. // be called immediately on SetCallback() in addition to after it has been
  307. // set. This is because completing the ReqRes happens in a separate critical
  308. // section from the one where the callback is called: there is a race where
  309. // SetCallback() is called between completing the ReqRes and dispatching the
  310. // callback.
  311. //
  312. // We also buffer the channel with 1 response, since SetCallback() will be
  313. // called synchronously if the reqres is already completed, in which case
  314. // it will block on sending to the channel since it hasn't gotten around to
  315. // receiving from it yet.
  316. //
  317. // ReqRes should really handle callback dispatch internally, to guarantee
  318. // that it's only called once and avoid the above race conditions.
  319. var once sync.Once
  320. ch := make(chan *types.Response, 1)
  321. reqres.SetCallback(func(res *types.Response) {
  322. once.Do(func() {
  323. ch <- res
  324. })
  325. })
  326. return <-ch
  327. }
  328. //----------------------------------------
  329. func (cli *grpcClient) FlushSync(ctx context.Context) error {
  330. return nil
  331. }
  332. func (cli *grpcClient) EchoSync(ctx context.Context, msg string) (*types.ResponseEcho, error) {
  333. reqres, err := cli.EchoAsync(ctx, msg)
  334. if err != nil {
  335. return nil, err
  336. }
  337. return cli.finishSyncCall(reqres).GetEcho(), cli.Error()
  338. }
  339. func (cli *grpcClient) InfoSync(
  340. ctx context.Context,
  341. req types.RequestInfo,
  342. ) (*types.ResponseInfo, error) {
  343. reqres, err := cli.InfoAsync(ctx, req)
  344. if err != nil {
  345. return nil, err
  346. }
  347. return cli.finishSyncCall(reqres).GetInfo(), cli.Error()
  348. }
  349. func (cli *grpcClient) DeliverTxSync(
  350. ctx context.Context,
  351. params types.RequestDeliverTx,
  352. ) (*types.ResponseDeliverTx, error) {
  353. reqres, err := cli.DeliverTxAsync(ctx, params)
  354. if err != nil {
  355. return nil, err
  356. }
  357. return cli.finishSyncCall(reqres).GetDeliverTx(), cli.Error()
  358. }
  359. func (cli *grpcClient) CheckTxSync(
  360. ctx context.Context,
  361. params types.RequestCheckTx,
  362. ) (*types.ResponseCheckTx, error) {
  363. reqres, err := cli.CheckTxAsync(ctx, params)
  364. if err != nil {
  365. return nil, err
  366. }
  367. return cli.finishSyncCall(reqres).GetCheckTx(), cli.Error()
  368. }
  369. func (cli *grpcClient) QuerySync(
  370. ctx context.Context,
  371. req types.RequestQuery,
  372. ) (*types.ResponseQuery, error) {
  373. reqres, err := cli.QueryAsync(ctx, req)
  374. if err != nil {
  375. return nil, err
  376. }
  377. return cli.finishSyncCall(reqres).GetQuery(), cli.Error()
  378. }
  379. func (cli *grpcClient) CommitSync(ctx context.Context) (*types.ResponseCommit, error) {
  380. reqres, err := cli.CommitAsync(ctx)
  381. if err != nil {
  382. return nil, err
  383. }
  384. return cli.finishSyncCall(reqres).GetCommit(), cli.Error()
  385. }
  386. func (cli *grpcClient) InitChainSync(
  387. ctx context.Context,
  388. params types.RequestInitChain,
  389. ) (*types.ResponseInitChain, error) {
  390. reqres, err := cli.InitChainAsync(ctx, params)
  391. if err != nil {
  392. return nil, err
  393. }
  394. return cli.finishSyncCall(reqres).GetInitChain(), cli.Error()
  395. }
  396. func (cli *grpcClient) BeginBlockSync(
  397. ctx context.Context,
  398. params types.RequestBeginBlock,
  399. ) (*types.ResponseBeginBlock, error) {
  400. reqres, err := cli.BeginBlockAsync(ctx, params)
  401. if err != nil {
  402. return nil, err
  403. }
  404. return cli.finishSyncCall(reqres).GetBeginBlock(), cli.Error()
  405. }
  406. func (cli *grpcClient) EndBlockSync(
  407. ctx context.Context,
  408. params types.RequestEndBlock,
  409. ) (*types.ResponseEndBlock, error) {
  410. reqres, err := cli.EndBlockAsync(ctx, params)
  411. if err != nil {
  412. return nil, err
  413. }
  414. return cli.finishSyncCall(reqres).GetEndBlock(), cli.Error()
  415. }
  416. func (cli *grpcClient) ListSnapshotsSync(
  417. ctx context.Context,
  418. params types.RequestListSnapshots,
  419. ) (*types.ResponseListSnapshots, error) {
  420. reqres, err := cli.ListSnapshotsAsync(ctx, params)
  421. if err != nil {
  422. return nil, err
  423. }
  424. return cli.finishSyncCall(reqres).GetListSnapshots(), cli.Error()
  425. }
  426. func (cli *grpcClient) OfferSnapshotSync(
  427. ctx context.Context,
  428. params types.RequestOfferSnapshot,
  429. ) (*types.ResponseOfferSnapshot, error) {
  430. reqres, err := cli.OfferSnapshotAsync(ctx, params)
  431. if err != nil {
  432. return nil, err
  433. }
  434. return cli.finishSyncCall(reqres).GetOfferSnapshot(), cli.Error()
  435. }
  436. func (cli *grpcClient) LoadSnapshotChunkSync(
  437. ctx context.Context,
  438. params types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
  439. reqres, err := cli.LoadSnapshotChunkAsync(ctx, params)
  440. if err != nil {
  441. return nil, err
  442. }
  443. return cli.finishSyncCall(reqres).GetLoadSnapshotChunk(), cli.Error()
  444. }
  445. func (cli *grpcClient) ApplySnapshotChunkSync(
  446. ctx context.Context,
  447. params types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
  448. reqres, err := cli.ApplySnapshotChunkAsync(ctx, params)
  449. if err != nil {
  450. return nil, err
  451. }
  452. return cli.finishSyncCall(reqres).GetApplySnapshotChunk(), cli.Error()
  453. }