Skip to content

Commit

Permalink
constructPacket method return ccv type instead of []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Sep 6, 2023
1 parent 717b3f8 commit 660eb8a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
10 changes: 4 additions & 6 deletions tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv.
// slash packet data to be sent from consumer to provider
func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.ConsumerBundle,
tmVal tmtypes.Validator, infractionType stakingtypes.Infraction,
) []byte {
) ccv.ConsumerPacketData {
valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID(
bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight()))

data := ccv.ConsumerPacketData{
return ccv.ConsumerPacketData{
Type: ccv.SlashPacket,
Data: &ccv.ConsumerPacketData_SlashPacketData{
SlashPacketData: &ccv.SlashPacketData{
Expand All @@ -444,22 +444,20 @@ func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.C
},
},
}
return data.GetBytes()
}

// constructVSCMaturedPacketFromConsumer constructs an IBC packet embedding
// VSC Matured packet data to be sent from consumer to provider
func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle) []byte {
func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle) ccv.ConsumerPacketData {
valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID(
bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight()))

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

// incrementTime increments the overall time by jumpPeriod
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() {
timeoutHeight = clienttypes.Height{}
timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())
)
data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sequence, err := s.getFirstBundle().Path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data)
slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sequence, err := s.getFirstBundle().Path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())
s.Require().NoError(err)

// Set outstanding slashing flag for first consumer, it's important to use the consumer's cons addr here
Expand All @@ -90,7 +90,7 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() {
valsetUpdateIdN := providerKeeper.GetValidatorSetUpdateId(s.providerCtx())

// receive the slash packet on the provider chain. RecvPacket() calls the provider endblocker twice
packet := s.newPacketFromConsumer(data, sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(slashPacket.GetBytes(), sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
err = s.path.EndpointB.RecvPacket(packet)
s.Require().NoError(err)

Expand Down Expand Up @@ -205,8 +205,8 @@ func (s *CCVTestSuite) TestRelayAndApplyDoubleSignPacket() {
timeoutHeight = clienttypes.Height{}
timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())
)
data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
packet := sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data)
slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
packet := sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())

// Advance a few more blocks to make sure any voting power changes would be reflected
s.providerChain.NextBlock()
Expand Down
76 changes: 39 additions & 37 deletions tests/integration/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() {
// Send a slash packet from consumer to provider
s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[0])
tmVal := s.providerChain.Vals.Validators[0]
data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data)
slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())

// Assert validator 0 is jailed and has no power
vals = providerStakingKeeper.GetAllValidators(s.providerCtx())
Expand All @@ -86,8 +86,8 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() {
// Now send a second slash packet from consumer to provider for a different validator.
s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[2])
tmVal = s.providerChain.Vals.Validators[2]
data = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data)
slashPacket = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())

// Require that slash packet has not been handled
vals = providerStakingKeeper.GetAllValidators(s.providerCtx())
Expand Down Expand Up @@ -172,10 +172,10 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() {
timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano())
)
for consumerChainID, bundle := range s.consumerBundles {
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data)
data = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data)
slashPacket := s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())
slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())
// Confirm packets were not queued on provider for this consumer
s.Require().Equal(uint64(0),
providerKeeper.GetThrottledPacketDataSize(s.providerCtx(), consumerChainID))
Expand Down Expand Up @@ -204,18 +204,18 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() {
// Send downtime slash packet from consumer to provider
tmVal := s.providerChain.Vals.Validators[idx]
valsToSlash = append(valsToSlash, *tmVal)
data := s.constructSlashPacketFromConsumer(
slashPacket := s.constructSlashPacketFromConsumer(
*bundle,
*tmVal,
stakingtypes.Infraction_INFRACTION_DOWNTIME,
)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())

// Send two trailing VSC matured packets from consumer to provider
data = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data)
data = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data)
slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())
slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle)
sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes())

idx++
}
Expand Down Expand Up @@ -319,7 +319,8 @@ func (s *CCVTestSuite) TestPacketSpam() {
infractionType = stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN
}
valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3]
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType))
slashPacket := s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType)
packetsData = append(packetsData, slashPacket.GetBytes())
}

