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

9 years ago
9 years ago
7 years ago
  1. package types // nolint: goimports
  2. import (
  3. context "golang.org/x/net/context"
  4. )
  5. // Application is an interface that enables any finite, deterministic state machine
  6. // to be driven by a blockchain-based replication engine via the ABCI.
  7. // All methods take a RequestXxx argument and return a ResponseXxx argument,
  8. // except CheckTx/DeliverTx, which take `tx []byte`, and `Commit`, which takes nothing.
  9. type Application interface {
  10. // Info/Query Connection
  11. Info(RequestInfo) ResponseInfo // Return application info
  12. SetOption(RequestSetOption) ResponseSetOption // Set application option
  13. Query(RequestQuery) ResponseQuery // Query for state
  14. // Mempool Connection
  15. CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool
  16. // Consensus Connection
  17. InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain with validators and other info from TendermintCore
  18. BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
  19. DeliverTx(RequestDeliverTx) ResponseDeliverTx // Deliver a tx for full processing
  20. EndBlock(RequestEndBlock) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
  21. Commit() ResponseCommit // Commit the state and return the application Merkle root hash
  22. }
  23. //-------------------------------------------------------
  24. // BaseApplication is a base form of Application
  25. var _ Application = (*BaseApplication)(nil)
  26. type BaseApplication struct {
  27. }
  28. func NewBaseApplication() *BaseApplication {
  29. return &BaseApplication{}
  30. }
  31. func (BaseApplication) Info(req RequestInfo) ResponseInfo {
  32. return ResponseInfo{}
  33. }
  34. func (BaseApplication) SetOption(req RequestSetOption) ResponseSetOption {
  35. return ResponseSetOption{}
  36. }
  37. func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
  38. return ResponseDeliverTx{Code: CodeTypeOK}
  39. }
  40. func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
  41. return ResponseCheckTx{Code: CodeTypeOK}
  42. }
  43. func (BaseApplication) Commit() ResponseCommit {
  44. return ResponseCommit{}
  45. }
  46. func (BaseApplication) Query(req RequestQuery) ResponseQuery {
  47. return ResponseQuery{Code: CodeTypeOK}
  48. }
  49. func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
  50. return ResponseInitChain{}
  51. }
  52. func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
  53. return ResponseBeginBlock{}
  54. }
  55. func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
  56. return ResponseEndBlock{}
  57. }
  58. //-------------------------------------------------------
  59. // GRPCApplication is a GRPC wrapper for Application
  60. type GRPCApplication struct {
  61. app Application
  62. }
  63. func NewGRPCApplication(app Application) *GRPCApplication {
  64. return &GRPCApplication{app}
  65. }
  66. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  67. return &ResponseEcho{Message: req.Message}, nil
  68. }
  69. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  70. return &ResponseFlush{}, nil
  71. }
  72. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  73. res := app.app.Info(*req)
  74. return &res, nil
  75. }
  76. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  77. res := app.app.SetOption(*req)
  78. return &res, nil
  79. }
  80. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  81. res := app.app.DeliverTx(*req)
  82. return &res, nil
  83. }
  84. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  85. res := app.app.CheckTx(*req)
  86. return &res, nil
  87. }
  88. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  89. res := app.app.Query(*req)
  90. return &res, nil
  91. }
  92. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  93. res := app.app.Commit()
  94. return &res, nil
  95. }
  96. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  97. res := app.app.InitChain(*req)
  98. return &res, nil
  99. }
  100. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  101. res := app.app.BeginBlock(*req)
  102. return &res, nil
  103. }
  104. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  105. res := app.app.EndBlock(*req)
  106. return &res, nil
  107. }