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.

210 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. func NewBaseApplication() *BaseApplication {
  38. return &BaseApplication{}
  39. }
  40. func (BaseApplication) Info(req RequestInfo) ResponseInfo {
  41. return ResponseInfo{}
  42. }
  43. func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
  44. return ResponseCheckTx{Code: CodeTypeOK}
  45. }
  46. func (BaseApplication) Commit() ResponseCommit {
  47. return ResponseCommit{}
  48. }
  49. func (BaseApplication) ExtendVote(req RequestExtendVote) ResponseExtendVote {
  50. return ResponseExtendVote{}
  51. }
  52. func (BaseApplication) VerifyVoteExtension(req RequestVerifyVoteExtension) ResponseVerifyVoteExtension {
  53. return ResponseVerifyVoteExtension{
  54. Result: ResponseVerifyVoteExtension_ACCEPT,
  55. }
  56. }
  57. func (BaseApplication) Query(req RequestQuery) ResponseQuery {
  58. return ResponseQuery{Code: CodeTypeOK}
  59. }
  60. func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
  61. return ResponseInitChain{}
  62. }
  63. func (BaseApplication) ListSnapshots(req RequestListSnapshots) ResponseListSnapshots {
  64. return ResponseListSnapshots{}
  65. }
  66. func (BaseApplication) OfferSnapshot(req RequestOfferSnapshot) ResponseOfferSnapshot {
  67. return ResponseOfferSnapshot{}
  68. }
  69. func (BaseApplication) LoadSnapshotChunk(req RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk {
  70. return ResponseLoadSnapshotChunk{}
  71. }
  72. func (BaseApplication) ApplySnapshotChunk(req RequestApplySnapshotChunk) ResponseApplySnapshotChunk {
  73. return ResponseApplySnapshotChunk{}
  74. }
  75. func (BaseApplication) PrepareProposal(req RequestPrepareProposal) ResponsePrepareProposal {
  76. return ResponsePrepareProposal{}
  77. }
  78. func (BaseApplication) ProcessProposal(req RequestProcessProposal) ResponseProcessProposal {
  79. return ResponseProcessProposal{}
  80. }
  81. func (BaseApplication) FinalizeBlock(req RequestFinalizeBlock) ResponseFinalizeBlock {
  82. txs := make([]*ResponseDeliverTx, len(req.Txs))
  83. for i := range req.Txs {
  84. txs[i] = &ResponseDeliverTx{Code: CodeTypeOK}
  85. }
  86. return ResponseFinalizeBlock{
  87. Txs: txs,
  88. }
  89. }
  90. //-------------------------------------------------------
  91. // GRPCApplication is a GRPC wrapper for Application
  92. type GRPCApplication struct {
  93. app Application
  94. }
  95. func NewGRPCApplication(app Application) *GRPCApplication {
  96. return &GRPCApplication{app}
  97. }
  98. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  99. return &ResponseEcho{Message: req.Message}, nil
  100. }
  101. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  102. return &ResponseFlush{}, nil
  103. }
  104. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  105. res := app.app.Info(*req)
  106. return &res, nil
  107. }
  108. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  109. res := app.app.CheckTx(*req)
  110. return &res, nil
  111. }
  112. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  113. res := app.app.Query(*req)
  114. return &res, nil
  115. }
  116. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  117. res := app.app.Commit()
  118. return &res, nil
  119. }
  120. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  121. res := app.app.InitChain(*req)
  122. return &res, nil
  123. }
  124. func (app *GRPCApplication) ListSnapshots(
  125. ctx context.Context, req *RequestListSnapshots) (*ResponseListSnapshots, error) {
  126. res := app.app.ListSnapshots(*req)
  127. return &res, nil
  128. }
  129. func (app *GRPCApplication) OfferSnapshot(
  130. ctx context.Context, req *RequestOfferSnapshot) (*ResponseOfferSnapshot, error) {
  131. res := app.app.OfferSnapshot(*req)
  132. return &res, nil
  133. }
  134. func (app *GRPCApplication) LoadSnapshotChunk(
  135. ctx context.Context, req *RequestLoadSnapshotChunk) (*ResponseLoadSnapshotChunk, error) {
  136. res := app.app.LoadSnapshotChunk(*req)
  137. return &res, nil
  138. }
  139. func (app *GRPCApplication) ApplySnapshotChunk(
  140. ctx context.Context, req *RequestApplySnapshotChunk) (*ResponseApplySnapshotChunk, error) {
  141. res := app.app.ApplySnapshotChunk(*req)
  142. return &res, nil
  143. }
  144. func (app *GRPCApplication) ExtendVote(
  145. ctx context.Context, req *RequestExtendVote) (*ResponseExtendVote, error) {
  146. res := app.app.ExtendVote(*req)
  147. return &res, nil
  148. }
  149. func (app *GRPCApplication) VerifyVoteExtension(
  150. ctx context.Context, req *RequestVerifyVoteExtension) (*ResponseVerifyVoteExtension, error) {
  151. res := app.app.VerifyVoteExtension(*req)
  152. return &res, nil
  153. }
  154. func (app *GRPCApplication) PrepareProposal(
  155. ctx context.Context, req *RequestPrepareProposal) (*ResponsePrepareProposal, error) {
  156. res := app.app.PrepareProposal(*req)
  157. return &res, nil
  158. }
  159. func (app *GRPCApplication) ProcessProposal(
  160. ctx context.Context, req *RequestProcessProposal) (*ResponseProcessProposal, error) {
  161. res := app.app.ProcessProposal(*req)
  162. return &res, nil
  163. }
  164. func (app *GRPCApplication) FinalizeBlock(
  165. ctx context.Context, req *RequestFinalizeBlock) (*ResponseFinalizeBlock, error) {
  166. res := app.app.FinalizeBlock(*req)
  167. return &res, nil
  168. }