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.

174 lines
5.7 KiB

9 years ago
9 years ago
7 years ago
  1. package types
  2. import (
  3. "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. Query(RequestQuery) ResponseQuery // Query for state
  13. // Mempool Connection
  14. CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool
  15. // Consensus Connection
  16. InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain w validators/other info from TendermintCore
  17. BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
  18. DeliverTx(RequestDeliverTx) ResponseDeliverTx // Deliver a tx for full processing
  19. EndBlock(RequestEndBlock) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
  20. Commit() ResponseCommit // Commit the state and return the application Merkle root hash
  21. // State Sync Connection
  22. ListSnapshots(RequestListSnapshots) ResponseListSnapshots // List available snapshots
  23. OfferSnapshot(RequestOfferSnapshot) ResponseOfferSnapshot // Offer a snapshot to the application
  24. LoadSnapshotChunk(RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk // Load a snapshot chunk
  25. ApplySnapshotChunk(RequestApplySnapshotChunk) ResponseApplySnapshotChunk // Apply a shapshot chunk
  26. }
  27. //-------------------------------------------------------
  28. // BaseApplication is a base form of Application
  29. var _ Application = (*BaseApplication)(nil)
  30. type BaseApplication struct {
  31. }
  32. func NewBaseApplication() *BaseApplication {
  33. return &BaseApplication{}
  34. }
  35. func (BaseApplication) Info(req RequestInfo) ResponseInfo {
  36. return ResponseInfo{}
  37. }
  38. func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
  39. return ResponseDeliverTx{Code: CodeTypeOK}
  40. }
  41. func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
  42. return ResponseCheckTx{Code: CodeTypeOK}
  43. }
  44. func (BaseApplication) Commit() ResponseCommit {
  45. return ResponseCommit{}
  46. }
  47. func (BaseApplication) Query(req RequestQuery) ResponseQuery {
  48. return ResponseQuery{Code: CodeTypeOK}
  49. }
  50. func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
  51. return ResponseInitChain{}
  52. }
  53. func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
  54. return ResponseBeginBlock{}
  55. }
  56. func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
  57. return ResponseEndBlock{}
  58. }
  59. func (BaseApplication) ListSnapshots(req RequestListSnapshots) ResponseListSnapshots {
  60. return ResponseListSnapshots{}
  61. }
  62. func (BaseApplication) OfferSnapshot(req RequestOfferSnapshot) ResponseOfferSnapshot {
  63. return ResponseOfferSnapshot{}
  64. }
  65. func (BaseApplication) LoadSnapshotChunk(req RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk {
  66. return ResponseLoadSnapshotChunk{}
  67. }
  68. func (BaseApplication) ApplySnapshotChunk(req RequestApplySnapshotChunk) ResponseApplySnapshotChunk {
  69. return ResponseApplySnapshotChunk{}
  70. }
  71. //-------------------------------------------------------
  72. // GRPCApplication is a GRPC wrapper for Application
  73. type GRPCApplication struct {
  74. app Application
  75. }
  76. func NewGRPCApplication(app Application) *GRPCApplication {
  77. return &GRPCApplication{app}
  78. }
  79. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  80. return &ResponseEcho{Message: req.Message}, nil
  81. }
  82. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  83. return &ResponseFlush{}, nil
  84. }
  85. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  86. res := app.app.Info(*req)
  87. return &res, nil
  88. }
  89. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  90. res := app.app.DeliverTx(*req)
  91. return &res, nil
  92. }
  93. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  94. res := app.app.CheckTx(*req)
  95. return &res, nil
  96. }
  97. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  98. res := app.app.Query(*req)
  99. return &res, nil
  100. }
  101. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  102. res := app.app.Commit()
  103. return &res, nil
  104. }
  105. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  106. res := app.app.InitChain(*req)
  107. return &res, nil
  108. }
  109. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  110. res := app.app.BeginBlock(*req)
  111. return &res, nil
  112. }
  113. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  114. res := app.app.EndBlock(*req)
  115. return &res, nil
  116. }
  117. func (app *GRPCApplication) ListSnapshots(
  118. ctx context.Context, req *RequestListSnapshots) (*ResponseListSnapshots, error) {
  119. res := app.app.ListSnapshots(*req)
  120. return &res, nil
  121. }
  122. func (app *GRPCApplication) OfferSnapshot(
  123. ctx context.Context, req *RequestOfferSnapshot) (*ResponseOfferSnapshot, error) {
  124. res := app.app.OfferSnapshot(*req)
  125. return &res, nil
  126. }
  127. func (app *GRPCApplication) LoadSnapshotChunk(
  128. ctx context.Context, req *RequestLoadSnapshotChunk) (*ResponseLoadSnapshotChunk, error) {
  129. res := app.app.LoadSnapshotChunk(*req)
  130. return &res, nil
  131. }
  132. func (app *GRPCApplication) ApplySnapshotChunk(
  133. ctx context.Context, req *RequestApplySnapshotChunk) (*ResponseApplySnapshotChunk, error) {
  134. res := app.app.ApplySnapshotChunk(*req)
  135. return &res, nil
  136. }