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.

145 lines
4.3 KiB

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