Skip to content

Commit

Permalink
fix: fix pool tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Sep 28, 2023
1 parent 11d648b commit 7a0cc83
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 56 deletions.
11 changes: 7 additions & 4 deletions x/pool/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
storeTypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

// Auth
authKeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
// Bank
bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
Expand All @@ -32,12 +30,13 @@ type (
authority string

stakersKeeper types.StakersKeeper
accountKeeper authKeeper.AccountKeeper
accountKeeper types.AccountKeeper
bankKeeper bankKeeper.Keeper
distrkeeper distributionKeeper.Keeper
mintKeeper mintKeeper.Keeper
upgradeKeeper types.UpgradeKeeper
teamKeeper teamKeeper.Keeper
fundersKeeper types.FundersKeeper
}
)

Expand All @@ -48,7 +47,7 @@ func NewKeeper(

authority string,

accountKeeper authKeeper.AccountKeeper,
accountKeeper types.AccountKeeper,
bankKeeper bankKeeper.Keeper,
distrKeeper distributionKeeper.Keeper,
mintKeeper mintKeeper.Keeper,
Expand Down Expand Up @@ -94,6 +93,10 @@ func SetStakersKeeper(k *Keeper, stakersKeeper types.StakersKeeper) {
k.stakersKeeper = stakersKeeper
}

func SetFundersKeeper(k *Keeper, fundersKeeper types.FundersKeeper) {
k.fundersKeeper = fundersKeeper
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
Expand Down
1 change: 1 addition & 0 deletions x/pool/keeper/msg_server_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (k msgServer) CreatePool(goCtx context.Context, req *types.MsgCreatePool) (
})

k.EnsurePoolAccount(ctx, id)
k.fundersKeeper.CreateFundingState(ctx, id)

_ = ctx.EventManager().EmitTypedEvent(&types.EventCreatePool{
Id: k.GetPoolCount(ctx) - 1,
Expand Down
22 changes: 18 additions & 4 deletions x/pool/keeper/msg_server_create_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
i "github.com/KYVENetwork/chain/testutil/integration"
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govV1Types "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -144,8 +145,6 @@ var _ = Describe("msg_server_create_pool.go", Ordered, func() {
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Disabled: false,
Funders: nil,
TotalFunds: 0,
Protocol: &types.Protocol{
Version: "0.0.0",
Binaries: "{}",
Expand All @@ -160,6 +159,13 @@ var _ = Describe("msg_server_create_pool.go", Ordered, func() {
CurrentStorageProviderId: 2,
CurrentCompressionId: 1,
}))

fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState).To(Equal(funderstypes.FundingState{
PoolId: 0,
ActiveFunderAddresses: nil,
TotalAmount: 0,
}))
})

