Skip to content

Commit

Permalink
partial: migrate ccv/keeper.go and most tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Sep 18, 2023
1 parent c92315c commit cb8a7a8
Show file tree
Hide file tree
Showing 16 changed files with 1,204 additions and 1,150 deletions.
22 changes: 12 additions & 10 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"

"cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"

storetypes "cosmossdk.io/store/types"
Expand All @@ -29,7 +30,8 @@ import (

// Keeper defines the Cross-Chain Validation Consumer Keeper
type Keeper struct {
storeKey storetypes.StoreKey
storeKey storetypes.StoreKey // TODO: maybe needs to be removed?
storeService store.KVStoreService
cdc codec.BinaryCodec
paramStore paramtypes.Subspace
scopedKeeper ccv.ScopedKeeper
Expand Down Expand Up @@ -338,7 +340,7 @@ func (k Keeper) GetLastStandaloneValidators(ctx sdk.Context) []stakingtypes.Vali
// i.e., the slice contains the IDs of the matured VSCPackets.
func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []ccv.MaturingVSCPacket) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})

defer iterator.Close()

Expand Down Expand Up @@ -370,7 +372,7 @@ func (k Keeper) GetElapsedPacketMaturityTimes(ctx sdk.Context) (maturingVSCPacke
// If two entries have the same maturityTime, then they are ordered by vscID.
func (k Keeper) GetAllPacketMaturityTimes(ctx sdk.Context) (maturingVSCPackets []ccv.MaturingVSCPacket) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PacketMaturityTimeBytePrefix})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -471,7 +473,7 @@ func (k Keeper) DeleteHeightValsetUpdateID(ctx sdk.Context, height uint64) {
// Thus, the returned array is in ascending order of heights.
func (k Keeper) GetAllHeightToValsetUpdateIDs(ctx sdk.Context) (heightToValsetUpdateIDs []ccv.HeightToValsetUpdateID) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.HeightValsetUpdateIDBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.HeightValsetUpdateIDBytePrefix})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -517,7 +519,7 @@ func (k Keeper) DeleteOutstandingDowntime(ctx sdk.Context, consAddress string) {
// Thus, the returned array is in ascending order of consAddresses.
func (k Keeper) GetAllOutstandingDowntimes(ctx sdk.Context) (downtimes []ccv.OutstandingDowntime) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.OutstandingDowntimeBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.OutstandingDowntimeBytePrefix})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down Expand Up @@ -566,7 +568,7 @@ func (k Keeper) DeleteCCValidator(ctx sdk.Context, addr []byte) {
// Thus, the returned array is in ascending order of addresses.
func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChainValidator) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.CrossChainValidatorBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.CrossChainValidatorBytePrefix})

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -582,7 +584,7 @@ func (k Keeper) GetAllCCValidator(ctx sdk.Context) (validators []types.CrossChai
func (k Keeper) GetAllValidators(ctx sdk.Context) (validators []stakingtypes.Validator) {
store := ctx.KVStore(k.storeKey)

iterator := sdk.KVStorePrefixIterator(store, stakingtypes.ValidatorsKey)
iterator := storetypes.KVStorePrefixIterator(store, stakingtypes.ValidatorsKey)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
Expand All @@ -609,7 +611,7 @@ func (k Keeper) getAndIncrementPendingPacketsIdx(ctx sdk.Context) (toReturn uint
// DeleteHeadOfPendingPackets deletes the head of the pending packets queue.
func (k Keeper) DeleteHeadOfPendingPackets(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
defer iterator.Close()
if !iterator.Valid() {
return
Expand Down Expand Up @@ -643,7 +645,7 @@ func (k Keeper) GetAllPendingPacketsWithIdx(ctx sdk.Context) []ConsumerPacketDat
store := ctx.KVStore(k.storeKey)
// Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey.
// See consistency with PendingDataPacketsKey().
iterator := sdk.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var packet ccv.ConsumerPacketData
Expand Down Expand Up @@ -675,7 +677,7 @@ func (k Keeper) DeleteAllPendingDataPackets(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
// Note: PendingDataPacketsBytePrefix is the correct prefix, NOT PendingDataPacketsByteKey.
// See consistency with PendingDataPacketsKey().
iterator := sdk.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
iterator := storetypes.KVStorePrefixIterator(store, []byte{types.PendingDataPacketsBytePrefix})
keysToDel := [][]byte{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand Down
87 changes: 43 additions & 44 deletions x/ccv/consumer/keeper/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

abci "github.com/cometbft/cometbft/abci/types"
tmrand "github.com/cometbft/cometbft/libs/rand"
tmtypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/interchain-security/v3/testutil/crypto"
Expand Down Expand Up @@ -178,64 +177,64 @@ func TestSlash(t *testing.T) {

// If we call slash with infraction type empty, standalone staking keeper's slash will not be called
// (if it was called, test would panic without mocking the call)
consumerKeeper.SlashWithInfractionReason(ctx, []byte{0x01, 0x02, 0x03}, 5, 6, sdk.NewDec(9.0), stakingtypes.Infraction_INFRACTION_UNSPECIFIED)
consumerKeeper.SlashWithInfractionReason(ctx, []byte{0x01, 0x02, 0x03}, 5, 6, math.LegacyNewDec(9.0), stakingtypes.Infraction_INFRACTION_UNSPECIFIED)

// Now setup a mock for Slash, and confirm that it is called against
// standalone staking keeper with valid infraction type
infractionHeight := int64(5)
mocks.MockStakingKeeper.EXPECT().SlashWithInfractionReason(
ctx, []byte{0x01, 0x02, 0x03}, infractionHeight, int64(6),
sdk.MustNewDecFromStr("0.05"), stakingtypes.Infraction_INFRACTION_UNSPECIFIED).Times(1) // We pass empty infraction to standalone staking keeper since it's not used
math.LegacyMustNewDecFromStr("0.05"), stakingtypes.Infraction_INFRACTION_UNSPECIFIED).Times(1) // We pass empty infraction to standalone staking keeper since it's not used

// Also setup init genesis height s.t. infraction height is before first consumer height
consumerKeeper.SetInitGenesisHeight(ctx, 4)
require.Equal(t, consumerKeeper.FirstConsumerHeight(ctx), int64(6))

consumerKeeper.SlashWithInfractionReason(ctx, []byte{0x01, 0x02, 0x03}, infractionHeight, 6,
sdk.MustNewDecFromStr("0.05"), stakingtypes.Infraction_INFRACTION_DOWNTIME)
math.LegacyMustNewDecFromStr("0.05"), stakingtypes.Infraction_INFRACTION_DOWNTIME)
}

// Tests the getter and setter behavior for historical info
func TestHistoricalInfo(t *testing.T) {
keeperParams := testkeeper.NewInMemKeeperParams(t)
consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
defer ctrl.Finish()
ctx = ctx.WithBlockHeight(15)

// Generate test validators, save them to store, and retrieve stored records
validators := GenerateValidators(t)
SetCCValidators(t, consumerKeeper, ctx, validators)
ccValidators := consumerKeeper.GetAllCCValidator(ctx)
require.Len(t, ccValidators, len(validators))

// iterate over validators and convert them to staking type
sVals := []stakingtypes.Validator{}
for _, v := range ccValidators {
pk, err := v.ConsPubKey()
require.NoError(t, err)

val, err := stakingtypes.NewValidator(nil, pk, stakingtypes.Description{})
require.NoError(t, err)

// set voting power to random value
val.Tokens = sdk.TokensFromConsensusPower(tmrand.NewRand().Int64(), sdk.DefaultPowerReduction)
sVals = append(sVals, val)
}

currentHeight := ctx.BlockHeight()

// create and store historical info
hi := stakingtypes.NewHistoricalInfo(ctx.BlockHeader(), sVals, sdk.DefaultPowerReduction)
consumerKeeper.SetHistoricalInfo(ctx, currentHeight, &hi)

// expect to get historical info
recv, found := consumerKeeper.GetHistoricalInfo(ctx, currentHeight)
require.True(t, found, "HistoricalInfo not found after set")
require.Equal(t, hi, recv, "HistoricalInfo not equal")

// verify that historical info valset has validators sorted in order
require.True(t, IsValSetSorted(recv.Valset, sdk.DefaultPowerReduction), "HistoricalInfo validators is not sorted")
}
// func TestHistoricalInfo(t *testing.T) {
// keeperParams := testkeeper.NewInMemKeeperParams(t)
// consumerKeeper, ctx, ctrl, _ := testkeeper.GetConsumerKeeperAndCtx(t, keeperParams)
// defer ctrl.Finish()
// ctx = ctx.WithBlockHeight(15)

// // Generate test validators, save them to store, and retrieve stored records
// validators := GenerateValidators(t)
// SetCCValidators(t, consumerKeeper, ctx, validators)
// ccValidators := consumerKeeper.GetAllCCValidator(ctx)
// require.Len(t, ccValidators, len(validators))

// // iterate over validators and convert them to staking type
// sVals := []stakingtypes.Validator{}
// for _, v := range ccValidators {
// pk, err := v.ConsPubKey()
// require.NoError(t, err)

// val, err := stakingtypes.NewValidator(nil, pk, stakingtypes.Description{})
// require.NoError(t, err)

// // set voting power to random value
// val.Tokens = sdk.TokensFromConsensusPower(tmrand.NewRand().Int64(), sdk.DefaultPowerReduction)
// sVals = append(sVals, val)
// }

// currentHeight := ctx.BlockHeight()

// // create and store historical info
// hi := stakingtypes.NewHistoricalInfo(ctx.BlockHeader(), sVals, sdk.DefaultPowerReduction)
// consumerKeeper.SetHistoricalInfo(ctx, currentHeight, &hi)

// // expect to get historical info
// recv, found := consumerKeeper.GetHistoricalInfo(ctx, currentHeight)
// require.True(t, found, "HistoricalInfo not found after set")
// require.Equal(t, hi, recv, "HistoricalInfo not equal")

// // verify that historical info valset has validators sorted in order
// require.True(t, IsValSetSorted(recv.Valset, sdk.DefaultPowerReduction), "HistoricalInfo validators is not sorted")
// }

// IsValSetSorted reports whether valset is sorted.
func IsValSetSorted(data []stakingtypes.Validator, powerReduction math.Int) bool {
Expand Down
Loading

0 comments on commit cb8a7a8

Please sign in to comment.