Skip to content

Commit

Permalink
partial: migrate some throttle integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSalopek committed Sep 27, 2023
1 parent 5ed4d52 commit 84b1977
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
39 changes: 36 additions & 3 deletions tests/integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/stretchr/testify/suite"

store "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"

abci "github.com/cometbft/cometbft/abci/types"
tmencoding "github.com/cometbft/cometbft/crypto/encoding"

icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing"
testutil "github.com/cosmos/interchain-security/v3/testutil/integration"
"github.com/cosmos/interchain-security/v3/testutil/simibc"
ccv "github.com/cosmos/interchain-security/v3/x/ccv/types"
)

Expand Down Expand Up @@ -389,7 +389,8 @@ func newPacketSniffer() *packetSniffer {
}

func (ps *packetSniffer) ListenFinalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error {
packets := simibc.ParsePacketsFromEvents(simibc.ABCIToSDKEvents(res.GetEvents()))
// TODO: @MSalopek this was deprecated, figure out how to use it
packets := ParsePacketsFromEvents(res.GetEvents())
for _, packet := range packets {
ps.packets[getSentPacketKey(packet.Sequence, packet.SourceChannel)] = packet
}
Expand All @@ -402,10 +403,42 @@ func getSentPacketKey(sequence uint64, channelID string) string {
return fmt.Sprintf("%s-%d", channelID, sequence)
}

func (*packetSniffer) ListenCommit(ctx context.Context, res abci.ResponseCommit) error {
func (*packetSniffer) ListenCommit(ctx context.Context, res abci.ResponseCommit, cs []*store.StoreKVPair) error {
return nil
}

func (*packetSniffer) Close() error { return nil }
func (*packetSniffer) Listeners() map[store.StoreKey][]store.ABCIListener { return nil }
func (*packetSniffer) Stream(wg *sync.WaitGroup) error { return nil }

// [legacy simibc method]
// ABCIToSDKEvents converts a list of ABCI events to Cosmos SDK events.
func ABCIToSDKEvents(abciEvents []abci.Event) sdk.Events {
var events sdk.Events
for _, evt := range abciEvents {
var attributes []sdk.Attribute
for _, attr := range evt.GetAttributes() {
attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value))
}

events = events.AppendEvent(sdk.NewEvent(evt.GetType(), attributes...))
}

return events
}

// [legacy simibc method]
// ParsePacketsFromEvents returns all packets found in events.
// func ParsePacketsFromEvents(events []sdk.Event) (packets []channeltypes.Packet) {
func ParsePacketsFromEvents(events []abci.Event) (packets []channeltypes.Packet) {
for i, ev := range events {
if ev.Type == channeltypes.EventTypeSendPacket {
packet, err := ibctesting.ParsePacketFromEvents(events[i:])
if err != nil {
panic(err)
}
packets = append(packets, packet)
}
}
return
}
39 changes: 31 additions & 8 deletions tests/integration/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() {
vals = providerStakingKeeper.GetAllValidators(s.providerCtx())
slashedVal := vals[0]
s.Require().True(slashedVal.IsJailed())
lastValPower := providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), slashedVal.GetOperator())

slashedValOperator, err := sdk.ValAddressFromHex(slashedVal.GetOperator())
s.Require().NoError(err)
lastValPower := providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), slashedValOperator)
s.Require().Equal(int64(0), lastValPower)

// Assert expected slash meter and allowance value
Expand Down Expand Up @@ -149,7 +152,10 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() {
// Assert validator 2 has no power, this should be apparent next block,
// since the staking endblocker runs before the ccv endblocker.
s.providerChain.NextBlock()
lastValPower = providerStakingKeeper.GetLastValidatorPower(cacheCtx, slashedVal.GetOperator())

slashedValOperator, err = sdk.ValAddressFromHex(slashedVal.GetOperator())
s.Require().NoError(err)
lastValPower = providerStakingKeeper.GetLastValidatorPower(cacheCtx, slashedValOperator)
s.Require().Equal(int64(0), lastValPower)
}
}
Expand Down Expand Up @@ -638,18 +644,30 @@ func (s *CCVTestSuite) TestSlashingSmallValidators() {

// Default slash meter replenish fraction is 0.05, so all sent packets should be handled immediately.
vals = providerStakingKeeper.GetAllValidators(s.providerCtx())

val0Operator, err := sdk.ValAddressFromHex(vals[0].GetOperator())
s.Require().NoError(err)
s.Require().False(vals[0].IsJailed())
s.Require().Equal(int64(1000),
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), vals[0].GetOperator()))
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), val0Operator))

