Skip to content

Commit

Permalink
renamed some chainIDs to consumerIds
Browse files Browse the repository at this point in the history
  • Loading branch information
insumity committed Aug 6, 2024
1 parent 794d63f commit e5e4867
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 116 deletions.
30 changes: 15 additions & 15 deletions x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1170,25 +1170,25 @@ func (k Keeper) DeleteMinimumPowerInTopN(
// a given consumer chain.
func (k Keeper) SetMinStake(
ctx sdk.Context,
chainID string,
consumerId string,
minStake uint64,
) {
store := ctx.KVStore(k.storeKey)

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

store.Set(types.MinStakeKey(chainID), buf)
store.Set(types.MinStakeKey(consumerId), buf)
}

// GetMinStake returns the minimum stake required for a validator to validate
// a given consumer chain.
func (k Keeper) GetMinStake(
ctx sdk.Context,
chainID string,
consumerId string,
) (uint64, bool) {
store := ctx.KVStore(k.storeKey)
buf := store.Get(types.MinStakeKey(chainID))
buf := store.Get(types.MinStakeKey(consumerId))
if buf == nil {
return 0, false
}
Expand All @@ -1199,54 +1199,54 @@ func (k Keeper) GetMinStake(
// a given consumer chain.
func (k Keeper) DeleteMinStake(
ctx sdk.Context,
chainID string,
consumerId string,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.MinStakeKey(chainID))
store.Delete(types.MinStakeKey(consumerId))
}

// SetInactiveValidatorsAllowed sets whether inactive validators are allowed to validate
// a given consumer chain.
func (k Keeper) SetInactiveValidatorsAllowed(
ctx sdk.Context,
chainID string,
consumerId string,
allowed bool,
) {
if allowed {
k.EnableInactiveValidators(ctx, chainID)
k.EnableInactiveValidators(ctx, consumerId)
} else {
k.DisableInactiveValidators(ctx, chainID)
k.DisableInactiveValidators(ctx, consumerId)
}
}

// EnableInactiveValidators sets the flag to signal that inactive validators are allowed to validate
// a given consumer chain.
func (k Keeper) EnableInactiveValidators(
ctx sdk.Context,
chainID string,
consumerId string,
) {
store := ctx.KVStore(k.storeKey)
store.Set(types.AllowInactiveValidatorsKey(chainID), []byte{})
store.Set(types.AllowInactiveValidatorsKey(consumerId), []byte{})
}

// AllowsInactiveValidators returns whether inactive validators are allowed to validate
// a given consumer chain.
func (k Keeper) AllowsInactiveValidators(
ctx sdk.Context,
chainID string,
consumerId string,
) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.AllowInactiveValidatorsKey(chainID))
return store.Has(types.AllowInactiveValidatorsKey(consumerId))
}

// DisableInactiveValidators removes the flag of whether inactive validators are allowed to validate
// a given consumer chain.
func (k Keeper) DisableInactiveValidators(
ctx sdk.Context,
chainID string,
consumerId string,
) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.AllowInactiveValidatorsKey(chainID))
store.Delete(types.AllowInactiveValidatorsKey(consumerId))
}

