Skip to content

Commit

Permalink
tests: fix broken tests (unit, integration) (#1805)
Browse files Browse the repository at this point in the history
* proto: update proto files (rm deprecated from non-deprecated)

* test: update parts of integration tests

* app: add missing addr codec registrations

* tests: disable cometmock tests in GH

* integration test: revert setup to main branch

* integration test: fix add consumern; fix consumer key assign

* integration: fix most tests; add todos

* chore: add todos for fixing tests

* fix packet timeout related test

* fix TestRewardsDistribution test

* fix val address conversion

* fix democracy tests

* fix TestSoftOptOut

* fix historical info test

* fixed TestRelayAndApplyDowntimePacket

* tests: fix double voting tests

* tests: update some comments

* fix: switch broken UT due to addr parsing

---------

Co-authored-by: stana-ethernal <[email protected]>
  • Loading branch information
MSalopek and stana-miric authored Apr 23, 2024
1 parent 67f5fa9 commit 781f691
Show file tree
Hide file tree
Showing 21 changed files with 368 additions and 275 deletions.
31 changes: 0 additions & 31 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,37 +229,6 @@ jobs:
run: |
make test-e2e-compatibility-tests-latest
test-cometmock:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
lfs: true
- name: checkout LFS objects
run: git lfs checkout
- uses: actions/setup-go@v5
with:
go-version: "1.21"
check-latest: true
cache: true
cache-dependency-path: go.sum
- uses: technote-space/[email protected]
id: git_diff
with:
PATTERNS: |
**/*.go
go.mod
go.sum
**/go.mod
**/go.sum
**/Makefile
Makefile
Dockerfile*
- name: cometmock tests
if: env.GIT_DIFF
run: |
make test-e2e-short-cometmock
test-trace:
runs-on: ubuntu-latest
steps:
Expand Down
12 changes: 6 additions & 6 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func New(
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec(sdk.Bech32MainPrefix),
sdk.Bech32MainPrefix,
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
sdk.GetConfig().GetBech32AccountAddrPrefix(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand All @@ -370,8 +370,8 @@ func New(
app.AccountKeeper,
app.BankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr),
authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)
app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
Expand Down Expand Up @@ -485,8 +485,8 @@ func New(
app.BankKeeper,
*app.GovKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr),
authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
authtypes.FeeCollectorName,
)

Expand Down
8 changes: 4 additions & 4 deletions app/sovereign/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ func New(
runtime.NewKVStoreService(keys[authtypes.StoreKey]),
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec(sdk.Bech32MainPrefix),
sdk.Bech32MainPrefix,
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
sdk.GetConfig().GetBech32AccountAddrPrefix(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down Expand Up @@ -338,8 +338,8 @@ func New(
app.AccountKeeper,
app.BankKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr),
authcodec.NewBech32Codec(sdk.Bech32PrefixConsAddr),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)

app.MintKeeper = mintkeeper.NewKeeper(
Expand Down
9 changes: 3 additions & 6 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import "cosmos_proto/cosmos.proto";
// chain are expected to validate the consumer chain at spawn time or get
// slashed. It is recommended that spawn time occurs after the proposal end
// time.
// Deprecated: Use MsgConsumerAddition instead
// Use MsgConsumerAddition to submit this proposal type.
message ConsumerAdditionProposal {
option deprecated = true;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";
Expand Down Expand Up @@ -93,9 +92,8 @@ message ConsumerAdditionProposal {
// remove (and stop) a consumer chain. If it passes, all the consumer chain's
// state is removed from the provider chain. The outstanding unbonding operation
// funds are released.
// Deprecated: Use MsgConsumerRemoval instead
// Use MsgConsumerRemoval to submit this proposal type.
message ConsumerRemovalProposal {
option deprecated = true;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";

// the title of the proposal
Expand Down Expand Up @@ -128,9 +126,8 @@ message EquivocationProposal {

// ChangeRewardDenomsProposal is a governance proposal on the provider chain to
// mutate the set of denoms accepted by the provider as rewards.
// Deprecated: Use MsgChangeRewardDenoms instead
// Use MsgChangeRewardDenoms to submit this proposal type.
message ChangeRewardDenomsProposal {
option deprecated = true;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";

// the title of the proposal
Expand Down
7 changes: 3 additions & 4 deletions tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func relayAllCommittedPackets(
s.Require().Equal(
expectedPackets,
len(commitments),
fmt.Sprintf("actual number of packet commitments does not match expectation; %s", msgAndArgs...),
fmt.Sprintf("actual number of packet commitments does not match expectation; expected: %d - got: %d", expectedPackets, len(commitments)),
)

// relay all packets from srcChain to counterparty
Expand All @@ -290,7 +290,7 @@ func relayAllCommittedPackets(
err := path.RelayPacket(packet)
s.Require().NoError(
err,
fmt.Sprintf("error while relaying packets; %s", msgAndArgs...),
fmt.Sprintf("error while relaying packets; %v", err),
)
}
}
Expand Down Expand Up @@ -371,10 +371,9 @@ func checkRedelegationEntryCompletionTime(
}

func getStakingUnbondingDelegationEntry(ctx sdk.Context, k testutil.TestStakingKeeper, id uint64) (stakingUnbondingOp stakingtypes.UnbondingDelegationEntry, found bool) {
found := false
stakingUbd, err := k.GetUnbondingDelegationByUnbondingID(ctx, id)
if err != nil {
return found
return
}

for _, entry := range stakingUbd.Entries {
Expand Down
81 changes: 47 additions & 34 deletions tests/integration/democracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ConsumerDemocracyTestSuite struct {
setupCallback DemocSetupCallback
}

// NewCCVTestSuite returns a new instance of ConsumerDemocracyTestSuite,
// NewConsumerDemocracyTestSuite returns a new instance of ConsumerDemocracyTestSuite,
// ready to be tested against using suite.Run().
func NewConsumerDemocracyTestSuite[T testutil.DemocConsumerApp](
democConsumerAppIniter icstestingutils.ValSetAppIniter,
Expand Down Expand Up @@ -80,41 +80,53 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
bondDenom, err := stakingKeeper.BondDenom(s.consumerCtx())
s.Require().NoError(err)

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

representatives, err := stakingKeeper.GetAllValidators(s.consumerCtx())
s.Require().NoError(err)
for _, representative := range representatives {
currentRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
previousRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
nextRepresentativesRewards[representative.OperatorAddress] = math.LegacyNewDec(0)
representativesTokens[representative.OperatorAddress] = representative.GetTokens()
}

distrModuleAccount := distrKeeper.GetDistributionAccount(s.consumerCtx())
providerRedistributeAccount := accountKeeper.GetModuleAccount(s.consumerCtx(), consumertypes.ConsumerToSendToProviderName)
// balance of consumer redistribute address will always be 0 when checked between 2 NextBlock() calls
consumerRedistributeAccount := accountKeeper.GetModuleAccount(s.consumerCtx(), consumertypes.ConsumerRedistributeName)

dk, ok := distrKeeper.(sdkdistrkeeper.Keeper)
s.Require().True(ok)
feePool, err := dk.FeePool.Get(s.consumerCtx().Context())
feePool, err := dk.FeePool.Get(s.consumerCtx())
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 := feePool.GetCommunityPool().AmountOf(bondDenom)
for key := range currentRepresentativesRewards {

// take balance of relevant accounts before advancing to the next block
previousDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
previousProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
previousConsumerRedistributeBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), consumerRedistributeAccount.GetAddress(), bondDenom).Amount)
previousCommunityPoolBalance := feePool.GetCommunityPool().AmountOf(bondDenom)
for key := range previousRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
representativeReward, err := distrKeeper.GetValidatorOutstandingRewards(s.consumerCtx(), representativeAddr)
s.Require().NoError(err)
currentRepresentativesRewards[key] = representativeReward.Rewards.AmountOf(bondDenom)
previousRepresentativesRewards[key] = representativeReward.Rewards.AmountOf(bondDenom)
}

// NextBlock will call the begin block and end block, respectively. Democracy module in the begin blocker sends the tokens from
// the consumer redistribute address to the distribution module, giving representatives and community fee pool its portion of rewards.
// Consumer module in endblocker, sends the fees from fee collector(auth module) to the consumer redistribute address and to
// provider rewards address
s.consumerChain.NextBlock()

// take balance of relevant accounts after new block is created
nextDistrModuleAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), distrModuleAccount.GetAddress(), bondDenom).Amount)
nextProviderFeeAccountBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), providerRedistributeAccount.GetAddress(), bondDenom).Amount)
nextConsumerRedistributeBalance := math.LegacyNewDecFromInt(bankKeeper.GetBalance(s.consumerCtx(), consumerRedistributeAccount.GetAddress(), bondDenom).Amount)
feePool, err = dk.FeePool.Get(s.consumerCtx())
s.Require().NoError(err)
s.Require().NotEmpty(feePool)
nextCommunityPoolBalance := feePool.GetCommunityPool().AmountOf(bondDenom)
for key := range nextRepresentativesRewards {
representativeAddr, _ := sdk.ValAddressFromBech32(key)
Expand All @@ -123,41 +135,44 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() {
nextRepresentativesRewards[key] = representativeReward.Rewards.AmountOf(bondDenom)
}

distrModuleDifference := nextDistrModuleAccountBalance.Sub(currentDistrModuleAccountBalance)
providerDifference := nextProviderFeeAccountBalance.Sub(currentProviderFeeAccountBalance)
communityPoolDifference := nextCommunityPoolBalance.Sub(currentCommunityPoolBalance)
distrModuleDifference := nextDistrModuleAccountBalance.Sub(previousDistrModuleAccountBalance)
providerDifference := nextProviderFeeAccountBalance.Sub(previousProviderFeeAccountBalance)
communityPoolDifference := nextCommunityPoolBalance.Sub(previousCommunityPoolBalance)
representativeDifference := map[string]math.LegacyDec{}
consumerRedistributeDifference := communityPoolDifference

for key, currentReward := range currentRepresentativesRewards {
representativeDifference[key] = nextRepresentativesRewards[key].Sub(currentReward)
consumerRedistributeDifference = consumerRedistributeDifference.Add(representativeDifference[key])
totalRepresentativeDifference := math.LegacyZeroDec()
for key, currentReward := range previousRepresentativesRewards {
diff := nextRepresentativesRewards[key].Sub(currentReward)
representativeDifference[key] = diff
totalRepresentativeDifference = totalRepresentativeDifference.Add(diff)
}

// confirm endblocker changes: consumer module distributes fees from fee collector to consumer redistribute address
// and address aimed for provider rewards
consumerRedistributionFraction := math.LegacyMustNewDecFromStr(s.consumerApp.GetConsumerKeeper().GetConsumerRedistributionFrac(s.consumerCtx()))

// 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)
// since we cannot know the balance of the fee collector when the consumer module transfers the fees, we can only calculate the total fees
// as a sum of the provider and consumer rewards and check if the reward is distributed in the expected proportion
totalFees := nextConsumerRedistributeBalance.Add(providerDifference)
s.Require().Equal(totalFees.Mul(consumerRedistributionFraction), nextConsumerRedistributeBalance)

// confirm begin blocker changes: democracy module distributes the fees from c onsumer redistribute address to representatives
// and community fee pool
// distribution module got tokens from previous consumer redistribute balance
s.Require().Equal(distrModuleDifference, previousConsumerRedistributeBalance)
// confirm that the percentage given to the community pool is equal to the configured community tax percentage.
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(),
consumerRedistributionFraction.MustFloat64(), float64(0.0001))
// check that the fraction actually kept by the provider is the correct fraction. using InEpsilon because the math code uses truncations
s.Require().InEpsilon(providerDifference.Quo(
providerDifference.Add(distrModuleDifference)).MustFloat64(),
math.LegacyNewDec(1).Sub(consumerRedistributionFraction).MustFloat64(), float64(0.0001))

s.Require().Equal(communityPoolDifference.Quo(previousConsumerRedistributeBalance), tax)
// confirm that the total amount given to the community pool plus all representatives is equal to the previous consumer redistribute balance
s.Require().Equal(previousConsumerRedistributeBalance, communityPoolDifference.Add(totalRepresentativeDifference))

// check that each representative has gotten the correct amount of rewards
totalRepresentativePower, err := stakingKeeper.GetValidatorSet().TotalBondedTokens(s.consumerCtx())
s.Require().NoError(err)

// check that each representative has gotten the correct amount of rewards
for key, representativeTokens := range representativesTokens {
powerFraction := math.LegacyNewDecFromInt(representativeTokens).QuoTruncate(math.LegacyNewDecFromInt(totalRepresentativePower))
s.Require().Equal(powerFraction, representativeDifference[key].Quo(consumerRedistributeDifference.Sub(communityPoolDifference)))
s.Require().Equal(powerFraction, representativeDifference[key].Quo(previousConsumerRedistributeBalance.Sub(communityPoolDifference)))
}
}

Expand Down Expand Up @@ -203,7 +218,6 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
// set current header time to be equal or later than voting end time in order to process proposal from active queue,
// once the proposal is added to the chain
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
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
oldMintParams, err := mintKeeper.Params.Get(s.consumerCtx())
Expand All @@ -227,7 +241,6 @@ func (s *ConsumerDemocracyTestSuite) TestDemocracyGovernanceWhitelisting() {
err = submitProposalWithDepositAndVote(govKeeper, s.consumerCtx(), []sdk.Msg{msg_1}, votingAccounts, proposer.GetAddress(), depositAmount)
s.Assert().NoError(err)
s.consumerChain.CurrentHeader.Time = s.consumerChain.CurrentHeader.Time.Add(*params.VotingPeriod)
s.consumerChain.NextBlock()
oldMintParam, err := mintKeeper.Params.Get(s.consumerCtx())
s.Require().NoError(err)
oldMintParamValue = oldMintParam.InflationMax
Expand Down
Loading

0 comments on commit 781f691

Please sign in to comment.