Skip to content

Commit

Permalink
test: add more tests for multiple denoms
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreithecker committed Apr 29, 2024
1 parent 871e820 commit 80d52aa
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 19 deletions.
14 changes: 14 additions & 0 deletions testutil/integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func (suite *KeeperTestSuite) GetBalanceFromAddress(address string) uint64 {
return uint64(balance.Amount.Int64())
}

func (suite *KeeperTestSuite) GetCoinsFromAddress(address string) sdk.Coins {
accAddress, err := sdk.AccAddressFromBech32(address)
if err != nil {
return sdk.NewCoins()
}

return suite.App().BankKeeper.GetAllBalances(suite.Ctx(), accAddress)
}

func (suite *KeeperTestSuite) GetBalanceFromPool(poolId uint64) uint64 {
pool, found := suite.App().PoolKeeper.GetPool(suite.Ctx(), poolId)
if !found {
Expand All @@ -30,6 +39,11 @@ func (suite *KeeperTestSuite) GetBalanceFromModule(moduleName string) uint64 {
return suite.App().BankKeeper.GetBalance(suite.Ctx(), moduleAcc, globalTypes.Denom).Amount.Uint64()
}

func (suite *KeeperTestSuite) GetCoinsFromModule(moduleName string) sdk.Coins {
moduleAcc := suite.App().AccountKeeper.GetModuleAccount(suite.Ctx(), moduleName).GetAddress()
return suite.App().BankKeeper.GetAllBalances(suite.Ctx(), moduleAcc)
}

func (suite *KeeperTestSuite) GetNextUploader() (nextStaker string, nextValaddress string) {
bundleProposal, _ := suite.App().BundlesKeeper.GetBundleProposal(suite.Ctx(), 0)

Expand Down
25 changes: 23 additions & 2 deletions testutil/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (suite *KeeperTestSuite) initDummyAccounts() {
}
}

func (suite *KeeperTestSuite) Mint(address string, amount uint64) error {
coins := sdk.NewCoins(sdk.NewInt64Coin(KYVE_DENOM, int64(amount)))
func (suite *KeeperTestSuite) MintDenom(address string, amount uint64, denom string) error {
coins := sdk.NewCoins(sdk.NewInt64Coin(denom, int64(amount)))
err := suite.app.BankKeeper.MintCoins(suite.ctx, mintTypes.ModuleName, coins)
if err != nil {
return err
Expand All @@ -152,6 +152,27 @@ func (suite *KeeperTestSuite) Mint(address string, amount uint64) error {
return nil
}

func (suite *KeeperTestSuite) MintDenomToModule(moduleAddress string, amount uint64, denom string) error {
coins := sdk.NewCoins(sdk.NewInt64Coin(denom, int64(amount)))
err := suite.app.BankKeeper.MintCoins(suite.ctx, mintTypes.ModuleName, coins)
if err != nil {
return err
}

suite.Commit()

err = suite.app.BankKeeper.SendCoinsFromModuleToModule(suite.ctx, mintTypes.ModuleName, moduleAddress, coins)
if err != nil {
return err
}

return nil
}

func (suite *KeeperTestSuite) Mint(address string, amount uint64) error {
return suite.MintDenom(address, amount, KYVE_DENOM)
}

type KeeperTestSuite struct {
suite.Suite

Expand Down
11 changes: 5 additions & 6 deletions x/delegation/keeper/keeper_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ func TestDelegationKeeper(t *testing.T) {
RunSpecs(t, fmt.Sprintf("x/%s Keeper Test Suite", types.ModuleName))
}

// TODO remove `kyveAmount` when the funding module supports multiple denoms
func PayoutRewards(s *i.KeeperTestSuite, staker string, kyveAmount uint64, otherCoins sdk.Coins) {
func PayoutRewards(s *i.KeeperTestSuite, staker string, coins sdk.Coins) {
fundingState, found := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(found).To(BeTrue())

// TODO support all denominations once the funding module supports it
// divide amount by number of active fundings so that total payout is equal to amount
activeFundings := s.App().FundersKeeper.GetActiveFundings(s.Ctx(), fundingState)
for _, funding := range activeFundings {
funding.AmountPerBundle = kyveAmount / uint64(len(activeFundings))
funding.AmountPerBundle = coins.AmountOf(globalTypes.Denom).Uint64() / uint64(len(activeFundings))
s.App().FundersKeeper.SetFunding(s.Ctx(), &funding)
}

payout, err := s.App().FundersKeeper.ChargeFundersOfPool(s.Ctx(), 0)
Expect(err).To(BeNil())
otherCoins = otherCoins.Add(sdk.NewInt64Coin(globalTypes.Denom, int64(kyveAmount)))
err = s.App().DelegationKeeper.PayoutRewards(s.Ctx(), staker, otherCoins, pooltypes.ModuleName)
err = s.App().DelegationKeeper.PayoutRewards(s.Ctx(), staker, coins, pooltypes.ModuleName)
Expect(err).NotTo(HaveOccurred())
Expect(kyveAmount).To(Equal(payout))
Expect(coins.AmountOf(globalTypes.Denom).Uint64()).To(Equal(payout))
}

func CreateFundedPool(s *i.KeeperTestSuite) {
Expand Down
5 changes: 3 additions & 2 deletions x/delegation/keeper/msg_server_delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
globalTypes "github.com/KYVENetwork/chain/x/global/types"
sdk "github.com/cosmos/cosmos-sdk/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -166,7 +167,7 @@ var _ = Describe("msg_server_delegate.go", Ordered, func() {
s.PerformValidityChecks()

// ACT
PayoutRewards(s, i.ALICE, 10*i.KYVE, nil)
PayoutRewards(s, i.ALICE, sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(10*i.KYVE))))

// ASSERT

Expand Down Expand Up @@ -214,7 +215,7 @@ var _ = Describe("msg_server_delegate.go", Ordered, func() {
Expect(s.App().DelegationKeeper.GetOutstandingRewards(s.Ctx(), i.ALICE, i.DUMMY[1])).To(BeEmpty())

// ACT
PayoutRewards(s, i.ALICE, 10*i.KYVE, nil)
PayoutRewards(s, i.ALICE, sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(10*i.KYVE))))

// ASSERT

Expand Down
7 changes: 4 additions & 3 deletions x/delegation/keeper/msg_server_undelegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"cosmossdk.io/math"
globalTypes "github.com/KYVENetwork/chain/x/global/types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"
sdk "github.com/cosmos/cosmos-sdk/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand Down Expand Up @@ -410,7 +411,7 @@ var _ = Describe("msg_server_undelegate.go", Ordered, func() {
// Alice: 100 100/130 * 10 * 1e9 = 7_692_307_692
// Dummy0: 10 10/130 * 10 * 1e9 = 769_230_769
// Dummy1: 20 20/130 * 10 * 1e9 = 1_538_461_538
PayoutRewards(s, i.ALICE, 10*i.KYVE, nil)
PayoutRewards(s, i.ALICE, sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(10*i.KYVE))))

// Collect
s.RunTxDelegatorSuccess(&types.MsgWithdrawRewards{
Expand Down Expand Up @@ -494,7 +495,7 @@ var _ = Describe("msg_server_undelegate.go", Ordered, func() {
// Alice: 50 50 / 75 * 10 * 1e9 = 6_666_666_666
// Dummy0: 5 5 / 75 * 10 * 1e9 = 666_666_666
// Dummy1: 20 20 / 75 * 10 * 1e9 = 2_666_666_666
PayoutRewards(s, i.ALICE, 10*i.KYVE, nil)
PayoutRewards(s, i.ALICE, sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(10*i.KYVE))))

// ASSERT
Expect(s.App().DelegationKeeper.GetOutstandingRewards(s.Ctx(), i.ALICE, i.DUMMY[0]).AmountOf(globalTypes.Denom).Uint64()).To(Equal(uint64(666_666_666)))
Expand Down Expand Up @@ -577,7 +578,7 @@ var _ = Describe("msg_server_undelegate.go", Ordered, func() {
// Alice: 25 25 / 32.5 * 1e10 = 7_692_307_692
// Dummy0: 2.5 2.5 / 32.5 * 1e10 = 769_230_769
// Dummy1: 5 5 / 32.5 * 1e10 = 1_538_461_538
PayoutRewards(s, i.ALICE, 10*i.KYVE, nil)
PayoutRewards(s, i.ALICE, sdk.NewCoins(sdk.NewInt64Coin(globalTypes.Denom, int64(10*i.KYVE))))

s.CommitAfterSeconds(s.App().DelegationKeeper.GetUnbondingDelegationTime(s.Ctx()) + 1)
s.CommitAfterSeconds(1)
Expand Down
Loading

0 comments on commit 80d52aa

Please sign in to comment.