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-05-04 04:09:03 -07:00
|
|
|
"math"
|
2019-02-04 18:20:56 -08:00
|
|
|
"strings"
|
|
|
|
|
2020-10-13 02:05:46 -07:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2021-11-16 11:24:38 -08:00
|
|
|
"github.com/tendermint/tendermint/rpc/coretypes"
|
2020-06-04 03:38:24 -07:00
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-09-10 11:26:47 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2019-02-04 18:20:56 -08:00
|
|
|
)
|
|
|
|
|
2020-09-07 07:47:12 -07:00
|
|
|
var cdc = codec.NewLegacyAmino()
|
2020-07-17 10:17:21 -07:00
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
func (gi GasInfo) String() string {
|
2021-11-30 07:29:09 -08:00
|
|
|
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &gi)
|
2020-03-26 09:46:10 -07:00
|
|
|
return string(bz)
|
2017-12-03 21:25:37 -08:00
|
|
|
}
|
2018-01-26 05:11:01 -08:00
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
func (r Result) String() string {
|
2021-11-30 07:29:09 -08:00
|
|
|
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &r)
|
2020-03-26 09:46:10 -07:00
|
|
|
return string(bz)
|
2018-01-26 05:11:01 -08:00
|
|
|
}
|
2019-02-04 18:20:56 -08:00
|
|
|
|
2020-03-26 09:46:10 -07:00
|
|
|
func (r Result) GetEvents() Events {
|
|
|
|
events := make(Events, len(r.Events))
|
|
|
|
for i, e := range r.Events {
|
|
|
|
events[i] = Event(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return events
|
2020-03-19 16:03:49 -07:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
// ABCIMessageLogs represents a slice of ABCIMessageLog.
|
|
|
|
type ABCIMessageLogs []ABCIMessageLog
|
|
|
|
|
2020-07-18 10:56:41 -07:00
|
|
|
func NewABCIMessageLog(i uint32, log string, events Events) ABCIMessageLog {
|
2019-09-12 08:05:27 -07:00
|
|
|
return ABCIMessageLog{
|
2020-07-18 10:56:41 -07:00
|
|
|
MsgIndex: i,
|
2019-09-12 08:05:27 -07:00
|
|
|
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 {
|
2020-07-17 10:17:21 -07:00
|
|
|
raw, err := cdc.MarshalJSON(logs)
|
2019-02-22 03:54:31 -08:00
|
|
|
if err == nil {
|
|
|
|
str = string(raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
// NewResponseResultTx returns a TxResponse given a ResultTx from tendermint
|
2021-11-16 11:24:38 -08:00
|
|
|
func NewResponseResultTx(res *coretypes.ResultTx, anyTx *codectypes.Any, timestamp string) *TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
2020-07-17 10:17:21 -07:00
|
|
|
return nil
|
2019-02-12 08:23:03 -08:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
parsedLogs, _ := ParseABCILogs(res.TxResult.Log)
|
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
return &TxResponse{
|
2019-02-04 18:20:56 -08:00
|
|
|
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,
|
2020-09-10 11:26:47 -07:00
|
|
|
Tx: anyTx,
|
2019-04-02 18:09:37 -07:00
|
|
|
Timestamp: timestamp,
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: res.TxResult.Events,
|
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.
|
2021-11-16 11:24:38 -08:00
|
|
|
func NewResponseFormatBroadcastTxCommit(res *coretypes.ResultBroadcastTxCommit) *TxResponse {
|
2019-04-16 10:02:36 -07:00
|
|
|
if res == nil {
|
2020-07-17 10:17:21 -07:00
|
|
|
return nil
|
2019-04-16 10:02:36 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
if !res.CheckTx.IsOK() {
|
|
|
|
return newTxResponseCheckTx(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newTxResponseDeliverTx(res)
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:24:38 -08:00
|
|
|
func newTxResponseCheckTx(res *coretypes.ResultBroadcastTxCommit) *TxResponse {
|
2019-03-25 17:54:23 -07:00
|
|
|
if res == nil {
|
2020-07-17 10:17:21 -07:00
|
|
|
return nil
|
2019-03-25 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var txHash string
|
|
|
|
if res.Hash != nil {
|
|
|
|
txHash = res.Hash.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedLogs, _ := ParseABCILogs(res.CheckTx.Log)
|
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
return &TxResponse{
|
2019-03-25 17:54:23 -07:00
|
|
|
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,
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: res.CheckTx.Events,
|
2019-03-25 17:54:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 11:24:38 -08:00
|
|
|
func newTxResponseDeliverTx(res *coretypes.ResultBroadcastTxCommit) *TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
2020-07-17 10:17:21 -07:00
|
|
|
return nil
|
2019-02-12 08:23:03 -08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
return &TxResponse{
|
2019-02-04 18:20:56 -08:00
|
|
|
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,
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: res.DeliverTx.Events,
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 08:00:41 -08:00
|
|
|
// NewResponseFormatBroadcastTx returns a TxResponse given a ResultBroadcastTx from tendermint
|
2021-11-16 11:24:38 -08:00
|
|
|
func NewResponseFormatBroadcastTx(res *coretypes.ResultBroadcastTx) *TxResponse {
|
2019-02-12 08:23:03 -08:00
|
|
|
if res == nil {
|
2020-07-17 10:17:21 -07:00
|
|
|
return nil
|
2019-02-12 08:23:03 -08:00
|
|
|
}
|
|
|
|
|
2019-02-22 03:54:31 -08:00
|
|
|
parsedLogs, _ := ParseABCILogs(res.Log)
|
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
return &TxResponse{
|
2020-05-12 08:26:54 -07:00
|
|
|
Code: res.Code,
|
|
|
|
Codespace: res.Codespace,
|
|
|
|
Data: res.Data.String(),
|
|
|
|
RawLog: res.Log,
|
|
|
|
Logs: parsedLogs,
|
|
|
|
TxHash: res.Hash.String(),
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r TxResponse) String() string {
|
2021-11-30 07:29:09 -08:00
|
|
|
bz, _ := codec.MarshalYAML(codec.NewProtoCodec(nil), &r)
|
|
|
|
return string(bz)
|
2019-02-04 18:20:56 -08:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
func NewSearchTxsResult(totalCount, count, page, limit uint64, txs []*TxResponse) *SearchTxsResult {
|
|
|
|
return &SearchTxsResult{
|
2019-05-04 04:09:03 -07:00
|
|
|
TotalCount: totalCount,
|
|
|
|
Count: count,
|
|
|
|
PageNumber: page,
|
2020-09-10 11:26:47 -07:00
|
|
|
PageTotal: uint64(math.Ceil(float64(totalCount) / float64(limit))),
|
2019-05-04 04:09:03 -07:00
|
|
|
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
|
|
|
}
|
2020-05-19 13:17:29 -07:00
|
|
|
|
2020-09-10 11:26:47 -07:00
|
|
|
var _, _ codectypes.UnpackInterfacesMessage = SearchTxsResult{}, TxResponse{}
|
2020-05-19 13:17:29 -07:00
|
|
|
|
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
|
|
//
|
|
|
|
// types.UnpackInterfaces needs to be called for each nested Tx because
|
|
|
|
// there are generally interfaces to unpack in Tx's
|
2020-09-10 11:26:47 -07:00
|
|
|
func (s SearchTxsResult) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
2020-05-19 13:17:29 -07:00
|
|
|
for _, tx := range s.Txs {
|
2020-09-10 11:26:47 -07:00
|
|
|
err := codectypes.UnpackInterfaces(tx, unpacker)
|
2020-05-19 13:17:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
2020-09-10 11:26:47 -07:00
|
|
|
func (r TxResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
2020-07-17 10:17:21 -07:00
|
|
|
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
|
2020-05-19 13:17:29 -07:00
|
|
|
}
|
2020-10-13 02:05:46 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
Closes: #10484
This PR makes the following big changes:
### 1. Change the tx.Handler interface
```diff
- CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error)
+ CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error)
// same for Deliver and Simulate
```
where:
```go
type Response struct {
GasWanted uint64
GasUsed uint64
// MsgResponses is an array containing each Msg service handler's response
// type, packed in an Any. This will get proto-serialized into the `Data` field
// in the ABCI Check/DeliverTx responses.
MsgResponses []*codectypes.Any
Log string
Events []abci.Event
}
```
### 2. Change what gets passed into the ABCI Check/DeliverTx `Data` field
Before, we were passing the concatenation of MsgResponse bytes into the `Data`. Now we are passing the proto-serialiazation of this struct:
```proto
message TxMsgData {
repeated google.protobuf.Any msg_responses = 2;
}
```
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-12-01 22:54:38 -08:00
|
|
|
any, err := codectypes.NewAnyWithValue(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-13 02:05:46 -07:00
|
|
|
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{
|
refactor: middleware refactor to change tx.Handler interface (#10527)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->
## Description
Closes: #10484
This PR makes the following big changes:
### 1. Change the tx.Handler interface
```diff
- CheckTx(ctx context.Context, tx sdk.Tx, req abci.RequestCheckTx) (abci.ResponseCheckTx, error)
+ CheckTx(ctx context.Context, req tx.Request, checkReq tx.RequestCheckTx) (tx.Response, tx.ResponseCheckTx, error)
// same for Deliver and Simulate
```
where:
```go
type Response struct {
GasWanted uint64
GasUsed uint64
// MsgResponses is an array containing each Msg service handler's response
// type, packed in an Any. This will get proto-serialized into the `Data` field
// in the ABCI Check/DeliverTx responses.
MsgResponses []*codectypes.Any
Log string
Events []abci.Event
}
```
### 2. Change what gets passed into the ABCI Check/DeliverTx `Data` field
Before, we were passing the concatenation of MsgResponse bytes into the `Data`. Now we are passing the proto-serialiazation of this struct:
```proto
message TxMsgData {
repeated google.protobuf.Any msg_responses = 2;
}
```
<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-12-01 22:54:38 -08:00
|
|
|
Data: data,
|
|
|
|
Events: events,
|
|
|
|
MsgResponses: []*codectypes.Any{any},
|
2020-10-13 02:05:46 -07:00
|
|
|
}, nil
|
|
|
|
}
|