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.

88 lines
2.9 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 methods return the appropriate protobuf ResponseXxx struct and
  18. // 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. Error() error
  26. Flush(context.Context) error
  27. Echo(ctx context.Context, msg string) (*types.ResponseEcho, error)
  28. Info(context.Context, types.RequestInfo) (*types.ResponseInfo, error)
  29. CheckTx(context.Context, types.RequestCheckTx) (*types.ResponseCheckTx, error)
  30. Query(context.Context, types.RequestQuery) (*types.ResponseQuery, error)
  31. Commit(context.Context) (*types.ResponseCommit, error)
  32. InitChain(context.Context, types.RequestInitChain) (*types.ResponseInitChain, error)
  33. PrepareProposal(context.Context, types.RequestPrepareProposal) (*types.ResponsePrepareProposal, error)
  34. ProcessProposal(context.Context, types.RequestProcessProposal) (*types.ResponseProcessProposal, error)
  35. ExtendVote(context.Context, types.RequestExtendVote) (*types.ResponseExtendVote, error)
  36. VerifyVoteExtension(context.Context, types.RequestVerifyVoteExtension) (*types.ResponseVerifyVoteExtension, error)
  37. FinalizeBlock(context.Context, types.RequestFinalizeBlock) (*types.ResponseFinalizeBlock, error)
  38. ListSnapshots(context.Context, types.RequestListSnapshots) (*types.ResponseListSnapshots, error)
  39. OfferSnapshot(context.Context, types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
  40. LoadSnapshotChunk(context.Context, types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
  41. ApplySnapshotChunk(context.Context, types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
  42. }
  43. //----------------------------------------
  44. // NewClient returns a new ABCI client of the specified transport type.
  45. // It returns an error if the transport is not "socket" or "grpc"
  46. func NewClient(logger log.Logger, addr, transport string, mustConnect bool) (Client, error) {
  47. switch transport {
  48. case "socket":
  49. return NewSocketClient(logger, addr, mustConnect), nil
  50. case "grpc":
  51. return NewGRPCClient(logger, addr, mustConnect), nil
  52. default:
  53. return nil, fmt.Errorf("unknown abci transport %s", transport)
  54. }
  55. }
  56. type requestAndResponse struct {
  57. *types.Request
  58. *types.Response
  59. mtx sync.Mutex
  60. signal chan struct{}
  61. }
  62. func makeReqRes(req *types.Request) *requestAndResponse {
  63. return &requestAndResponse{
  64. Request: req,
  65. Response: nil,
  66. signal: make(chan struct{}),
  67. }
  68. }
  69. // markDone marks the ReqRes object as done.
  70. func (r *requestAndResponse) markDone() {
  71. r.mtx.Lock()
  72. defer r.mtx.Unlock()
  73. close(r.signal)
  74. }