Skip to content

Commit

Permalink
fix(delayedack): malformed trackingPacketKey by ensuring base64 encod…
Browse files Browse the repository at this point in the history
…ing/decoding during Genesis export/import (#1668)
  • Loading branch information
NagaTulasi authored Dec 24, 2024
1 parent afc677f commit 12bfcad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
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 @@ import (
// 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)

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 InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
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")
}

0 comments on commit 12bfcad

Please sign in to comment.