From f153426ab2c0caa1e644218800483c620be41ccd Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 26 Nov 2024 15:25:08 +0100 Subject: [PATCH] chore: move generic txdecoder (#22653) --- client/tx_config.go | 43 ++++++++++++++++++++++++++++++++ simapp/v2/simdv2/cmd/commands.go | 43 +------------------------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/client/tx_config.go b/client/tx_config.go index f96cc9ddea..e5751343e9 100644 --- a/client/tx_config.go +++ b/client/tx_config.go @@ -1,8 +1,10 @@ package client import ( + "errors" "time" + "cosmossdk.io/core/transaction" txsigning "cosmossdk.io/x/tx/signing" codectypes "github.com/cosmos/cosmos-sdk/codec/types" @@ -68,3 +70,44 @@ type ( SetExtensionOptions(extOpts ...*codectypes.Any) } ) + +var _ transaction.Codec[transaction.Tx] = &DefaultTxDecoder[transaction.Tx]{} + +// DefaultTxDecoder is a generic transaction decoder that implements the transaction.Codec interface. +type DefaultTxDecoder[T transaction.Tx] struct { + TxConfig TxConfig +} + +// Decode decodes a binary transaction into type T using the TxConfig's TxDecoder. +func (t *DefaultTxDecoder[T]) Decode(bz []byte) (T, error) { + var out T + tx, err := t.TxConfig.TxDecoder()(bz) + if err != nil { + return out, err + } + + var ok bool + out, ok = tx.(T) + if !ok { + return out, errors.New("unexpected Tx type") + } + + return out, nil +} + +// DecodeJSON decodes a JSON transaction into type T using the TxConfig's TxJSONDecoder. +func (t *DefaultTxDecoder[T]) DecodeJSON(bz []byte) (T, error) { + var out T + tx, err := t.TxConfig.TxJSONDecoder()(bz) + if err != nil { + return out, err + } + + var ok bool + out, ok = tx.(T) + if !ok { + return out, errors.New("unexpected Tx type") + } + + return out, nil +} diff --git a/simapp/v2/simdv2/cmd/commands.go b/simapp/v2/simdv2/cmd/commands.go index 9fb41250ec..1edd3052ec 100644 --- a/simapp/v2/simdv2/cmd/commands.go +++ b/simapp/v2/simdv2/cmd/commands.go @@ -1,7 +1,6 @@ package cmd import ( - "errors" "io" "github.com/spf13/cobra" @@ -108,7 +107,7 @@ func InitRootCmd[T transaction.Tx]( simApp.Store(), simApp.App.AppManager, simApp.AppCodec(), - &genericTxDecoder[T]{deps.TxConfig}, + &client.DefaultTxDecoder[T]{TxConfig: deps.TxConfig}, simApp.App.QueryHandlers(), simApp.App.SchemaDecoderResolver(), initCometOptions[T](), @@ -243,43 +242,3 @@ func RootCommandPersistentPreRun(clientCtx client.Context) func(*cobra.Command, return nil } } - -var _ transaction.Codec[transaction.Tx] = &genericTxDecoder[transaction.Tx]{} - -type genericTxDecoder[T transaction.Tx] struct { - txConfig client.TxConfig -} - -// Decode implements transaction.Codec. -func (t *genericTxDecoder[T]) Decode(bz []byte) (T, error) { - var out T - tx, err := t.txConfig.TxDecoder()(bz) - if err != nil { - return out, err - } - - var ok bool - out, ok = tx.(T) - if !ok { - return out, errors.New("unexpected Tx type") - } - - return out, nil -} - -// DecodeJSON implements transaction.Codec. -func (t *genericTxDecoder[T]) DecodeJSON(bz []byte) (T, error) { - var out T - tx, err := t.txConfig.TxJSONDecoder()(bz) - if err != nil { - return out, err - } - - var ok bool - out, ok = tx.(T) - if !ok { - return out, errors.New("unexpected Tx type") - } - - return out, nil -}