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.

118 lines
3.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package types
  2. import (
  3. context "golang.org/x/net/context"
  4. )
  5. // Applications
  6. type Application interface {
  7. // Return application info
  8. Info() ResponseInfo
  9. // Set application option (e.g. mode=mempool, mode=consensus)
  10. SetOption(key string, value string) (log string)
  11. // Deliver a tx
  12. DeliverTx(tx []byte) Result
  13. // Validate a tx for the mempool
  14. CheckTx(tx []byte) Result
  15. // Query for state
  16. Query(query []byte) Result
  17. // Get proof for state
  18. Proof(key []byte, blockHeight int64) Result
  19. // Return the application Merkle root hash
  20. Commit() Result
  21. }
  22. // Some applications can choose to implement BlockchainAware
  23. type BlockchainAware interface {
  24. // Initialize blockchain
  25. // validators: genesis validators from TendermintCore
  26. InitChain(validators []*Validator)
  27. // Signals the beginning of a block
  28. BeginBlock(hash []byte, header *Header)
  29. // Signals the end of a block
  30. // diffs: changed validators from app to TendermintCore
  31. EndBlock(height uint64) ResponseEndBlock
  32. }
  33. //------------------------------------
  34. type GRPCApplication struct {
  35. app Application
  36. }
  37. func NewGRPCApplication(app Application) *GRPCApplication {
  38. return &GRPCApplication{app}
  39. }
  40. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  41. return &ResponseEcho{req.Message}, nil
  42. }
  43. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  44. return &ResponseFlush{}, nil
  45. }
  46. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  47. resInfo := app.app.Info()
  48. return &resInfo, nil
  49. }
  50. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  51. return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil
  52. }
  53. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  54. r := app.app.DeliverTx(req.Tx)
  55. return &ResponseDeliverTx{r.Code, r.Data, r.Log}, nil
  56. }
  57. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  58. r := app.app.CheckTx(req.Tx)
  59. return &ResponseCheckTx{r.Code, r.Data, r.Log}, nil
  60. }
  61. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  62. r := app.app.Query(req.Query)
  63. return &ResponseQuery{r.Code, r.Data, r.Log}, nil
  64. }
  65. func (app *GRPCApplication) Proof(ctx context.Context, req *RequestProof) (*ResponseProof, error) {
  66. r := app.app.Proof(req.Key, req.Height)
  67. return &ResponseProof{r.Code, r.Data, r.Log}, nil
  68. }
  69. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  70. r := app.app.Commit()
  71. return &ResponseCommit{r.Code, r.Data, r.Log}, nil
  72. }
  73. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  74. if chainAware, ok := app.app.(BlockchainAware); ok {
  75. chainAware.InitChain(req.Validators)
  76. }
  77. return &ResponseInitChain{}, nil
  78. }
  79. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  80. if chainAware, ok := app.app.(BlockchainAware); ok {
  81. chainAware.BeginBlock(req.Hash, req.Header)
  82. }
  83. return &ResponseBeginBlock{}, nil
  84. }
  85. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  86. if chainAware, ok := app.app.(BlockchainAware); ok {
  87. resEndBlock := chainAware.EndBlock(req.Height)
  88. return &resEndBlock, nil
  89. }
  90. return &ResponseEndBlock{}, nil
  91. }