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.

109 lines
3.1 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() (info string)
  9. // Set application option (e.g. mode=mempool, mode=consensus)
  10. SetOption(key string, value string) (log string)
  11. // Append a tx
  12. AppendTx(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(height uint64)
  27. // Signals the end of a block
  28. // diffs: changed validators from app to TendermintCore
  29. EndBlock(height uint64) (diffs []*Validator)
  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. return &ResponseInfo{app.app.Info()}, nil
  46. }
  47. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  48. return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil
  49. }
  50. func (app *GRPCApplication) AppendTx(ctx context.Context, req *RequestAppendTx) (*ResponseAppendTx, error) {
  51. r := app.app.AppendTx(req.Tx)
  52. return &ResponseAppendTx{r.Code, r.Data, r.Log}, nil
  53. }
  54. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  55. r := app.app.CheckTx(req.Tx)
  56. return &ResponseCheckTx{r.Code, r.Data, r.Log}, nil
  57. }
  58. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  59. r := app.app.Query(req.Query)
  60. return &ResponseQuery{r.Code, r.Data, r.Log}, nil
  61. }
  62. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  63. r := app.app.Commit()
  64. return &ResponseCommit{r.Code, r.Data, r.Log}, nil
  65. }
  66. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  67. if chainAware, ok := app.app.(BlockchainAware); ok {
  68. chainAware.InitChain(req.Validators)
  69. }
  70. return &ResponseInitChain{}, nil
  71. }
  72. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  73. if chainAware, ok := app.app.(BlockchainAware); ok {
  74. chainAware.BeginBlock(req.Height)
  75. }
  76. return &ResponseBeginBlock{}, nil
  77. }
  78. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  79. if chainAware, ok := app.app.(BlockchainAware); ok {
  80. diffs := chainAware.EndBlock(req.Height)
  81. return &ResponseEndBlock{diffs}, nil
  82. }
  83. return &ResponseEndBlock{}, nil
  84. }