chore: move generic txdecoder (backport #22653) (#22654)

Co-authored-by: Marko <marko@baricevic.me>
This commit is contained in:
mergify[bot]
2024-11-26 15:50:43 +01:00
committed by GitHub
co-authored by Marko
parent d52f5e8642
commit 26e869e112
2 changed files with 44 additions and 42 deletions
+43
View File
@@ -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
}
+1 -42
View File
@@ -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
}