Skip to content

Commit

Permalink
add newPacketFromConsumer/Provider helper funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Sep 6, 2023
1 parent cdb43f9 commit 717b3f8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 69 deletions.
27 changes: 17 additions & 10 deletions tests/integration/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,26 @@ func redelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valSrcAddr sdk.ValAddre
}
}

func (s *CCVTestSuite) newPacketFromProvider(data []byte, sequence uint64, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) channeltypes.Packet {
return channeltypes.NewPacket(data, sequence,
path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID,
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
timeoutHeight, timeoutTimestamp)
}

func (s *CCVTestSuite) newPacketFromConsumer(data []byte, sequence uint64, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) channeltypes.Packet {
return channeltypes.NewPacket(data, sequence,
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
}

// sendOnProviderRecvOnConsumer sends a packet from the provider chain and receives it on the consumer chain
func sendOnProviderRecvOnConsumer(s *CCVTestSuite, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) channeltypes.Packet {
sequence, err := path.EndpointB.SendPacket(timeoutHeight, timeoutTimestamp, data)
s.Require().NoError(err)

packet := channeltypes.NewPacket(data, sequence,
path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID,
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromProvider(data, sequence, path, timeoutHeight, timeoutTimestamp)

err = path.EndpointA.RecvPacket(packet)
s.Require().NoError(err)
Expand All @@ -235,10 +246,7 @@ func sendOnConsumerRecvOnProvider(s *CCVTestSuite, path *ibctesting.Path, timeou
sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data)
s.Require().NoError(err)

packet := channeltypes.NewPacket(data, sequence,
path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID,
path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, sequence, path, timeoutHeight, timeoutTimestamp)

err = path.EndpointB.RecvPacket(packet)
s.Require().NoError(err)
Expand Down Expand Up @@ -410,8 +418,7 @@ func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv.
oldBlockTime := ctx.BlockTime()
timeout := uint64(oldBlockTime.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano())

packet := channeltypes.NewPacket(packetData.GetBytes(), 1, ccv.ConsumerPortID, suite.path.EndpointA.ChannelID,
ccv.ProviderPortID, suite.path.EndpointB.ChannelID, clienttypes.Height{}, timeout)
packet := suite.newPacketFromConsumer(packetData.GetBytes(), 1, suite.path, clienttypes.Height{}, timeout)

return channeltypes.CommitPacket(suite.consumerChain.App.AppCodec(), packet)
}
Expand Down
10 changes: 3 additions & 7 deletions tests/integration/slashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +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 := channeltypes.NewPacket(data, sequence,
s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID,
s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
err = s.path.EndpointB.RecvPacket(packet)
s.Require().NoError(err)

Expand Down Expand Up @@ -266,9 +263,8 @@ func (s *CCVTestSuite) TestSlashPacketAcknowledgement() {
SlashPacketData: &spd,
},
)
packet := channeltypes.NewPacket(cpd.GetBytes(), // Consumer always sends v1 packet data
1, ccv.ConsumerPortID, s.path.EndpointA.ChannelID,
ccv.ProviderPortID, s.path.EndpointB.ChannelID, clienttypes.Height{}, 0)
packet := s.newPacketFromConsumer(cpd.GetBytes(), // Consumer always sends v1 packet data
1, s.path, clienttypes.Height{}, 0)

// Map infraction height on provider so validation passes and provider returns valid ack result
providerKeeper.SetValsetUpdateBlockHeight(s.providerCtx(), spd.ValsetUpdateId, 47923)
Expand Down
51 changes: 10 additions & 41 deletions tests/integration/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -331,10 +330,7 @@ func (s *CCVTestSuite) TestPacketSpam() {
for sequence, data := range packetsData {
consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, uint64(sequence),
firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID,
firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, uint64(sequence), firstBundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

Expand Down Expand Up @@ -393,10 +389,7 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() {
for sequence, data := range packetsData {
consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, uint64(sequence),
firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID,
firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, uint64(sequence), firstBundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

Expand Down Expand Up @@ -501,10 +494,7 @@ func (s *CCVTestSuite) TestQueueOrdering() {
for i, data := range packetsData {
consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, ibcSeqs[i],
firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID,
firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqs[i], firstBundle.Path, timeoutHeight, timeoutTimestamp)
// Type depends on index packets were appended from above
if (i+5)%10 == 0 {
vscMaturedPacketData := consumerPacketData.GetVscMaturedPacketData()
Expand Down Expand Up @@ -727,10 +717,7 @@ func (s *CCVTestSuite) TestSlashSameValidator() {
for i, data := range packetsData {
consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, uint64(i),
s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID,
s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, uint64(i), s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

Expand Down Expand Up @@ -792,10 +779,7 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes
ibcSeqNum := uint64(i)
consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, ibcSeqNum,
s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID,
s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData())
}

Expand Down Expand Up @@ -834,10 +818,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, ibcSeqNum,
ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID,
ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(),
packet, *packetData.GetVscMaturedPacketData())
}
Expand All @@ -851,10 +832,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME)
packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, ibcSeqNum,
ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID,
ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(),
packet, *packetData.GetSlashPacketData())
}
Expand All @@ -867,10 +845,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() {
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
packetData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData)
packet := channeltypes.NewPacket(data, ibcSeqNum,
ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID,
ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(),
packet, *packetData.GetVscMaturedPacketData())
}
Expand Down Expand Up @@ -941,10 +916,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() {
data := s.constructVSCMaturedPacketFromConsumer(*bundle)
packetData := ccvtypes.ConsumerPacketData{}
ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData)
packet := channeltypes.NewPacket(data, ibcSeqNum,
ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID,
ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(),
packet, *packetData.GetVscMaturedPacketData())
}
Expand All @@ -958,10 +930,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() {
*s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME)
consumderPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket
s.Require().NoError(err)
packet := channeltypes.NewPacket(data, ibcSeqNum,
ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID,
ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp)
providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumderPacketData.GetSlashPacketData())
}
}
Expand Down
10 changes: 2 additions & 8 deletions tests/integration/throttle_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ func (s *CCVTestSuite) TestSlashRetries() {
timeoutHeight = clienttypes.Height{}
timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano())
)
packet1 := channeltypes.NewPacket(packetData1, 1,
s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID,
s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet1 := s.newPacketFromConsumer(packetData1, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)
// Mock the sending of the packet on consumer
consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket,
&ccvtypes.ConsumerPacketData_SlashPacketData{
Expand Down Expand Up @@ -114,10 +111,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)
packet2 := channeltypes.NewPacket(packetData2, 1,
s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID,
s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID,
timeoutHeight, timeoutTimestamp)
packet2 := s.newPacketFromConsumer(packetData2, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp)

consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket,
&ccvtypes.ConsumerPacketData_SlashPacketData{
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/valset_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"time"

clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -71,8 +70,7 @@ func (suite *CCVTestSuite) TestQueueAndSendVSCMaturedPackets() {
)

// send first packet
packet := channeltypes.NewPacket(pd.GetBytes(), 1, ccv.ProviderPortID, suite.path.EndpointB.ChannelID, ccv.ConsumerPortID, suite.path.EndpointA.ChannelID,
clienttypes.NewHeight(1, 0), 0)
packet := suite.newPacketFromProvider(pd.GetBytes(), 1, suite.path, clienttypes.NewHeight(1, 0), 0)
ack := consumerKeeper.OnRecvVSCPacket(suite.consumerChain.GetContext(), packet, pd)
suite.Require().NotNil(ack, "OnRecvVSCPacket did not return ack")
suite.Require().True(ack.Success(), "OnRecvVSCPacket did not return a Success Acknowledgment")
Expand Down

0 comments on commit 717b3f8

Please sign in to comment.