Skip to content

Commit

Permalink
chore: move generic txdecoder (backport #22653) (#22654)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <marko@baricevic.me>
  • Loading branch information
mergify[bot] and tac0turtle authored Nov 26, 2024
1 parent d52f5e8 commit 26e869e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
43 changes: 43 additions & 0 deletions client/tx_config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
43 changes: 1 addition & 42 deletions simapp/v2/simdv2/cmd/commands.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"io"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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](),
Expand Down Expand Up @@ -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
}

0 comments on commit 26e869e

Please sign in to comment.