Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(x/auth): properly populate tx config options and deprecate ProtoCodecMarshaler (backport #17946) #17952

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
appBuilder *runtime.AppBuilder
txConfig client.TxConfig
cdc codec.Codec
pcdc codec.ProtoCodecMarshaler
interfaceRegistry codectypes.InterfaceRegistry
err error
)
Expand All @@ -98,7 +97,6 @@ func TestBaseApp_BlockGas(t *testing.T) {
&interfaceRegistry,
&txConfig,
&cdc,
&pcdc,
&appBuilder)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestMsgService(t *testing.T) {

var (
appBuilder *runtime.AppBuilder
cdc codec.ProtoCodecMarshaler
cdc codec.Codec
interfaceRegistry codectypes.InterfaceRegistry
)
err := depinject.Inject(
Expand Down
11 changes: 8 additions & 3 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ func (f Factory) WithExtensionOptions(extOpts ...*codectypes.Any) Factory {
func (f Factory) BuildUnsignedTx(msgs ...sdk.Msg) (client.TxBuilder, error) {
if f.offline && f.generateOnly {
if f.chainID != "" {
return nil, fmt.Errorf("chain ID cannot be used when offline and generate-only flags are set")
return nil, errors.New("chain ID cannot be used when offline and generate-only flags are set")
}
} else if f.chainID == "" {
return nil, fmt.Errorf("chain ID required but not specified")
return nil, errors.New("chain ID required but not specified")
}

fees := f.fees
Expand Down Expand Up @@ -370,7 +370,12 @@ func (f Factory) PrintUnsignedTx(clientCtx client.Context, msgs ...sdk.Msg) erro
return err
}

json, err := clientCtx.TxConfig.TxJSONEncoder()(unsignedTx.GetTx())
encoder := f.txConfig.TxJSONEncoder()
if encoder == nil {
return errors.New("cannot print unsigned tx: tx json encoder is nil")
}

json, err := encoder(unsignedTx.GetTx())
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
}

if !clientCtx.SkipConfirm {
txBytes, err := clientCtx.TxConfig.TxJSONEncoder()(tx.GetTx())
encoder := txf.txConfig.TxJSONEncoder()
if encoder == nil {
return errors.New("failed to encode transaction: tx json encoder is nil")
}

txBytes, err := encoder(tx.GetTx())
if err != nil {
return err
}
Expand Down
7 changes: 2 additions & 5 deletions codec/proto_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

// ProtoCodecMarshaler defines an interface for codecs that utilize Protobuf for both
// binary and JSON encoding.
// Deprecated: Use Codec instead.
type ProtoCodecMarshaler interface {
Codec
InterfaceRegistry() types.InterfaceRegistry
}

// ProtoCodec defines a codec that utilizes Protobuf for both binary and JSON
Expand All @@ -34,10 +34,7 @@ type ProtoCodec struct {
interfaceRegistry types.InterfaceRegistry
}

var (
_ Codec = &ProtoCodec{}
_ ProtoCodecMarshaler = &ProtoCodec{}
)
var _ Codec = (*ProtoCodec)(nil)

// NewProtoCodec returns a reference to a new ProtoCodec
func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec {
Expand Down
3 changes: 1 addition & 2 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func ProvideApp(interfaceRegistry codectypes.InterfaceRegistry) (
codec.Codec,
*codec.LegacyAmino,
*AppBuilder,
codec.ProtoCodecMarshaler,
*baseapp.MsgServiceRouter,
appmodule.AppModule,
protodesc.Resolver,
Expand Down Expand Up @@ -119,7 +118,7 @@ func ProvideApp(interfaceRegistry codectypes.InterfaceRegistry) (
}
appBuilder := &AppBuilder{app}

return cdc, amino, appBuilder, cdc, msgServiceRouter, appModule{app}, protoFiles, protoTypes, nil
return cdc, amino, appBuilder, msgServiceRouter, appModule{app}, protoFiles, protoTypes, nil
}

type AppInputs struct {
Expand Down
12 changes: 8 additions & 4 deletions x/auth/tx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type config struct {
encoder sdk.TxEncoder
jsonDecoder sdk.TxDecoder
jsonEncoder sdk.TxEncoder
protoCodec codec.ProtoCodecMarshaler
protoCodec codec.Codec
signingContext *txsigning.Context
}

Expand Down Expand Up @@ -73,7 +73,7 @@ var DefaultSignModes = []signingtypes.SignMode{
// We prefer to use depinject to provide client.TxConfig, but we permit this constructor usage. Within the SDK,
// this constructor is primarily used in tests, but also sees usage in app chains like:
// https://github.com/evmos/evmos/blob/719363fbb92ff3ea9649694bd088e4c6fe9c195f/encoding/config.go#L37
func NewTxConfig(protoCodec codec.ProtoCodecMarshaler, enabledSignModes []signingtypes.SignMode,
func NewTxConfig(protoCodec codec.Codec, enabledSignModes []signingtypes.SignMode,
customSignModes ...txsigning.SignModeHandler,
) client.TxConfig {
txConfig, err := NewTxConfigWithOptions(protoCodec, ConfigOptions{
Expand Down Expand Up @@ -165,9 +165,13 @@ func NewSigningHandlerMap(configOptions ConfigOptions) (*txsigning.HandlerMap, e

// NewTxConfigWithOptions returns a new protobuf TxConfig using the provided ProtoCodec, ConfigOptions and
// custom sign mode handlers. If ConfigOptions is an empty struct then default values will be used.
func NewTxConfigWithOptions(protoCodec codec.ProtoCodecMarshaler, configOptions ConfigOptions) (client.TxConfig, error) {
func NewTxConfigWithOptions(protoCodec codec.Codec, configOptions ConfigOptions) (client.TxConfig, error) {
txConfig := &config{
protoCodec: protoCodec,
protoCodec: protoCodec,
decoder: configOptions.ProtoDecoder,
encoder: configOptions.ProtoEncoder,
jsonDecoder: configOptions.JSONDecoder,
jsonEncoder: configOptions.JSONEncoder,
}
if configOptions.ProtoDecoder == nil {
txConfig.decoder = DefaultTxDecoder(protoCodec)
Expand Down
4 changes: 2 additions & 2 deletions x/auth/tx/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ModuleInputs struct {
Config *txconfigv1.Config
AddressCodec address.Codec
ValidatorAddressCodec runtime.ValidatorAddressCodec
ProtoCodecMarshaler codec.ProtoCodecMarshaler
Codec codec.Codec
ProtoFileResolver txsigning.ProtoFileResolver
// BankKeeper is the expected bank keeper to be passed to AnteHandlers
BankKeeper authtypes.BankKeeper `optional:"true"`
Expand Down Expand Up @@ -87,7 +87,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
txConfigOptions.TextualCoinMetadataQueryFn = NewBankKeeperCoinMetadataQueryFn(in.MetadataBankKeeper)
}

txConfig, err := tx.NewTxConfigWithOptions(in.ProtoCodecMarshaler, txConfigOptions)
txConfig, err := tx.NewTxConfigWithOptions(in.Codec, txConfigOptions)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/tx/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// DefaultTxDecoder returns a default protobuf TxDecoder using the provided Marshaler.
func DefaultTxDecoder(cdc codec.ProtoCodecMarshaler) sdk.TxDecoder {
func DefaultTxDecoder(cdc codec.Codec) sdk.TxDecoder {
return func(txBytes []byte) (sdk.Tx, error) {
// Make sure txBytes follow ADR-027.
err := rejectNonADR027TxRaw(txBytes)
Expand Down Expand Up @@ -79,7 +79,7 @@ func DefaultTxDecoder(cdc codec.ProtoCodecMarshaler) sdk.TxDecoder {
}

// DefaultJSONTxDecoder returns a default protobuf JSON TxDecoder using the provided Marshaler.
func DefaultJSONTxDecoder(cdc codec.ProtoCodecMarshaler) sdk.TxDecoder {
func DefaultJSONTxDecoder(cdc codec.Codec) sdk.TxDecoder {
return func(txBytes []byte) (sdk.Tx, error) {
var theTx tx.Tx
err := cdc.UnmarshalJSON(txBytes, &theTx)
Expand Down
2 changes: 1 addition & 1 deletion x/auth/tx/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DefaultTxEncoder() sdk.TxEncoder {
}

// DefaultJSONTxEncoder returns a default protobuf JSON TxEncoder using the provided Marshaler.
func DefaultJSONTxEncoder(cdc codec.ProtoCodecMarshaler) sdk.TxEncoder {
func DefaultJSONTxEncoder(cdc codec.Codec) sdk.TxEncoder {
return func(tx sdk.Tx) ([]byte, error) {
txWrapper, ok := tx.(*wrapper)
if ok {
Expand Down