Skip to content

Commit

Permalink
tests: cleanup integration tests (#1367)
Browse files Browse the repository at this point in the history
* tests: update integration/common.go

* tests: update democ (try cast testDistr to sdkdistr keeper)

* tests: update distr (try cast testDistr to sdkdistr keeper)

* tests: update client tests (send RecoverClient msg)

* tests: update slash tests (bitstamps, interfaces)

* tests: update stop_consumer tests

* tests: update unbonding tests

* tests: update throttle tests

* tests: update throttle_retry tests

* tests: update slashing tests (possibly broken, methods are private in x/evidence)

* partial: update democ tests (use Param collections)
  • Loading branch information
MSalopek committed Oct 17, 2023
1 parent cf06800 commit c036a10
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 619 deletions.
24 changes: 12 additions & 12 deletions app/consumer-democracy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,10 +877,10 @@ func (app *App) GetTestSlashingKeeper() testutil.TestSlashingKeeper {
return app.SlashingKeeper
}

// // GetTestEvidenceKeeper implements the ConsumerApp interface.
// func (app *App) GetTestEvidenceKeeper() testutil.TestEvidenceKeeper {
// return app.EvidenceKeeper
// }
// GetTestEvidenceKeeper implements the ConsumerApp interface.
func (app *App) GetTestEvidenceKeeper() evidencekeeper.Keeper {
return app.EvidenceKeeper
}

// GetTestStakingKeeper implements the ConsumerApp interface.
func (app *App) GetTestStakingKeeper() testutil.TestStakingKeeper {
Expand All @@ -892,15 +892,15 @@ func (app *App) GetTestDistributionKeeper() testutil.TestDistributionKeeper {
return app.DistrKeeper
}

// // GetTestMintKeeper implements the ConsumerApp interface.
// func (app *App) GetTestMintKeeper() testutil.TestMintKeeper {
// return app.MintKeeper
// }
// GetTestMintKeeper implements the ConsumerApp interface.
func (app *App) GetTestMintKeeper() mintkeeper.Keeper {
return app.MintKeeper
}

// // GetTestGovKeeper implements the ConsumerApp interface.
// func (app *App) GetTestGovKeeper() testutil.TestGovKeeper {
// return app.GovKeeper
// }
// GetTestGovKeeper implements the ConsumerApp interface.
func (app *App) GetTestGovKeeper() govkeeper.Keeper {
return app.GovKeeper
}

// TestingApp functions

Expand Down
5 changes: 5 additions & 0 deletions app/consumer/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@ func (app *App) GetTestSlashingKeeper() testutil.TestSlashingKeeper {
return app.SlashingKeeper
}

// GetTestEvidenceKeeper implements the ConsumerApp interface.
func (app *App) GetTestEvidenceKeeper() evidencekeeper.Keeper {
return app.EvidenceKeeper
}

// TestingApp functions

// GetBaseApp implements the TestingApp interface.
Expand Down
18 changes: 9 additions & 9 deletions app/sovereign/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,9 @@ func (app *App) GetTestSlashingKeeper() testutil.TestSlashingKeeper {
return app.SlashingKeeper
}

// func (app *App) GetTestEvidenceKeeper() testutil.TestEvidenceKeeper {
// return app.EvidenceKeeper
// }
func (app *App) GetTestEvidenceKeeper() evidencekeeper.Keeper {
return app.EvidenceKeeper
}

func (app *App) GetTestStakingKeeper() testutil.TestStakingKeeper {
return app.StakingKeeper
Expand All @@ -769,13 +769,13 @@ func (app *App) GetTestDistributionKeeper() testutil.TestDistributionKeeper {
return app.DistrKeeper
}

// func (app *App) GetTestMintKeeper() testutil.TestMintKeeper {
// return app.MintKeeper
// }
func (app *App) GetTestMintKeeper() mintkeeper.Keeper {
return app.MintKeeper
}

// func (app *App) GetTestGovKeeper() testutil.TestGovKeeper {
// return app.GovKeeper
// }
func (app *App) GetTestGovKeeper() govkeeper.Keeper {
return app.GovKeeper
}

// TestingApp functions

Expand Down
33 changes: 21 additions & 12 deletions tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (s *CCVTestSuite) getValByIdx(index int) (validator stakingtypes.Validator,
}

func (s *CCVTestSuite) getVal(ctx sdk.Context, valAddr sdk.ValAddress) stakingtypes.Validator {
validator, found := s.providerApp.GetTestStakingKeeper().GetValidator(s.providerCtx(), valAddr)
s.Require().True(found)
validator, err := s.providerApp.GetTestStakingKeeper().GetValidator(s.providerCtx(), valAddr)
s.Require().NoError(err)
return validator
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func delegateByIdx(s *CCVTestSuite, delAddr sdk.AccAddress, bondAmt math.Int, id

// undelegate unbonds an amount of delegator shares from a given validator
func undelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valAddr sdk.ValAddress, sharesAmount math.LegacyDec) (valsetUpdateId uint64) {
_, err := s.providerApp.GetTestStakingKeeper().Undelegate(s.providerCtx(), delAddr, valAddr, sharesAmount)
_, _, err := s.providerApp.GetTestStakingKeeper().Undelegate(s.providerCtx(), delAddr, valAddr, sharesAmount)
s.Require().NoError(err)

// save the current valset update ID
Expand All @@ -202,10 +202,11 @@ func redelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valSrcAddr sdk.ValAddre
)
s.Require().NoError(err)

providerUnbondingPeriod := stakingKeeper.UnbondingTime(ctx)
providerUnbondingPeriod, err := stakingKeeper.UnbondingTime(ctx)
s.Require().NoError(err)

valSrc, found := stakingKeeper.GetValidator(ctx, valSrcAddr)
s.Require().True(found)
valSrc, err := stakingKeeper.GetValidator(ctx, valSrcAddr)
s.Require().NoError(err)

// Completion time of redelegation operation will be after unbonding period if source val is bonded
if valSrc.IsBonded() {
Expand Down Expand Up @@ -302,7 +303,8 @@ func relayAllCommittedPackets(
// to be one day larger than the consumer unbonding period.
func incrementTimeByUnbondingPeriod(s *CCVTestSuite, chainType ChainType) {
// Get unboding periods
providerUnbondingPeriod := s.providerApp.GetTestStakingKeeper().UnbondingTime(s.providerCtx())
providerUnbondingPeriod, err := s.providerApp.GetTestStakingKeeper().UnbondingTime(s.providerCtx())
s.Require().NoError(err)
consumerUnbondingPeriod := s.consumerApp.GetConsumerKeeper().GetUnbondingPeriod(s.consumerCtx())
var jumpPeriod time.Duration
if chainType == Provider {
Expand Down Expand Up @@ -355,8 +357,8 @@ func checkCCVUnbondingOp(s *CCVTestSuite, providerCtx sdk.Context, chainID strin
func checkRedelegations(s *CCVTestSuite, delAddr sdk.AccAddress,
expect uint16,
) []stakingtypes.Redelegation {
redelegations := s.providerApp.GetTestStakingKeeper().GetRedelegations(s.providerCtx(), delAddr, 2)

redelegations, err := s.providerApp.GetTestStakingKeeper().GetRedelegations(s.providerCtx(), delAddr, 2)
s.Require().NoError(err)
s.Require().Len(redelegations, int(expect))
return redelegations
}
Expand All @@ -369,8 +371,12 @@ func checkRedelegationEntryCompletionTime(
}

func getStakingUnbondingDelegationEntry(ctx sdk.Context, k testutil.TestStakingKeeper, id uint64) (stakingUnbondingOp stakingtypes.UnbondingDelegationEntry, found bool) {
stakingUbd, found := k.GetUnbondingDelegationByUnbondingID(ctx, id)
stakingUbd, err := k.GetUnbondingDelegationByUnbondingID(ctx, id)
if err != nil {
panic(fmt.Sprintf("could not get unbonding delegation", err))
}

found = false
for _, entry := range stakingUbd.Entries {
if entry.UnbondingId == id {
stakingUnbondingOp = entry
Expand Down Expand Up @@ -604,8 +610,11 @@ func (s *CCVTestSuite) setupValidatorPowers() {

stakingKeeper := s.providerApp.GetTestStakingKeeper()
for _, val := range s.providerChain.Vals.Validators {
power := stakingKeeper.GetLastValidatorPower(s.providerCtx(), sdk.ValAddress(val.Address))
power, err := stakingKeeper.GetLastValidatorPower(s.providerCtx(), sdk.ValAddress(val.Address))
s.Require().NoError(err)
s.Require().Equal(int64(1000), power)
}
s.Require().Equal(int64(4000), stakingKeeper.GetLastTotalPower(s.providerCtx()).Int64())
totalPower, err := stakingKeeper.GetLastTotalPower(s.providerCtx())
s.Require().NoError(err)
s.Require().Equal(int64(4000), totalPower.Int64())
}
67 changes: 47 additions & 20 deletions tests/integration/democracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

sdkdistrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing"
testutil "github.com/cosmos/interchain-security/v3/testutil/integration"
consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
Expand Down Expand Up @@ -74,13 +76,16 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
accountKeeper := s.consumerApp.GetTestAccountKeeper()
distrKeeper := s.consumerApp.GetTestDistributionKeeper()
bankKeeper := s.consumerApp.GetTestBankKeeper()
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())
bondDenom, err := stakingKeeper.BondDenom(s.consumerCtx())
s.Require().NoError(err)

currentRepresentativesRewards := map[string]math.LegacyDec{}
nextRepresentativesRewards := map[string]math.LegacyDec{}
representativesTokens := map[string]math.Int{}

for _, representative := range stakingKeeper.GetAllValidators(s.consumerCtx()) {
representatives, err := stakingKeeper.GetAllValidators(s.consumerCtx())
s.Require().NoError(err)
for _, representative := range representatives {
currentRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
nextRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
representativesTokens[representative.OperatorAddress] = representative.GetTokens()
Expand All @@ -90,24 +95,31 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
providerRedistributeAccount := accountKeeper.GetModuleAccount(s.consumerCtx(), consumertypes.ConsumerToSendToProviderName)
// balance of consumer redistribute address will always be 0 when checked between 2 NextBlock() calls

dk, ok := distrKeeper.(sdkdistrkeeper.Keeper)
s.Require().True(ok)
feePool, err := dk.FeePool.Get(s.consumerCtx().Context())
s.Require().NoError(err)
s.Require().NotEmpty(feePool)
currentDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
currentProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
currentCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
currentCommunityPoolBalance := feePool.GetCommunityPool().AmountOf(bondDenom)
for key := range currentRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
representativeReward := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr).Rewards.AmountOf(bondDenom)
currentRepresentativesRewards[key] = representativeReward
representativeReward, err := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr)
s.Require().NoError(err)
currentRepresentativesRewards[key] = representativeReward.Rewards.AmountOf(bondDenom)
}

s.consumerChain.NextBlock()

nextDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
nextProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
nextCommunityPoolBalance := distrKeeper.GetFeePoolCommunityCoins(s.consumerCtx()).AmountOf(bondDenom)
nextCommunityPoolBalance := feePool.GetCommunityPool().AmountOf(bondDenom)
for key := range nextRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
representativeReward := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr).Rewards.AmountOf(bondDenom)
nextRepresentativesRewards[key] = representativeReward
representativeReward, err := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr)
s.Require().NoError(err)
nextRepresentativesRewards[key] = representativeReward.Rewards.AmountOf(bondDenom)
}

distrModuleDifference := nextDistrModuleAccountBalance.Sub(currentDistrModuleAccountBalance)
Expand All @@ -126,8 +138,9 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
// confirm that the total amount given to the community pool plus all representatives is equal to the total amount taken out of distribution
s.Require().Equal(distrModuleDifference, consumerRedistributeDifference)
// confirm that the percentage given to the community pool is equal to the configured community tax percentage.
s.Require().Equal(communityPoolDifference.Quo(consumerRedistributeDifference),
distrKeeper.GetCommunityTax(s.consumerCtx()))
tax, err := distrKeeper.GetCommunityTax(s.consumerCtx())
s.Require().NoError(err)
s.Require().Equal(communityPoolDifference.Quo(consumerRedistributeDifference), tax)
// check that the fraction actually kept by the consumer is the correct fraction. using InEpsilon because the math code uses truncations
s.Require().InEpsilon(distrModuleDifference.Quo(
providerDifference.Add(distrModuleDifference)).MustFloat64(),
Expand All @@ -147,28 +160,33 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
}
}

// @MSalopek -> this is broken for v50
func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
govKeeper := s.consumerApp.GetTestGovKeeper()
params := govKeeper.GetParams(s.consumerCtx())
params, err := govKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)

stakingKeeper := s.consumerApp.GetTestStakingKeeper()
bankKeeper := s.consumerApp.GetTestBankKeeper()
accountKeeper := s.consumerApp.GetTestAccountKeeper()
mintKeeper := s.consumerApp.GetTestMintKeeper()
newAuthParamValue := uint64(128)
newMintParamValue := math.LegacyNewDecWithPrec(1, 1) // "0.100000000000000000"
votingAccounts := s.consumerChain.SenderAccounts
bondDenom := stakingKeeper.BondDenom(s.consumerCtx())
bondDenom, err := stakingKeeper.BondDenom(s.consumerCtx())
s.Require().NoError(err)
depositAmount := params.MinDeposit
duration := (3 * time.Second)
params.VotingPeriod = &duration
err := govKeeper.SetParams(s.consumerCtx(), params)
err = govKeeper.Params.Set(s.consumerCtx(), params)
s.Assert().NoError(err)
proposer := s.consumerChain.SenderAccount
s.consumerChain.NextBlock()
votersOldBalances := getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts)

// submit proposal with forbidden and allowed changes
mintParams := mintKeeper.GetParams(s.consumerCtx())
mintParams, err := mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)
mintParams.InflationMax = newMintParamValue
msg_1 := &minttypes.MsgUpdateParams{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Expand All @@ -188,11 +206,15 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
s.consumerChain.NextBlock()
// at this moment, proposal is added, but not yet executed. we are saving old param values for comparison
oldAuthParamValue := accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
oldMintParamValue := mintKeeper.GetParams(s.consumerCtx()).InflationMax
oldMintParams, err := mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)
oldMintParamValue := oldMintParams.InflationMax
s.consumerChain.NextBlock()
// at this moment, proposal is executed or deleted if forbidden
currentAuthParamValue := accountKeeper.GetParams(s.consumerCtx()).MaxMemoCharacters
currentMintParamValue := mintKeeper.GetParams(s.consumerCtx()).InflationMax
currentMintParam, err := mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)
currentMintParamValue := currentMintParam.InflationMax
// check that parameters are not changed, since the proposal contained both forbidden and allowed changes
s.Assert().Equal(oldAuthParamValue, currentAuthParamValue)
s.Assert().NotEqual(newAuthParamValue, currentAuthParamValue)
Expand All @@ -206,9 +228,14 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
s.Assert().NoError(err)
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
s.consumerChain.NextBlock()
oldMintParamValue = mintKeeper.GetParams(s.consumerCtx()).InflationMax
oldMintParam, err := mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)
oldMintParamValue = oldMintParam.InflationMax
s.consumerChain.NextBlock()
currentMintParamValue = mintKeeper.GetParams(s.consumerCtx()).InflationMax
currentMintParam, err = mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)

