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.

411 lines
11 KiB

8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748) * use READ lock/unlock in ConsensusState#GetLastHeight Refs #2721 * do not use defers when there's no need * fix peer formatting (output its address instead of the pointer) ``` [54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout" ``` https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581 * panic if peer has no state https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165 It's confusing that sometimes we check if peer has a state, but most of the times we expect it to be there 1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138 2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited) I will change everything to always assume peer has a state and panic otherwise that should help identify issues earlier * abci/localclient: extend lock on app callback App callback should be protected by lock as well (note this was already done for InitChainAsync, why not for others???). Otherwise, when we execute the block, tx might come in and call the callback in the same time we're updating it in execBlockOnProxyApp => DATA RACE Fixes #2721 Consensus state is locked ``` goroutine 113333 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*RWMutex).RLock(0xc001800090) /usr/local/go/src/sync/rwmutex.go:50 +0x4e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248 9c0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236 ``` because localClient is locked ``` goroutine 1899 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670 created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132 ``` tx comes in and CheckTx is executed right when we execute the block ``` goroutine 111044 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0x0) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8 reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...) /usr/local/go/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344) /usr/local/go/src/reflect/value.go:308 +0xa4 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188 net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2361 +0x127 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394 net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2741 +0xab net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0) /usr/local/go/src/net/http/server.go:1847 +0x646 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2851 +0x2f5 ``` * consensus: use read lock in Receive#VoteMessage * use defer to unlock mutex because application might panic * use defer in every method of the localClient * add a changelog entry * drain channels before Unsubscribe(All) Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13 for the detailed explanation of the issue. We'll need to fix it someday. Make sure to keep an eye on https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md * retry instead of panic when peer has no state in reactors other than consensus in /dump_consensus_state RPC endpoint, skip a peer with no state * rpc/core/mempool: simplify error messages * rpc/core/mempool: use time.After instead of timer also, do not log DeliverTx result (to be consistent with other memthods) * unlock before calling the callback in reqRes#SetCallback
6 years ago
8 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. package abcicli
  2. import (
  3. "bufio"
  4. "container/list"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "reflect"
  9. "sync"
  10. "time"
  11. "github.com/tendermint/tendermint/abci/types"
  12. cmn "github.com/tendermint/tendermint/libs/common"
  13. )
  14. const reqQueueSize = 256 // TODO make configurable
  15. // const maxResponseSize = 1048576 // 1MB TODO make configurable
  16. const flushThrottleMS = 20 // Don't wait longer than...
  17. var _ Client = (*socketClient)(nil)
  18. // This is goroutine-safe, but users should beware that
  19. // the application in general is not meant to be interfaced
  20. // with concurrent callers.
  21. type socketClient struct {
  22. cmn.BaseService
  23. addr string
  24. mustConnect bool
  25. conn net.Conn
  26. reqQueue chan *ReqRes
  27. flushTimer *cmn.ThrottleTimer
  28. mtx sync.Mutex
  29. err error
  30. reqSent *list.List // list of requests sent, waiting for response
  31. resCb func(*types.Request, *types.Response) // called on all requests, if set.
  32. }
  33. func NewSocketClient(addr string, mustConnect bool) *socketClient {
  34. cli := &socketClient{
  35. reqQueue: make(chan *ReqRes, reqQueueSize),
  36. flushTimer: cmn.NewThrottleTimer("socketClient", flushThrottleMS),
  37. mustConnect: mustConnect,
  38. addr: addr,
  39. reqSent: list.New(),
  40. resCb: nil,
  41. }
  42. cli.BaseService = *cmn.NewBaseService(nil, "socketClient", cli)
  43. return cli
  44. }
  45. func (cli *socketClient) OnStart() error {
  46. if err := cli.BaseService.OnStart(); err != nil {
  47. return err
  48. }
  49. var err error
  50. var conn net.Conn
  51. RETRY_LOOP:
  52. for {
  53. conn, err = cmn.Connect(cli.addr)
  54. if err != nil {
  55. if cli.mustConnect {
  56. return err
  57. }
  58. cli.Logger.Error(fmt.Sprintf("abci.socketClient failed to connect to %v. Retrying...", cli.addr), "err", err)
  59. time.Sleep(time.Second * dialRetryIntervalSeconds)
  60. continue RETRY_LOOP
  61. }
  62. cli.conn = conn
  63. go cli.sendRequestsRoutine(conn)
  64. go cli.recvResponseRoutine(conn)
  65. return nil
  66. }
  67. }
  68. func (cli *socketClient) OnStop() {
  69. cli.BaseService.OnStop()
  70. cli.mtx.Lock()
  71. defer cli.mtx.Unlock()
  72. if cli.conn != nil {
  73. // does this really need a mutex?
  74. cli.conn.Close()
  75. }
  76. cli.flushQueue()
  77. }
  78. // Stop the client and set the error
  79. func (cli *socketClient) StopForError(err error) {
  80. if !cli.IsRunning() {
  81. return
  82. }
  83. cli.mtx.Lock()
  84. if cli.err == nil {
  85. cli.err = err
  86. }
  87. cli.mtx.Unlock()
  88. cli.Logger.Error(fmt.Sprintf("Stopping abci.socketClient for error: %v", err.Error()))
  89. cli.Stop()
  90. }
  91. func (cli *socketClient) Error() error {
  92. cli.mtx.Lock()
  93. defer cli.mtx.Unlock()
  94. return cli.err
  95. }
  96. // Set listener for all responses
  97. // NOTE: callback may get internally generated flush responses.
  98. func (cli *socketClient) SetResponseCallback(resCb Callback) {
  99. cli.mtx.Lock()
  100. cli.resCb = resCb
  101. cli.mtx.Unlock()
  102. }
  103. //----------------------------------------
  104. func (cli *socketClient) sendRequestsRoutine(conn net.Conn) {
  105. w := bufio.NewWriter(conn)
  106. for {
  107. select {
  108. case <-cli.flushTimer.Ch:
  109. select {
  110. case cli.reqQueue <- NewReqRes(types.ToRequestFlush()):
  111. default:
  112. // Probably will fill the buffer, or retry later.
  113. }
  114. case <-cli.Quit():
  115. return
  116. case reqres := <-cli.reqQueue:
  117. cli.willSendReq(reqres)
  118. err := types.WriteMessage(reqres.Request, w)
  119. if err != nil {
  120. cli.StopForError(fmt.Errorf("Error writing msg: %v", err))
  121. return
  122. }
  123. // cli.Logger.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request)
  124. if _, ok := reqres.Request.Value.(*types.Request_Flush); ok {
  125. err = w.Flush()
  126. if err != nil {
  127. cli.StopForError(fmt.Errorf("Error flushing writer: %v", err))
  128. return
  129. }
  130. }
  131. }
  132. }
  133. }
  134. func (cli *socketClient) recvResponseRoutine(conn net.Conn) {
  135. r := bufio.NewReader(conn) // Buffer reads
  136. for {
  137. var res = &types.Response{}
  138. err := types.ReadMessage(r, res)
  139. if err != nil {
  140. cli.StopForError(err)
  141. return
  142. }
  143. switch r := res.Value.(type) {
  144. case *types.Response_Exception:
  145. // XXX After setting cli.err, release waiters (e.g. reqres.Done())
  146. cli.StopForError(errors.New(r.Exception.Error))
  147. return
  148. default:
  149. // cli.Logger.Debug("Received response", "responseType", reflect.TypeOf(res), "response", res)
  150. err := cli.didRecvResponse(res)
  151. if err != nil {
  152. cli.StopForError(err)
  153. return
  154. }
  155. }
  156. }
  157. }
  158. func (cli *socketClient) willSendReq(reqres *ReqRes) {
  159. cli.mtx.Lock()
  160. defer cli.mtx.Unlock()
  161. cli.reqSent.PushBack(reqres)
  162. }
  163. func (cli *socketClient) didRecvResponse(res *types.Response) error {
  164. cli.mtx.Lock()
  165. defer cli.mtx.Unlock()
  166. // Get the first ReqRes
  167. next := cli.reqSent.Front()
  168. if next == nil {
  169. return fmt.Errorf("Unexpected result type %v when nothing expected", reflect.TypeOf(res.Value))
  170. }
  171. reqres := next.Value.(*ReqRes)
  172. if !resMatchesReq(reqres.Request, res) {
  173. return fmt.Errorf("Unexpected result type %v when response to %v expected",
  174. reflect.TypeOf(res.Value), reflect.TypeOf(reqres.Request.Value))
  175. }
  176. reqres.Response = res // Set response
  177. reqres.Done() // Release waiters
  178. cli.reqSent.Remove(next) // Pop first item from linked list
  179. // Notify reqRes listener if set (request specific callback).
  180. // NOTE: it is possible this callback isn't set on the reqres object.
  181. // at this point, in which case it will be called after, when it is set.
  182. // TODO: should we move this after the resCb call so the order is always consistent?
  183. if cb := reqres.GetCallback(); cb != nil {
  184. cb(res)
  185. }
  186. // Notify client listener if set (global callback).
  187. if cli.resCb != nil {
  188. cli.resCb(reqres.Request, res)
  189. }
  190. return nil
  191. }
  192. //----------------------------------------
  193. func (cli *socketClient) EchoAsync(msg string) *ReqRes {
  194. return cli.queueRequest(types.ToRequestEcho(msg))
  195. }
  196. func (cli *socketClient) FlushAsync() *ReqRes {
  197. return cli.queueRequest(types.ToRequestFlush())
  198. }
  199. func (cli *socketClient) InfoAsync(req types.RequestInfo) *ReqRes {
  200. return cli.queueRequest(types.ToRequestInfo(req))
  201. }
  202. func (cli *socketClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
  203. return cli.queueRequest(types.ToRequestSetOption(req))
  204. }
  205. func (cli *socketClient) DeliverTxAsync(req types.RequestDeliverTx) *ReqRes {
  206. return cli.queueRequest(types.ToRequestDeliverTx(req))
  207. }
  208. func (cli *socketClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes {
  209. return cli.queueRequest(types.ToRequestCheckTx(req))
  210. }
  211. func (cli *socketClient) QueryAsync(req types.RequestQuery) *ReqRes {
  212. return cli.queueRequest(types.ToRequestQuery(req))
  213. }
  214. func (cli *socketClient) CommitAsync() *ReqRes {
  215. return cli.queueRequest(types.ToRequestCommit())
  216. }
  217. func (cli *socketClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
  218. return cli.queueRequest(types.ToRequestInitChain(req))
  219. }
  220. func (cli *socketClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
  221. return cli.queueRequest(types.ToRequestBeginBlock(req))
  222. }
  223. func (cli *socketClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
  224. return cli.queueRequest(types.ToRequestEndBlock(req))
  225. }
  226. //----------------------------------------
  227. func (cli *socketClient) FlushSync() error {
  228. reqRes := cli.queueRequest(types.ToRequestFlush())
  229. if err := cli.Error(); err != nil {
  230. return err
  231. }
  232. reqRes.Wait() // NOTE: if we don't flush the queue, its possible to get stuck here
  233. return cli.Error()
  234. }
  235. func (cli *socketClient) EchoSync(msg string) (*types.ResponseEcho, error) {
  236. reqres := cli.queueRequest(types.ToRequestEcho(msg))
  237. cli.FlushSync()
  238. return reqres.Response.GetEcho(), cli.Error()
  239. }
  240. func (cli *socketClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
  241. reqres := cli.queueRequest(types.ToRequestInfo(req))
  242. cli.FlushSync()
  243. return reqres.Response.GetInfo(), cli.Error()
  244. }
  245. func (cli *socketClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
  246. reqres := cli.queueRequest(types.ToRequestSetOption(req))
  247. cli.FlushSync()
  248. return reqres.Response.GetSetOption(), cli.Error()
  249. }
  250. func (cli *socketClient) DeliverTxSync(req types.RequestDeliverTx) (*types.ResponseDeliverTx, error) {
  251. reqres := cli.queueRequest(types.ToRequestDeliverTx(req))
  252. cli.FlushSync()
  253. return reqres.Response.GetDeliverTx(), cli.Error()
  254. }
  255. func (cli *socketClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) {
  256. reqres := cli.queueRequest(types.ToRequestCheckTx(req))
  257. cli.FlushSync()
  258. return reqres.Response.GetCheckTx(), cli.Error()
  259. }
  260. func (cli *socketClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
  261. reqres := cli.queueRequest(types.ToRequestQuery(req))
  262. cli.FlushSync()
  263. return reqres.Response.GetQuery(), cli.Error()
  264. }
  265. func (cli *socketClient) CommitSync() (*types.ResponseCommit, error) {
  266. reqres := cli.queueRequest(types.ToRequestCommit())
  267. cli.FlushSync()
  268. return reqres.Response.GetCommit(), cli.Error()
  269. }
  270. func (cli *socketClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
  271. reqres := cli.queueRequest(types.ToRequestInitChain(req))
  272. cli.FlushSync()
  273. return reqres.Response.GetInitChain(), cli.Error()
  274. }
  275. func (cli *socketClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
  276. reqres := cli.queueRequest(types.ToRequestBeginBlock(req))
  277. cli.FlushSync()
  278. return reqres.Response.GetBeginBlock(), cli.Error()
  279. }
  280. func (cli *socketClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
  281. reqres := cli.queueRequest(types.ToRequestEndBlock(req))
  282. cli.FlushSync()
  283. return reqres.Response.GetEndBlock(), cli.Error()
  284. }
  285. //----------------------------------------
  286. func (cli *socketClient) queueRequest(req *types.Request) *ReqRes {
  287. reqres := NewReqRes(req)
  288. // TODO: set cli.err if reqQueue times out
  289. cli.reqQueue <- reqres
  290. // Maybe auto-flush, or unset auto-flush
  291. switch req.Value.(type) {
  292. case *types.Request_Flush:
  293. cli.flushTimer.Unset()
  294. default:
  295. cli.flushTimer.Set()
  296. }
  297. return reqres
  298. }
  299. func (cli *socketClient) flushQueue() {
  300. // mark all in-flight messages as resolved (they will get cli.Error())
  301. for req := cli.reqSent.Front(); req != nil; req = req.Next() {
  302. reqres := req.Value.(*ReqRes)
  303. reqres.Done()
  304. }
  305. // mark all queued messages as resolved
  306. LOOP:
  307. for {
  308. select {
  309. case reqres := <-cli.reqQueue:
  310. reqres.Done()
  311. default:
  312. break LOOP
  313. }
  314. }
  315. }
  316. //----------------------------------------
  317. func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {
  318. switch req.Value.(type) {
  319. case *types.Request_Echo:
  320. _, ok = res.Value.(*types.Response_Echo)
  321. case *types.Request_Flush:
  322. _, ok = res.Value.(*types.Response_Flush)
  323. case *types.Request_Info:
  324. _, ok = res.Value.(*types.Response_Info)
  325. case *types.Request_SetOption:
  326. _, ok = res.Value.(*types.Response_SetOption)
  327. case *types.Request_DeliverTx:
  328. _, ok = res.Value.(*types.Response_DeliverTx)
  329. case *types.Request_CheckTx:
  330. _, ok = res.Value.(*types.Response_CheckTx)
  331. case *types.Request_Commit:
  332. _, ok = res.Value.(*types.Response_Commit)
  333. case *types.Request_Query:
  334. _, ok = res.Value.(*types.Response_Query)
  335. case *types.Request_InitChain:
  336. _, ok = res.Value.(*types.Response_InitChain)
  337. case *types.Request_BeginBlock:
  338. _, ok = res.Value.(*types.Response_BeginBlock)
  339. case *types.Request_EndBlock:
  340. _, ok = res.Value.(*types.Response_EndBlock)
  341. }
  342. return ok
  343. }