Skip to content

Commit

Permalink
test: fix groups test / sanitize messages (#3516)
Browse files Browse the repository at this point in the history
* sanitizate messages in broadcast messages function

* Update e2e/testsuite/sanitize/messages.go

---------

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
  • Loading branch information
chatton and colin-axner authored May 3, 2023
1 parent 48341b4 commit 6894016
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
50 changes: 50 additions & 0 deletions e2e/testsuite/sanitize/messages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sanitize

import (
sdk "github.com/cosmos/cosmos-sdk/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
grouptypes "github.com/cosmos/cosmos-sdk/x/group"

"github.com/cosmos/ibc-go/e2e/semverutil"
)

var (
// groupsv1ProposalTitleAndSummary represents the releases that support the new title and summary fields.
groupsv1ProposalTitleAndSummary = semverutil.FeatureReleases{
MajorVersion: "v7",
}
// govv1ProposalTitleAndSummary represents the releases that support the new title and summary fields.
govv1ProposalTitleAndSummary = semverutil.FeatureReleases{
MajorVersion: "v7",
}
)

// Messages removes any fields that are not supported by the chain version.
// For example, any fields that have been added in later sdk releases.
func Messages(tag string, msgs ...sdk.Msg) []sdk.Msg {
sanitizedMsgs := make([]sdk.Msg, len(msgs))
for i := range msgs {
sanitizedMsgs[i] = removeUnknownFields(tag, msgs[i])
}
return sanitizedMsgs
}

// removeUnknownFields removes any fields that are not supported by the chain version.
// The input message is returned if no changes are made.
func removeUnknownFields(tag string, msg sdk.Msg) sdk.Msg {
switch msg := msg.(type) {
case *govtypesv1.MsgSubmitProposal:
if !govv1ProposalTitleAndSummary.IsSupported(tag) {
msg.Title = ""
msg.Summary = ""
}
return msg
case *grouptypes.MsgSubmitProposal:
if !groupsv1ProposalTitleAndSummary.IsSupported(tag) {
msg.Title = ""
msg.Summary = ""
}
return msg
}
return msg
}
28 changes: 18 additions & 10 deletions e2e/testsuite/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/strangelove-ventures/interchaintest/v7/ibc"
test "github.com/strangelove-ventures/interchaintest/v7/testutil"

"github.com/cosmos/ibc-go/e2e/semverutil"
"github.com/cosmos/ibc-go/e2e/testsuite/sanitize"
"github.com/cosmos/ibc-go/e2e/testvalues"
feetypes "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
Expand All @@ -31,6 +31,9 @@ import (
func (s *E2ETestSuite) BroadcastMessages(ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, msgs ...sdk.Msg) (sdk.TxResponse, error) {
broadcaster := cosmos.NewBroadcaster(s.T(), chain)

// strip out any fields that may not be supported for the given chain version.
msgs = sanitize.Messages(chain.Nodes()[0].Image.Version, msgs...)

var seq uint64
broadcaster.ConfigureClientContextOptions(func(clientContext client.Context) client.Context {
// use a codec with all the types our tests care about registered.
Expand Down Expand Up @@ -110,17 +113,27 @@ func containsMessage(s string, messages []string) bool {
// AssertValidTxResponse verifies that an sdk.TxResponse
// has non-empty values.
func (s *E2ETestSuite) AssertValidTxResponse(resp sdk.TxResponse) {
errorMsg := fmt.Sprintf("%+v", resp)
errorMsg := addDebuggingInformation(fmt.Sprintf("%+v", resp))

s.Require().NotEmpty(resp.TxHash, errorMsg)
s.Require().NotEqual(int64(0), resp.GasUsed, errorMsg)
s.Require().NotEqual(int64(0), resp.GasWanted, errorMsg)
s.Require().NotEmpty(resp.Events, errorMsg)
s.Require().NotEmpty(resp.Data, errorMsg)
}

// govv1ProposalTitleAndSummary represents the releases that support the new title and summary fields.
var govv1ProposalTitleAndSummary = semverutil.FeatureReleases{
MajorVersion: "v7",
// addDebuggingInformation adds additional debugging information to the error message
// based on common types of errors that can occur.
func addDebuggingInformation(errorMsg string) string {
if strings.Contains(errorMsg, "errUnknownField") {
errorMsg += `
This error is likely due to a new an unrecognized proto field being provided to a chain using an older version of the sdk.
If this is a compatibility test, ensure that the fields are being sanitized in the sanitize.Messages function.
`
}
return errorMsg
}

// ExecuteGovProposalV1 submits a governance proposal using the provided user and message and uses all validators
Expand All @@ -133,11 +146,6 @@ func (s *E2ETestSuite) ExecuteGovProposalV1(ctx context.Context, msg sdk.Msg, ch
msgSubmitProposal, err := govtypesv1.NewMsgSubmitProposal(msgs, sdk.NewCoins(sdk.NewCoin(chain.Config().Denom, govtypesv1.DefaultMinDepositTokens)), sender.String(), "", fmt.Sprintf("e2e gov proposal: %d", proposalID), fmt.Sprintf("executing gov proposal %d", proposalID))
s.Require().NoError(err)

if !govv1ProposalTitleAndSummary.IsSupported(chain.Nodes()[0].Image.Version) {
msgSubmitProposal.Title = ""
msgSubmitProposal.Summary = ""
}

resp, err := s.BroadcastMessages(ctx, chain, user, msgSubmitProposal)
s.AssertValidTxResponse(resp)
s.Require().NoError(err)
Expand Down

0 comments on commit 6894016

Please sign in to comment.