Skip to content

Commit

Permalink
chore: added pool status descriptions & refactored highest delegation…
Browse files Browse the repository at this point in the history
… logic
  • Loading branch information
troykessler committed Nov 6, 2023
1 parent 9a9533f commit dc66a16
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
24 changes: 16 additions & 8 deletions proto/kyve/pool/v1beta1/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ option go_package = "github.com/KYVENetwork/chain/x/pool/types";
enum PoolStatus {
option (gogoproto.goproto_enum_prefix) = false;

// POOL_STATUS_UNSPECIFIED ...
// POOL_STATUS_UNSPECIFIED indicates and unknown status, likely
// due to an error
POOL_STATUS_UNSPECIFIED = 0;
// POOL_STATUS_ACTIVE ...
// POOL_STATUS_ACTIVE indicates, that the pool is running
// normally
POOL_STATUS_ACTIVE = 1;
// POOL_STATUS_DISABLED ...
// POOL_STATUS_DISABLED indicates, that the pool was disabled
// by the governance and does not continue until it is enabled
// by the governance again
POOL_STATUS_DISABLED = 2;
// POOL_STATUS_NO_FUNDS ...
// POOL_STATUS_NO_FUNDS indicates, that the pool currently has no
// funds, but is continuing normally anyway, due to inflation splitting
POOL_STATUS_NO_FUNDS = 3;
// POOL_STATUS_NOT_ENOUGH_DELEGATION ...
// POOL_STATUS_NOT_ENOUGH_DELEGATION indicates, that the min delegation
// requirement has not been met and that the pool is halted
POOL_STATUS_NOT_ENOUGH_DELEGATION = 4;
// POOL_STATUS_UPGRADING ...
// POOL_STATUS_UPGRADING indicates, that the runtime is currently
// being upgraded and that the pool is halted
POOL_STATUS_UPGRADING = 5;
// POOL_STATUS_VP_TOO_HIGH ...
POOL_STATUS_VP_TOO_HIGH = 6;
// POOL_STATUS_VOTING_POWER_TOO_HIGH indicates, that one validator
// has more than 50% voting power and that the pool is halted
POOL_STATUS_VOTING_POWER_TOO_HIGH = 6;
}

// Protocol holds all info about the current pool version and the
Expand Down
16 changes: 3 additions & 13 deletions x/bundles/keeper/logic_bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,16 @@ func (k Keeper) AssertPoolCanRun(ctx sdk.Context, poolId uint64) error {
return types.ErrPoolDisabled
}

totalDelegation := uint64(0)
maxDelegation := uint64(0)

// Get the total delegation and the highest delegation of a staker in the pool
for _, address := range k.stakerKeeper.GetAllStakerAddressesOfPool(ctx, poolId) {
delegation := k.delegationKeeper.GetDelegationAmount(ctx, address)
totalDelegation += delegation

if delegation > maxDelegation {
maxDelegation = delegation
}
}
// Get the total and the highest delegation of a single validator in the pool
totalDelegation, highestDelegation := k.delegationKeeper.GetTotalAndHighestDelegationOfPool(ctx, poolId)

// Error if min delegation is not reached
if totalDelegation < pool.MinDelegation {
return types.ErrMinDelegationNotReached
}

// Error if the top staker has more than 50%
if maxDelegation*2 > totalDelegation {
if highestDelegation*2 > totalDelegation {
return types.ErrVPTooHigh
}

Expand Down
1 change: 1 addition & 0 deletions x/bundles/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type StakerKeeper interface {
type DelegationKeeper interface {
GetDelegationAmount(ctx sdk.Context, staker string) uint64
GetDelegationOfPool(ctx sdk.Context, poolId uint64) uint64
GetTotalAndHighestDelegationOfPool(ctx sdk.Context, poolId uint64) (uint64, uint64)
PayoutRewards(ctx sdk.Context, staker string, amount uint64, payerModuleName string) error
SlashDelegators(ctx sdk.Context, poolId uint64, staker string, slashType delegationTypes.SlashType)
}
16 changes: 16 additions & 0 deletions x/delegation/keeper/exported_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func (k Keeper) GetDelegationOfPool(ctx sdk.Context, poolId uint64) uint64 {
return totalDelegation
}

// GetTotalAndHighestDelegationOfPool returns the total delegation amount and
// the highest delegation of a single validator in a pool
func (k Keeper) GetTotalAndHighestDelegationOfPool(ctx sdk.Context, poolId uint64) (totalDelegation, highestDelegation uint64) {
// Get the total delegation and the highest delegation of a staker in the pool
for _, address := range k.stakersKeeper.GetAllStakerAddressesOfPool(ctx, poolId) {
delegation := k.GetDelegationAmount(ctx, address)
totalDelegation += delegation

if delegation > highestDelegation {
highestDelegation = delegation
}
}

return
}

// PayoutRewards transfers `amount` $nKYVE from the `payerModuleName`-module to the delegation module.
// It then awards these tokens internally to all delegators of staker `staker`.
// Delegators can then receive these rewards if they call the `withdraw`-transaction.
Expand Down
16 changes: 3 additions & 13 deletions x/query/keeper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,8 @@ func (k Keeper) GetFullStaker(ctx sdk.Context, stakerAddress string) *types.Full
}

func (k Keeper) GetPoolStatus(ctx sdk.Context, pool *pooltypes.Pool) pooltypes.PoolStatus {
totalDelegation := uint64(0)
maxDelegation := uint64(0)

// Get the total delegation and the highest delegation of a staker in the pool
for _, address := range k.stakerKeeper.GetAllStakerAddressesOfPool(ctx, pool.Id) {
delegation := k.delegationKeeper.GetDelegationAmount(ctx, address)
totalDelegation += delegation

if delegation > maxDelegation {
maxDelegation = delegation
}
}
// Get the total and the highest delegation of a single validator in the pool
totalDelegation, highestDelegation := k.delegationKeeper.GetTotalAndHighestDelegationOfPool(ctx, pool.Id)

var poolStatus pooltypes.PoolStatus

Expand All @@ -103,7 +93,7 @@ func (k Keeper) GetPoolStatus(ctx sdk.Context, pool *pooltypes.Pool) pooltypes.P
poolStatus = pooltypes.POOL_STATUS_DISABLED
} else if totalDelegation < pool.MinDelegation {
poolStatus = pooltypes.POOL_STATUS_NOT_ENOUGH_DELEGATION
} else if maxDelegation*2 > totalDelegation {
} else if highestDelegation*2 > totalDelegation {
poolStatus = pooltypes.POOL_STATUS_VP_TOO_HIGH
} else if pool.TotalFunds == 0 {
poolStatus = pooltypes.POOL_STATUS_NO_FUNDS
Expand Down

0 comments on commit dc66a16

Please sign in to comment.