cosmos-sdk/x/gov/client/utils/query_test.go
Callum Waters eb5b11be8a
feat: implement new gov msg & query servers (#10868)
## Description

Ref: #9438

This PR performs the major work of swapping out the v1beta1 msg server and query server for the new one which can process a proposal as an array of messages. This PR still retains the legacy servers which simply wrap around the new ones, providing the same interface as before.

In order to keep backwards compatibility, a new msg, `MsgExecLegacyContent` has been created which allows `Content` to become a `Msg` type and still be used as part of the new implementation. 


---

### 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)
2022-01-21 11:14:00 +00:00

194 lines
5.1 KiB
Go

package utils_test
import (
"context"
"regexp"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/rpc/client/mock"
"github.com/tendermint/tendermint/rpc/coretypes"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
)
type TxSearchMock struct {
txConfig client.TxConfig
mock.Client
txs []tmtypes.Tx
}
func (mock TxSearchMock) TxSearch(ctx context.Context, query string, prove bool, page, perPage *int, orderBy string) (*coretypes.ResultTxSearch, error) {
if page == nil {
*page = 0
}
if perPage == nil {
*perPage = 0
}
// Get the `message.action` value from the query.
messageAction := regexp.MustCompile(`message\.action='(.*)' .*$`)
msgType := messageAction.FindStringSubmatch(query)[1]
// Filter only the txs that match the query
matchingTxs := make([]tmtypes.Tx, 0)
for _, tx := range mock.txs {
sdkTx, err := mock.txConfig.TxDecoder()(tx)
if err != nil {
return nil, err
}
for _, msg := range sdkTx.GetMsgs() {
if msg.(legacytx.LegacyMsg).Type() == msgType {
matchingTxs = append(matchingTxs, tx)
break
}
}
}
start, end := client.Paginate(len(mock.txs), *page, *perPage, 100)
if start < 0 || end < 0 {
// nil result with nil error crashes utils.QueryTxsByEvents
return &coretypes.ResultTxSearch{}, nil
}
if len(matchingTxs) < end {
return &coretypes.ResultTxSearch{}, nil
}
txs := matchingTxs[start:end]
rst := &coretypes.ResultTxSearch{Txs: make([]*coretypes.ResultTx, len(txs)), TotalCount: len(txs)}
for i := range txs {
rst.Txs[i] = &coretypes.ResultTx{Tx: txs[i]}
}
return rst, nil
}
func (mock TxSearchMock) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
// any non nil Block needs to be returned. used to get time value
return &coretypes.ResultBlock{Block: &tmtypes.Block{}}, nil
}
func TestGetPaginatedVotes(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
type testCase struct {
description string
page, limit int
msgs [][]sdk.Msg
votes []v1beta2.Vote
}
acc1 := make(sdk.AccAddress, 20)
acc1[0] = 1
acc2 := make(sdk.AccAddress, 20)
acc2[0] = 2
acc1Msgs := []sdk.Msg{
v1beta2.NewMsgVote(acc1, 0, v1beta2.OptionYes),
v1beta2.NewMsgVote(acc1, 0, v1beta2.OptionYes),
}
acc2Msgs := []sdk.Msg{
v1beta2.NewMsgVote(acc2, 0, v1beta2.OptionYes),
v1beta2.NewMsgVoteWeighted(acc2, 0, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
}
for _, tc := range []testCase{
{
description: "1MsgPerTxAll",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
acc2Msgs[:1],
},
votes: []v1beta2.Vote{
v1beta2.NewVote(0, acc1, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
v1beta2.NewVote(0, acc2, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes))},
},
{
description: "2MsgPerTx1Chunk",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs,
acc2Msgs,
},
votes: []v1beta2.Vote{
v1beta2.NewVote(0, acc1, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
v1beta2.NewVote(0, acc1, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
},
},
{
description: "2MsgPerTx2Chunk",
page: 2,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs,
acc2Msgs,
},
votes: []v1beta2.Vote{
v1beta2.NewVote(0, acc2, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
v1beta2.NewVote(0, acc2, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes)),
},
},
{
description: "IncompleteSearchTx",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
},
votes: []v1beta2.Vote{v1beta2.NewVote(0, acc1, v1beta2.NewNonSplitVoteOption(v1beta2.OptionYes))},
},
{
description: "InvalidPage",
page: -1,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
},
},
{
description: "OutOfBounds",
page: 2,
limit: 10,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
},
},
} {
tc := tc
t.Run(tc.description, func(t *testing.T) {
var marshalled = make([]tmtypes.Tx, len(tc.msgs))
cli := TxSearchMock{txs: marshalled, txConfig: encCfg.TxConfig}
clientCtx := client.Context{}.
WithLegacyAmino(encCfg.Amino).
WithClient(cli).
WithTxConfig(encCfg.TxConfig)
for i := range tc.msgs {
txBuilder := clientCtx.TxConfig.NewTxBuilder()
err := txBuilder.SetMsgs(tc.msgs[i]...)
require.NoError(t, err)
tx, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
require.NoError(t, err)
marshalled[i] = tx
}
params := v1beta2.NewQueryProposalVotesParams(0, tc.page, tc.limit)
votesData, err := utils.QueryVotesByTxQuery(clientCtx, params)
require.NoError(t, err)
votes := []v1beta2.Vote{}
require.NoError(t, clientCtx.LegacyAmino.UnmarshalJSON(votesData, &votes))
require.Equal(t, len(tc.votes), len(votes))
for i := range votes {
require.Equal(t, tc.votes[i], votes[i])
}
})
}
}