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.

178 lines
3.7 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "github.com/tendermint/go-wire/data"
  5. )
  6. // Result is a common result object for ABCI calls.
  7. // CONTRACT: a zero Result is OK.
  8. type Result struct {
  9. Code CodeType `json:"code"`
  10. Data data.Bytes `json:"data"`
  11. Log string `json:"log"` // Can be non-deterministic
  12. Tags []*KVPair `json:"tags"`
  13. }
  14. func NewResult(code CodeType, data []byte, log string) Result {
  15. return Result{
  16. Code: code,
  17. Data: data,
  18. Log: log,
  19. }
  20. }
  21. func (res Result) IsOK() bool {
  22. return res.Code == CodeType_OK
  23. }
  24. func (res Result) IsErr() bool {
  25. return res.Code != CodeType_OK
  26. }
  27. func (res Result) IsSameCode(compare Result) bool {
  28. return res.Code == compare.Code
  29. }
  30. func (res Result) Error() string {
  31. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  32. }
  33. func (res Result) String() string {
  34. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  35. }
  36. func (res Result) PrependLog(log string) Result {
  37. return Result{
  38. Code: res.Code,
  39. Data: res.Data,
  40. Log: log + ";" + res.Log,
  41. }
  42. }
  43. func (res Result) AppendLog(log string) Result {
  44. return Result{
  45. Code: res.Code,
  46. Data: res.Data,
  47. Log: res.Log + ";" + log,
  48. }
  49. }
  50. func (res Result) SetLog(log string) Result {
  51. return Result{
  52. Code: res.Code,
  53. Data: res.Data,
  54. Log: log,
  55. }
  56. }
  57. func (res Result) SetData(data []byte) Result {
  58. return Result{
  59. Code: res.Code,
  60. Data: data,
  61. Log: res.Log,
  62. }
  63. }
  64. //----------------------------------------
  65. // NOTE: if data == nil and log == "", same as zero Result.
  66. func NewResultOK(data []byte, log string) Result {
  67. return Result{
  68. Code: CodeType_OK,
  69. Data: data,
  70. Log: log,
  71. }
  72. }
  73. func NewError(code CodeType, log string) Result {
  74. return Result{
  75. Code: code,
  76. Log: log,
  77. }
  78. }
  79. //----------------------------------------
  80. // Convenience methods for turning the
  81. // pb type into one using data.Bytes
  82. // Convert ResponseCheckTx to standard Result
  83. func (r *ResponseCheckTx) Result() Result {
  84. return Result{
  85. Code: r.Code,
  86. Data: r.Data,
  87. Log: r.Log,
  88. }
  89. }
  90. // IsErr returns true if Code is something other than OK.
  91. func (r ResponseCheckTx) IsErr() bool {
  92. return r.Code != CodeType_OK
  93. }
  94. // Error implements error interface by formatting response as string.
  95. func (r ResponseCheckTx) Error() string {
  96. return fmtError(r.Code, r.Log)
  97. }
  98. // Convert ResponseDeliverTx to standard Result
  99. func (r *ResponseDeliverTx) Result() Result {
  100. return Result{
  101. Code: r.Code,
  102. Data: r.Data,
  103. Log: r.Log,
  104. Tags: r.Tags,
  105. }
  106. }
  107. // IsErr returns true if Code is something other than OK.
  108. func (r ResponseDeliverTx) IsErr() bool {
  109. return r.Code != CodeType_OK
  110. }
  111. // Error implements error interface by formatting response as string.
  112. func (r ResponseDeliverTx) Error() string {
  113. return fmtError(r.Code, r.Log)
  114. }
  115. type ResultQuery struct {
  116. Code CodeType `json:"code"`
  117. Index int64 `json:"index"`
  118. Key data.Bytes `json:"key"`
  119. Value data.Bytes `json:"value"`
  120. Proof data.Bytes `json:"proof"`
  121. Height uint64 `json:"height"`
  122. Log string `json:"log"`
  123. }
  124. func (r *ResponseQuery) Result() *ResultQuery {
  125. return &ResultQuery{
  126. Code: r.Code,
  127. Index: r.Index,
  128. Key: r.Key,
  129. Value: r.Value,
  130. Proof: r.Proof,
  131. Height: r.Height,
  132. Log: r.Log,
  133. }
  134. }
  135. // IsErr returns true if Code is something other than OK.
  136. func (r ResponseCommit) IsErr() bool {
  137. return r.Code != CodeType_OK
  138. }
  139. // Error implements error interface by formatting response as string.
  140. func (r ResponseCommit) Error() string {
  141. return fmtError(r.Code, r.Log)
  142. }
  143. func fmtError(code CodeType, log string) string {
  144. codeAsStr, ok := code2string[code]
  145. if ok {
  146. return fmt.Sprintf("%s (%v): %s", codeAsStr, code, log)
  147. } else {
  148. return fmt.Sprintf("Unknown error (%v): %s", code, log)
  149. }
  150. }