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.

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