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: wrap VSCMatured/Slash packets into a consumer packet type #626

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 0 additions & 23 deletions proto/interchain_security/ccv/consumer/v1/consumer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,3 @@ message CrossChainValidator {
];
}

// ConsumerPacketType indicates interchain security specific packet types.
enum ConsumerPacketType {
option (gogoproto.goproto_enum_prefix) = false;

// UNSPECIFIED packet type
CONSUMER_PACKET_TYPE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "UnspecifiedPacket"];
// Slash packet
CONSUMER_PACKET_TYPE_SLASH = 1 [(gogoproto.enumvalue_customname) = "SlashPacket"];
// VSCMatured packet
CONSUMER_PACKET_TYPE_VSCM = 2 [(gogoproto.enumvalue_customname) = "VscMaturedPacket"];
}

// ConsumerPacket contains raw packet bytes and packet type.
message ConsumerPacket {
ConsumerPacketType type = 1;
bytes data = 2;
}

// ConsumerPackets is a list of data packets.
message ConsumerPackets {
repeated ConsumerPacket list = 1
[ (gogoproto.nullable) = false ];
}
9 changes: 5 additions & 4 deletions proto/interchain_security/ccv/consumer/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package interchain_security.ccv.consumer.v1;

option go_package = "github.com/cosmos/interchain-security/x/ccv/consumer/types";

import "google/protobuf/duration.proto";
import "interchain_security/ccv/v1/ccv.proto";
import "interchain_security/ccv/consumer/v1/consumer.proto";
import "tendermint/abci/types.proto";
import "ibc/lightclients/tendermint/v1/tendermint.proto";
import "ibc/core/channel/v1/channel.proto";
import "google/protobuf/duration.proto";
import "gogoproto/gogo.proto";
import "interchain_security/ccv/consumer/v1/consumer.proto";
import "tendermint/abci/types.proto";

