Skip to content

Commit

Permalink
refactor: remove encoding funcs for DenomTrace (#6411)
Browse files Browse the repository at this point in the history
* refactor: remove encoding funcs for DenomTrace

* lint
  • Loading branch information
colin-axner authored May 29, 2024
1 parent e07599e commit d96d8e4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 56 deletions.
36 changes: 0 additions & 36 deletions modules/apps/transfer/keeper/encoding.go

This file was deleted.

6 changes: 3 additions & 3 deletions modules/apps/transfer/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (k Keeper) DenomTraces(c context.Context, req *types.QueryDenomTracesReques
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DenomTraceKey)

pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
result, err := k.UnmarshalDenomTrace(value)
if err != nil {
var denomTrace types.DenomTrace
if err := k.cdc.Unmarshal(value, &denomTrace); err != nil {
return err
}

traces = append(traces, result)
traces = append(traces, denomTrace)
return nil
})
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (k Keeper) GetDenomTrace(ctx sdk.Context, denomTraceHash cmtbytes.HexBytes)
return types.DenomTrace{}, false
}

denomTrace := k.MustUnmarshalDenomTrace(bz)
var denomTrace types.DenomTrace
k.cdc.MustUnmarshal(bz, &denomTrace)

return denomTrace, true
}

Expand All @@ -165,7 +167,8 @@ func (k Keeper) HasDenomTrace(ctx sdk.Context, denomTraceHash cmtbytes.HexBytes)
// SetDenomTrace sets a new {trace hash -> denom trace} pair to the store.
func (k Keeper) SetDenomTrace(ctx sdk.Context, denomTrace types.DenomTrace) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DenomTraceKey)
bz := k.MustMarshalDenomTrace(denomTrace)
bz := k.cdc.MustMarshal(&denomTrace)

store.Set(denomTrace.Hash(), bz)
}

Expand All @@ -188,7 +191,9 @@ func (k Keeper) IterateDenomTraces(ctx sdk.Context, cb func(denomTrace types.Den

defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() })
for ; iterator.Valid(); iterator.Next() {
denomTrace := k.MustUnmarshalDenomTrace(iterator.Value())
var denomTrace types.DenomTrace
k.cdc.MustUnmarshal(iterator.Value(), &denomTrace)

if cb(denomTrace) {
break
}
Expand Down
4 changes: 2 additions & 2 deletions modules/apps/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func (AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Weight
}

// RegisterStoreDecoder registers a decoder for transfer module's types
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore(am.keeper)
func (AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[types.StoreKey] = simulation.NewDecodeStore()
}

// WeightedOperations returns the all the transfer module operations with their respective weights.
Expand Down
12 changes: 4 additions & 8 deletions modules/apps/transfer/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ import (
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
)

// TransferUnmarshaler defines the expected encoding store functions.
type TransferUnmarshaler interface {
MustUnmarshalDenomTrace([]byte) types.DenomTrace
}

// NewDecodeStore returns a decoder function closure that unmarshals the KVPair's
// Value to the corresponding DenomTrace type.
func NewDecodeStore(cdc TransferUnmarshaler) func(kvA, kvB kv.Pair) string {
func NewDecodeStore() func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.PortKey):
return fmt.Sprintf("Port A: %s\nPort B: %s", string(kvA.Value), string(kvB.Value))

case bytes.Equal(kvA.Key[:1], types.DenomTraceKey):
denomTraceA := cdc.MustUnmarshalDenomTrace(kvA.Value)
denomTraceB := cdc.MustUnmarshalDenomTrace(kvB.Value)
var denomTraceA, denomTraceB types.DenomTrace
types.ModuleCdc.MustUnmarshal(kvA.Value, &denomTraceA)
types.ModuleCdc.MustUnmarshal(kvB.Value, &denomTraceB)
return fmt.Sprintf("DenomTrace A: %s\nDenomTrace B: %s", denomTraceA.IBCDenom(), denomTraceB.IBCDenom())

default:
Expand Down
6 changes: 2 additions & 4 deletions modules/apps/transfer/simulation/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import (

"github.com/cosmos/ibc-go/v8/modules/apps/transfer/simulation"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/cosmos/ibc-go/v8/testing/simapp"
)

func TestDecodeStore(t *testing.T) {
app := simapp.Setup(t, false)
dec := simulation.NewDecodeStore(app.TransferKeeper)
dec := simulation.NewDecodeStore()

trace := types.DenomTrace{
BaseDenom: "uatom",
Expand All @@ -30,7 +28,7 @@ func TestDecodeStore(t *testing.T) {
},
{
Key: types.DenomTraceKey,
Value: app.TransferKeeper.MustMarshalDenomTrace(trace),
Value: types.ModuleCdc.MustMarshal(&trace),
},
{
Key: []byte{0x99},
Expand Down

0 comments on commit d96d8e4

Please sign in to comment.