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.

157 lines
5.2 KiB

  1. package abciclient
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "github.com/tendermint/tendermint/abci/types"
  7. tmsync "github.com/tendermint/tendermint/internal/libs/sync"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/libs/service"
  10. )
  11. const (
  12. dialRetryIntervalSeconds = 3
  13. echoRetryIntervalSeconds = 1
  14. )
  15. //go:generate ../../scripts/mockery_generate.sh Client
  16. // Client defines an interface for an ABCI client.
  17. //
  18. // All `Async` methods return a `ReqRes` object and an error.
  19. // All `Sync` methods return the appropriate protobuf ResponseXxx struct and an error.
  20. //
  21. // NOTE these are client errors, eg. ABCI socket connectivity issues.
  22. // Application-related errors are reflected in response via ABCI error codes
  23. // and logs.
  24. type Client interface {
  25. service.Service
  26. SetResponseCallback(Callback)
  27. Error() error
  28. // Asynchronous requests
  29. FlushAsync(context.Context) (*ReqRes, error)
  30. EchoAsync(ctx context.Context, msg string) (*ReqRes, error)
  31. InfoAsync(context.Context, types.RequestInfo) (*ReqRes, error)
  32. DeliverTxAsync(context.Context, types.RequestDeliverTx) (*ReqRes, error)
  33. CheckTxAsync(context.Context, types.RequestCheckTx) (*ReqRes, error)
  34. QueryAsync(context.Context, types.RequestQuery) (*ReqRes, error)
  35. CommitAsync(context.Context) (*ReqRes, error)
  36. InitChainAsync(context.Context, types.RequestInitChain) (*ReqRes, error)
  37. BeginBlockAsync(context.Context, types.RequestBeginBlock) (*ReqRes, error)
  38. EndBlockAsync(context.Context, types.RequestEndBlock) (*ReqRes, error)
  39. ListSnapshotsAsync(context.Context, types.RequestListSnapshots) (*ReqRes, error)
  40. OfferSnapshotAsync(context.Context, types.RequestOfferSnapshot) (*ReqRes, error)
  41. LoadSnapshotChunkAsync(context.Context, types.RequestLoadSnapshotChunk) (*ReqRes, error)
  42. ApplySnapshotChunkAsync(context.Context, types.RequestApplySnapshotChunk) (*ReqRes, error)
  43. // Synchronous requests
  44. FlushSync(context.Context) error
  45. EchoSync(ctx context.Context, msg string) (*types.ResponseEcho, error)
  46. InfoSync(context.Context, types.RequestInfo) (*types.ResponseInfo, error)
  47. DeliverTxSync(context.Context, types.RequestDeliverTx) (*types.ResponseDeliverTx, error)
  48. CheckTxSync(context.Context, types.RequestCheckTx) (*types.ResponseCheckTx, error)
  49. QuerySync(context.Context, types.RequestQuery) (*types.ResponseQuery, error)
  50. CommitSync(context.Context) (*types.ResponseCommit, error)
  51. InitChainSync(context.Context, types.RequestInitChain) (*types.ResponseInitChain, error)
  52. BeginBlockSync(context.Context, types.RequestBeginBlock) (*types.ResponseBeginBlock, error)
  53. EndBlockSync(context.Context, types.RequestEndBlock) (*types.ResponseEndBlock, error)
  54. ListSnapshotsSync(context.Context, types.RequestListSnapshots) (*types.ResponseListSnapshots, error)
  55. OfferSnapshotSync(context.Context, types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
  56. LoadSnapshotChunkSync(context.Context, types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
  57. ApplySnapshotChunkSync(context.Context, types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
  58. }
  59. //----------------------------------------
  60. // NewClient returns a new ABCI client of the specified transport type.
  61. // It returns an error if the transport is not "socket" or "grpc"
  62. func NewClient(logger log.Logger, addr, transport string, mustConnect bool) (client Client, err error) {
  63. switch transport {
  64. case "socket":
  65. client = NewSocketClient(logger, addr, mustConnect)
  66. case "grpc":
  67. client = NewGRPCClient(logger, addr, mustConnect)
  68. default:
  69. err = fmt.Errorf("unknown abci transport %s", transport)
  70. }
  71. return
  72. }
  73. type Callback func(*types.Request, *types.Response)
  74. type ReqRes struct {
  75. *types.Request
  76. *sync.WaitGroup
  77. *types.Response // Not set atomically, so be sure to use WaitGroup.
  78. mtx tmsync.Mutex
  79. done bool // Gets set to true once *after* WaitGroup.Done().
  80. cb func(*types.Response) // A single callback that may be set.
  81. }
  82. func NewReqRes(req *types.Request) *ReqRes {
  83. return &ReqRes{
  84. Request: req,
  85. WaitGroup: waitGroup1(),
  86. Response: nil,
  87. done: false,
  88. cb: nil,
  89. }
  90. }
  91. // Sets sets the callback. If reqRes is already done, it will call the cb
  92. // immediately. Note, reqRes.cb should not change if reqRes.done and only one
  93. // callback is supported.
  94. func (r *ReqRes) SetCallback(cb func(res *types.Response)) {
  95. r.mtx.Lock()
  96. if r.done {
  97. r.mtx.Unlock()
  98. cb(r.Response)
  99. return
  100. }
  101. r.cb = cb
  102. r.mtx.Unlock()
  103. }
  104. // InvokeCallback invokes a thread-safe execution of the configured callback
  105. // if non-nil.
  106. func (r *ReqRes) InvokeCallback() {
  107. r.mtx.Lock()
  108. defer r.mtx.Unlock()
  109. if r.cb != nil {
  110. r.cb(r.Response)
  111. }
  112. }
  113. // GetCallback returns the configured callback of the ReqRes object which may be
  114. // nil. Note, it is not safe to concurrently call this in cases where it is
  115. // marked done and SetCallback is called before calling GetCallback as that
  116. // will invoke the callback twice and create a potential race condition.
  117. //
  118. // ref: https://github.com/tendermint/tendermint/issues/5439
  119. func (r *ReqRes) GetCallback() func(*types.Response) {
  120. r.mtx.Lock()
  121. defer r.mtx.Unlock()
  122. return r.cb
  123. }
  124. // SetDone marks the ReqRes object as done.
  125. func (r *ReqRes) SetDone() {
  126. r.mtx.Lock()
  127. r.done = true
  128. r.mtx.Unlock()
  129. }
  130. func waitGroup1() (wg *sync.WaitGroup) {
  131. wg = &sync.WaitGroup{}
  132. wg.Add(1)
  133. return
  134. }