Skip to content

Commit

Permalink
cleanup gov test thx
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Apr 12, 2024
1 parent fb5ef7c commit 511fb0d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
38 changes: 29 additions & 9 deletions e2e/helpers/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,54 @@ import (
"strconv"
"testing"

govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"

"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/stretchr/testify/require"
)

func ValidatorVote(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, proposalID string, voteOp string, searchHeightDelta int64) {
if err := chain.VoteOnProposalAllValidators(ctx, proposalID, voteOp); err != nil {
t.Fatal(err)
}
func ValidatorVoteV1(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, proposalID string, voteOp string, searchHeightDelta int64) {
err := chain.VoteOnProposalAllValidators(ctx, proposalID, voteOp)
require.NoError(t, err, "failed to vote on proposal", proposalID, "with all validators")

height, err := chain.Height(ctx)
require.NoError(t, err, "failed to get height")

propID, err := strconv.ParseUint(proposalID, 10, 64)
require.NoError(t, err, "failed to parse proposalID")

resp, _ := cosmos.PollForProposalStatus(ctx, chain, height, (height-2)+searchHeightDelta, propID, govv1beta1.StatusPassed)
t.Log("PollForProposalStatusV1beta1 resp", resp)
// try this, if NPE then try the other\
resp, err := cosmos.PollForProposalStatusV1(ctx, chain, height, height+searchHeightDelta, propID, govv1.ProposalStatus_PROPOSAL_STATUS_PASSED)
require.NoError(t, err, "failed to poll for proposal status", err)

require.NotNil(t, resp, "ValidatorVote proposal not found:", fmt.Sprintf("proposalID: %s", proposalID))
t.Log("PollForProposalStatusV1 resp", resp)

require.EqualValues(t, govv1beta1.StatusPassed, resp.Status, "proposal status did not change to passed in expected number of blocks")
}

func SubmitParamChangeProp(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, updatedParams []cosmos.ProtoMessage, sender string, waitForBlocks int64) string {
func ValidatorVoteV1Beta1(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, proposalID string, voteOp string, searchHeightDelta int64) {
err := chain.VoteOnProposalAllValidators(ctx, proposalID, voteOp)
require.NoError(t, err, "failed to vote on proposal", proposalID, "with all validators")

height, err := chain.Height(ctx)
require.NoError(t, err, "failed to get height")

propID, err := strconv.ParseUint(proposalID, 10, 64)
require.NoError(t, err, "failed to parse proposalID")

resp, err := cosmos.PollForProposalStatus(ctx, chain, height, height+searchHeightDelta, propID, govv1beta1.StatusPassed)
require.NoError(t, err, "failed to poll for proposal status", err)

require.NotNil(t, resp, "ValidatorVote proposal not found:", fmt.Sprintf("proposalID: %s", proposalID))
t.Log("PollForProposalStatusV1 resp", resp)

require.EqualValues(t, govv1beta1.StatusPassed, resp.Status, "proposal status did not change to passed in expected number of blocks")
}

func SubmitParamChangeProp(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, updatedParams []cosmos.ProtoMessage, sender string) string {
expedited := false
proposal, err := chain.BuildProposal(updatedParams, "UpdateParams", "params", "ipfs://CID", fmt.Sprintf(`50%s`, chain.Config().Denom), sender, expedited)
require.NoError(t, err, "error building proposal")
Expand All @@ -40,7 +62,5 @@ func SubmitParamChangeProp(t *testing.T, ctx context.Context, chain *cosmos.Cosm
t.Log("txProp", txProp)
require.NoError(t, err, "error submitting proposal")

ValidatorVote(t, ctx, chain, txProp.ProposalID, cosmos.ProposalVoteYes, waitForBlocks)

return txProp.ProposalID
}
51 changes: 26 additions & 25 deletions e2e/poa_gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var GovModuleAddress = "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn"
var (
GovModuleAddress = "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn"
RandAcc = "cosmos1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrql8a"
)

func TestPOAGovernance(t *testing.T) {
if testing.Short() {
Expand All @@ -35,9 +38,6 @@ func TestPOAGovernance(t *testing.T) {
t.Fatal(err)
}

users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
incorrectUser := users[0]

// get validator operator addresses
validators := helpers.GetValidatorsOperatingAddresses(t, ctx, chain)
require.Equal(t, len(validators), numVals)
Expand All @@ -46,31 +46,49 @@ func TestPOAGovernance(t *testing.T) {

// === Test Cases ===
testGovernance(t, ctx, chain, acc0, validators)
testUpdatePOAParams(t, ctx, chain, acc0, incorrectUser)
testUpdatePOAParams(t, ctx, chain, acc0)
}

func testGovernance(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, acc0 ibc.Wallet, validators []string) {
t.Log("\n===== TEST GOVERNANCE =====")

t.Run("success: gov proposal update params", func(t *testing.T) {
require.Equal(t, len(helpers.GetPOAParams(t, ctx, chain).Admins), 2)

updatedParams := []cosmos.ProtoMessage{
&poa.MsgUpdateParams{
Sender: GovModuleAddress,
Params: poa.Params{
Admins: []string{acc0.FormattedAddress(), GovModuleAddress, RandAcc},
},
},
}
propId := helpers.SubmitParamChangeProp(t, ctx, chain, acc0, updatedParams, GovModuleAddress)
helpers.ValidatorVoteV1(t, ctx, chain, propId, cosmos.ProposalVoteYes, 25)
require.Equal(t, len(helpers.GetPOAParams(t, ctx, chain).Admins), 3)
})

t.Run("success: gov proposal validator change", func(t *testing.T) {
// ibc.ChainConfig key: app_state.poa.params.admins must contain the governance address.
propID := helpers.SubmitGovernanceProposalForValidatorChanges(t, ctx, chain, acc0, validators[0], 1_234_567, true, GovModuleAddress)
helpers.ValidatorVote(t, ctx, chain, propID, cosmos.ProposalVoteYes, 25)
helpers.ValidatorVoteV1(t, ctx, chain, propID, cosmos.ProposalVoteYes, 25)

// validate the validator[0] was set to 1_234_567
val := helpers.GetValidators(t, ctx, chain).Validators[0]
require.Equal(t, val.Tokens, "1234567")
p := helpers.GetPOAConsensusPower(t, ctx, chain, val.OperatorAddress)
require.EqualValues(t, 1_234_567/1_000_000, p)
})

}

func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, acc0, incorrectUser ibc.Wallet) {
func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, acc0 ibc.Wallet) {
t.Log("\n===== TEST UPDATE POA PARAMS =====")
var tx helpers.TxResponse
var err error

users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
incorrectUser := users[0]

t.Run("fail: update-params message from a non authorized user", func(t *testing.T) {
tx, err = helpers.POAUpdateParams(t, ctx, chain, incorrectUser, []string{incorrectUser.FormattedAddress()}, true)
if err != nil {
Expand Down Expand Up @@ -140,21 +158,4 @@ func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
}
})

t.Run("success: gov proposal update params", func(t *testing.T) {
randAcc := "cosmos1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqnrql8a"

require.Equal(t, len(helpers.GetPOAParams(t, ctx, chain).Admins), 4)

updatedParams := []cosmos.ProtoMessage{
&poa.MsgUpdateParams{
Sender: GovModuleAddress,
Params: poa.Params{
Admins: []string{acc0.FormattedAddress(), GovModuleAddress, randAcc},
},
},
}
helpers.SubmitParamChangeProp(t, ctx, chain, acc0, updatedParams, GovModuleAddress, 25)
require.Equal(t, len(helpers.GetPOAParams(t, ctx, chain).Admins), 3)
})

}

0 comments on commit 511fb0d

Please sign in to comment.