func (k Keeper) UnbondingCanComplete(ctx sdk.Context, id uint64) error {
Expand Down
24 changes: 12 additions & 12 deletions x/ccv/provider/keeper/key_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ func (k Keeper) DeleteValidatorByConsumerAddr(ctx sdk.Context, consumerId string
// - or there exists a timestamp in ConsumerAddrsToPrune s.t. cAddr in ConsumerAddrsToPrune(timestamp)
func (k Keeper) AppendConsumerAddrsToPrune(
ctx sdk.Context,
chainID string,
consumerId string,
pruneTs time.Time,
consumerAddr types.ConsumerConsAddress,
) {
store := ctx.KVStore(k.storeKey)
storeKey := types.ConsumerAddrsToPruneV2Key(chainID, pruneTs)
storeKey := types.ConsumerAddrsToPruneV2Key(consumerId, pruneTs)
bz := store.Get(storeKey)
var consumerAddrsToPrune types.AddressList
if bz != nil {
Expand All @@ -275,12 +275,12 @@ func (k Keeper) AppendConsumerAddrsToPrune(
// Note that this method is only used in testing.
func (k Keeper) GetConsumerAddrsToPrune(
ctx sdk.Context,
chainID string,
consumerId string,
ts time.Time,
) (consumerAddrsToPrune types.AddressList) {
store := ctx.KVStore(k.storeKey)

bz := store.Get(types.ConsumerAddrsToPruneV2Key(chainID, ts))
bz := store.Get(types.ConsumerAddrsToPruneV2Key(consumerId, ts))
if bz == nil {
return
}
Expand All @@ -297,28 +297,28 @@ func (k Keeper) GetConsumerAddrsToPrune(
// The returned addresses are removed from the store.
//
// Note that the list of all consumer addresses is stored under keys with the following format:
// ConsumerAddrsToPruneV2BytePrefix | len(chainID) | chainID | timestamp
// ConsumerAddrsToPruneV2BytePrefix | len(consumerId) | consumerId | timestamp
// Thus, this method returns all the consumer addresses stored under keys in the following range:
// (ConsumerAddrsToPruneV2BytePrefix | len(chainID) | chainID | ts') where ts' <= ts
// (ConsumerAddrsToPruneV2BytePrefix | len(consumerId) | consumerId | ts') where ts' <= ts
func (k Keeper) ConsumeConsumerAddrsToPrune(
ctx sdk.Context,
chainID string,
consumerId string,
ts time.Time,
) (consumerAddrsToPrune types.AddressList) {
store := ctx.KVStore(k.storeKey)
consumerAddrsToPruneKeyPrefix := types.ConsumerAddrsToPruneV2KeyPrefix()
startPrefix := types.ConsumerIdWithLenKey(consumerAddrsToPruneKeyPrefix, chainID)
startPrefix := types.ConsumerIdWithLenKey(consumerAddrsToPruneKeyPrefix, consumerId)
iterator := store.Iterator(startPrefix,
storetypes.InclusiveEndBytes(types.ConsumerAddrsToPruneV2Key(chainID, ts)))
storetypes.InclusiveEndBytes(types.ConsumerAddrsToPruneV2Key(consumerId, ts)))
defer iterator.Close()

var keysToDel [][]byte
for ; iterator.Valid(); iterator.Next() {
// Sanity check
if _, pruneTs, err := types.ParseChainIdAndTsKey(consumerAddrsToPruneKeyPrefix, iterator.Key()); err != nil {
if _, pruneTs, err := types.ParseConsumerIdAndTsKey(consumerAddrsToPruneKeyPrefix, iterator.Key()); err != nil {
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in AppendConsumerAddrsToPrune.
k.Logger(ctx).Error("ParseChainIdAndTsKey failed",
k.Logger(ctx).Error("ParseConsumerIdAndTsKey failed",
"key", string(iterator.Key()),
"error", err.Error(),
)
Expand Down Expand Up @@ -364,7 +364,7 @@ func (k Keeper) GetAllConsumerAddrsToPrune(ctx sdk.Context, consumerId string) (
iterator := storetypes.KVStorePrefixIterator(store, iteratorPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
_, ts, err := types.ParseChainIdAndTsKey(consumerAddrsToPruneKeyPrefix, iterator.Key())
_, ts, err := types.ParseConsumerIdAndTsKey(consumerAddrsToPruneKeyPrefix, iterator.Key())
if err != nil {
// An error here would indicate something is very wrong,
// store keys are assumed to be correctly serialized in AppendConsumerAddrsToPrune.
Expand Down
78 changes: 39 additions & 39 deletions x/ccv/provider/keeper/partial_set_security.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func (k Keeper) HandleOptIn(ctx sdk.Context, consumerId string, providerAddr typ
return nil
}

// HandleOptOut prepares validator `providerAddr` to opt out from running `chainID`.
// HandleOptOut prepares validator `providerAddr` to opt out from running `consumerId`.
// Note that the validator only opts out at the end of an epoch.
func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) error {
if _, found := k.GetConsumerClientId(ctx, chainID); !found {
func (k Keeper) HandleOptOut(ctx sdk.Context, consumerId string, providerAddr types.ProviderConsAddress) error {
if _, found := k.GetConsumerClientId(ctx, consumerId); !found {
// A validator can only opt out from a running chain. We check this by checking the consumer client id, because
// `SetConsumerClientId` is set when the chain starts in `CreateConsumerClientInCachedCtx` of `BeginBlockInit`.
return errorsmod.Wrapf(
types.ErrUnknownConsumerId,
"opting out of an unknown or not running consumer chain, with id: %s", chainID)
"opting out of an unknown or not running consumer chain, with id: %s", consumerId)
}

if topN, found := k.GetTopN(ctx, chainID); found && topN > 0 {
if topN, found := k.GetTopN(ctx, consumerId); found && topN > 0 {
// a validator cannot opt out from a Top N chain if the validator is in the Top N validators
validator, err := k.stakingKeeper.GetValidatorByConsAddr(ctx, providerAddr.ToSdkConsAddr())
if err != nil {
Expand All @@ -84,27 +84,27 @@ func (k Keeper) HandleOptOut(ctx sdk.Context, chainID string, providerAddr types
if err != nil {
return err
}
minPowerInTopN, found := k.GetMinimumPowerInTopN(ctx, chainID)
minPowerInTopN, found := k.GetMinimumPowerInTopN(ctx, consumerId)
if !found {
return errorsmod.Wrapf(
types.ErrUnknownConsumerId,
"Could not find minimum power in top N for chain with id: %s", chainID)
"Could not find minimum power in top N for chain with consumer id: %s", consumerId)
}

if power >= minPowerInTopN {
return errorsmod.Wrapf(
types.ErrCannotOptOutFromTopN,
"validator with power (%d) cannot opt out from Top N chain (%s) because all validators"+
" with at least %d power have to validate", power, chainID, minPowerInTopN)
"validator with power (%d) cannot opt out from Top N chain with consumer id (%s) because all validators"+
" with at least %d power have to validate", power, consumerId, minPowerInTopN)
}
}

k.DeleteOptedIn(ctx, chainID, providerAddr)
k.DeleteOptedIn(ctx, consumerId, providerAddr)
return nil
}

// OptInTopNValidators opts in to `chainID` all the `bondedValidators` that have at least `minPowerToOptIn` power
func (k Keeper) OptInTopNValidators(ctx sdk.Context, chainID string, bondedValidators []stakingtypes.Validator, minPowerToOptIn int64) {
// OptInTopNValidators opts in to `consumerId` all the `bondedValidators` that have at least `minPowerToOptIn` power
func (k Keeper) OptInTopNValidators(ctx sdk.Context, consumerId string, bondedValidators []stakingtypes.Validator, minPowerToOptIn int64) {
for _, val := range bondedValidators {
valAddr, err := sdk.ValAddressFromBech32(val.GetOperator())
if err != nil {
Expand All @@ -127,7 +127,7 @@ func (k Keeper) OptInTopNValidators(ctx sdk.Context, chainID string, bondedValid
}

// if validator already exists it gets overwritten
k.SetOptedIn(ctx, chainID, types.NewProviderConsAddress(consAddr))
k.SetOptedIn(ctx, consumerId, types.NewProviderConsAddress(consAddr))
} // else validators that do not belong to the top N validators but were opted in, remain opted in
}
}
Expand Down Expand Up @@ -178,15 +178,15 @@ func (k Keeper) ComputeMinPowerInTopN(ctx sdk.Context, bondedValidators []stakin
return 0, fmt.Errorf("should never reach this point with topN (%d), totalPower (%d), and powerSum (%d)", topN, totalPower, powerSum)
}

// CapValidatorSet caps the provided `validators` if chain `chainID` is an Opt In chain with a validator-set cap. If cap
// is `k`, `CapValidatorSet` returns the first `k` validators from `validators` with the highest power.
func (k Keeper) CapValidatorSet(ctx sdk.Context, chainID string, validators []types.ConsensusValidator) []types.ConsensusValidator {
if topN, found := k.GetTopN(ctx, chainID); found && topN > 0 {
// CapValidatorSet caps the provided `validators` if chain with `consumerId` is an Opt In chain with a validator-set cap.
// If cap is `k`, `CapValidatorSet` returns the first `k` validators from `validators` with the highest power.
func (k Keeper) CapValidatorSet(ctx sdk.Context, consumerId string, validators []types.ConsensusValidator) []types.ConsensusValidator {
if topN, found := k.GetTopN(ctx, consumerId); found && topN > 0 {
// is a no-op if the chain is a Top N chain
return validators
}

if validatorSetCap, found := k.GetValidatorSetCap(ctx, chainID); found && validatorSetCap != 0 && int(validatorSetCap) < len(validators) {
if validatorSetCap, found := k.GetValidatorSetCap(ctx, consumerId); found && validatorSetCap != 0 && int(validatorSetCap) < len(validators) {
sort.Slice(validators, func(i, j int) bool {
return validators[i].Power > validators[j].Power
})
Expand All @@ -197,15 +197,15 @@ func (k Keeper) CapValidatorSet(ctx sdk.Context, chainID string, validators []ty
}
}

// CapValidatorsPower caps the power of the validators on chain `chainID` and returns an updated slice of validators
// CapValidatorsPower caps the power of the validators on chain with `consumerId` and returns an updated slice of validators
// with their new powers. Works on a best-basis effort because there are cases where we cannot guarantee that all validators
// on the consumer chain have less power than the set validators-power cap. For example, if we have 10 validators and
// the power cap is set to 5%, we need at least one validator to have more than 10% of the voting power on the consumer chain.
func (k Keeper) CapValidatorsPower(ctx sdk.Context, chainID string, validators []types.ConsensusValidator) []types.ConsensusValidator {
if p, found := k.GetValidatorsPowerCap(ctx, chainID); found && p > 0 {
func (k Keeper) CapValidatorsPower(ctx sdk.Context, consumerId string, validators []types.ConsensusValidator) []types.ConsensusValidator {
if p, found := k.GetValidatorsPowerCap(ctx, consumerId); found && p > 0 {
return NoMoreThanPercentOfTheSum(validators, p)
} else {
// is a no-op if power cap is not set for `chainID`
// is a no-op if power cap is not set for `consumerId`
return validators
}
}
Expand Down Expand Up @@ -310,23 +310,23 @@ func NoMoreThanPercentOfTheSum(validators []types.ConsensusValidator, percent ui
return updatedValidators
}

// CanValidateChain returns true if the validator `providerAddr` is opted-in to chain `chainID` and the allowlist and
// denylist do not prevent the validator from validating the chain.
func (k Keeper) CanValidateChain(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) bool {
// CanValidateChain returns true if the validator `providerAddr` is opted-in to chain with `consumerId` and the allowlist
// and denylist do not prevent the validator from validating the chain.
func (k Keeper) CanValidateChain(ctx sdk.Context, consumerId string, providerAddr types.ProviderConsAddress) bool {
// only consider opted-in validators
return k.IsOptedIn(ctx, chainID, providerAddr) &&
return k.IsOptedIn(ctx, consumerId, providerAddr) &&
// if an allowlist is declared, only consider allowlisted validators
(k.IsAllowlistEmpty(ctx, chainID) ||
k.IsAllowlisted(ctx, chainID, providerAddr)) &&
(k.IsAllowlistEmpty(ctx, consumerId) ||
k.IsAllowlisted(ctx, consumerId, providerAddr)) &&
// if a denylist is declared, only consider denylisted validators
(k.IsDenylistEmpty(ctx, chainID) ||
!k.IsDenylisted(ctx, chainID, providerAddr))
(k.IsDenylistEmpty(ctx, consumerId) ||
!k.IsDenylisted(ctx, consumerId, providerAddr))
}

// FulfillsMinStake returns true if the validator `providerAddr` has enough stake to validate chain `chainID`
// FulfillsMinStake returns true if the validator `providerAddr` has enough stake to validate chain with `consumerId`
// by checking its staked tokens against the minimum stake required to validate the chain.
func (k Keeper) FulfillsMinStake(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) bool {
minStake, found := k.GetMinStake(ctx, chainID)
func (k Keeper) FulfillsMinStake(ctx sdk.Context, consumerId string, providerAddr types.ProviderConsAddress) bool {
minStake, found := k.GetMinStake(ctx, consumerId)
if !found {
return true
}
Expand All @@ -342,15 +342,15 @@ func (k Keeper) FulfillsMinStake(ctx sdk.Context, chainID string, providerAddr t
}

// ComputeNextValidators computes the validators for the upcoming epoch based on the currently `bondedValidators`.
func (k Keeper) ComputeNextValidators(ctx sdk.Context, chainID string, bondedValidators []stakingtypes.Validator) []types.ConsensusValidator {
func (k Keeper) ComputeNextValidators(ctx sdk.Context, consumerId string, bondedValidators []stakingtypes.Validator) []types.ConsensusValidator {
// sort the bonded validators by number of staked tokens in descending order
sort.Slice(bondedValidators, func(i, j int) bool {
return bondedValidators[i].GetBondedTokens().GT(bondedValidators[j].GetBondedTokens())
})

// if inactive validators are not allowed, only consider the first `MaxProviderConsensusValidators` validators
// since those are the ones that participate in consensus
allowInactiveVals := k.AllowsInactiveValidators(ctx, chainID)
allowInactiveVals := k.AllowsInactiveValidators(ctx, consumerId)
if !allowInactiveVals {
// only leave the first MaxProviderConsensusValidators bonded validators
maxProviderConsensusVals := k.GetMaxProviderConsensusValidators(ctx)
Expand All @@ -359,11 +359,11 @@ func (k Keeper) ComputeNextValidators(ctx sdk.Context, chainID string, bondedVal
}
}

nextValidators := k.FilterValidators(ctx, chainID, bondedValidators,
nextValidators := k.FilterValidators(ctx, consumerId, bondedValidators,
func(providerAddr types.ProviderConsAddress) bool {
return k.CanValidateChain(ctx, chainID, providerAddr) && k.FulfillsMinStake(ctx, chainID, providerAddr)
return k.CanValidateChain(ctx, consumerId, providerAddr) && k.FulfillsMinStake(ctx, consumerId, providerAddr)
})

nextValidators = k.CapValidatorSet(ctx, chainID, nextValidators)
return k.CapValidatorsPower(ctx, chainID, nextValidators)
nextValidators = k.CapValidatorSet(ctx, consumerId, nextValidators)
return k.CapValidatorsPower(ctx, consumerId, nextValidators)
}
Loading

0 comments on commit e5e4867

Please sign in to comment.