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.

138 lines
4.4 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. *types.Response // Not set atomically, so be sure to use WaitGroup.
  66. mtx sync.Mutex
  67. signal chan struct{}
  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. Response: nil,
  74. signal: make(chan struct{}),
  75. cb: nil,
  76. }
  77. }
  78. // Sets sets the callback. If reqRes is already done, it will call the cb
  79. // immediately. Note, reqRes.cb should not change if reqRes.done and only one
  80. // callback is supported.
  81. func (r *ReqRes) SetCallback(cb func(res *types.Response)) {
  82. r.mtx.Lock()
  83. select {
  84. case <-r.signal:
  85. r.mtx.Unlock()
  86. cb(r.Response)
  87. default:
  88. r.cb = cb
  89. r.mtx.Unlock()
  90. }
  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. defer r.mtx.Unlock()
  116. close(r.signal)
  117. }