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.

87 lines
3.2 KiB

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