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.

86 lines
3.0 KiB

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. // Info/Query Connection
  8. Info() ResponseInfo // Return application info
  9. SetOption(key string, value string) (log string) // Set application option
  10. Query(reqQuery RequestQuery) ResponseQuery // Query for state
  11. // Mempool Connection
  12. CheckTx(tx []byte) Result // Validate a tx for the mempool
  13. // Consensus Connection
  14. InitChain(validators []*Validator) // Initialize blockchain with validators from TendermintCore
  15. BeginBlock(hash []byte, header *Header) // Signals the beginning of a block
  16. DeliverTx(tx []byte) Result // Deliver a tx for full processing
  17. EndBlock(height uint64) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
  18. Commit() Result // Commit the state and return the application Merkle root hash
  19. }
  20. //------------------------------------
  21. // GRPC wrapper for application
  22. type GRPCApplication struct {
  23. app Application
  24. }
  25. func NewGRPCApplication(app Application) *GRPCApplication {
  26. return &GRPCApplication{app}
  27. }
  28. func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
  29. return &ResponseEcho{req.Message}, nil
  30. }
  31. func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
  32. return &ResponseFlush{}, nil
  33. }
  34. func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
  35. resInfo := app.app.Info()
  36. return &resInfo, nil
  37. }
  38. func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
  39. return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil
  40. }
  41. func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
  42. r := app.app.DeliverTx(req.Tx)
  43. return &ResponseDeliverTx{r.Code, r.Data, r.Log}, nil
  44. }
  45. func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
  46. r := app.app.CheckTx(req.Tx)
  47. return &ResponseCheckTx{r.Code, r.Data, r.Log}, nil
  48. }
  49. func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
  50. resQuery := app.app.Query(*req)
  51. return &resQuery, nil
  52. }
  53. func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
  54. r := app.app.Commit()
  55. return &ResponseCommit{r.Code, r.Data, r.Log}, nil
  56. }
  57. func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
  58. app.app.InitChain(req.Validators)
  59. return &ResponseInitChain{}, nil // NOTE: empty return
  60. }
  61. func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
  62. app.app.BeginBlock(req.Hash, req.Header)
  63. return &ResponseBeginBlock{}, nil // NOTE: empty return
  64. }
  65. func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
  66. resEndBlock := app.app.EndBlock(req.Height)
  67. return &resEndBlock, nil
  68. }