// GenesisState defines the CCV consumer chain genesis state
message GenesisState {
Expand All @@ -34,7 +35,7 @@ message GenesisState {
repeated OutstandingDowntime outstanding_downtime_slashing = 10
[ (gogoproto.nullable) = false ];
// PendingConsumerPackets nil on new chain, filled in on restart.
ConsumerPackets pending_consumer_packets = 11
interchain_security.ccv.v1.ConsumerPacketDataList pending_consumer_packets = 11
[ (gogoproto.nullable) = false ];
// LastTransmissionBlockHeight nil on new chain, filled in on restart.
interchain_security.ccv.consumer.v1.LastTransmissionBlockHeight last_transmission_block_height = 12
Expand Down
30 changes: 30 additions & 0 deletions proto/interchain_security/ccv/v1/ccv.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,33 @@ message SlashPacketData {
message MaturedUnbondingOps {
repeated uint64 ids = 1;
}

// ConsumerPacketData contains a consumer packet data and a type tag
message ConsumerPacketData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this protobuf type is defined in ccv.proto, I'd prefer that it doesn't have the word "consumer" in it, because that may be confusing if the type is used on both consumer and provider.

In other places such as throttle.go, this type is referred to as ThrottledPacketData

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be open to another name that's not ThrottledPacketData as well, like PacketDataWrapper

ConsumerPacketDataType type = 1;

oneof data {
SlashPacketData slashPacketData = 2;
VSCMaturedPacketData vscMaturedPacketData = 3;
}
}


// ConsumerPacketDataList is a list of consumer packet data packets.
message ConsumerPacketDataList {
repeated ConsumerPacketData list = 1
[ (gogoproto.nullable) = false ];
}


// ConsumerPacketType indicates interchain security specific packet types.
enum ConsumerPacketDataType {
option (gogoproto.goproto_enum_prefix) = false;

// UNSPECIFIED packet type
CONSUMER_PACKET_TYPE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "UnspecifiedPacket"];
// Slash packet
CONSUMER_PACKET_TYPE_SLASH = 1 [(gogoproto.enumvalue_customname) = "SlashPacket"];
// VSCMatured packet
CONSUMER_PACKET_TYPE_VSCM = 2 [(gogoproto.enumvalue_customname) = "VscMaturedPacket"];
}
45 changes: 35 additions & 10 deletions tests/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,20 @@ func (suite *CCVTestSuite) SendEmptyVSCPacket() {
// commitSlashPacket returns a commit hash for the given slash packet data
// Note that it must be called before sending the embedding IBC packet.
func (suite *CCVTestSuite) commitSlashPacket(ctx sdk.Context, packetData ccv.SlashPacketData) []byte {

consumerPacket := ccv.ConsumerPacketData{
Type: ccv.SlashPacket,
Data: &ccv.ConsumerPacketData_SlashPacketData{
SlashPacketData: &packetData,
},
}

return suite.commitConsumerPacket(ctx, consumerPacket)
}

// commitConsumerPacket returns a commit hash for the given consumer packet data
// Note that it must be called before sending the embedding IBC packet.
func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv.ConsumerPacketData) []byte {
oldBlockTime := ctx.BlockTime()
timeout := uint64(oldBlockTime.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

Expand All @@ -394,20 +408,26 @@ func (suite *CCVTestSuite) commitSlashPacket(ctx sdk.Context, packetData ccv.Sla
return channeltypes.CommitPacket(suite.consumerChain.App.AppCodec(), packet)
}

// constructSlashPacketFromConsumer constructs a slash packet to be sent from consumer to provider,
// constructSlashPacketFromConsumer constructs an IBC packet embedding
// slash packet data to be sent from consumer to provider
func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.ConsumerBundle,
tmVal tmtypes.Validator, infractionType stakingtypes.InfractionType, ibcSeqNum uint64) channeltypes.Packet {

valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID(
bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight()))

data := ccv.SlashPacketData{
Validator: abci.Validator{
Address: tmVal.Address,
Power: tmVal.VotingPower,
data := ccv.ConsumerPacketData{
Type: ccv.SlashPacket,
Data: &ccv.ConsumerPacketData_SlashPacketData{
SlashPacketData: &ccv.SlashPacketData{
Validator: abci.Validator{
Address: tmVal.Address,
Power: tmVal.VotingPower,
},
ValsetUpdateId: valsetUpdateId,
Infraction: infractionType,
},
},
ValsetUpdateId: valsetUpdateId,
Infraction: infractionType,
}

return channeltypes.NewPacket(data.GetBytes(),
Expand All @@ -421,15 +441,20 @@ func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.C
)
}

// constructVSCMaturedPacketFromConsumer constructs a VSC Matured packet
// to be sent from consumer to provider
// constructVSCMaturedPacketFromConsumer constructs an IBC packet embedding
// VSC Matured packet data to be sent from consumer to provider
func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle,
ibcSeqNum uint64) channeltypes.Packet {

valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID(
bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight()))

data := ccv.VSCMaturedPacketData{ValsetUpdateId: valsetUpdateId}
data := ccv.ConsumerPacketData{
Type: ccv.VscMaturedPacket,
Data: &ccv.ConsumerPacketData_VscMaturedPacketData{
VscMaturedPacketData: &ccv.VSCMaturedPacketData{ValsetUpdateId: valsetUpdateId},
},
}

return channeltypes.NewPacket(data.GetBytes(),
ibcSeqNum,
Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (suite *CCVTestSuite) TestHandleSlashPacketDoubleSigning() {
)

providerKeeper.HandleSlashPacket(suite.providerCtx(), suite.consumerChain.ChainID,
ccv.NewSlashPacketData(
*ccv.NewSlashPacketData(
abci.Validator{Address: tmVal.Address, Power: 0},
uint64(0),
stakingtypes.DoubleSign,
Expand Down Expand Up @@ -331,7 +331,7 @@ func (suite *CCVTestSuite) TestOnRecvSlashPacketErrors() {
// Expect no error ack if validator does not exist
// TODO: this behavior should be changed to return an error ack,
// see: https://github.com/cosmos/interchain-security/issues/546
ack := providerKeeper.OnRecvSlashPacket(ctx, packet, slashingPkt)
ack := providerKeeper.OnRecvSlashPacket(ctx, packet, *slashingPkt)
suite.Require().True(ack.Success())

val := suite.providerChain.Vals.Validators[0]
Expand Down Expand Up @@ -359,7 +359,7 @@ func (suite *CCVTestSuite) TestOnRecvSlashPacketErrors() {
valInfo.Address = sdk.ConsAddress(tmAddr).String()
providerSlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(tmAddr), valInfo)

errAck = providerKeeper.OnRecvSlashPacket(ctx, packet, slashingPkt)
errAck = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashingPkt)
suite.Require().False(errAck.Success())

// Expect nothing was queued
Expand All @@ -368,7 +368,7 @@ func (suite *CCVTestSuite) TestOnRecvSlashPacketErrors() {

// expect to queue entries for the slash request
slashingPkt.Infraction = stakingtypes.DoubleSign
ack = providerKeeper.OnRecvSlashPacket(ctx, packet, slashingPkt)
ack = providerKeeper.OnRecvSlashPacket(ctx, packet, *slashingPkt)
suite.Require().True(ack.Success())
suite.Require().Equal(1, len(providerKeeper.GetAllGlobalSlashEntries(ctx)))
suite.Require().Equal(uint64(1), (providerKeeper.GetThrottledPacketDataSize(ctx, consumerChainID)))
Expand Down Expand Up @@ -463,7 +463,7 @@ func (suite *CCVTestSuite) TestHandleSlashPacketDistribution() {
)

// slash
providerKeeper.HandleSlashPacket(suite.providerChain.GetContext(), suite.consumerChain.ChainID, slashPacket)
providerKeeper.HandleSlashPacket(suite.providerChain.GetContext(), suite.consumerChain.ChainID, *slashPacket)

ubd, found := providerStakingKeeper.GetUnbondingDelegation(suite.providerChain.GetContext(), delAddr, valAddr)
suite.Require().True(found)
Expand Down Expand Up @@ -520,7 +520,7 @@ func (suite *CCVTestSuite) TestValidatorDowntime() {
consumerKeeper.GetHeightValsetUpdateID(ctx, uint64(missedBlockThreshold-sdk.ValidatorUpdateDelay-1)),
stakingtypes.Downtime,
)
expCommit := suite.commitSlashPacket(ctx, packetData)
expCommit := suite.commitSlashPacket(ctx, *packetData)

// Miss 50 blocks and expect a slash packet to be sent
for ; height <= missedBlockThreshold; height++ {
Expand Down Expand Up @@ -624,7 +624,7 @@ func (suite *CCVTestSuite) TestValidatorDoubleSigning() {
suite.consumerApp.GetConsumerKeeper().GetHeightValsetUpdateID(ctx, uint64(infractionHeight-sdk.ValidatorUpdateDelay)),
stakingtypes.DoubleSign,
)
expCommit := suite.commitSlashPacket(ctx, packetData)
expCommit := suite.commitSlashPacket(ctx, *packetData)

// expect to send slash packet when handling double-sign evidence
suite.consumerApp.GetE2eEvidenceKeeper().HandleEquivocationEvidence(ctx, e)
Expand Down
30 changes: 15 additions & 15 deletions tests/e2e/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ func (s *CCVTestSuite) TestPacketSpam() {

// Recv 500 packets from consumer to provider in same block
for _, packet := range packets {
slashPacketData := ccvtypes.SlashPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &slashPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, slashPacketData)
consumerPacketData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &consumerPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

// Execute block to handle packets in endblock
Expand Down Expand Up @@ -383,21 +383,21 @@ func (s *CCVTestSuite) TestQueueOrdering() {

// Recv 500 packets from consumer to provider in same block
for i, packet := range packets {
consumerPacketData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &consumerPacketData)
// Type depends on index packets were appended from above
if (i+5)%10 == 0 {
vscMaturedPacketData := ccvtypes.VSCMaturedPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &vscMaturedPacketData)
vscMaturedPacketData := consumerPacketData.GetVscMaturedPacketData()
vscMaturedPacketData.ValsetUpdateId = uint64(i + 1000)
providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, vscMaturedPacketData)
providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *vscMaturedPacketData)
} else {
slashPacketData := ccvtypes.SlashPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &slashPacketData)
// Set valset update id to be 2000 + index to assert ordering
slashPacketData := consumerPacketData.GetSlashPacketData()
slashPacketData.ValsetUpdateId = uint64(i + 2000)
// Set block height mapping so packet is not dropped
providerKeeper.SetValsetUpdateBlockHeight(s.providerCtx(),
slashPacketData.ValsetUpdateId, uint64(firstBundle.GetCtx().BlockHeight()))
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, slashPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *slashPacketData)
}
}

Expand Down Expand Up @@ -600,9 +600,9 @@ func (s *CCVTestSuite) TestSlashSameValidator() {

// Recv and queue all slash packets.
for _, packet := range packets {
slashPacketData := ccvtypes.SlashPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &slashPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, slashPacketData)
consumerPacketData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &consumerPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

// We should have 6 pending slash packet entries queued.
Expand Down Expand Up @@ -668,9 +668,9 @@ func (s CCVTestSuite) TestSlashAllValidators() {

// Recv and queue all slash packets.
for _, packet := range packets {
slashPacketData := ccvtypes.SlashPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &slashPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, slashPacketData)
consumerPacketData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &consumerPacketData)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

// We should have 24 pending slash packet entries queued.
Expand Down
Loading