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

tests: regression tests for types sent over wire as json #1160

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions x/ccv/consumer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,21 @@ func TestOnAcknowledgementPacket(t *testing.T) {
// Set an established provider channel for later in test
consumerKeeper.SetProviderChannel(ctx, channelIDToProvider)

packetData := types.NewSlashPacketData(
slashPacketData := types.NewSlashPacketData(
abci.Validator{Address: bytes.HexBytes{}, Power: int64(1)}, uint64(1), stakingtypes.Infraction_INFRACTION_DOWNTIME,
)

// The type that'd be JSON marshaled and sent over the wire
consumerPacketData := types.NewConsumerPacketData(
types.SlashPacket,
&types.ConsumerPacketData_SlashPacketData{
SlashPacketData: slashPacketData,
},
)

// AcknowledgePacket is in reference to a packet originally sent from this (consumer) module.
packet := channeltypes.NewPacket(
packetData.GetBytes(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrong type was previously used in this test. I've adjusted it to use the proper consumer packet data type

consumerPacketData.GetBytes(),
1,
types.ConsumerPortID, // Source port
channelIDToDestChain, // Source channel
Expand Down
12 changes: 2 additions & 10 deletions x/ccv/types/ccv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (vsc ValidatorSetChangePacketData) ValidateBasic() error {
return nil
}

// GetBytes marshals the ValidatorSetChangePacketData into JSON string bytes
// to be sent over the wire with IBC.
func (vsc ValidatorSetChangePacketData) GetBytes() []byte {
valUpdateBytes := ModuleCdc.MustMarshalJSON(&vsc)
return valUpdateBytes
Expand All @@ -48,11 +50,6 @@ func (mat VSCMaturedPacketData) ValidateBasic() error {
return nil
}

func (mat VSCMaturedPacketData) GetBytes() []byte {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This and the other GetBytes methods should never be used. VSCMatured and SlashPacket data are both wrapped as ConsumerPacketData when sent over the wire. The ConsumerPacketData.GetBytes() method should be used and is tested in this PR

bytes := ModuleCdc.MustMarshalJSON(&mat)
return bytes
}

func NewSlashPacketData(validator abci.Validator, valUpdateId uint64, infractionType stakingtypes.Infraction) *SlashPacketData {
return &SlashPacketData{
Validator: validator,
Expand Down Expand Up @@ -90,11 +87,6 @@ func (vdt SlashPacketData) ValidateBasic() error {
return nil
}

func (vdt SlashPacketData) GetBytes() []byte {
valDowntimeBytes := ModuleCdc.MustMarshalJSON(&vdt)
return valDowntimeBytes
}

func (vdt SlashPacketData) ToV1() *SlashPacketDataV1 {
return NewSlashPacketDataV1(vdt.Validator, vdt.ValsetUpdateId, vdt.Infraction)
}
Expand Down
135 changes: 133 additions & 2 deletions x/ccv/types/ccv_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package types_test

import (
"strings"
"testing"

abci "github.com/cometbft/cometbft/abci/types"

Check failure on line 7 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default,blank,dot,prefix(cosmossdk.io),prefix(github.com/cosmos/cosmos-sdk),prefix(github.com/cometbft/cometbft),prefix(github.com/cosmos/interchain-security) (gci)
"github.com/stretchr/testify/require"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

Check failure on line 13 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default,blank,dot,prefix(cosmossdk.io),prefix(github.com/cosmos/cosmos-sdk),prefix(github.com/cometbft/cometbft),prefix(github.com/cosmos/interchain-security) (gci)
abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/interchain-security/v3/testutil/crypto"
"github.com/cosmos/interchain-security/v3/x/ccv/types"
)

Expand Down Expand Up @@ -93,3 +95,132 @@
require.Nil(t, err)
require.Equal(t, vpd, recovered, "unmarshaled packet data does not equal original value")
}

// TestVSCPacketDataWireBytes is a regression test that the JSON schema
// for ValidatorSetChangePacketData (sent over the wire) does not change.
func TestVSCPacketDataWireBytes(t *testing.T) {

Check failure on line 102 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
cId1 := crypto.NewCryptoIdentityFromIntSeed(4732894)
cId2 := crypto.NewCryptoIdentityFromIntSeed(4732895)

pd := types.NewValidatorSetChangePacketData(
[]abci.ValidatorUpdate{
{
PubKey: cId1.TMProtoCryptoPublicKey(),
Power: 30,
},
{
PubKey: cId2.TMProtoCryptoPublicKey(),
Power: 20,
},
},
73,
[]string{"slash", "acks", "example"},
)

jsonBz := pd.GetBytes()
str := string(jsonBz)

// Expected string formatted for human readability
expectedStr := `{
"validator_updates": [
{
"pub_key": {
"ed25519": "SMxP2pXAuxQC7FmBn4dh4Kt5eYdQFWC/wN7oWobZKds="
},
"power": "30"
},
{
"pub_key": {
"ed25519": "J/nGy0vCXhgVbr8S71B4ZgHi4fsMqtDxDlERZ+gG238="
},
"power": "20"
}
],
"valset_update_id": "73",
"slash_acks": ["slash", "acks", "example"]
}`

// Remove newlines, tabs, and spaces for comparison
expectedStr = strings.ReplaceAll(expectedStr, "\n", "")
expectedStr = strings.ReplaceAll(expectedStr, "\t", "")
expectedStr = strings.ReplaceAll(expectedStr, " ", "")

require.Equal(t, expectedStr, str)
}

// TestSlashPacketDataWireBytes is a regression test that the JSON schema
// for SlashPacketData (sent over the wire) does not change.
func TestSlashPacketDataWireBytes(t *testing.T) {

Check failure on line 155 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
// Construct consumer packet data wrapping slash packet data
cId := crypto.NewCryptoIdentityFromIntSeed(4732894342)
slashPacketData := types.NewSlashPacketData(
abci.Validator{Address: cId.SDKValConsAddress(),

Check failure on line 159 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
Power: int64(4328)},
uint64(894732),
stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN,
)

// The type that'd be JSON marshaled and sent over the wire
cpd := types.NewConsumerPacketData(
types.SlashPacket,
&types.ConsumerPacketData_SlashPacketData{
SlashPacketData: slashPacketData,
},
)

jsonBz := cpd.GetBytes()
str := string(jsonBz)

// Expected string formatted for human readability
expectedStr := `{
"type": "CONSUMER_PACKET_TYPE_SLASH",
"slashPacketData": {
"validator": {
"address": "BP9q4oXCgubvoujOKyxIxd+3IwM=",
"power": "4328"
},
"valset_update_id": "894732",
"infraction": "INFRACTION_TYPE_DOUBLE_SIGN"
}
}`

// Remove newlines, tabs, and spaces for comparison
expectedStr = strings.ReplaceAll(expectedStr, "\n", "")
expectedStr = strings.ReplaceAll(expectedStr, "\t", "")
expectedStr = strings.ReplaceAll(expectedStr, " ", "")

require.Equal(t, expectedStr, str)
}

// TestVSCMaturedPacketDataWireBytes is a regression test that the JSON schema
// for VSCMaturedPacketData (sent over the wire) does not change.
func TestVSCMaturedPacketDataWireBytes(t *testing.T) {

Check failure on line 200 in x/ccv/types/ccv_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
// Construct consumer packet data wrapping vsc matured packet data
cpd := types.ConsumerPacketData{
Type: types.VscMaturedPacket,
Data: &types.ConsumerPacketData_VscMaturedPacketData{
VscMaturedPacketData: types.NewVSCMaturedPacketData(84923),
},
}

jsonBz := cpd.GetBytes()
str := string(jsonBz)

// Expected string formatted for human readability
expectedStr := `{
"type": "CONSUMER_PACKET_TYPE_VSCM",
"vscMaturedPacketData": {
"valset_update_id": "84923"
}
}`

// Remove newlines, tabs, and spaces for comparison
expectedStr = strings.ReplaceAll(expectedStr, "\n", "")
expectedStr = strings.ReplaceAll(expectedStr, "\t", "")
expectedStr = strings.ReplaceAll(expectedStr, " ", "")

require.Equal(t, expectedStr, str)
}
Loading