2020-03-04 09:49:59 -08:00
|
|
|
package types_test
|
2018-08-06 12:00:49 -07:00
|
|
|
|
|
|
|
import (
|
2020-03-04 09:49:59 -08:00
|
|
|
"encoding/hex"
|
2020-10-13 02:05:46 -07:00
|
|
|
"fmt"
|
2020-03-04 09:49:59 -08:00
|
|
|
"strings"
|
2018-08-06 12:00:49 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-10-13 02:05:46 -07:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-28 03:46:49 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-03-04 09:49:59 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
"github.com/tendermint/tendermint/libs/bytes"
|
2021-11-16 11:24:38 -08:00
|
|
|
"github.com/tendermint/tendermint/rpc/coretypes"
|
2020-01-16 13:46:51 -08:00
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-10-13 02:05:46 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2020-03-04 09:49:59 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-06 12:00:49 -07:00
|
|
|
)
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
type resultTestSuite struct {
|
2020-09-28 03:46:49 -07:00
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func TestResultTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(resultTestSuite))
|
2020-09-28 03:46:49 -07:00
|
|
|
}
|
2019-02-22 03:54:31 -08:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *resultTestSuite) SetupSuite() {
|
|
|
|
s.T().Parallel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *resultTestSuite) TestParseABCILog() {
|
2020-09-28 03:46:49 -07:00
|
|
|
logs := `[{"log":"","msg_index":1,"success":true}]`
|
2020-03-04 09:49:59 -08:00
|
|
|
res, err := sdk.ParseABCILogs(logs)
|
2020-09-28 03:46:49 -07:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(res, 1)
|
|
|
|
s.Require().Equal(res[0].Log, "")
|
|
|
|
s.Require().Equal(res[0].MsgIndex, uint32(1))
|
2019-02-22 03:54:31 -08:00
|
|
|
}
|
2019-09-12 08:05:27 -07:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *resultTestSuite) TestABCIMessageLog() {
|
2020-09-07 07:47:12 -07:00
|
|
|
cdc := codec.NewLegacyAmino()
|
2020-03-04 09:49:59 -08:00
|
|
|
events := sdk.Events{sdk.NewEvent("transfer", sdk.NewAttribute("sender", "foo"))}
|
|
|
|
msgLog := sdk.NewABCIMessageLog(0, "", events)
|
|
|
|
msgLogs := sdk.ABCIMessageLogs{msgLog}
|
2020-07-17 10:17:21 -07:00
|
|
|
bz, err := cdc.MarshalJSON(msgLogs)
|
2020-09-28 03:46:49 -07:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(string(bz), msgLogs.String())
|
2019-09-12 08:05:27 -07:00
|
|
|
}
|
2020-03-04 09:49:59 -08:00
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *resultTestSuite) TestNewSearchTxsResult() {
|
2020-07-17 10:17:21 -07:00
|
|
|
got := sdk.NewSearchTxsResult(150, 20, 2, 20, []*sdk.TxResponse{})
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().Equal(&sdk.SearchTxsResult{
|
2020-03-04 09:49:59 -08:00
|
|
|
TotalCount: 150,
|
|
|
|
Count: 20,
|
|
|
|
PageNumber: 2,
|
|
|
|
PageTotal: 8,
|
|
|
|
Limit: 20,
|
2020-07-17 10:17:21 -07:00
|
|
|
Txs: []*sdk.TxResponse{},
|
2020-03-04 09:49:59 -08:00
|
|
|
}, got)
|
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *resultTestSuite) TestResponseResultTx() {
|
2020-03-04 09:49:59 -08:00
|
|
|
deliverTxResult := abci.ResponseDeliverTx{
|
|
|
|
Codespace: "codespace",
|
|
|
|
Code: 1,
|
|
|
|
Data: []byte("data"),
|
|
|
|
Log: `[]`,
|
|
|
|
Info: "info",
|
|
|
|
GasWanted: 100,
|
|
|
|
GasUsed: 90,
|
|
|
|
}
|
2021-11-16 11:24:38 -08:00
|
|
|
resultTx := &coretypes.ResultTx{
|
2020-03-04 09:49:59 -08:00
|
|
|
Hash: bytes.HexBytes([]byte("test")),
|
|
|
|
Height: 10,
|
|
|
|
TxResult: deliverTxResult,
|
|
|
|
}
|
|
|
|
logs, err := sdk.ParseABCILogs(`[]`)
|
2020-09-28 03:46:49 -07:00
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-17 10:17:21 -07:00
|
|
|
want := &sdk.TxResponse{
|
2020-03-04 09:49:59 -08:00
|
|
|
TxHash: "74657374",
|
|
|
|
Height: 10,
|
|
|
|
Codespace: "codespace",
|
|
|
|
Code: 1,
|
|
|
|
Data: strings.ToUpper(hex.EncodeToString([]byte("data"))),
|
|
|
|
RawLog: `[]`,
|
|
|
|
Logs: logs,
|
|
|
|
Info: "info",
|
|
|
|
GasWanted: 100,
|
|
|
|
GasUsed: 90,
|
2020-09-10 11:26:47 -07:00
|
|
|
Tx: nil,
|
2020-03-04 09:49:59 -08:00
|
|
|
Timestamp: "timestamp",
|
|
|
|
}
|
|
|
|
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().Equal(want, sdk.NewResponseResultTx(resultTx, nil, "timestamp"))
|
|
|
|
s.Require().Equal((*sdk.TxResponse)(nil), sdk.NewResponseResultTx(nil, nil, "timestamp"))
|
2021-11-30 07:29:09 -08:00
|
|
|
s.Require().Equal(`code: 1
|
|
|
|
codespace: codespace
|
|
|
|
data: "64617461"
|
|
|
|
events: []
|
|
|
|
gas_used: "90"
|
|
|
|
gas_wanted: "100"
|
|
|
|
height: "10"
|
|
|
|
info: info
|
|
|
|
logs: []
|
|
|
|
raw_log: '[]'
|
|
|
|
timestamp: timestamp
|
|
|
|
tx: null
|
|
|
|
txhash: "74657374"
|
|
|
|
`, sdk.NewResponseResultTx(resultTx, nil, "timestamp").String())
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().True(sdk.TxResponse{}.Empty())
|
|
|
|
s.Require().False(want.Empty())
|
2020-03-04 09:49:59 -08:00
|
|
|
|
2021-11-16 11:24:38 -08:00
|
|
|
resultBroadcastTx := &coretypes.ResultBroadcastTx{
|
2020-05-12 08:26:54 -07:00
|
|
|
Code: 1,
|
|
|
|
Codespace: "codespace",
|
|
|
|
Data: []byte("data"),
|
|
|
|
Log: `[]`,
|
|
|
|
Hash: bytes.HexBytes([]byte("test")),
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
2020-09-28 03:46:49 -07:00
|
|
|
|
|
|
|
s.Require().Equal(&sdk.TxResponse{
|
2020-05-12 08:26:54 -07:00
|
|
|
Code: 1,
|
|
|
|
Codespace: "codespace",
|
|
|
|
Data: "64617461",
|
|
|
|
RawLog: `[]`,
|
|
|
|
Logs: logs,
|
|
|
|
TxHash: "74657374",
|
2020-03-04 09:49:59 -08:00
|
|
|
}, sdk.NewResponseFormatBroadcastTx(resultBroadcastTx))
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().Equal((*sdk.TxResponse)(nil), sdk.NewResponseFormatBroadcastTx(nil))
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
|
|
|
|
2020-09-29 04:29:10 -07:00
|
|
|
func (s *resultTestSuite) TestResponseFormatBroadcastTxCommit() {
|
2020-03-04 09:49:59 -08:00
|
|
|
// test nil
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().Equal((*sdk.TxResponse)(nil), sdk.NewResponseFormatBroadcastTxCommit(nil))
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
logs, err := sdk.ParseABCILogs(`[]`)
|
2020-09-28 03:46:49 -07:00
|
|
|
s.Require().NoError(err)
|
2020-03-04 09:49:59 -08:00
|
|
|
|
|
|
|
// test checkTx
|
2021-11-16 11:24:38 -08:00
|
|
|
checkTxResult := &coretypes.ResultBroadcastTxCommit{
|
2020-03-04 09:49:59 -08:00
|
|
|
Height: 10,
|
|
|
|
Hash: bytes.HexBytes([]byte("test")),
|
|
|
|
CheckTx: abci.ResponseCheckTx{
|
|
|
|
Code: 90,
|
|
|
|
Data: nil,
|
|
|
|
Log: `[]`,
|
|
|
|
Info: "info",
|
|
|
|
GasWanted: 99,
|
|
|
|
GasUsed: 100,
|
|
|
|
Codespace: "codespace",
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: []abci.Event{
|
|
|
|
{
|
|
|
|
Type: "message",
|
|
|
|
Attributes: []abci.EventAttribute{
|
|
|
|
{
|
|
|
|
Key: "action",
|
|
|
|
Value: "foo",
|
|
|
|
Index: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-04 09:49:59 -08:00
|
|
|
},
|
|
|
|
}
|
2021-11-16 11:24:38 -08:00
|
|
|
deliverTxResult := &coretypes.ResultBroadcastTxCommit{
|
2020-03-04 09:49:59 -08:00
|
|
|
Height: 10,
|
|
|
|
Hash: bytes.HexBytes([]byte("test")),
|
|
|
|
DeliverTx: abci.ResponseDeliverTx{
|
|
|
|
Code: 90,
|
|
|
|
Data: nil,
|
|
|
|
Log: `[]`,
|
|
|
|
Info: "info",
|
|
|
|
GasWanted: 99,
|
|
|
|
GasUsed: 100,
|
|
|
|
Codespace: "codespace",
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: []abci.Event{
|
|
|
|
{
|
|
|
|
Type: "message",
|
|
|
|
Attributes: []abci.EventAttribute{
|
|
|
|
{
|
|
|
|
Key: "action",
|
|
|
|
Value: "foo",
|
|
|
|
Index: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-04 09:49:59 -08:00
|
|
|
},
|
|
|
|
}
|
2020-07-17 10:17:21 -07:00
|
|
|
want := &sdk.TxResponse{
|
2020-03-04 09:49:59 -08:00
|
|
|
Height: 10,
|
|
|
|
TxHash: "74657374",
|
|
|
|
Codespace: "codespace",
|
|
|
|
Code: 90,
|
|
|
|
Data: "",
|
|
|
|
RawLog: `[]`,
|
|
|
|
Logs: logs,
|
|
|
|
Info: "info",
|
|
|
|
GasWanted: 99,
|
|
|
|
GasUsed: 100,
|
2021-11-30 07:29:09 -08:00
|
|
|
Events: []abci.Event{
|
|
|
|
{
|
|
|
|
Type: "message",
|
|
|
|
Attributes: []abci.EventAttribute{
|
|
|
|
{
|
|
|
|
Key: "action",
|
|
|
|
Value: "foo",
|
|
|
|
Index: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
2020-09-28 03:46:49 -07:00
|
|
|
|
|
|
|
s.Require().Equal(want, sdk.NewResponseFormatBroadcastTxCommit(checkTxResult))
|
|
|
|
s.Require().Equal(want, sdk.NewResponseFormatBroadcastTxCommit(deliverTxResult))
|
2020-03-04 09:49:59 -08:00
|
|
|
}
|
2020-10-13 02:05:46 -07:00
|
|
|
|
|
|
|
func TestWrapServiceResult(t *testing.T) {
|
|
|
|
ctx := sdk.Context{}
|
|
|
|
|
|
|
|
res, err := sdk.WrapServiceResult(ctx, nil, fmt.Errorf("test"))
|
|
|
|
require.Nil(t, res)
|
|
|
|
require.NotNil(t, 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
|
|
|
res, err = sdk.WrapServiceResult(ctx, &testdata.Dog{}, nil)
|
2020-10-13 02:05:46 -07:00
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Empty(t, res.Events)
|
|
|
|
|
|
|
|
ctx = ctx.WithEventManager(sdk.NewEventManager())
|
|
|
|
ctx.EventManager().EmitEvent(sdk.NewEvent("test"))
|
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
|
|
|
res, err = sdk.WrapServiceResult(ctx, &testdata.Dog{}, nil)
|
2020-10-13 02:05:46 -07:00
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, res.Events, 1)
|
|
|
|
|
|
|
|
spot := testdata.Dog{Name: "spot"}
|
|
|
|
res, err = sdk.WrapServiceResult(ctx, &spot, nil)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, res.Events, 1)
|
|
|
|
var spot2 testdata.Dog
|
|
|
|
err = proto.Unmarshal(res.Data, &spot2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, spot, spot2)
|
|
|
|
}
|