-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
258 lines (217 loc) · 6.3 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package types
import (
"encoding/hex"
"encoding/json"
"math"
"strings"
"github.com/gogo/protobuf/proto"
abci "github.com/reapchain/reapchain-core/abci/types"
ctypes "github.com/reapchain/reapchain-core/rpc/core/types"
"github.com/reapchain/cosmos-sdk/codec"
codectypes "github.com/reapchain/cosmos-sdk/codec/types"
)
var cdc = codec.NewLegacyAmino()
func (gi GasInfo) String() string {
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &gi)
return string(bz)
}
func (r Result) String() string {
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &r)
return string(bz)
}
func (r Result) GetEvents() Events {
events := make(Events, len(r.Events))
for i, e := range r.Events {
events[i] = Event(e)
}
return events
}
// ABCIMessageLogs represents a slice of ABCIMessageLog.
type ABCIMessageLogs []ABCIMessageLog
func NewABCIMessageLog(i uint32, log string, events Events) ABCIMessageLog {
return ABCIMessageLog{
MsgIndex: i,
Log: log,
Events: StringifyEvents(events.ToABCIEvents()),
}
}
// String implements the fmt.Stringer interface for the ABCIMessageLogs type.
func (logs ABCIMessageLogs) String() (str string) {
if logs != nil {
raw, err := cdc.MarshalJSON(logs)
if err == nil {
str = string(raw)
}
}
return str
}
// NewResponseResultTx returns a TxResponse given a ResultTx from reapchain
func NewResponseResultTx(res *ctypes.ResultTx, anyTx *codectypes.Any, timestamp string) *TxResponse {
if res == nil {
return nil
}
parsedLogs, _ := ParseABCILogs(res.TxResult.Log)
return &TxResponse{
TxHash: res.Hash.String(),
Height: res.Height,
Codespace: res.TxResult.Codespace,
Code: res.TxResult.Code,
Data: strings.ToUpper(hex.EncodeToString(res.TxResult.Data)),
RawLog: res.TxResult.Log,
Logs: parsedLogs,
Info: res.TxResult.Info,
GasWanted: res.TxResult.GasWanted,
GasUsed: res.TxResult.GasUsed,
Tx: anyTx,
Timestamp: timestamp,
Events: res.TxResult.Events,
}
}
// NewResponseFormatBroadcastTxCommit returns a TxResponse given a
// ResultBroadcastTxCommit from reapchain.
func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return nil
}
if !res.CheckTx.IsOK() {
return newTxResponseCheckTx(res)
}
return newTxResponseDeliverTx(res)
}
func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return nil
}
var txHash string
if res.Hash != nil {
txHash = res.Hash.String()
}
parsedLogs, _ := ParseABCILogs(res.CheckTx.Log)
return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.CheckTx.Codespace,
Code: res.CheckTx.Code,
Data: strings.ToUpper(hex.EncodeToString(res.CheckTx.Data)),
RawLog: res.CheckTx.Log,
Logs: parsedLogs,
Info: res.CheckTx.Info,
GasWanted: res.CheckTx.GasWanted,
GasUsed: res.CheckTx.GasUsed,
Events: res.CheckTx.Events,
}
}
func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return nil
}
var txHash string
if res.Hash != nil {
txHash = res.Hash.String()
}
parsedLogs, _ := ParseABCILogs(res.DeliverTx.Log)
return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.DeliverTx.Codespace,
Code: res.DeliverTx.Code,
Data: strings.ToUpper(hex.EncodeToString(res.DeliverTx.Data)),
RawLog: res.DeliverTx.Log,
Logs: parsedLogs,
Info: res.DeliverTx.Info,
GasWanted: res.DeliverTx.GasWanted,
GasUsed: res.DeliverTx.GasUsed,
Events: res.DeliverTx.Events,
}
}
// NewResponseFormatBroadcastTx returns a TxResponse given a ResultBroadcastTx from reapchain
func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) *TxResponse {
if res == nil {
return nil
}
parsedLogs, _ := ParseABCILogs(res.Log)
return &TxResponse{
Code: res.Code,
Codespace: res.Codespace,
Data: res.Data.String(),
RawLog: res.Log,
Logs: parsedLogs,
TxHash: res.Hash.String(),
}
}
func (r TxResponse) String() string {
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &r)
return string(bz)
}
// Empty returns true if the response is empty
func (r TxResponse) Empty() bool {
return r.TxHash == "" && r.Logs == nil
}
func NewSearchTxsResult(totalCount, count, page, limit uint64, txs []*TxResponse) *SearchTxsResult {
return &SearchTxsResult{
TotalCount: totalCount,
Count: count,
PageNumber: page,
PageTotal: uint64(math.Ceil(float64(totalCount) / float64(limit))),
Limit: limit,
Txs: txs,
}
}
// ParseABCILogs attempts to parse a stringified ABCI tx log into a slice of
// ABCIMessageLog types. It returns an error upon JSON decoding failure.
func ParseABCILogs(logs string) (res ABCIMessageLogs, err error) {
err = json.Unmarshal([]byte(logs), &res)
return res, err
}
var _, _ codectypes.UnpackInterfacesMessage = SearchTxsResult{}, TxResponse{}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
//
// types.UnpackInterfaces needs to be called for each nested Tx because
// there are generally interfaces to unpack in Tx's
func (s SearchTxsResult) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
for _, tx := range s.Txs {
err := codectypes.UnpackInterfaces(tx, unpacker)
if err != nil {
return err
}
}
return nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (r TxResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if r.Tx != nil {
var tx Tx
return unpacker.UnpackAny(r.Tx, &tx)
}
return nil
}
// GetTx unpacks the Tx from within a TxResponse and returns it
func (r TxResponse) GetTx() Tx {
if tx, ok := r.Tx.GetCachedValue().(Tx); ok {
return tx
}
return nil
}
// WrapServiceResult wraps a result from a protobuf RPC service method call in
// a Result object or error. This method takes care of marshaling the res param to
// protobuf and attaching any events on the ctx.EventManager() to the Result.
func WrapServiceResult(ctx Context, res proto.Message, err error) (*Result, error) {
if err != nil {
return nil, err
}
var data []byte
if res != nil {
data, err = proto.Marshal(res)
if err != nil {
return nil, err
}
}
var events []abci.Event
if evtMgr := ctx.EventManager(); evtMgr != nil {
events = evtMgr.ABCIEvents()
}
return &Result{
Data: data,
Events: events,
}, nil
}