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 malformed tracking_packet_key by ensuring base64 encoding/decoding during Genesis export/import #1668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions x/eibc/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package eibc

import (
"encoding/base64"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension/v3/x/eibc/keeper"
"github.com/dymensionxyz/dymension/v3/x/eibc/types"
Expand All @@ -9,10 +12,20 @@
// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
// Add the demand orders

for _, demandOrder := range genState.DemandOrders {
// Create a copy of demandOrder to avoid reusing the same memory address
demandOrderCopy := demandOrder

// Decode base64 tracking_packet_key if it exists
if demandOrderCopy.TrackingPacketKey != "" {
decodedKey, err := base64.StdEncoding.DecodeString(demandOrderCopy.TrackingPacketKey)
if err != nil {
panic(fmt.Errorf("failed to decode tracking_packet_key: %v", err))

Check failure on line 24 in x/eibc/genesis.go

View workflow job for this annotation

GitHub Actions / golangci-lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
demandOrderCopy.TrackingPacketKey = string(decodedKey)
}

err := k.SetDemandOrder(ctx, &demandOrderCopy)
if err != nil {
panic(err)
Expand All @@ -24,14 +37,25 @@
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)

// Add the demand orders
allDemandOrders, err := k.ListAllDemandOrders(ctx)
if err != nil {
panic(err)
}

genesis.DemandOrders = make([]types.DemandOrder, len(allDemandOrders))
for i, order := range allDemandOrders {
genesis.DemandOrders[i] = *order
// Create a copy to avoid modifying the original
orderCopy := *order

// Base64 encode tracking_packet_key if it exists
if orderCopy.TrackingPacketKey != "" {
encodedKey := base64.StdEncoding.EncodeToString([]byte(orderCopy.TrackingPacketKey))
orderCopy.TrackingPacketKey = encodedKey
}

genesis.DemandOrders[i] = orderCopy
}

return genesis
Expand Down
20 changes: 17 additions & 3 deletions x/eibc/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eibc_test

import (
"encoding/base64"
"testing"

"cosmossdk.io/math"
Expand Down Expand Up @@ -74,15 +75,28 @@ func TestExportGenesis(t *testing.T) {
TrackingPacketStatus: commontypes.Status_PENDING,
},
}

for _, demandOrder := range demandOrders {
demandOrderCopy := demandOrder
err := k.SetDemandOrder(ctx, &demandOrderCopy)
require.NoError(t, err)
}

k.SetParams(ctx, params)
// Verify the exported genesis
got := eibc.ExportGenesis(ctx, *k)
require.NotNil(t, got)
require.ElementsMatch(t, demandOrders, got.DemandOrders)
require.Equal(t, params, got.Params)

require.NotNil(t, got, "ExportGenesis should not return nil")

require.Equal(t, params, got.Params, "Params should match the set params")

expectedDemandOrders := make([]types.DemandOrder, len(demandOrders))
for i, order := range demandOrders {
orderCopy := order
encodedKey := base64.StdEncoding.EncodeToString([]byte(order.TrackingPacketKey))
orderCopy.TrackingPacketKey = encodedKey
expectedDemandOrders[i] = orderCopy
}

require.ElementsMatch(t, expectedDemandOrders, got.DemandOrders, "DemandOrders should match after encoding TrackingPacketKey")
}
Loading