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.

226 lines
7.3 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. PrepareProposal(RequestPrepareProposal) ResponsePrepareProposal
  18. ProcessProposal(RequestProcessProposal) ResponseProcessProposal
  19. // Signals the beginning of a block
  20. BeginBlock(RequestBeginBlock) ResponseBeginBlock
  21. // Deliver a tx for full processing
  22. DeliverTx(RequestDeliverTx) ResponseDeliverTx
  23. // Signals the end of a block, returns changes to the validator set
  24. EndBlock(RequestEndBlock) ResponseEndBlock
  25. // Commit the state and return the application Merkle root hash
  26. Commit() ResponseCommit
  27. // Create application specific vote extension
  28. ExtendVote(RequestExtendVote) ResponseExtendVote
  29. // Verify application's vote extension data
  30. VerifyVoteExtension(RequestVerifyVoteExtension) ResponseVerifyVoteExtension
  31. // State Sync Connection
  32. ListSnapshots(RequestListSnapshots) ResponseListSnapshots // List available snapshots
  33. OfferSnapshot(RequestOfferSnapshot) ResponseOfferSnapshot // Offer a snapshot to the application
  34. LoadSnapshotChunk(RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk // Load a snapshot chunk
  35. ApplySnapshotChunk(RequestApplySnapshotChunk) ResponseApplySnapshotChunk // Apply a shapshot chunk
  36. }
  37. //-------------------------------------------------------
  38. // BaseApplication is a base form of Application
  39. var _ Application = (*BaseApplication)(nil)
  40. type BaseApplication struct {
  41. }
  42. func NewBaseApplication() *BaseApplication {
  43. return &BaseApplication{}
  44. }
  45. func (BaseApplication) Info(req RequestInfo) ResponseInfo {
  46. return ResponseInfo{}
  47. }
  48. func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
  49. return ResponseDeliverTx{Code: CodeTypeOK}
  50. }
  51. func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
  52. return ResponseCheckTx{Code: CodeTypeOK}
  53. }
  54. func (BaseApplication) Commit() ResponseCommit {
  55. return ResponseCommit{}
  56. }
  57. func (BaseApplication) ExtendVote(req RequestExtendVote) ResponseExtendVote {
  58. return ResponseExtendVote{}
  59. }
  60. func (BaseApplication) VerifyVoteExtension(req RequestVerifyVoteExtension) ResponseVerifyVoteExtension {
  61. return ResponseVerifyVoteExtension{
  62. Result: ResponseVerifyVoteExtension_ACCEPT,
  63. }
  64. }
  65. func (BaseApplication) Query(req RequestQuery) ResponseQuery {
  66. return ResponseQuery{Code: CodeTypeOK}
  67. }
  68. func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
  69. return ResponseInitChain{}
  70. }
  71. func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
  72. return ResponseBeginBlock{}
  73. }
  74. func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
  75. return ResponseEndBlock{}
  76. }
  77. func (BaseApplication) ListSnapshots(req RequestListSnapshots) ResponseListSnapshots {
  78. return ResponseListSnapshots{}
  79. }
  80. func (BaseApplication) OfferSnapshot(req RequestOfferSnapshot) ResponseOfferSnapshot {
  81. return ResponseOfferSnapshot{}
  82. }
  83. func (BaseApplication) LoadSnapshotChunk(req RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk {
  84. return ResponseLoadSnapshotChunk{}
  85. }
  86. func (BaseApplication) ApplySnapshotChunk(req RequestApplySnapshotChunk) ResponseApplySnapshotChunk {
  87. return ResponseApplySnapshotChunk{}
  88. }
  89. func (BaseApplication) PrepareProposal(req RequestPrepareProposal) ResponsePrepareProposal {
  90. return ResponsePrepareProposal{}
  91. }
  92. func (BaseApplication) ProcessProposal(req RequestProcessProposal) ResponseProcessProposal {
  93. return ResponseProcessProposal{}
  94. }
  95. //-------------------------------------------------------
  96. // GRPCApplication is a GRPC wrapper for Application
  97. type GRPCApplication struct {
  98. app Application
  99. }
  100. func NewGRPCApplication(app Application) *GRPCApplication {
  101. return &GRPCApplication{app}
  102. }
  103. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  104. return &ResponseEcho{Message: req.Message}, nil
  105. }
  106. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  107. return &ResponseFlush{}, nil
  108. }
  109. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  110. res := app.app.Info(*req)
  111. return &res, nil
  112. }
  113. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  114. res := app.app.DeliverTx(*req)
  115. return &res, nil
  116. }
  117. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  118. res := app.app.CheckTx(*req)
  119. return &res, nil
  120. }
  121. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  122. res := app.app.Query(*req)
  123. return &res, nil
  124. }
  125. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  126. res := app.app.Commit()
  127. return &res, nil
  128. }
  129. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  130. res := app.app.InitChain(*req)
  131. return &res, nil
  132. }
  133. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  134. res := app.app.BeginBlock(*req)
  135. return &res, nil
  136. }
  137. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  138. res := app.app.EndBlock(*req)
  139. return &res, nil
  140. }
  141. func (app *GRPCApplication) ListSnapshots(
  142. ctx context.Context, req *RequestListSnapshots) (*ResponseListSnapshots, error) {
  143. res := app.app.ListSnapshots(*req)
  144. return &res, nil
  145. }
  146. func (app *GRPCApplication) OfferSnapshot(
  147. ctx context.Context, req *RequestOfferSnapshot) (*ResponseOfferSnapshot, error) {
  148. res := app.app.OfferSnapshot(*req)
  149. return &res, nil
  150. }
  151. func (app *GRPCApplication) LoadSnapshotChunk(
  152. ctx context.Context, req *RequestLoadSnapshotChunk) (*ResponseLoadSnapshotChunk, error) {
  153. res := app.app.LoadSnapshotChunk(*req)
  154. return &res, nil
  155. }
  156. func (app *GRPCApplication) ApplySnapshotChunk(
  157. ctx context.Context, req *RequestApplySnapshotChunk) (*ResponseApplySnapshotChunk, error) {
  158. res := app.app.ApplySnapshotChunk(*req)
  159. return &res, nil
  160. }
  161. func (app *GRPCApplication) ExtendVote(
  162. ctx context.Context, req *RequestExtendVote) (*ResponseExtendVote, error) {
  163. res := app.app.ExtendVote(*req)
  164. return &res, nil
  165. }
  166. func (app *GRPCApplication) VerifyVoteExtension(
  167. ctx context.Context, req *RequestVerifyVoteExtension) (*ResponseVerifyVoteExtension, error) {
  168. res := app.app.VerifyVoteExtension(*req)
  169. return &res, nil
  170. }
  171. func (app *GRPCApplication) PrepareProposal(
  172. ctx context.Context, req *RequestPrepareProposal) (*ResponsePrepareProposal, error) {
  173. res := app.app.PrepareProposal(*req)
  174. return &res, nil
  175. }
  176. func (app *GRPCApplication) ProcessProposal(
  177. ctx context.Context, req *RequestProcessProposal) (*ResponseProcessProposal, error) {
  178. res := app.app.ProcessProposal(*req)
  179. return &res, nil
  180. }