Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor changes in expired consumer handling #507

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions app/consumer-democracy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,6 @@ func (app *App) GetE2eEvidenceKeeper() e2e.E2eEvidenceKeeper {
return app.EvidenceKeeper
}

// GetUpgradeKeeper implements the ConsumerApp interface.
func (app *App) GetUpgradeKeeper() upgradekeeper.Keeper {
return app.UpgradeKeeper
}

// GetE2eStakingKeeper implements the ConsumerApp interface.
func (app *App) GetE2eStakingKeeper() e2e.E2eStakingKeeper {
return app.StakingKeeper
Expand Down
5 changes: 0 additions & 5 deletions app/consumer/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,6 @@ func (app *App) GetE2eEvidenceKeeper() e2e.E2eEvidenceKeeper {
return app.EvidenceKeeper
}

// GetUpgradeKeeper implements the ConsumerApp interface.
func (app *App) GetUpgradeKeeper() upgradekeeper.Keeper {
return app.UpgradeKeeper
}

// TestingApp functions

// GetBaseApp implements the TestingApp interface.
Expand Down
3 changes: 0 additions & 3 deletions testutil/e2e/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
consumerkeeper "github.com/cosmos/interchain-security/x/ccv/consumer/keeper"
providerkeeper "github.com/cosmos/interchain-security/x/ccv/provider/keeper"
Expand Down Expand Up @@ -62,8 +61,6 @@ type ConsumerApp interface {
GetE2eSlashingKeeper() E2eSlashingKeeper
// Returns an evidence keeper interface with more capabilities than the expected_keepers interface
GetE2eEvidenceKeeper() E2eEvidenceKeeper
// Returns the upgrade keeper interface
GetUpgradeKeeper() upgradekeeper.Keeper
}

type DemocConsumerApp interface {
Expand Down
8 changes: 5 additions & 3 deletions x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ func (k Keeper) GetPacketMaturityTime(ctx sdk.Context, vscId uint64) uint64 {
return binary.BigEndian.Uint64(bz)
}

// DeletePacketMaturityTime deletes the packet maturity time for a given received VSC packet id
func (k Keeper) DeletePacketMaturityTime(ctx sdk.Context, vscId uint64) {
// DeletePacketMaturityTimes deletes the packet maturity time for given received VSC packet ids
func (k Keeper) DeletePacketMaturityTimes(ctx sdk.Context, vscIds ...uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.PacketMaturityTimeKey(vscId))
for _, vscId := range vscIds {
store.Delete(types.PacketMaturityTimeKey(vscId))
}
}

// VerifyProviderChain verifies that the chain trying to connect on the channel handshake
Expand Down
8 changes: 7 additions & 1 deletion x/ccv/consumer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestPacketMaturityTime(t *testing.T) {
consumerKeeper.SetPacketMaturityTime(ctx, 5, 15)
consumerKeeper.SetPacketMaturityTime(ctx, 6, 40)

consumerKeeper.DeletePacketMaturityTime(ctx, 6)
consumerKeeper.DeletePacketMaturityTimes(ctx, 6)

require.Equal(t, uint64(10), consumerKeeper.GetPacketMaturityTime(ctx, 1))
require.Equal(t, uint64(25), consumerKeeper.GetPacketMaturityTime(ctx, 2))
Expand All @@ -110,6 +110,12 @@ func TestPacketMaturityTime(t *testing.T) {
i++
return false // do not stop the iteration
})

// delete all vscs remaining in state
consumerKeeper.DeletePacketMaturityTimes(ctx, 1, 2, 5)
require.Equal(t, uint64(0), consumerKeeper.GetPacketMaturityTime(ctx, 1))
require.Equal(t, uint64(0), consumerKeeper.GetPacketMaturityTime(ctx, 2))
require.Equal(t, uint64(0), consumerKeeper.GetPacketMaturityTime(ctx, 5))
}

// TestCrossChainValidator tests the getter, setter, and deletion method for cross chain validator records
Expand Down
28 changes: 27 additions & 1 deletion x/ccv/consumer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -91,6 +92,7 @@ func (k Keeper) QueueVSCMaturedPackets(ctx sdk.Context) {

currentTime := uint64(ctx.BlockTime().UnixNano())

maturedVscIds := []uint64{}
for maturityIterator.Valid() {
vscId := types.IdFromPacketMaturityTimeKey(maturityIterator.Key())
if currentTime >= binary.BigEndian.Uint64(maturityIterator.Value()) {
Expand All @@ -104,12 +106,26 @@ func (k Keeper) QueueVSCMaturedPackets(ctx sdk.Context) {
Type: types.VscMaturedPacket,
Data: vscPacket.GetBytes(),
})
k.DeletePacketMaturityTime(ctx, vscId)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeVSCMatured,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeChainID, ctx.ChainID()),
sdk.NewAttribute(ccv.AttributeConsumerHeight, strconv.Itoa(int(ctx.BlockHeight()))),
sdk.NewAttribute(ccv.AttributeValSetUpdateID, strconv.Itoa(int(vscId))),
sdk.NewAttribute(ccv.AttributeTimestamp, strconv.Itoa(int(currentTime))),
),
)

maturedVscIds = append(maturedVscIds, vscId)
} else {
break
}
maturityIterator.Next()
}

k.DeletePacketMaturityTimes(ctx, maturedVscIds...)
}

// QueueSlashPacket appends a slash packet containing the given validator data and slashing info to queue.
Expand Down Expand Up @@ -137,6 +153,16 @@ func (k Keeper) QueueSlashPacket(ctx sdk.Context, validator abci.Validator, vals
Type: types.SlashPacket,
Data: slashPacket.GetBytes(),
})

ctx.EventManager().EmitEvent(
sdk.NewEvent(
ccv.EventTypeConsumerSlashRequest,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(ccv.AttributeValidatorAddress, sdk.ConsAddress(validator.Address).String()),
sdk.NewAttribute(ccv.AttributeValSetUpdateID, strconv.Itoa(int(valsetUpdateID))),
sdk.NewAttribute(ccv.AttributeInfractionType, infraction.String()),
),
)
}

// SendPackets iterates queued packets and sends them in FIFO order.
Expand Down
3 changes: 2 additions & 1 deletion x/ccv/provider/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ func (k Keeper) SendPacketsToChain(ctx sdk.Context, chainID, channelID string) {
// QueueVSCPackets queues latest validator updates for every registered consumer chain
func (k Keeper) QueueVSCPackets(ctx sdk.Context) {
valUpdateID := k.GetValidatorSetUpdateId(ctx) // curent valset update ID
// check whether there are changes in the validator set;
// get the validator updates from the staking module
valUpdates := k.stakingKeeper.GetValidatorUpdates(ctx)

k.IterateConsumerChains(ctx, func(ctx sdk.Context, chainID, clientID string) (stop bool) {
// check whether there are changes in the validator set;
// note that this also entails unbonding operations
// w/o changes in the voting power of the validators in the validator set
unbondingOps, _ := k.GetUnbondingOpsFromIndex(ctx, chainID, valUpdateID)
Expand Down
4 changes: 2 additions & 2 deletions x/ccv/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const (

EventTypeExecuteConsumerChainSlash = "execute_consumer_chain_slash"
EventTypeFeeDistribution = "fee_distribution"
EventTypeSendSlashPacket = "send_slash_packet"
EventTypeSendMaturedVSCPacket = "send_matured_vsc_packet"
EventTypeConsumerSlashRequest = "consumer_slash_request"
EventTypeVSCMatured = "vsc_matured"

AttributeKeyAckSuccess = "success"
AttributeKeyAck = "acknowledgement"
Expand Down