currentMintParamValue = currentMintParam.InflationMax
// check that parameters are changed, since the proposal contained only allowed changes
s.Assert().Equal(newMintParamValue, currentMintParamValue)
s.Assert().NotEqual(oldMintParamValue, currentMintParamValue)
Expand All @@ -231,10 +258,10 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
s.Assert().Equal(votersOldBalances, getAccountsBalances(s.consumerCtx(), bankKeeper, bondDenom, votingAccounts))
}

func submitProposalWithDepositAndVote(govKeeper testutil.TestGovKeeper, ctx sdk.Context, msgs []sdk.Msg,
func submitProposalWithDepositAndVote(govKeeper govkeeper.Keeper, ctx sdk.Context, msgs []sdk.Msg,
accounts []ibctesting.SenderAccount, proposer sdk.AccAddress, depositAmount sdk.Coins,
) error {
proposal, err := govKeeper.SubmitProposal(ctx, msgs, "", "title", "summary", proposer)
proposal, err := govKeeper.SubmitProposal(ctx, msgs, "", "title", "summary", proposer, false)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion tests/integration/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
sdkdistrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"

icstestingutils "github.com/cosmos/interchain-security/v3/testutil/integration"
consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper"
Expand Down Expand Up @@ -111,7 +112,17 @@ func (s *CCVTestSuite) TestRewardsDistribution() {
s.Require().Equal(0, len(rewardCoins))

// check that the fee pool has the expected amount of coins
communityCoins := s.providerApp.GetTestDistributionKeeper().GetFeePoolCommunityCoins(s.providerCtx())
testDistKeeper := s.providerApp.GetTestDistributionKeeper()
// try casting to the sdk distribution keeper
sdkDistKeeper, ok := testDistKeeper.(sdkdistrkeeper.Keeper)
s.Require().True(ok)
s.Require().NotEmpty(sdkDistKeeper)

feePool, err := sdkDistKeeper.FeePool.Get(s.consumerCtx().Context())
s.Require().NoError(err)
s.Require().NotEmpty(feePool)

communityCoins := feePool.GetCommunityPool()
s.Require().True(communityCoins[ibcCoinIndex].Amount.Equal(sdk.NewDecCoinFromCoin(providerExpectedRewards[0]).Amount))
}

Expand Down
9 changes: 5 additions & 4 deletions tests/integration/expired_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,11 @@ func upgradeExpiredClient(s *CCVTestSuite, clientTo ChainType) {
tmClientState.AllowUpdateAfterExpiry = true
hostChain.App.GetIBCKeeper().ClientKeeper.SetClientState(hostChain.GetContext(), substitute, tmClientState)

content := clienttypes.NewClientUpdateProposal(ibctesting.Title, ibctesting.Description, subject, substitute)
recoverMsg := clienttypes.NewMsgRecoverClient(hostChain.App.GetIBCKeeper().GetAuthority(), subject, substitute)
err = recoverMsg.ValidateBasic()
s.Require().NoError(err)

updateProp, ok := content.(*clienttypes.ClientUpdateProposal)
s.Require().True(ok)
err = hostChain.App.GetIBCKeeper().ClientKeeper.ClientUpdateProposal(hostChain.GetContext(), updateProp)
res, err := hostChain.App.GetIBCKeeper().RecoverClient(hostChain.GetContext(), recoverMsg)
s.Require().NoError(err)
s.Require().NotNil(res)
}
3 changes: 2 additions & 1 deletion tests/integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ func (s CCVTestSuite) validateEndpointsClientConfig(consumerBundle icstestinguti
"unexpected unbonding period in consumer client state",
)

providerUnbondingPeriod := providerStakingKeeper.UnbondingTime(s.providerCtx())
providerUnbondingPeriod, err := providerStakingKeeper.UnbondingTime(s.providerCtx())
s.Require().NoError(err)
cs, ok = consumerBundle.App.GetIBCKeeper().ClientKeeper.GetClientState(
consumerBundle.GetCtx(), consumerBundle.Path.EndpointA.ClientID)
s.Require().True(ok)
Expand Down
Loading

0 comments on commit c036a10

Please sign in to comment.