val1Operator, err := sdk.ValAddressFromHex(vals[1].GetOperator())
s.Require().NoError(err)
s.Require().True(vals[1].IsJailed())
s.Require().Equal(int64(0),
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), vals[1].GetOperator()))
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), val1Operator))

val2Operator, err := sdk.ValAddressFromHex(vals[2].GetOperator())
s.Require().NoError(err)
s.Require().True(vals[2].IsJailed())
s.Require().Equal(int64(0),
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), vals[2].GetOperator()))
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), val2Operator))

val3Operator, err := sdk.ValAddressFromHex(vals[3].GetOperator())
s.Require().NoError(err)
s.Require().True(vals[3].IsJailed())
s.Require().Equal(int64(0),
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), vals[3].GetOperator()))
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), val3Operator))
}

// TestSlashMeterAllowanceChanges tests scenarios where the slash meter allowance is expected to change.
Expand Down Expand Up @@ -969,8 +987,10 @@ func (s *CCVTestSuite) confirmValidatorJailed(tmVal tmtypes.Validator, checkPowe
s.Require().True(sdkVal.IsJailed())

if checkPower {
valOperator, err := sdk.ValAddressFromHex(sdkVal.GetOperator())
s.Require().NoError(err)
valPower := s.providerApp.GetTestStakingKeeper().GetLastValidatorPower(
s.providerCtx(), sdkVal.GetOperator())
s.providerCtx(), valOperator)
s.Require().Equal(int64(0), valPower)
}
}
Expand All @@ -979,8 +999,11 @@ func (s *CCVTestSuite) confirmValidatorNotJailed(tmVal tmtypes.Validator, expect
sdkVal, found := s.providerApp.GetTestStakingKeeper().GetValidator(
s.providerCtx(), sdk.ValAddress(tmVal.Address))
s.Require().True(found)

valOperator, err := sdk.ValAddressFromHex(sdkVal.GetOperator())
s.Require().NoError(err)
valPower := s.providerApp.GetTestStakingKeeper().GetLastValidatorPower(
s.providerCtx(), sdkVal.GetOperator())
s.providerCtx(), valOperator)
s.Require().Equal(expectedPower, valPower)
s.Require().False(sdkVal.IsJailed())
}
Expand Down
14 changes: 11 additions & 3 deletions tests/integration/throttle_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

provider "github.com/cosmos/interchain-security/v3/x/ccv/provider"
Expand Down Expand Up @@ -83,8 +84,12 @@ func (s *CCVTestSuite) TestSlashRetries() {
// Default slash meter replenish fraction is 0.05, so packet should be handled on provider.
vals = s.providerApp.GetTestStakingKeeper().GetAllValidators(s.providerCtx())
s.Require().True(vals[1].IsJailed())

val1Operator, err := sdk.ValAddressFromHex(vals[1].GetOperator())
s.Require().NoError(err)

s.Require().Equal(int64(0),
s.providerApp.GetTestStakingKeeper().GetLastValidatorPower(s.providerCtx(), vals[1].GetOperator()))
s.providerApp.GetTestStakingKeeper().GetLastValidatorPower(s.providerCtx(), val1Operator))
s.Require().Equal(uint64(0), providerKeeper.GetThrottledPacketDataSize(s.providerCtx(),
s.getFirstBundle().Chain.ChainID))

Expand All @@ -93,7 +98,7 @@ func (s *CCVTestSuite) TestSlashRetries() {

// Apply ack back on consumer
ackForConsumer := expectedv1Ack
err := consumerKeeper.OnAcknowledgementPacket(s.consumerCtx(), packet1, ackForConsumer)
err = consumerKeeper.OnAcknowledgementPacket(s.consumerCtx(), packet1, ackForConsumer)
s.Require().NoError(err)

// Slash record should have been deleted, head of pending packets should have been popped
Expand Down Expand Up @@ -136,8 +141,11 @@ func (s *CCVTestSuite) TestSlashRetries() {

// Val shouldn't be jailed on provider. Slash packet was queued
s.Require().False(vals[2].IsJailed())
val2Operator, err := sdk.ValAddressFromHex(vals[2].GetOperator())
s.Require().NoError(err)

s.Require().Equal(int64(1000),
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), vals[2].GetOperator()))
providerStakingKeeper.GetLastValidatorPower(s.providerCtx(), val2Operator))
s.Require().Equal(uint64(1), providerKeeper.GetThrottledPacketDataSize(s.providerCtx(),
s.getFirstBundle().Chain.ChainID))

Expand Down

0 comments on commit 84b1977

Please sign in to comment.