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

fix: infraction marshaling between sdk 45 and 47 #1089

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions x/ccv/consumer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper"
consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types"
"github.com/cosmos/interchain-security/v3/x/ccv/types"
ccv "github.com/cosmos/interchain-security/v3/x/ccv/types"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -307,3 +308,20 @@ func TestSendPacketsFailure(t *testing.T) {
consumerKeeper.SendPackets(ctx)
require.Equal(t, 3, len(consumerKeeper.GetPendingPackets(ctx).List))
}

func TestSlashPacketDataJSON(t *testing.T) {
spd := types.NewSlashPacketData(
abci.Validator{Address: bytes.HexBytes{}, Power: int64(1)},
uint64(1),
stakingtypes.Infraction_INFRACTION_DOWNTIME,
)
cpd := ccv.ConsumerPacketData{
Type: ccv.SlashPacket,
Data: &ccv.ConsumerPacketData_SlashPacketData{
SlashPacketData: spd,
},
}
bz := cpd.GetBytes()
str := string(bz)
fmt.Println(str)
}
35 changes: 35 additions & 0 deletions x/ccv/types/ccv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package types

import (
"encoding/json"
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -101,5 +103,38 @@ func (cp ConsumerPacketData) ValidateBasic() (err error) {

func (cp ConsumerPacketData) GetBytes() []byte {
bytes := ModuleCdc.MustMarshalJSON(&cp)

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 shim only works when the provider is on 45, we could also add an appropriate shim on the provider on it's 47 version

if cp.Type == SlashPacket {
type Validator struct {
Address string `json:"address"`
Power string `json:"power"`
}
type SlashPacketData struct {
Validator Validator `json:"validator"`
ValsetUpdateID string `json:"valset_update_id"`
// Same as protbuf generated type, but infraction is string instead of enum
Infraction string `json:"infraction"`
}
type NewSchema struct {
Type string `json:"type"`
SlashPacketData SlashPacketData `json:"slashPacketData"`
}
// Unmarshal the JSON string into the struct
var data NewSchema
err := json.Unmarshal(bytes, &data)
if err != nil {
panic(err)
}

// Modify the value of the "infraction" field By adding "TYPE" after first underscore
data.SlashPacketData.Infraction = strings.Replace(
data.SlashPacketData.Infraction, "_", "_TYPE_", 1)

// Replace bytes with modified JSON string
bytes, err = json.Marshal(data)
if err != nil {
return nil
}
}
return bytes
}