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.

137 lines
2.6 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. }
  13. func NewResult(code CodeType, data []byte, log string) Result {
  14. return Result{
  15. Code: code,
  16. Data: data,
  17. Log: log,
  18. }
  19. }
  20. func (res Result) IsOK() bool {
  21. return res.Code == CodeType_OK
  22. }
  23. func (res Result) IsErr() bool {
  24. return res.Code != CodeType_OK
  25. }
  26. func (res Result) IsSameCode(compare Result) bool {
  27. return res.Code == compare.Code
  28. }
  29. func (res Result) Error() string {
  30. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  31. }
  32. func (res Result) String() string {
  33. return fmt.Sprintf("ABCI{code:%v, data:%X, log:%v}", res.Code, res.Data, res.Log)
  34. }
  35. func (res Result) PrependLog(log string) Result {
  36. return Result{
  37. Code: res.Code,
  38. Data: res.Data,
  39. Log: log + ";" + res.Log,
  40. }
  41. }
  42. func (res Result) AppendLog(log string) Result {
  43. return Result{
  44. Code: res.Code,
  45. Data: res.Data,
  46. Log: res.Log + ";" + log,
  47. }
  48. }
  49. func (res Result) SetLog(log string) Result {
  50. return Result{
  51. Code: res.Code,
  52. Data: res.Data,
  53. Log: log,
  54. }
  55. }
  56. func (res Result) SetData(data []byte) Result {
  57. return Result{
  58. Code: res.Code,
  59. Data: data,
  60. Log: res.Log,
  61. }
  62. }
  63. //----------------------------------------
  64. // NOTE: if data == nil and log == "", same as zero Result.
  65. func NewResultOK(data []byte, log string) Result {
  66. return Result{
  67. Code: CodeType_OK,
  68. Data: data,
  69. Log: log,
  70. }
  71. }
  72. func NewError(code CodeType, log string) Result {
  73. return Result{
  74. Code: code,
  75. Log: log,
  76. }
  77. }
  78. //----------------------------------------
  79. // Convenience methods for turning the
  80. // pb type into one using data.Bytes
  81. // Convert ResponseCheckTx to standard Result
  82. func (r *ResponseCheckTx) Result() Result {
  83. return Result{
  84. Code: r.Code,
  85. Data: r.Data,
  86. Log: r.Log,
  87. }
  88. }
  89. // Convert ResponseDeliverTx to standard Result
  90. func (r *ResponseDeliverTx) Result() Result {
  91. return Result{
  92. Code: r.Code,
  93. Data: r.Data,
  94. Log: r.Log,
  95. }
  96. }
  97. type ResultQuery struct {
  98. Code CodeType `json:"code"`
  99. Index int64 `json:"index"`
  100. Key data.Bytes `json:"key"`
  101. Value data.Bytes `json:"value"`
  102. Proof data.Bytes `json:"proof"`
  103. Height uint64 `json:"height"`
  104. Log string `json:"log"`
  105. }
  106. func (r *ResponseQuery) Result() *ResultQuery {
  107. return &ResultQuery{
  108. Code: r.Code,
  109. Index: r.Index,
  110. Key: r.Key,
  111. Value: r.Value,
  112. Proof: r.Proof,
  113. Height: r.Height,
  114. Log: r.Log,
  115. }
  116. }