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.

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