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.

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