Skip to content

Commit

Permalink
chore: add logic for funding multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
mbreithecker committed Oct 27, 2023
1 parent 2866707 commit de41f2a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
4 changes: 4 additions & 0 deletions x/bundles/keeper/keeper_suite_funding_bundles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var _ = Describe("funding bundles", Ordered, func() {
// init new clean chain
s = i.NewCleanChain()

params := s.App().FundersKeeper.GetParams(s.Ctx())
params.MinFundingMultiple = 0
s.App().FundersKeeper.SetParams(s.Ctx(), params)

// create clean pool for every test case
gov := s.App().GovKeeper.GetGovernanceAccount(s.Ctx()).GetAddress().String()
msg := &pooltypes.MsgCreatePool{
Expand Down
1 change: 1 addition & 0 deletions x/bundles/keeper/keeper_suite_inflation_splitting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var _ = Describe("inflation splitting", Ordered, func() {
params := s.App().FundersKeeper.GetParams(s.Ctx())
params.MinFundingAmountPerBundle = 1_000
params.MinFundingAmount = 100
params.MinFundingMultiple = 0
s.App().FundersKeeper.SetParams(s.Ctx(), params)

// create funders
Expand Down
5 changes: 3 additions & 2 deletions x/funders/keeper/logic_funders.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,16 @@ func (k Keeper) GetLowestFunding(fundings []types.Funding) (lowestFunding *types
//
// and minimum funding amount
func (k Keeper) ensureParamsCompatibility(ctx sdk.Context, funding types.Funding) error {

params := k.GetParams(ctx)
if funding.AmountPerBundle < params.MinFundingAmountPerBundle {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrAmountPerBundleTooLow.Error(), params.MinFundingAmountPerBundle)
}
if funding.Amount < params.MinFundingAmount {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingAmount.Error(), params.MinFundingAmount)
}
if funding.AmountPerBundle*params.MinFundingMultiple > funding.Amount {
return errors.Wrapf(errorsTypes.ErrInvalidRequest, types.ErrMinFundingMultiple.Error(), funding.AmountPerBundle, params.MinFundingAmount, funding.Amount)
}
return nil
}

Expand All @@ -124,7 +126,6 @@ func (k Keeper) ensureParamsCompatibility(ctx sdk.Context, funding types.Funding
// new funding can be added.
// CONTRACT: no KV Writing on newFunding and fundingState
func (k Keeper) ensureFreeSlot(ctx sdk.Context, newFunding *types.Funding, fundingState *types.FundingState) error {

activeFundings := k.GetActiveFundings(ctx, *fundingState)
// check if slots are still available
if len(activeFundings) < types.MaxFunders {
Expand Down
4 changes: 4 additions & 0 deletions x/funders/keeper/logic_funders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var _ = Describe("logic_funders.go", Ordered, func() {
}
s.RunTxPoolSuccess(msg)

params := s.App().FundersKeeper.GetParams(s.Ctx())
params.MinFundingMultiple = 5
s.App().FundersKeeper.SetParams(s.Ctx(), params)

// create funder
s.RunTxFundersSuccess(&funderstypes.MsgCreateFunder{
Creator: i.ALICE,
Expand Down
1 change: 0 additions & 1 deletion x/funders/keeper/msg_server_fund_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
// If the funders list is full, it checks if the funder wants to fund
// more than the current lowest funder. If so, the current lowest funder
// will get their tokens back and removed form the active funders list.
// TODO: what if amount_per_bundle is higher than the amount? A funder that knows that he is the next uploader could just fund a huge amount which gets payed to only himself.
func (k msgServer) FundPool(goCtx context.Context, msg *types.MsgFundPool) (*types.MsgFundPoolResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
15 changes: 15 additions & 0 deletions x/funders/keeper/msg_server_fund_pool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper_test

import (
"fmt"

i "github.com/KYVENetwork/chain/testutil/integration"
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"
Expand Down Expand Up @@ -417,4 +419,17 @@ var _ = Describe("msg_server_fund_pool.go", Ordered, func() {
AmountPerBundle: 1,
})
})

It("Try to fund without fulfilling min_funding_multiple", func() {
// ASSERT
res, err := s.RunTx(&funderstypes.MsgFundPool{
Creator: i.ALICE,
PoolId: 0,
Amount: 2 * i.KYVE,
AmountPerBundle: 1 * i.KYVE,
})
fmt.Println(res)
fmt.Println(err.Error())
Expect(err.Error()).To(Equal("per_bundle_amount (1000000000kyve) times min_funding_multiple (1000000000) is smaller than funded_amount (2000000000kyve): invalid request"))
})
})
16 changes: 8 additions & 8 deletions x/funders/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

// x/funders module sentinel errors
var (
ErrFunderAlreadyExists = errors.Register(ModuleName, 1100, "funder with address %v already exists")
ErrFunderDoesNotExist = errors.Register(ModuleName, 1101, "funder with address %v does not exist")
ErrFundsTooLow = errors.Register(ModuleName, 1102, "minimum funding amount of %vkyve not reached")
ErrAmountPerBundleTooLow = errors.Register(ModuleName, 1103, "minimum amount per bundle of %vkyve not reached")
ErrMinFundingAmount = errors.Register(ModuleName, 1104, "minimum funding amount of %vkyve not reached")
ErrFundingDoesNotExist = errors.Register(ModuleName, 1105, "funding for pool %v and funder %v does not exist")
ErrFundingIsUsedUp = errors.Register(ModuleName, 1106, "funding for pool %v and funder %v is used up")

ErrFunderAlreadyExists = errors.Register(ModuleName, 1100, "funder with address %v already exists")
ErrFunderDoesNotExist = errors.Register(ModuleName, 1101, "funder with address %v does not exist")
ErrFundsTooLow = errors.Register(ModuleName, 1102, "minimum funding amount of %vkyve not reached")
ErrAmountPerBundleTooLow = errors.Register(ModuleName, 1103, "minimum amount per bundle of %vkyve not reached")
ErrMinFundingAmount = errors.Register(ModuleName, 1104, "minimum funding amount of %vkyve not reached")
ErrFundingDoesNotExist = errors.Register(ModuleName, 1105, "funding for pool %v and funder %v does not exist")
ErrFundingIsUsedUp = errors.Register(ModuleName, 1106, "funding for pool %v and funder %v is used up")
ErrFundingStateDoesNotExist = errors.Register(ModuleName, 1107, "funding state for pool %v does not exist")
ErrMinFundingMultiple = errors.Register(ModuleName, 1108, "per_bundle_amount (%dkyve) times min_funding_multiple (%d) is smaller than funded_amount (%vkyve)")
)
4 changes: 3 additions & 1 deletion x/funders/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package types

import (
"cosmossdk.io/errors"
"encoding/json"

"cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down

0 comments on commit de41f2a

Please sign in to comment.