// Recv 500 packets from consumer to provider in same block
Expand Down Expand Up @@ -378,7 +379,8 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() {
// Increment ibc seq num for each packet (starting at 1)
ibcSeqNum++
valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3]
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN))
slashPacket := s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)
packetsData = append(packetsData, slashPacket.GetBytes())
}

// Recv 500 packets from consumer to provider in same block
Expand Down Expand Up @@ -477,13 +479,13 @@ func (s *CCVTestSuite) TestQueueOrdering() {

// Instantiate a vsc matured packet every 10th packet
if ibcSeqNum%10 == 0 {
packetsData = append(packetsData, s.constructVSCMaturedPacketFromConsumer(firstBundle))
packetsData = append(packetsData, s.constructVSCMaturedPacketFromConsumer(firstBundle).GetBytes())
continue
}
// Else instantiate a slash packet

valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3]
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME))
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes())
}

// Recv 500 packets from consumer to provider in same block
Expand Down Expand Up @@ -626,12 +628,12 @@ func (s *CCVTestSuite) TestSlashingSmallValidators() {
tmval1 := s.providerChain.Vals.Validators[1]
tmval2 := s.providerChain.Vals.Validators[2]
tmval3 := s.providerChain.Vals.Validators[3]
data1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME)
data2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME)
data3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data1)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data2)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data3)
slashPacket1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME)
slashPacket2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME)
slashPacket3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME)
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket1.GetBytes())
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket2.GetBytes())
sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket3.GetBytes())

// Default slash meter replenish fraction is 0.05, so all sent packets should be handled immediately.
vals = providerStakingKeeper.GetAllValidators(s.providerCtx())
Expand Down Expand Up @@ -701,12 +703,12 @@ func (s *CCVTestSuite) TestSlashSameValidator() {
s.setDefaultValSigningInfo(*tmval3)

packetsData := [][]byte{
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(),
}

// Recv and queue all slash packets.
Expand Down Expand Up @@ -759,14 +761,14 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes
for _, val := range s.providerChain.Vals.Validators {
s.setDefaultValSigningInfo(*val)
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(
s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME))
s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes())
}

// add 5 more slash packets for each validator, that will be handled in the same block.
for _, val := range s.providerChain.Vals.Validators {
for i := 0; i < 5; i++ {
packetsData = append(packetsData, s.constructSlashPacketFromConsumer(
s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME))
s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes())
}
}

Expand Down Expand Up @@ -815,7 +817,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
for _, bundle := range s.consumerBundles {
for i := 0; i < 50; i++ {
ibcSeqNum := uint64(i)
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes()
packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
Expand All @@ -829,7 +831,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
for i := 50; i < 100; i++ {
ibcSeqNum := uint64(i)
data := s.constructSlashPacketFromConsumer(*bundle,
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME)
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()
packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
Expand All @@ -842,7 +844,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
for _, bundle := range s.consumerBundles {
for i := 100; i < 150; i++ {
ibcSeqNum := uint64(i)
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes()
packetData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
Expand Down Expand Up @@ -913,7 +915,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() {
for _, bundle := range s.consumerBundles {
for i := 0; i < 100; i++ {
ibcSeqNum := uint64(i)
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes()
packetData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
Expand All @@ -927,7 +929,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() {
for i := 100; i < 150; i++ {
ibcSeqNum := uint64(i)
data := s.constructSlashPacketFromConsumer(*bundle,
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME)
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()
consumderPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/throttle_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *CCVTestSuite) TestSlashRetries() {

// Construct a mock slash packet from consumer
tmval1 := s.providerChain.Vals.Validators[1]
packetData1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME)
packetData1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()
var (
timeoutHeight = clienttypes.Height{}
timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano())
Expand Down Expand Up @@ -110,7 +110,7 @@ func (s *CCVTestSuite) TestSlashRetries() {

// Construct and mock the sending of a second packet on consumer
tmval2 := s.providerChain.Vals.Validators[2]
packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME)
packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()
packet2 := s.newPacketFromConsumer(packetData2, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)

consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket,
Expand Down

0 comments on commit 660eb8a

Please sign in to comment.