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.

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