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.0 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. // Convert ResponseDeliverTx to standard Result
  95. func (r *ResponseDeliverTx) Result() Result {
  96. return Result{
  97. Code: r.Code,
  98. Data: r.Data,
  99. Log: r.Log,
  100. Tags: r.Tags,
  101. }
  102. }
  103. // IsErr returns true if Code is something other than OK.
  104. func (r ResponseDeliverTx) IsErr() bool {
  105. return r.Code != CodeType_OK
  106. }
  107. type ResultQuery struct {
  108. Code CodeType `json:"code"`
  109. Index int64 `json:"index"`
  110. Key data.Bytes `json:"key"`
  111. Value data.Bytes `json:"value"`
  112. Proof data.Bytes `json:"proof"`
  113. Height uint64 `json:"height"`
  114. Log string `json:"log"`
  115. }
  116. func (r *ResponseQuery) Result() *ResultQuery {
  117. return &ResultQuery{
  118. Code: r.Code,
  119. Index: r.Index,
  120. Key: r.Key,
  121. Value: r.Value,
  122. Proof: r.Proof,
  123. Height: r.Height,
  124. Log: r.Log,
  125. }
  126. }
  127. // IsErr returns true if Code is something other than OK.
  128. func (r ResponseCommit) IsErr() bool {
  129. return r.Code != CodeType_OK
  130. }