It("Create another pool", func() {
Expand Down Expand Up @@ -247,8 +253,6 @@ var _ = Describe("msg_server_create_pool.go", Ordered, func() {
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Disabled: false,
Funders: nil,
TotalFunds: 0,
Protocol: &types.Protocol{
Version: "0.0.0",
Binaries: "{}",
Expand All @@ -263,6 +267,13 @@ var _ = Describe("msg_server_create_pool.go", Ordered, func() {
CurrentStorageProviderId: 2,
CurrentCompressionId: 1,
}))

fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 1)
Expect(fundingState).To(Equal(funderstypes.FundingState{
PoolId: 1,
ActiveFunderAddresses: nil,
TotalAmount: 0,
}))
})

It("Create pool with invalid binaries", func() {
Expand Down Expand Up @@ -303,5 +314,8 @@ var _ = Describe("msg_server_create_pool.go", Ordered, func() {

_, found := s.App().PoolKeeper.GetPool(s.Ctx(), 0)
Expect(found).To(BeFalse())

_, found = s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(found).To(BeFalse())
})
})
4 changes: 4 additions & 0 deletions x/pool/keeper/msg_server_disable_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (k msgServer) DisablePool(
k.stakersKeeper.LeavePool(ctx, staker, pool.Id)
}

if err := k.fundersKeeper.DefundFundingState(ctx, pool.Id); err != nil {
return nil, err
}

// send remaining pool assets to treasury
if balance := k.bankKeeper.GetBalance(ctx, pool.GetPoolAccount(), globalTypes.Denom).Amount.Uint64(); balance > 0 {
if err := util.TransferFromAddressToTreasury(k.distrkeeper, ctx, pool.GetPoolAccount().String(), balance); err != nil {
Expand Down
129 changes: 88 additions & 41 deletions x/pool/keeper/msg_server_disable_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
i "github.com/KYVENetwork/chain/testutil/integration"
bundletypes "github.com/KYVENetwork/chain/x/bundles/types"
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
globalTypes "github.com/KYVENetwork/chain/x/global/types"
stakertypes "github.com/KYVENetwork/chain/x/stakers/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -38,49 +39,64 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {

gov := s.App().GovKeeper.GetGovernanceAccount(s.Ctx()).GetAddress().String()
votingPeriod := s.App().GovKeeper.GetParams(s.Ctx()).VotingPeriod
fundingAmount := 100 * i.KYVE

BeforeEach(func() {
s = i.NewCleanChain()

// create clean pool for every test case
s.App().PoolKeeper.AppendPool(s.Ctx(), types.Pool{
Name: "PoolTest",
MaxBundleSize: 100,
StartKey: "0",
UploadInterval: 60,
OperatingCost: 10_000,
Protocol: &types.Protocol{
Version: "0.0.0",
Binaries: "{}",
LastUpgrade: uint64(s.Ctx().BlockTime().Unix()),
},
UpgradePlan: &types.UpgradePlan{},
})
msg := &types.MsgCreatePool{
Authority: gov,
Name: "PoolTest",
Runtime: "@kyve/test",
Logo: "ar://Tewyv2P5VEG8EJ6AUQORdqNTectY9hlOrWPK8wwo-aU",
Config: "ar://DgdB-2hLrxjhyEEbCML__dgZN5_uS7T6Z5XDkaFh3P0",
StartKey: "0",
UploadInterval: 60,
OperatingCost: 10_000,
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Version: "0.0.0",
Binaries: "{}",
StorageProviderId: 2,
CompressionId: 1,
}
s.RunTxPoolSuccess(msg)

s.RunTxPoolSuccess(&types.MsgFundPool{
s.RunTxPoolSuccess(&funderstypes.MsgCreateFunder{
Creator: i.ALICE,
Id: 0,
Amount: 100 * i.KYVE,
Moniker: "Alice",
})

s.App().PoolKeeper.AppendPool(s.Ctx(), types.Pool{
Name: "PoolTest2",
MaxBundleSize: 100,
StartKey: "0",
UploadInterval: 60,
OperatingCost: 10_000,
Protocol: &types.Protocol{
Version: "0.0.0",
Binaries: "{}",
LastUpgrade: uint64(s.Ctx().BlockTime().Unix()),
},
UpgradePlan: &types.UpgradePlan{},
s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.ALICE,
PoolId: 0,
Amount: fundingAmount,
AmountPerBundle: 1 * i.KYVE,
})

s.RunTxPoolSuccess(&types.MsgFundPool{
Creator: i.ALICE,
Id: 1,
Amount: 100 * i.KYVE,
msg = &types.MsgCreatePool{
Authority: gov,
Name: "PoolTest2",
Runtime: "@kyve/test",
Logo: "ar://Tewyv2P5VEG8EJ6AUQORdqNTectY9hlOrWPK8wwo-aU",
Config: "ar://DgdB-2hLrxjhyEEbCML__dgZN5_uS7T6Z5XDkaFh3P0",
StartKey: "0",
UploadInterval: 60,
OperatingCost: 10_000,
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Version: "0.0.0",
Binaries: "{}",
StorageProviderId: 2,
CompressionId: 1,
}
s.RunTxPoolSuccess(msg)

s.RunTxPoolSuccess(&funderstypes.MsgFundPool{
Creator: i.ALICE,
PoolId: 1,
Amount: fundingAmount,
AmountPerBundle: 1 * i.KYVE,
})
})

Expand Down Expand Up @@ -164,6 +180,10 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {

p, v := BuildGovernanceTxs(s, []sdk.Msg{msg})

fundersModuleAddr := s.App().AccountKeeper.GetModuleAddress(funderstypes.ModuleName)
balanceBefore := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAddr, globalTypes.Denom).Amount.Uint64()
Expect(balanceBefore).To(BeNumerically(">", uint64(0)))

// ACT
_, submitErr := s.RunTx(&p)
_, voteErr := s.RunTx(&v)
Expand All @@ -184,9 +204,14 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {
bundleProposal, _ := s.App().BundlesKeeper.GetBundleProposal(s.Ctx(), 0)
Expect(bundleProposal.StorageId).To(BeEmpty())

// assert empty pool balance
b := s.App().BankKeeper.GetBalance(s.Ctx(), pool.GetPoolAccount(), globalTypes.Denom).Amount.Uint64()
Expect(b).To(BeZero())
// assert smaller funding balance
balance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAddr, globalTypes.Denom).Amount.Uint64()
Expect(balance).To(Equal(balanceBefore - fundingAmount))

// assert empty funding state
fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.TotalAmount).To(BeZero())
Expect(fundingState.ActiveFunderAddresses).To(BeEmpty())
})

It("Disable pool which is active and has a balance", func() {
Expand All @@ -202,8 +227,13 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {
s.Commit()
}

b := s.App().BankKeeper.GetBalance(s.Ctx(), pool.GetPoolAccount(), globalTypes.Denom).Amount.Uint64()
Expect(b).To(BeNumerically(">", uint64(0)))
fundersModuleAddr := s.App().AccountKeeper.GetModuleAddress(funderstypes.ModuleName)
balanceBefore := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAddr, globalTypes.Denom).Amount.Uint64()
Expect(balanceBefore).To(BeNumerically(">", uint64(0)))

fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.TotalAmount).To(BeNumerically(">", uint64(0)))
Expect(fundingState.ActiveFunderAddresses).To(HaveLen(1))

msg := &types.MsgDisablePool{
Authority: gov,
Expand Down Expand Up @@ -232,9 +262,14 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {
bundleProposal, _ := s.App().BundlesKeeper.GetBundleProposal(s.Ctx(), 0)
Expect(bundleProposal.StorageId).To(BeEmpty())

// assert empty pool balance
b = s.App().BankKeeper.GetBalance(s.Ctx(), pool.GetPoolAccount(), globalTypes.Denom).Amount.Uint64()
Expect(b).To(BeZero())
// assert smaller funding balance
balance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAddr, globalTypes.Denom).Amount.Uint64()
Expect(balance).To(Equal(balanceBefore - fundingState.TotalAmount))

// assert empty funding state
fundingState, _ = s.App().FundersKeeper.GetFundingState(s.Ctx(), 0)
Expect(fundingState.TotalAmount).To(BeZero())
Expect(fundingState.ActiveFunderAddresses).To(BeEmpty())
})

It("Disable pool which is active", func() {
Expand Down Expand Up @@ -338,6 +373,18 @@ var _ = Describe("msg_server_disable_pool.go", Ordered, func() {

bundleProposal, _ = s.App().BundlesKeeper.GetBundleProposal(s.Ctx(), 1)
Expect(bundleProposal.StorageId).To(BeEmpty())

// assert empty funding balance
fundersModuleAddr := s.App().AccountKeeper.GetModuleAddress(funderstypes.ModuleName)
balance := s.App().BankKeeper.GetBalance(s.Ctx(), fundersModuleAddr, globalTypes.Denom).Amount.Uint64()
Expect(balance).To(BeZero())

// assert empty funding states
for _, poolId := range []uint64{0, 1} {
fundingState, _ := s.App().FundersKeeper.GetFundingState(s.Ctx(), poolId)
Expect(fundingState.TotalAmount).To(BeZero())
Expect(fundingState.ActiveFunderAddresses).To(BeEmpty())
}
})

It("Kick out all stakers from pool", func() {
Expand Down
6 changes: 0 additions & 6 deletions x/pool/keeper/msg_server_update_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ var _ = Describe("msg_server_update_pool.go", Ordered, func() {
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Disabled: false,
Funders: nil,
TotalFunds: 0,
Protocol: &types.Protocol{
Version: "",
Binaries: "",
Expand Down Expand Up @@ -187,8 +185,6 @@ var _ = Describe("msg_server_update_pool.go", Ordered, func() {
MinDelegation: 0,
MaxBundleSize: 0,
Disabled: false,
Funders: nil,
TotalFunds: 0,
Protocol: &types.Protocol{
Version: "",
Binaries: "",
Expand Down Expand Up @@ -262,8 +258,6 @@ var _ = Describe("msg_server_update_pool.go", Ordered, func() {
MinDelegation: 100 * i.KYVE,
MaxBundleSize: 100,
Disabled: false,
Funders: nil,
TotalFunds: 0,
Protocol: &types.Protocol{
Version: "",
Binaries: "",
Expand Down
11 changes: 10 additions & 1 deletion x/pool/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

type AccountKeeper interface{}
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
SetAccount(ctx sdk.Context, acc types.AccountI)
}

type UpgradeKeeper interface {
ScheduleUpgrade(ctx sdk.Context, plan upgradetypes.Plan) error
Expand All @@ -15,3 +19,8 @@ type StakersKeeper interface {
LeavePool(ctx sdk.Context, staker string, poolId uint64)
GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (stakers []string)
}

type FundersKeeper interface {
CreateFundingState(ctx sdk.Context, poolId uint64)
DefundFundingState(ctx sdk.Context, poolId uint64) (err error)
}

0 comments on commit 7a0cc83

Please sign in to comment.