Skip to content

Commit

Permalink
refactor: move power shaping params to power_shaping.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoke committed Sep 3, 2024
1 parent c226518 commit 0777f87
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 581 deletions.
215 changes: 0 additions & 215 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,221 +867,6 @@ func (k Keeper) DeleteConsumerCommissionRate(
store.Delete(types.ConsumerCommissionRateKey(consumerId, providerAddr))
}

// SetAllowlist allowlists validator with `providerAddr` address on chain `consumerId`
func (k Keeper) SetAllowlist(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.AllowlistKey(consumerId, providerAddr), []byte{})
}

// GetAllowList returns all allowlisted validators
func (k Keeper) GetAllowList(
ctx sdk.Context,
consumerId string,
) (providerConsAddresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.StringIdWithLenKey(types.AllowlistKeyPrefix(), consumerId)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
providerConsAddresses = append(providerConsAddresses, types.NewProviderConsAddress(iterator.Key()[len(key):]))
}

return providerConsAddresses
}

// IsAllowlisted returns `true` if validator with `providerAddr` has been allowlisted on chain `consumerId`
func (k Keeper) IsAllowlisted(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) bool {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.AllowlistKey(consumerId, providerAddr))
return bz != nil
}

// DeleteAllowlist deletes all allowlisted validators
func (k Keeper) DeleteAllowlist(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.StringIdWithLenKey(types.AllowlistKeyPrefix(), consumerId))
defer iterator.Close()

keysToDel := [][]byte{}
for ; iterator.Valid(); iterator.Next() {
keysToDel = append(keysToDel, iterator.Key())
}

for _, key := range keysToDel {
store.Delete(key)
}
}

// IsAllowlistEmpty returns `true` if no validator is allowlisted on chain `consumerId`
func (k Keeper) IsAllowlistEmpty(ctx sdk.Context, consumerId string) bool {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.StringIdWithLenKey(types.AllowlistKeyPrefix(), consumerId))
defer iterator.Close()

return !iterator.Valid()
}

// UpdateAllowlist populates the allowlist store for the consumer chain with this consumer id
func (k Keeper) UpdateAllowlist(ctx sdk.Context, consumerId string, allowlist []string) {
k.DeleteAllowlist(ctx, consumerId)
for _, address := range allowlist {
consAddr, err := sdk.ConsAddressFromBech32(address)
if err != nil {
continue
}

k.SetAllowlist(ctx, consumerId, types.NewProviderConsAddress(consAddr))
}
}

// SetDenylist denylists validator with `providerAddr` address on chain `consumerId`
func (k Keeper) SetDenylist(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.DenylistKey(consumerId, providerAddr), []byte{})
}

// GetDenyList returns all denylisted validators
func (k Keeper) GetDenyList(
ctx sdk.Context,
consumerId string,
) (providerConsAddresses []types.ProviderConsAddress) {
store := ctx.KVStore(k.storeKey)
key := types.StringIdWithLenKey(types.DenylistKeyPrefix(), consumerId)
iterator := storetypes.KVStorePrefixIterator(store, key)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
providerConsAddresses = append(providerConsAddresses, types.NewProviderConsAddress(iterator.Key()[len(key):]))
}

return providerConsAddresses
}

// IsDenylisted returns `true` if validator with `providerAddr` has been denylisted on chain `consumerId`
func (k Keeper) IsDenylisted(
ctx sdk.Context,
consumerId string,
providerAddr types.ProviderConsAddress,
) bool {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.DenylistKey(consumerId, providerAddr))
return bz != nil
}

// DeleteDenylist deletes all denylisted validators
func (k Keeper) DeleteDenylist(ctx sdk.Context, consumerId string) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.StringIdWithLenKey(types.DenylistKeyPrefix(), consumerId))
defer iterator.Close()

keysToDel := [][]byte{}
for ; iterator.Valid(); iterator.Next() {
keysToDel = append(keysToDel, iterator.Key())
}

for _, key := range keysToDel {
store.Delete(key)
}
}

// IsDenylistEmpty returns `true` if no validator is denylisted on chain `consumerId`
func (k Keeper) IsDenylistEmpty(ctx sdk.Context, consumerId string) bool {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, types.StringIdWithLenKey(types.DenylistKeyPrefix(), consumerId))
defer iterator.Close()

return !iterator.Valid()
}

// UpdateDenylist populates the denylist store for the consumer chain with this consumer id
func (k Keeper) UpdateDenylist(ctx sdk.Context, consumerId string, denylist []string) {
k.DeleteDenylist(ctx, consumerId)
for _, address := range denylist {
consAddr, err := sdk.ConsAddressFromBech32(address)
if err != nil {
continue
}

k.SetDenylist(ctx, consumerId, types.NewProviderConsAddress(consAddr))
}
}

// SetMinimumPowerInTopN sets the minimum power required for a validator to be in the top N
// for a given consumer chain.
func (k Keeper) SetMinimumPowerInTopN(
ctx sdk.Context,
consumerId string,
power int64,
) {
store := ctx.KVStore(k.storeKey)

buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(power))

store.Set(types.MinimumPowerInTopNKey(consumerId), buf)
}

// GetMinimumPowerInTopN returns the minimum power required for a validator to be in the top N
// for a given consumer chain.
func (k Keeper) GetMinimumPowerInTopN(
ctx sdk.Context,
consumerId string,
) (int64, bool) {
store := ctx.KVStore(k.storeKey)
buf := store.Get(types.MinimumPowerInTopNKey(consumerId))
if buf == nil {
return 0, false
}
return int64(binary.BigEndian.Uint64(buf)), true
}

// DeleteMinimumPowerInTopN removes the minimum power required for a validator to be in the top N
// for a given consumer chain.
func (k Keeper) DeleteMinimumPowerInTopN(
ctx sdk.Context,
consumerId string,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.MinimumPowerInTopNKey(consumerId))
}

// UpdateMinimumPowerInTopN populates the minimum power in Top N for the consumer chain with this consumer id
func (k Keeper) UpdateMinimumPowerInTopN(ctx sdk.Context, consumerId string, oldTopN uint32, newTopN uint32) error {
// if the top N changes, we need to update the new minimum power in top N
if newTopN != oldTopN {
if newTopN > 0 {
// if the chain receives a non-zero top N value, store the minimum power in the top N
bondedValidators, err := k.GetLastProviderConsensusActiveValidators(ctx)
if err != nil {
return err
}
minPower, err := k.ComputeMinPowerInTopN(ctx, bondedValidators, newTopN)
if err != nil {
return err
}
k.SetMinimumPowerInTopN(ctx, consumerId, minPower)
} else {
// if the chain receives a zero top N value, we delete the min power
k.DeleteMinimumPowerInTopN(ctx, consumerId)
}
}

return nil
}

func (k Keeper) UnbondingCanComplete(ctx sdk.Context, id uint64) error {
return k.stakingKeeper.UnbondingCanComplete(ctx, id)
}
Expand Down
Loading

0 comments on commit 0777f87

Please sign in to comment.