Skip to content

Commit

Permalink
refactor!: Refactor the validator set storage and add provider consen…
Browse files Browse the repository at this point in the history
…sus validator storage (#1990)

* Refactor validator set storage

* Add comment for getTotalPower

* Add provider consensus validator set storage

* Add key to key test

* Add unit test for LastTotalProviderConsensusPower

* Address comments
  • Loading branch information
p-offtermatt committed Jul 2, 2024
1 parent ba88527 commit c052624
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion x/ccv/provider/keeper/provider_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (k Keeper) SetLastProviderConsensusValidator(
}

// SetLastProviderConsensusValSet resets the stored last validator set sent to the consensus engine on the provider
// to the provided nextValidators.
// to the provided `nextValidators.
func (k Keeper) SetLastProviderConsensusValSet(ctx sdk.Context, nextValidators []types.ConsumerValidator) {
k.setValSet(ctx, []byte{types.LastProviderConsensusValsPrefix}, nextValidators)
}
Expand Down
22 changes: 22 additions & 0 deletions x/ccv/provider/keeper/provider_consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
"testing"

"cosmossdk.io/math"
"github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -100,3 +101,24 @@ func TestDeleteLastProviderConsensusValSet(t *testing.T) {
storedValidators := providerKeeper.GetLastProviderConsensusValSet(ctx)
require.Empty(t, storedValidators, "validator set should be empty")
}

func TestGetLastTotalProviderConsensusPower(t *testing.T) {
providerKeeper, ctx, ctrl, _ := testkeeper.GetProviderKeeperAndCtx(t, testkeeper.NewInMemKeeperParams(t))
defer ctrl.Finish()
validator1 := types.ConsumerValidator{
ProviderConsAddr: []byte("providerConsAddr1"),
Power: 2,
ConsumerPublicKey: &crypto.PublicKey{},
}
validator2 := types.ConsumerValidator{
ProviderConsAddr: []byte("providerConsAddr2"),
Power: 3,
ConsumerPublicKey: &crypto.PublicKey{},
}
nextValidators := []types.ConsumerValidator{validator1, validator2}
providerKeeper.SetLastProviderConsensusValSet(ctx, nextValidators)
// Get the total power of the last stored validator set
totalPower := providerKeeper.GetLastTotalProviderConsensusPower(ctx)
expectedTotalPower := math.NewInt(5)
require.Equal(t, expectedTotalPower, totalPower, "total power does not match")
}
4 changes: 2 additions & 2 deletions x/ccv/provider/keeper/validator_set_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (k Keeper) isValidator(ctx sdk.Context, prefix []byte, providerAddr types.P
// getValSet returns all the validators stored under the given prefix.
func (k Keeper) getValSet(
ctx sdk.Context,
key []byte,
prefix []byte,
) (validators []types.ConsumerValidator) {
store := ctx.KVStore(k.storeKey)
iterator := storetypes.KVStorePrefixIterator(store, key)
iterator := storetypes.KVStorePrefixIterator(store, prefix)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand Down
16 changes: 8 additions & 8 deletions x/ccv/provider/keeper/validator_set_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
ccv "github.com/cosmos/interchain-security/v5/x/ccv/types"
)

// GetConsumerChainKey returns the store key for consumer validators of the consumer chain with `chainID`
func (k Keeper) GetConsumerChainKey(ctx sdk.Context, chainID string) []byte {
// GetConsumerChainConsensusValidatorsKey returns the store key for consumer validators of the consumer chain with `chainID`
func (k Keeper) GetConsumerChainConsensusValidatorsKey(ctx sdk.Context, chainID string) []byte {
return types.ChainIdWithLenKey(types.ConsumerValidatorBytePrefix, chainID)
}

Expand All @@ -23,13 +23,13 @@ func (k Keeper) SetConsumerValidator(
chainID string,
validator types.ConsumerValidator,
) {
k.setValidator(ctx, k.GetConsumerChainKey(ctx, chainID), validator)
k.setValidator(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID), validator)
}

// SetConsumerValSet resets the current consumer validators with the `nextValidators` computed by
// `FilterValidators` and hence this method should only be called after `FilterValidators` has completed.
func (k Keeper) SetConsumerValSet(ctx sdk.Context, chainID string, nextValidators []types.ConsumerValidator) {
k.setValSet(ctx, k.GetConsumerChainKey(ctx, chainID), nextValidators)
k.setValSet(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID), nextValidators)
}

// DeleteConsumerValidator removes consumer validator with `providerAddr` address
Expand All @@ -38,29 +38,29 @@ func (k Keeper) DeleteConsumerValidator(
chainID string,
providerConsAddr types.ProviderConsAddress,
) {
k.deleteValidator(ctx, k.GetConsumerChainKey(ctx, chainID), providerConsAddr)
k.deleteValidator(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID), providerConsAddr)
}

// DeleteConsumerValSet deletes all the stored consumer validators for chain `chainID`
func (k Keeper) DeleteConsumerValSet(
ctx sdk.Context,
chainID string,
) {
k.deleteValSet(ctx, k.GetConsumerChainKey(ctx, chainID))
k.deleteValSet(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID))
}

// IsConsumerValidator returns `true` if the consumer validator with `providerAddr` exists for chain `chainID`
// and `false` otherwise
func (k Keeper) IsConsumerValidator(ctx sdk.Context, chainID string, providerAddr types.ProviderConsAddress) bool {
return k.isValidator(ctx, k.GetConsumerChainKey(ctx, chainID), providerAddr)
return k.isValidator(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID), providerAddr)
}

// GetConsumerValSet returns all the consumer validators for chain `chainID`
func (k Keeper) GetConsumerValSet(
ctx sdk.Context,
chainID string,
) []types.ConsumerValidator {
return k.getValSet(ctx, k.GetConsumerChainKey(ctx, chainID))
return k.getValSet(ctx, k.GetConsumerChainConsensusValidatorsKey(ctx, chainID))
}

// DiffValidators compares the current and the next epoch's consumer validators and returns the `ValidatorUpdate` diff
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const (
// minimum power required to be in the top N per consumer chain.
MinimumPowerInTopNBytePrefix

// LastProviderConsensusValsPrefix is byte prefix for storing the last validator set
// LastProviderConsensusValsPrefix is the byte prefix for storing the last validator set
// sent to the consensus engine of the provider chain
LastProviderConsensusValsPrefix

Expand Down
1 change: 1 addition & 0 deletions x/ccv/provider/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func getAllKeyPrefixes() []byte {
providertypes.ConsumerRewardsAllocationBytePrefix,
providertypes.ConsumerCommissionRatePrefix,
providertypes.MinimumPowerInTopNBytePrefix,
providertypes.LastProviderConsensusValsPrefix,
providertypes.ParametersByteKey,
}
}
Expand Down

0 comments on commit c052624

Please sign in to comment.