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.

146 lines
4.5 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. CheckTxAsync(context.Context, types.RequestCheckTx) (*ReqRes, error)
  30. // Synchronous requests
  31. Flush(context.Context) error
  32. Echo(ctx context.Context, msg string) (*types.ResponseEcho, error)
  33. Info(context.Context, types.RequestInfo) (*types.ResponseInfo, error)
  34. CheckTx(context.Context, types.RequestCheckTx) (*types.ResponseCheckTx, error)
  35. Query(context.Context, types.RequestQuery) (*types.ResponseQuery, error)
  36. Commit(context.Context) (*types.ResponseCommit, error)
  37. InitChain(context.Context, types.RequestInitChain) (*types.ResponseInitChain, error)
  38. PrepareProposal(context.Context, types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error)
  39. ProcessProposal(context.Context, types.RequestProcessProposal) (*types.ResponseProcessProposal, error)
  40. ExtendVote(context.Context, types.RequestExtendVote) (*types.ResponseExtendVote, error)
  41. VerifyVoteExtension(context.Context, types.RequestVerifyVoteExtension) (*types.ResponseVerifyVoteExtension, error)
  42. FinalizeBlock(context.Context, types.RequestFinalizeBlock) (*types.ResponseFinalizeBlock, error)
  43. ListSnapshots(context.Context, types.RequestListSnapshots) (*types.ResponseListSnapshots, error)
  44. OfferSnapshot(context.Context, types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
  45. LoadSnapshotChunk(context.Context, types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
  46. ApplySnapshotChunk(context.Context, types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
  47. }
  48. //----------------------------------------
  49. // NewClient returns a new ABCI client of the specified transport type.
  50. // It returns an error if the transport is not "socket" or "grpc"
  51. func NewClient(logger log.Logger, addr, transport string, mustConnect bool) (client Client, err error) {
  52. switch transport {
  53. case "socket":
  54. client = NewSocketClient(logger, addr, mustConnect)
  55. case "grpc":
  56. client = NewGRPCClient(logger, addr, mustConnect)
  57. default:
  58. err = fmt.Errorf("unknown abci transport %s", transport)
  59. }
  60. return
  61. }
  62. type Callback func(*types.Request, *types.Response)
  63. type ReqRes struct {
  64. *types.Request
  65. *sync.WaitGroup
  66. *types.Response // Not set atomically, so be sure to use WaitGroup.
  67. mtx sync.Mutex
  68. done bool // Gets set to true once *after* WaitGroup.Done().
  69. cb func(*types.Response) // A single callback that may be set.
  70. }
  71. func NewReqRes(req *types.Request) *ReqRes {
  72. return &ReqRes{
  73. Request: req,
  74. WaitGroup: waitGroup1(),
  75. Response: nil,
  76. done: false,
  77. cb: nil,
  78. }
  79. }
  80. // Sets sets the callback. If reqRes is already done, it will call the cb
  81. // immediately. Note, reqRes.cb should not change if reqRes.done and only one
  82. // callback is supported.
  83. func (r *ReqRes) SetCallback(cb func(res *types.Response)) {
  84. r.mtx.Lock()
  85. if r.done {
  86. r.mtx.Unlock()
  87. cb(r.Response)
  88. return
  89. }
  90. r.cb = cb
  91. r.mtx.Unlock()
  92. }
  93. // InvokeCallback invokes a thread-safe execution of the configured callback
  94. // if non-nil.
  95. func (r *ReqRes) InvokeCallback() {
  96. r.mtx.Lock()
  97. defer r.mtx.Unlock()
  98. if r.cb != nil {
  99. r.cb(r.Response)
  100. }
  101. }
  102. // GetCallback returns the configured callback of the ReqRes object which may be
  103. // nil. Note, it is not safe to concurrently call this in cases where it is
  104. // marked done and SetCallback is called before calling GetCallback as that
  105. // will invoke the callback twice and create a potential race condition.
  106. //
  107. // ref: https://github.com/tendermint/tendermint/issues/5439
  108. func (r *ReqRes) GetCallback() func(*types.Response) {
  109. r.mtx.Lock()
  110. defer r.mtx.Unlock()
  111. return r.cb
  112. }
  113. // SetDone marks the ReqRes object as done.
  114. func (r *ReqRes) SetDone() {
  115. r.mtx.Lock()
  116. r.done = true
  117. r.mtx.Unlock()
  118. }
  119. func waitGroup1() (wg *sync.WaitGroup) {
  120. wg = &sync.WaitGroup{}
  121. wg.Add(1)
  122. return
  123. }