OSDN Git Service

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