cosmos-sdk/x/gov/client/utils/query_test.go
Amaury dfe3e7a8d7
Remove ServiceMsgs from ADR-031 (#9139)
* wip

* wip

* wip

* wip on refactoring adr 031 type URLs

* Fix msg_service_router

* Fix gov client queries

* Fix some modules tests

* Remove all instances of "*.Msg/*"

* Uncomment code

* Remove commented code

* // simulation.NewWeightedOperation(

* Fix CopyTx

* Fix more tests

* Fix x/gov test

* proto.MessageName->sdk.MsgName

* Fix authz tests

* Use MsgRoute in feegrant and staking/authz

* Fix more tests

* Fix sims?

* Add norace tag

* Add CL

* rebuild rosetta api test data

* Update ADR

* Rename MsgRoute -> MsgTypeURL

* Fix codec registration

* Remove sdk.GetLegacySignBytes

* Update types/tx_msg.go

* Update x/authz/simulation/operations.go

* Move LegacyMsg to legacytx

* Update CHANGELOG.md

Co-authored-by: Aaron Craelius <aaron@regen.network>

* Remove NewAnyWithCustomTypeURL

* Keep support for ServiceMsgs

* Fix TxBody UnpackInterfaces

* Fix test

* Address review

* Remove support for ServiceMsg typeURLs

* Fix lint

* Update changelog

* Fix tests

* Use sdk.MsgTypeURL everywhere

* Fix tests

* Fix rosetta, run make rosetta-data

* Fix rosetta thanks to froydi

* Address reviews

* Fix test

* Remove stray log

* Update CL

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Aaron Craelius <aaron@regen.network>
2021-04-30 11:00:47 +00:00

194 lines
5.0 KiB
Go

package utils_test
import (
"context"
"regexp"
"testing"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/rpc/client/mock"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
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/legacy/legacytx"
"github.com/cosmos/cosmos-sdk/x/gov/client/utils"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
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) (*ctypes.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 &ctypes.ResultTxSearch{}, nil
}
if len(matchingTxs) < end {
return &ctypes.ResultTxSearch{}, nil
}
txs := matchingTxs[start:end]
rst := &ctypes.ResultTxSearch{Txs: make([]*ctypes.ResultTx, len(txs)), TotalCount: len(txs)}
for i := range txs {
rst.Txs[i] = &ctypes.ResultTx{Tx: txs[i]}
}
return rst, nil
}
func (mock TxSearchMock) Block(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) {
// any non nil Block needs to be returned. used to get time value
return &ctypes.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 []types.Vote
}
acc1 := make(sdk.AccAddress, 20)
acc1[0] = 1
acc2 := make(sdk.AccAddress, 20)
acc2[0] = 2
acc1Msgs := []sdk.Msg{
types.NewMsgVote(acc1, 0, types.OptionYes),
types.NewMsgVote(acc1, 0, types.OptionYes),
}
acc2Msgs := []sdk.Msg{
types.NewMsgVote(acc2, 0, types.OptionYes),
types.NewMsgVoteWeighted(acc2, 0, types.NewNonSplitVoteOption(types.OptionYes)),
}
for _, tc := range []testCase{
{
description: "1MsgPerTxAll",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
acc2Msgs[:1],
},
votes: []types.Vote{
types.NewVote(0, acc1, types.NewNonSplitVoteOption(types.OptionYes)),
types.NewVote(0, acc2, types.NewNonSplitVoteOption(types.OptionYes))},
},
{
description: "2MsgPerTx1Chunk",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs,
acc2Msgs,
},
votes: []types.Vote{
types.NewVote(0, acc1, types.NewNonSplitVoteOption(types.OptionYes)),
types.NewVote(0, acc1, types.NewNonSplitVoteOption(types.OptionYes)),
},
},
{
description: "2MsgPerTx2Chunk",
page: 2,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs,
acc2Msgs,
},
votes: []types.Vote{
types.NewVote(0, acc2, types.NewNonSplitVoteOption(types.OptionYes)),
types.NewVote(0, acc2, types.NewNonSplitVoteOption(types.OptionYes)),
},
},
{
description: "IncompleteSearchTx",
page: 1,
limit: 2,
msgs: [][]sdk.Msg{
acc1Msgs[:1],
},
votes: []types.Vote{types.NewVote(0, acc1, types.NewNonSplitVoteOption(types.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 := types.NewQueryProposalVotesParams(0, tc.page, tc.limit)
votesData, err := utils.QueryVotesByTxQuery(clientCtx, params)
require.NoError(t, err)
votes := []types.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])
}
})
}
}