2017-12-03 21:25:37 -08:00
|
|
|
package types
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
import (
|
2019-04-03 07:49:34 -07:00
|
|
|
"encoding/hex"
|
2019-02-22 03:54:31 -08:00
|
|
|
"encoding/json"
|
2019-02-04 18:20:56 -08:00
|
|
|
"fmt"
|
2019-05-04 04:09:03 -07:00
|
|
|
"math"
|
2019-02-04 18:20:56 -08:00
|
|
|
"strings"
|
|
|
|
|
2019-09-12 08:05:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
|
|
|
)
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
// GasInfo defines tx execution gas context.
|
|
|
|
type GasInfo struct {
|
2017-12-26 17:04:48 -08:00
|
|
|
// GasWanted is the maximum units of work we allow this tx to perform.
|
2018-11-19 09:13:45 -08:00
|
|
|
GasWanted uint64
|
2017-12-03 21:25:37 -08:00
|
|
|
|
2018-04-10 20:51:09 -07:00
|
|
|
// GasUsed is the amount of gas actually consumed. NOTE: unimplemented
|
2018-11-19 09:13:45 -08:00
|
|
|
GasUsed uint64
|
2017-12-03 21:25:37 -08:00
|
|
|
}
|
2018-01-26 05:11:01 -08:00
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
// 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
|
2018-01-26 05:11:01 -08:00
|
|
|
}
|
2019-02-04 18:20:56 -08:00
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
// ABCIMessageLogs represents a slice of ABCIMessageLog.
|
|
|
|
type ABCIMessageLogs []ABCIMessageLog
|
|
|
|
|
|
|
|
// ABCIMessageLog defines a structure containing an indexed tx ABCI message log.
|
|
|
|
type ABCIMessageLog struct {
|
2019-04-29 20:50:44 -07:00
|
|
|
MsgIndex uint16 `json:"msg_index"`
|
2019-02-22 03:54:31 -08:00
|
|
|
Log string `json:"log"`
|
2019-09-12 08:05:27 -07:00
|
|
|
|
|
|
|
// Events contains a slice of Event objects that were emitted during some
|
|
|
|
// execution.
|
|
|
|
Events StringEvents `json:"events"`
|
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
func NewABCIMessageLog(i uint16, log string, events Events) ABCIMessageLog {
|
2019-09-12 08:05:27 -07:00
|
|
|
return ABCIMessageLog{
|
|
|
|
MsgIndex: i,
|
|
|
|
Log: log,
|
|
|
|
Events: StringifyEvents(events.ToABCIEvents()),
|
|
|
|
}
|
2019-02-22 03:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// String implements the fmt.Stringer interface for the ABCIMessageLogs type.
|
|
|
|
func (logs ABCIMessageLogs) String() (str string) {
|
|
|
|
if logs != nil {
|
2019-09-12 08:05:27 -07:00
|
|
|
raw, err := codec.Cdc.MarshalJSON(logs)
|
2019-02-22 03:54:31 -08:00
|
|
|
if err == nil {
|
|
|
|
str = string(raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxResponse defines a structure containing relevant tx data and metadata. The
|
|
|
|
// tags are stringified and the log is JSON decoded.
|
2019-02-04 18:20:56 -08:00
|
|
|
type TxResponse struct {
|
2019-02-22 03:54:31 -08:00
|
|
|
Height int64 `json:"height"`
|
|
|
|
TxHash string `json:"txhash"`
|
2019-12-27 09:57:54 -08:00
|
|
|
Codespace string `json:"codespace,omitempty"`
|
2019-02-22 03:54:31 -08:00
|
|
|
Code uint32 `json:"code,omitempty"`
|
2019-04-03 07:49:34 -07:00
|
|
|
Data string `json:"data,omitempty"`
|
2019-03-25 17:54:23 -07:00
|
|
|
RawLog string `json:"raw_log,omitempty"`
|
2019-02-22 03:54:31 -08:00
|
|
|
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"`
|
2019-04-02 18:09:37 -07:00
|
|
|
Timestamp string `json:"timestamp,omitempty"`
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
// NewResponseResultTx returns a TxResponse given a ResultTx from tendermint
|
2019-04-02 18:09:37 -07:00
|
|
|
func NewResponseResultTx(res *ctypes.ResultTx, tx Tx, timestamp string) TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
|
|
|
return TxResponse{}
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
parsedLogs, _ := ParseABCILogs(res.TxResult.Log)
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
return TxResponse{
|
|
|
|
TxHash: res.Hash.String(),
|
|
|
|
Height: res.Height,
|
2019-12-27 09:57:54 -08:00
|
|
|
Codespace: res.TxResult.Codespace,
|
2019-02-04 18:20:56 -08:00
|
|
|
Code: res.TxResult.Code,
|
2019-04-03 07:49:34 -07:00
|
|
|
Data: strings.ToUpper(hex.EncodeToString(res.TxResult.Data)),
|
2019-03-25 17:54:23 -07:00
|
|
|
RawLog: res.TxResult.Log,
|
2019-02-22 03:54:31 -08:00
|
|
|
Logs: parsedLogs,
|
2019-02-04 18:20:56 -08:00
|
|
|
Info: res.TxResult.Info,
|
|
|
|
GasWanted: res.TxResult.GasWanted,
|
|
|
|
GasUsed: res.TxResult.GasUsed,
|
|
|
|
Tx: tx,
|
2019-04-02 18:09:37 -07:00
|
|
|
Timestamp: timestamp,
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
// NewResponseFormatBroadcastTxCommit returns a TxResponse given a
|
|
|
|
// ResultBroadcastTxCommit from tendermint.
|
2019-02-04 18:20:56 -08:00
|
|
|
func NewResponseFormatBroadcastTxCommit(res *ctypes.ResultBroadcastTxCommit) TxResponse {
|
2019-04-16 10:02:36 -07:00
|
|
|
if res == nil {
|
|
|
|
return TxResponse{}
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
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,
|
2019-12-27 09:57:54 -08:00
|
|
|
Codespace: res.CheckTx.Codespace,
|
2019-03-25 17:54:23 -07:00
|
|
|
Code: res.CheckTx.Code,
|
2019-04-03 07:49:34 -07:00
|
|
|
Data: strings.ToUpper(hex.EncodeToString(res.CheckTx.Data)),
|
2019-03-25 17:54:23 -07:00
|
|
|
RawLog: res.CheckTx.Log,
|
|
|
|
Logs: parsedLogs,
|
|
|
|
Info: res.CheckTx.Info,
|
|
|
|
GasWanted: res.CheckTx.GasWanted,
|
|
|
|
GasUsed: res.CheckTx.GasUsed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
|
|
|
return TxResponse{}
|
|
|
|
}
|
|
|
|
|
2019-02-11 02:28:23 -08:00
|
|
|
var txHash string
|
|
|
|
if res.Hash != nil {
|
|
|
|
txHash = res.Hash.String()
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
parsedLogs, _ := ParseABCILogs(res.DeliverTx.Log)
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
return TxResponse{
|
|
|
|
Height: res.Height,
|
2019-02-11 02:28:23 -08:00
|
|
|
TxHash: txHash,
|
2019-12-27 09:57:54 -08:00
|
|
|
Codespace: res.DeliverTx.Codespace,
|
2019-02-04 18:20:56 -08:00
|
|
|
Code: res.DeliverTx.Code,
|
2019-04-03 07:49:34 -07:00
|
|
|
Data: strings.ToUpper(hex.EncodeToString(res.DeliverTx.Data)),
|
2019-03-25 17:54:23 -07:00
|
|
|
RawLog: res.DeliverTx.Log,
|
2019-02-22 03:54:31 -08:00
|
|
|
Logs: parsedLogs,
|
2019-02-04 18:20:56 -08:00
|
|
|
Info: res.DeliverTx.Info,
|
|
|
|
GasWanted: res.DeliverTx.GasWanted,
|
|
|
|
GasUsed: res.DeliverTx.GasUsed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
// NewResponseFormatBroadcastTx returns a TxResponse given a ResultBroadcastTx from tendermint
|
2019-02-04 18:20:56 -08:00
|
|
|
func NewResponseFormatBroadcastTx(res *ctypes.ResultBroadcastTx) TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
|
|
|
return TxResponse{}
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
parsedLogs, _ := ParseABCILogs(res.Log)
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
return TxResponse{
|
|
|
|
Code: res.Code,
|
2019-04-03 07:49:34 -07:00
|
|
|
Data: res.Data.String(),
|
2019-03-25 17:54:23 -07:00
|
|
|
RawLog: res.Log,
|
2019-02-22 03:54:31 -08:00
|
|
|
Logs: parsedLogs,
|
2019-02-04 18:20:56 -08:00
|
|
|
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))
|
|
|
|
}
|
2019-04-03 07:49:34 -07:00
|
|
|
if r.Data != "" {
|
|
|
|
sb.WriteString(fmt.Sprintf(" Data: %s\n", r.Data))
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
2019-03-25 17:54:23 -07:00
|
|
|
if r.RawLog != "" {
|
|
|
|
sb.WriteString(fmt.Sprintf(" Raw Log: %s\n", r.RawLog))
|
|
|
|
}
|
2019-02-22 03:54:31 -08:00
|
|
|
if r.Logs != nil {
|
|
|
|
sb.WriteString(fmt.Sprintf(" Logs: %s\n", r.Logs))
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
2019-04-02 18:09:37 -07:00
|
|
|
if r.Timestamp != "" {
|
|
|
|
sb.WriteString(fmt.Sprintf(" Timestamp: %s\n", r.Timestamp))
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:20:56 -08:00
|
|
|
return strings.TrimSpace(sb.String())
|
|
|
|
}
|
2019-02-15 08:00:41 -08:00
|
|
|
|
|
|
|
// Empty returns true if the response is empty
|
|
|
|
func (r TxResponse) Empty() bool {
|
2019-02-22 03:54:31 -08:00
|
|
|
return r.TxHash == "" && r.Logs == nil
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:09:03 -07:00
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
// 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
|
2019-02-15 08:00:41 -08:00
|
|
|
}
|