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.

184 lines
6.0 KiB

9 years ago
9 years ago
7 years ago
  1. package types
  2. import (
  3. context "golang.org/x/net/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. SetOption(RequestSetOption) ResponseSetOption // Set application option
  13. Query(RequestQuery) ResponseQuery // Query for state
  14. // Mempool Connection
  15. CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool
  16. // Consensus Connection
  17. InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain w validators/other info from TendermintCore
  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. // State Sync Connection
  23. ListSnapshots(RequestListSnapshots) ResponseListSnapshots // List available snapshots
  24. OfferSnapshot(RequestOfferSnapshot) ResponseOfferSnapshot // Offer a snapshot to the application
  25. LoadSnapshotChunk(RequestLoadSnapshotChunk) ResponseLoadSnapshotChunk // Load a snapshot chunk
  26. ApplySnapshotChunk(RequestApplySnapshotChunk) ResponseApplySnapshotChunk // Apply a shapshot chunk
  27. }
  28. //-------------------------------------------------------
  29. // BaseApplication is a base form of Application
  30. var _ Application = (*BaseApplication)(nil)
  31. type BaseApplication struct {
  32. }
  33. func NewBaseApplication() *BaseApplication {
  34. return &BaseApplication{}
  35. }
  36. func (BaseApplication) Info(req RequestInfo) ResponseInfo {
  37. return ResponseInfo{}
  38. }
  39. func (BaseApplication) SetOption(req RequestSetOption) ResponseSetOption {
  40. return ResponseSetOption{}
  41. }
  42. func (BaseApplication) DeliverTx(req RequestDeliverTx) ResponseDeliverTx {
  43. return ResponseDeliverTx{Code: CodeTypeOK}
  44. }
  45. func (BaseApplication) CheckTx(req RequestCheckTx) ResponseCheckTx {
  46. return ResponseCheckTx{Code: CodeTypeOK}
  47. }
  48. func (BaseApplication) Commit() ResponseCommit {
  49. return ResponseCommit{}
  50. }
  51. func (BaseApplication) Query(req RequestQuery) ResponseQuery {
  52. return ResponseQuery{Code: CodeTypeOK}
  53. }
  54. func (BaseApplication) InitChain(req RequestInitChain) ResponseInitChain {
  55. return ResponseInitChain{}
  56. }
  57. func (BaseApplication) BeginBlock(req RequestBeginBlock) ResponseBeginBlock {
  58. return ResponseBeginBlock{}
  59. }
  60. func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
  61. return ResponseEndBlock{}
  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. //-------------------------------------------------------
  76. // GRPCApplication is a GRPC wrapper for Application
  77. type GRPCApplication struct {
  78. app Application
  79. }
  80. func NewGRPCApplication(app Application) *GRPCApplication {
  81. return &GRPCApplication{app}
  82. }
  83. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  84. return &ResponseEcho{Message: req.Message}, nil
  85. }
  86. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  87. return &ResponseFlush{}, nil
  88. }
  89. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  90. res := app.app.Info(*req)
  91. return &res, nil
  92. }
  93. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  94. res := app.app.SetOption(*req)
  95. return &res, nil
  96. }
  97. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  98. res := app.app.DeliverTx(*req)
  99. return &res, nil
  100. }
  101. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  102. res := app.app.CheckTx(*req)
  103. return &res, nil
  104. }
  105. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  106. res := app.app.Query(*req)
  107. return &res, nil
  108. }
  109. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  110. res := app.app.Commit()
  111. return &res, nil
  112. }
  113. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  114. res := app.app.InitChain(*req)
  115. return &res, nil
  116. }
  117. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  118. res := app.app.BeginBlock(*req)
  119. return &res, nil
  120. }
  121. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  122. res := app.app.EndBlock(*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. }