/
result.go
393 lines (337 loc) · 9.69 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package types
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/codec"
"github.com/tendermint/go-amino"
ctypes "github.com/brc20-collab/brczero/libs/tendermint/rpc/core/types"
)
// GasInfo defines tx execution gas context.
type GasInfo struct {
// GasWanted is the maximum units of work we allow this tx to perform.
GasWanted uint64
// GasUsed is the amount of gas actually consumed.
GasUsed uint64
}
// Result is the union of ResponseFormat and ResponseCheckTx.
type Result struct {
// Data is any data returned from message or handler execution. It MUST be length
// prefixed in order to separate data from multiple message executions.
Data []byte
// Log contains the log information from message or handler execution.
Log string
// Events contains a slice of Event objects that were emitted during message or
// handler execution.
Events Events
Info []byte
}
// SimulationResponse defines the response generated when a transaction is successfully
// simulated by the Baseapp.
type SimulationResponse struct {
GasInfo
Result *Result
}
// ABCIMessageLogs represents a slice of ABCIMessageLog.
type ABCIMessageLogs []ABCIMessageLog
// ABCIMessageLog defines a structure containing an indexed tx ABCI message log.
type ABCIMessageLog struct {
MsgIndex uint16 `json:"msg_index"`
Log string `json:"log"`
// Events contains a slice of Event objects that were emitted during some
// execution.
Events StringEvents `json:"events"`
}
func (log ABCIMessageLog) MarshalJsonToBuffer(buf *bytes.Buffer) error {
var err error
err = buf.WriteByte('{')
if err != nil {
return err
}
buf.WriteString(`"msg_index":`)
blob, err := json.Marshal(log.MsgIndex)
if err != nil {
return err
}
_, err = buf.Write(blob)
if err != nil {
return err
}
err = buf.WriteByte(',')
if err != nil {
return err
}
buf.WriteString(`"log":`)
blob, err = json.Marshal(log.Log)
if err != nil {
return err
}
_, err = buf.Write(blob)
if err != nil {
return err
}
err = buf.WriteByte(',')
if err != nil {
return err
}
buf.WriteString(`"events":`)
err = log.Events.MarshalJsonToBuffer(buf)
if err != nil {
return err
}
return buf.WriteByte('}')
}
func NewABCIMessageLog(i uint16, log string, events Events) ABCIMessageLog {
return ABCIMessageLog{
MsgIndex: i,
Log: log,
Events: StringifyEvents(events),
}
}
// String implements the fmt.Stringer interface for the ABCIMessageLogs type.
func (logs ABCIMessageLogs) String() (str string) {
if logs != nil {
raw, err := logs.MarshalToJson()
if err != nil {
raw, err = codec.Cdc.MarshalJSON(logs)
}
if err == nil {
str = string(raw)
}
}
return str
}
var abciMessageLogsBufferPool = amino.NewBufferPool()
func (logs ABCIMessageLogs) MarshalToJson() ([]byte, error) {
buf := abciMessageLogsBufferPool.Get()
defer abciMessageLogsBufferPool.Put(buf)
err := logs.MarshalJsonToBuffer(buf)
if err != nil {
return nil, err
}
return amino.GetBytesBufferCopy(buf), nil
}
func (logs ABCIMessageLogs) MarshalJsonToBuffer(buf *bytes.Buffer) error {
var err error
if logs == nil {
_, err = buf.WriteString("null")
return err
}
err = buf.WriteByte('[')
if err != nil {
return err
}
for i, log := range logs {
if i != 0 {
err = buf.WriteByte(',')
if err != nil {
return err
}
}
err = log.MarshalJsonToBuffer(buf)
if err != nil {
return err
}
}
return buf.WriteByte(']')
}
// TxResponse defines a structure containing relevant tx data and metadata. The
// tags are stringified and the log is JSON decoded.
type TxResponse struct {
Height int64 `json:"height"`
TxHash string `json:"txhash"`
Codespace string `json:"codespace,omitempty"`
Code uint32 `json:"code,omitempty"`
Data string `json:"data,omitempty"`
RawLog string `json:"raw_log,omitempty"`
Logs ABCIMessageLogs `json:"logs,omitempty"`
Info string `json:"info,omitempty"`
GasWanted int64 `json:"gas_wanted,omitempty"`
GasUsed int64 `json:"gas_used,omitempty"`
Tx Tx `json:"tx,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}
// NewResponseResultTx returns a TxResponse given a ResultTx from tendermint
func NewResponseResultTx(res *ctypes.ResultTx, tx Tx, timestamp string) TxResponse {
if res == nil {
return TxResponse{}
}
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: tx,
Timestamp: timestamp,
}
}
// NewResponseFormatBroadcastTxCommit returns a TxResponse given a
// ResultBroadcastTxCommit from tendermint.
func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) TxResponse {
if res == nil {
return TxResponse{}
}
if !res.CheckTx.IsOK() {
return newTxResponseCheckTx(res)
}
return newTxResponseDeliverTx(res)
}
func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
if res == nil {
return TxResponse{}
}
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,
}
}
func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
if res == nil {
return TxResponse{}
}
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,
}
}
// NewResponseFormatBroadcastTx returns a TxResponse given a ResultBroadcastTx from tendermint
func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) TxResponse {
if res == nil {
return TxResponse{}
}
parsedLogs, _ := ParseABCILogs(res.Log)
return TxResponse{
Code: res.Code,
Data: res.Data.String(),
RawLog: res.Log,
Logs: parsedLogs,
TxHash: res.Hash.String(),
}
}
func (r TxResponse) String() string {
var sb strings.Builder
sb.WriteString("Response:\n")
if r.Height > 0 {
sb.WriteString(fmt.Sprintf(" Height: %d\n", r.Height))
}
if r.TxHash != "" {
sb.WriteString(fmt.Sprintf(" TxHash: %s\n", r.TxHash))
}
if r.Code > 0 {
sb.WriteString(fmt.Sprintf(" Code: %d\n", r.Code))
}
if r.Data != "" {
sb.WriteString(fmt.Sprintf(" Data: %s\n", r.Data))
}
if r.RawLog != "" {
sb.WriteString(fmt.Sprintf(" Raw Log: %s\n", r.RawLog))
}
if r.Logs != nil {
sb.WriteString(fmt.Sprintf(" Logs: %s\n", r.Logs))
}
if r.Info != "" {
sb.WriteString(fmt.Sprintf(" Info: %s\n", r.Info))
}
if r.GasWanted != 0 {
sb.WriteString(fmt.Sprintf(" GasWanted: %d\n", r.GasWanted))
}
if r.GasUsed != 0 {
sb.WriteString(fmt.Sprintf(" GasUsed: %d\n", r.GasUsed))
}
if r.Codespace != "" {
sb.WriteString(fmt.Sprintf(" Codespace: %s\n", r.Codespace))
}
if r.Timestamp != "" {
sb.WriteString(fmt.Sprintf(" Timestamp: %s\n", r.Timestamp))
}
return strings.TrimSpace(sb.String())
}
// Empty returns true if the response is empty
func (r TxResponse) Empty() bool {
return r.TxHash == "" && r.Logs == nil
}
// SearchTxsResult defines a structure for querying txs pageable
type SearchTxsResult struct {
TotalCount int `json:"total_count"` // Count of all txs
Count int `json:"count"` // Count of txs in current page
PageNumber int `json:"page_number"` // Index of current page, start from 1
PageTotal int `json:"page_total"` // Count of total pages
Limit int `json:"limit"` // Max count txs per page
Txs []TxResponse `json:"txs"` // List of txs in current page
}
func NewSearchTxsResult(totalCount, count, page, limit int, txs []TxResponse) SearchTxsResult {
return SearchTxsResult{
TotalCount: totalCount,
Count: count,
PageNumber: page,
PageTotal: int(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
}
// 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 []Event
if evtMgr := ctx.EventManager(); evtMgr != nil {
events = evtMgr.Events()
}
return &Result{
Data: data,
Events: events,
}, nil
}