From d96d8e4a08bcc6b0d93371e3bbb039b7e6480098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 29 May 2024 10:38:45 +0200 Subject: [PATCH] refactor: remove encoding funcs for DenomTrace (#6411) * refactor: remove encoding funcs for DenomTrace * lint --- modules/apps/transfer/keeper/encoding.go | 36 ------------------- modules/apps/transfer/keeper/grpc_query.go | 6 ++-- modules/apps/transfer/keeper/keeper.go | 11 ++++-- modules/apps/transfer/module.go | 4 +-- modules/apps/transfer/simulation/decoder.go | 12 +++---- .../apps/transfer/simulation/decoder_test.go | 6 ++-- 6 files changed, 19 insertions(+), 56 deletions(-) delete mode 100644 modules/apps/transfer/keeper/encoding.go diff --git a/modules/apps/transfer/keeper/encoding.go b/modules/apps/transfer/keeper/encoding.go deleted file mode 100644 index 5ba0723878d..00000000000 --- a/modules/apps/transfer/keeper/encoding.go +++ /dev/null @@ -1,36 +0,0 @@ -package keeper - -import ( - "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types" -) - -// UnmarshalDenomTrace attempts to decode and return an DenomTrace object from -// raw encoded bytes. -func (k Keeper) UnmarshalDenomTrace(bz []byte) (types.DenomTrace, error) { - var denomTrace types.DenomTrace - if err := k.cdc.Unmarshal(bz, &denomTrace); err != nil { - return types.DenomTrace{}, err - } - - return denomTrace, nil -} - -// MustUnmarshalDenomTrace attempts to decode and return an DenomTrace object from -// raw encoded bytes. It panics on error. -func (k Keeper) MustUnmarshalDenomTrace(bz []byte) types.DenomTrace { - var denomTrace types.DenomTrace - k.cdc.MustUnmarshal(bz, &denomTrace) - return denomTrace -} - -// MarshalDenomTrace attempts to encode an DenomTrace object and returns the -// raw encoded bytes. -func (k Keeper) MarshalDenomTrace(denomTrace types.DenomTrace) ([]byte, error) { - return k.cdc.Marshal(&denomTrace) -} - -// MustMarshalDenomTrace attempts to encode an DenomTrace object and returns the -// raw encoded bytes. It panics on error. -func (k Keeper) MustMarshalDenomTrace(denomTrace types.DenomTrace) []byte { - return k.cdc.MustMarshal(&denomTrace) -} diff --git a/modules/apps/transfer/keeper/grpc_query.go b/modules/apps/transfer/keeper/grpc_query.go index 57091453fb7..0e27d2e1c48 100644 --- a/modules/apps/transfer/keeper/grpc_query.go +++ b/modules/apps/transfer/keeper/grpc_query.go @@ -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 { diff --git a/modules/apps/transfer/keeper/keeper.go b/modules/apps/transfer/keeper/keeper.go index 6ee9ee7edfe..644ed0c7b6e 100644 --- a/modules/apps/transfer/keeper/keeper.go +++ b/modules/apps/transfer/keeper/keeper.go @@ -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 } @@ -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) } @@ -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 } diff --git a/modules/apps/transfer/module.go b/modules/apps/transfer/module.go index 1e2852bdb5b..8e9bf1d54f8 100644 --- a/modules/apps/transfer/module.go +++ b/modules/apps/transfer/module.go @@ -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. diff --git a/modules/apps/transfer/simulation/decoder.go b/modules/apps/transfer/simulation/decoder.go index bcd8d8c2c73..c224b76cfd4 100644 --- a/modules/apps/transfer/simulation/decoder.go +++ b/modules/apps/transfer/simulation/decoder.go @@ -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: diff --git a/modules/apps/transfer/simulation/decoder_test.go b/modules/apps/transfer/simulation/decoder_test.go index 3d273e2c7dc..29a77ced412 100644 --- a/modules/apps/transfer/simulation/decoder_test.go +++ b/modules/apps/transfer/simulation/decoder_test.go @@ -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", @@ -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},