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.

154 lines
3.9 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/gogo/protobuf/jsonpb"
  6. types "github.com/tendermint/tendermint/proto/tendermint/types"
  7. )
  8. const (
  9. CodeTypeOK uint32 = 0
  10. )
  11. // IsOK returns true if Code is OK.
  12. func (r ResponseCheckTx) IsOK() bool {
  13. return r.Code == CodeTypeOK
  14. }
  15. // IsErr returns true if Code is something other than OK.
  16. func (r ResponseCheckTx) IsErr() bool {
  17. return r.Code != CodeTypeOK
  18. }
  19. // IsOK returns true if Code is OK.
  20. func (r ResponseDeliverTx) IsOK() bool {
  21. return r.Code == CodeTypeOK
  22. }
  23. // IsErr returns true if Code is something other than OK.
  24. func (r ResponseDeliverTx) IsErr() bool {
  25. return r.Code != CodeTypeOK
  26. }
  27. // IsOK returns true if Code is OK.
  28. func (r ResponseQuery) IsOK() bool {
  29. return r.Code == CodeTypeOK
  30. }
  31. // IsErr returns true if Code is something other than OK.
  32. func (r ResponseQuery) IsErr() bool {
  33. return r.Code != CodeTypeOK
  34. }
  35. // IsOK returns true if Code is OK
  36. func (r ResponseVerifyVoteExtension) IsOK() bool {
  37. return r.Result <= ResponseVerifyVoteExtension_ACCEPT
  38. }
  39. // IsErr returns true if Code is something other than OK.
  40. func (r ResponseVerifyVoteExtension) IsErr() bool {
  41. return r.Result > ResponseVerifyVoteExtension_ACCEPT
  42. }
  43. //---------------------------------------------------------------------------
  44. // override JSON marshaling so we emit defaults (ie. disable omitempty)
  45. var (
  46. jsonpbMarshaller = jsonpb.Marshaler{
  47. EnumsAsInts: true,
  48. EmitDefaults: true,
  49. }
  50. jsonpbUnmarshaller = jsonpb.Unmarshaler{}
  51. )
  52. func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
  53. s, err := jsonpbMarshaller.MarshalToString(r)
  54. return []byte(s), err
  55. }
  56. func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
  57. reader := bytes.NewBuffer(b)
  58. return jsonpbUnmarshaller.Unmarshal(reader, r)
  59. }
  60. func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
  61. s, err := jsonpbMarshaller.MarshalToString(r)
  62. return []byte(s), err
  63. }
  64. func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
  65. reader := bytes.NewBuffer(b)
  66. return jsonpbUnmarshaller.Unmarshal(reader, r)
  67. }
  68. func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
  69. s, err := jsonpbMarshaller.MarshalToString(r)
  70. return []byte(s), err
  71. }
  72. func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
  73. reader := bytes.NewBuffer(b)
  74. return jsonpbUnmarshaller.Unmarshal(reader, r)
  75. }
  76. func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
  77. s, err := jsonpbMarshaller.MarshalToString(r)
  78. return []byte(s), err
  79. }
  80. func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
  81. reader := bytes.NewBuffer(b)
  82. return jsonpbUnmarshaller.Unmarshal(reader, r)
  83. }
  84. func (r *EventAttribute) MarshalJSON() ([]byte, error) {
  85. s, err := jsonpbMarshaller.MarshalToString(r)
  86. return []byte(s), err
  87. }
  88. func (r *EventAttribute) UnmarshalJSON(b []byte) error {
  89. reader := bytes.NewBuffer(b)
  90. return jsonpbUnmarshaller.Unmarshal(reader, r)
  91. }
  92. // Some compile time assertions to ensure we don't
  93. // have accidental runtime surprises later on.
  94. // jsonEncodingRoundTripper ensures that asserted
  95. // interfaces implement both MarshalJSON and UnmarshalJSON
  96. type jsonRoundTripper interface {
  97. json.Marshaler
  98. json.Unmarshaler
  99. }
  100. var _ jsonRoundTripper = (*ResponseCommit)(nil)
  101. var _ jsonRoundTripper = (*ResponseQuery)(nil)
  102. var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
  103. var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
  104. var _ jsonRoundTripper = (*EventAttribute)(nil)
  105. // -----------------------------------------------
  106. // construct Result data
  107. func RespondExtendVote(appDataToSign, appDataSelfAuthenticating []byte) ResponseExtendVote {
  108. return ResponseExtendVote{
  109. VoteExtension: &types.VoteExtension{
  110. AppDataToSign: appDataToSign,
  111. AppDataSelfAuthenticating: appDataSelfAuthenticating,
  112. },
  113. }
  114. }
  115. func RespondVerifyVoteExtension(ok bool) ResponseVerifyVoteExtension {
  116. result := ResponseVerifyVoteExtension_REJECT
  117. if ok {
  118. result = ResponseVerifyVoteExtension_ACCEPT
  119. }
  120. return ResponseVerifyVoteExtension{
  121. Result: result,
  122. }
  123. }