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.

110 lines
3.2 KiB

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. // Return the application Merkle root hash
  18. Commit() Result
  19. }
  20. // Some applications can choose to implement BlockchainAware
  21. type BlockchainAware interface {
  22. // Initialize blockchain
  23. // validators: genesis validators from TendermintCore
  24. InitChain(validators []*Validator)
  25. // Signals the beginning of a block
  26. BeginBlock(hash []byte, header *Header)
  27. // Signals the end of a block
  28. // diffs: changed validators from app to TendermintCore
  29. EndBlock(height uint64) ResponseEndBlock
  30. }
  31. //------------------------------------
  32. type GRPCApplication struct {
  33. app Application
  34. }
  35. func NewGRPCApplication(app Application) *GRPCApplication {
  36. return &GRPCApplication{app}
  37. }
  38. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  39. return &ResponseEcho{req.Message}, nil
  40. }
  41. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  42. return &ResponseFlush{}, nil
  43. }
  44. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  45. resInfo := app.app.Info()
  46. return &resInfo, nil
  47. }
  48. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  49. return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil
  50. }
  51. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  52. r := app.app.DeliverTx(req.Tx)
  53. return &ResponseDeliverTx{r.Code, r.Data, r.Log}, nil
  54. }
  55. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  56. r := app.app.CheckTx(req.Tx)
  57. return &ResponseCheckTx{r.Code, r.Data, r.Log}, nil
  58. }
  59. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  60. r := app.app.Query(req.Query)
  61. return &ResponseQuery{r.Code, r.Data, r.Log}, nil
  62. }
  63. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  64. r := app.app.Commit()
  65. return &ResponseCommit{r.Code, r.Data, r.Log}, nil
  66. }
  67. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  68. if chainAware, ok := app.app.(BlockchainAware); ok {
  69. chainAware.InitChain(req.Validators)
  70. }
  71. return &ResponseInitChain{}, nil
  72. }
  73. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  74. if chainAware, ok := app.app.(BlockchainAware); ok {
  75. chainAware.BeginBlock(req.Hash, req.Header)
  76. }
  77. return &ResponseBeginBlock{}, nil
  78. }
  79. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  80. if chainAware, ok := app.app.(BlockchainAware); ok {
  81. resEndBlock := chainAware.EndBlock(req.Height)
  82. return &resEndBlock, nil
  83. }
  84. return &ResponseEndBlock{}, nil
  85. }