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.

136 lines
2.5 KiB

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