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.

159 lines
4.0 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. // IsUnknown returns true if Code is Unknown
  36. func (r ResponseVerifyVoteExtension) IsUnknown() bool {
  37. return r.Result == ResponseVerifyVoteExtension_UNKNOWN
  38. }
  39. // IsOK returns true if Code is OK
  40. func (r ResponseVerifyVoteExtension) IsOK() bool {
  41. return r.Result == ResponseVerifyVoteExtension_ACCEPT
  42. }
  43. // IsErr returns true if Code is something other than OK.
  44. func (r ResponseVerifyVoteExtension) IsErr() bool {
  45. return r.Result != ResponseVerifyVoteExtension_ACCEPT
  46. }
  47. //---------------------------------------------------------------------------
  48. // override JSON marshaling so we emit defaults (ie. disable omitempty)
  49. var (
  50. jsonpbMarshaller = jsonpb.Marshaler{
  51. EnumsAsInts: true,
  52. EmitDefaults: true,
  53. }
  54. jsonpbUnmarshaller = jsonpb.Unmarshaler{}
  55. )
  56. func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
  57. s, err := jsonpbMarshaller.MarshalToString(r)
  58. return []byte(s), err
  59. }
  60. func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
  61. reader := bytes.NewBuffer(b)
  62. return jsonpbUnmarshaller.Unmarshal(reader, r)
  63. }
  64. func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) {
  65. s, err := jsonpbMarshaller.MarshalToString(r)
  66. return []byte(s), err
  67. }
  68. func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error {
  69. reader := bytes.NewBuffer(b)
  70. return jsonpbUnmarshaller.Unmarshal(reader, r)
  71. }
  72. func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
  73. s, err := jsonpbMarshaller.MarshalToString(r)
  74. return []byte(s), err
  75. }
  76. func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
  77. reader := bytes.NewBuffer(b)
  78. return jsonpbUnmarshaller.Unmarshal(reader, r)
  79. }
  80. func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
  81. s, err := jsonpbMarshaller.MarshalToString(r)
  82. return []byte(s), err
  83. }
  84. func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
  85. reader := bytes.NewBuffer(b)
  86. return jsonpbUnmarshaller.Unmarshal(reader, r)
  87. }
  88. func (r *EventAttribute) MarshalJSON() ([]byte, error) {
  89. s, err := jsonpbMarshaller.MarshalToString(r)
  90. return []byte(s), err
  91. }
  92. func (r *EventAttribute) UnmarshalJSON(b []byte) error {
  93. reader := bytes.NewBuffer(b)
  94. return jsonpbUnmarshaller.Unmarshal(reader, r)
  95. }
  96. // Some compile time assertions to ensure we don't
  97. // have accidental runtime surprises later on.
  98. // jsonEncodingRoundTripper ensures that asserted
  99. // interfaces implement both MarshalJSON and UnmarshalJSON
  100. type jsonRoundTripper interface {
  101. json.Marshaler
  102. json.Unmarshaler
  103. }
  104. var _ jsonRoundTripper = (*ResponseCommit)(nil)
  105. var _ jsonRoundTripper = (*ResponseQuery)(nil)
  106. var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
  107. var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
  108. var _ jsonRoundTripper = (*EventAttribute)(nil)
  109. // -----------------------------------------------
  110. // construct Result data
  111. func RespondExtendVote(appDataToSign, appDataSelfAuthenticating []byte) ResponseExtendVote {
  112. return ResponseExtendVote{
  113. VoteExtension: &types.VoteExtension{
  114. AppDataToSign: appDataToSign,
  115. AppDataSelfAuthenticating: appDataSelfAuthenticating,
  116. },
  117. }
  118. }
  119. func RespondVerifyVoteExtension(ok bool) ResponseVerifyVoteExtension {
  120. result := ResponseVerifyVoteExtension_REJECT
  121. if ok {
  122. result = ResponseVerifyVoteExtension_ACCEPT
  123. }
  124. return ResponseVerifyVoteExtension{
  125. Result: result,
  126. }
  127. }