Skip to content

Commit

Permalink
update: NewTxConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Feb 29, 2024
1 parent 03b9b0f commit 99b6d57
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 44 deletions.
6 changes: 4 additions & 2 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,10 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_NoOpMempoolTxSelection()
// create a codec for marshaling
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())
signingCtx := cdc.InterfaceRegistry().SigningContext()

// create a baseapp along with a tx config for tx generation
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
app := baseapp.NewBaseApp(s.T().Name(), log.NewNopLogger(), dbm.NewMemDB(), txConfig.TxDecoder())

// create a proposal handler
Expand Down Expand Up @@ -422,7 +423,8 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_NoOpMempoolTxSelection()
func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_PriorityNonceMempoolTxSelection() {
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
signingCtx := cdc.InterfaceRegistry().SigningContext()
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)

var (
secret1 = []byte("secret1")
Expand Down
9 changes: 5 additions & 4 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -65,8 +64,9 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
t.Helper()
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())
signingCtx := cdc.InterfaceRegistry().SigningContext()

txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
db := dbm.NewMemDB()
logBuffer := new(bytes.Buffer)
logger := log.NewLogger(logBuffer, log.ColorOption(false))
Expand Down Expand Up @@ -499,11 +499,12 @@ func TestBaseAppOptionSeal(t *testing.T) {
}

func TestTxDecoder(t *testing.T) {
cdc := codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())
signingCtx := cdc.InterfaceRegistry().SigningContext()

// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)

tx := newTxCounter(t, txConfig, 1, 0)
txBytes, err := txConfig.TxEncoder()(tx)
Expand Down
3 changes: 2 additions & 1 deletion baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ func TestMsgService(t *testing.T) {
), &appBuilder, &cdc, &interfaceRegistry)
require.NoError(t, err)
app := appBuilder.Build(dbm.NewMemDB(), nil)
signingCtx := interfaceRegistry.SigningContext()

// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
// set the TxDecoder in the BaseApp for minimal tx simulations
app.SetTxDecoder(txConfig.TxDecoder())

Expand Down
2 changes: 1 addition & 1 deletion client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ type Context struct {
ValidatorAddressCodec address.Codec
ConsensusAddressCodec address.Codec

// Bech32 address prefixes
// Bech32 address prefixes.
AddressPrefix string
ValidatorPrefix string
}
Expand Down
4 changes: 3 additions & 1 deletion client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (

func newTestTxConfig() (client.TxConfig, codec.Codec) {
encodingConfig := moduletestutil.MakeTestEncodingConfig()
return authtx.NewTxConfig(codec.NewProtoCodec(encodingConfig.InterfaceRegistry), authtx.DefaultSignModes, "cosmos", "cosmosvaloper"), encodingConfig.Codec
cdc := codec.NewProtoCodec(encodingConfig.InterfaceRegistry)
signingCtx := cdc.InterfaceRegistry().SigningContext()
return authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes), encodingConfig.Codec
}

// mockContext is a mock client.Context to return arbitrary simulation response, used to
Expand Down
9 changes: 6 additions & 3 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func NewSimApp(
})
appCodec := codec.NewProtoCodec(interfaceRegistry)
legacyAmino := codec.NewLegacyAmino()
txConfig := authtx.NewTxConfig(appCodec, authtx.DefaultSignModes, sdk.GetConfig().GetBech32AccountAddrPrefix(), sdk.GetConfig().GetBech32ValidatorAddrPrefix())
signingCtx := interfaceRegistry.SigningContext()
txConfig := authtx.NewTxConfig(appCodec, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)

std.RegisterLegacyAminoCodec(legacyAmino)
std.RegisterInterfaces(interfaceRegistry)
Expand Down Expand Up @@ -315,8 +316,10 @@ func NewSimApp(
txConfigOpts := authtx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewBankKeeperCoinMetadataQueryFn(app.BankKeeper),
AddressPrefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
ValidatorPrefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
SigningOptions: &signing.Options{
AddressCodec: signingCtx.AddressCodec(),
ValidatorAddressCodec: signingCtx.ValidatorAddressCodec(),
},
}
txConfig, err = authtx.NewTxConfigWithOptions(
appCodec,
Expand Down
7 changes: 5 additions & 2 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"cosmossdk.io/x/auth/tx"
authtxconfig "cosmossdk.io/x/auth/tx/config"
"cosmossdk.io/x/auth/types"
txsigning "cosmossdk.io/x/tx/signing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
Expand Down Expand Up @@ -82,8 +83,10 @@ func NewRootCmd() *cobra.Command {
txConfigOpts := tx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: authtxconfig.NewGRPCCoinMetadataQueryFn(initClientCtx),
AddressPrefix: sdk.GetConfig().GetBech32AccountAddrPrefix(),
ValidatorPrefix: sdk.GetConfig().GetBech32ValidatorAddrPrefix(),
SigningOptions: &txsigning.Options{
AddressCodec: initClientCtx.InterfaceRegistry.SigningContext().AddressCodec(),
ValidatorAddressCodec: initClientCtx.InterfaceRegistry.SigningContext().ValidatorAddressCodec(),
},
}
txConfig, err := tx.NewTxConfigWithOptions(
initClientCtx.Codec,
Expand Down
8 changes: 6 additions & 2 deletions simapp/simd/cmd/root_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"github.com/spf13/cobra"

authv1 "cosmossdk.io/api/cosmos/auth/module/v1"
stakingv1 "cosmossdk.io/api/cosmos/staking/module/v1"
"cosmossdk.io/client/v2/autocli"
clientv2keyring "cosmossdk.io/client/v2/autocli/keyring"
"cosmossdk.io/core/address"
Expand Down Expand Up @@ -103,6 +105,8 @@ func ProvideClientContext(
addressCodec address.Codec,
validatorAddressCodec runtime.ValidatorAddressCodec,
consensusAddressCodec runtime.ConsensusAddressCodec,
authConfig *authv1.Module,
stakingConfig *stakingv1.Module,
) client.Context {
var err error

Expand All @@ -117,8 +121,8 @@ func ProvideClientContext(
WithConsensusAddressCodec(consensusAddressCodec).
WithHomeDir(simapp.DefaultNodeHome).
WithViper(""). // uses by default the binary name as prefix
WithAddressPrefix(txConfigOpts.AddressPrefix).
WithValidatorPrefix(txConfigOpts.ValidatorPrefix)
WithAddressPrefix(authConfig.Bech32Prefix).
WithValidatorPrefix(stakingConfig.Bech32PrefixValidator)

// Read the config to overwrite the default values with the values from the config file
customClientTemplate, customClientConfig := initClientConfig()
Expand Down
3 changes: 2 additions & 1 deletion testutil/integration/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func NewIntegrationApp(
interfaceRegistry := codectypes.NewInterfaceRegistry()
moduleManager := module.NewManagerFromMap(modules)
moduleManager.RegisterInterfaces(interfaceRegistry)
signingCtx := interfaceRegistry.SigningContext()

txConfig := authtx.NewTxConfig(codec.NewProtoCodec(interfaceRegistry), authtx.DefaultSignModes, "cosmos", "cosmosvaloper")
txConfig := authtx.NewTxConfig(codec.NewProtoCodec(interfaceRegistry), signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
bApp := baseapp.NewBaseApp(appName, logger, db, txConfig.TxDecoder(), baseapp.SetChainID(appName))
bApp.MountKVStores(keys)

Expand Down
3 changes: 2 additions & 1 deletion testutil/sims/simulation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ func SetupSimulation(config simtypes.Config, dirPrefix, dbName string, verbose,
// SimulationOperations retrieves the simulation params from the provided file path
// and returns all the modules weighted operations
func SimulationOperations(app runtime.AppSimI, cdc codec.Codec, config simtypes.Config) []simtypes.WeightedOperation {
signingCtx := cdc.InterfaceRegistry().SigningContext()
simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
TxConfig: authtx.NewTxConfig(cdc, authtx.DefaultSignModes, "cosmos", "cosmosvaloper"), // TODO(tip): we should extract this from app
TxConfig: authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes), // TODO(tip): we should extract this from app
BondDenom: sdk.DefaultBondDenom,
}

Expand Down
10 changes: 6 additions & 4 deletions types/module/testutil/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type TestEncodingConfig struct {
func MakeTestEncodingConfig(modules ...module.AppModule) TestEncodingConfig {
aminoCodec := codec.NewLegacyAmino()
interfaceRegistry := testutil.CodecOptions{}.NewInterfaceRegistry()
codec := codec.NewProtoCodec(interfaceRegistry)
cdc := codec.NewProtoCodec(interfaceRegistry)
signingCtx := interfaceRegistry.SigningContext()

encCfg := TestEncodingConfig{
InterfaceRegistry: interfaceRegistry,
Codec: codec,
TxConfig: tx.NewTxConfig(codec, tx.DefaultSignModes, "cosmos", "cosmosvaloper"),
Codec: cdc,
TxConfig: tx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), tx.DefaultSignModes),
Amino: aminoCodec,
}

Expand All @@ -46,7 +47,8 @@ func MakeTestEncodingConfig(modules ...module.AppModule) TestEncodingConfig {
func MakeTestTxConfig() client.TxConfig {
interfaceRegistry := testutil.CodecOptions{}.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(interfaceRegistry)
return tx.NewTxConfig(cdc, tx.DefaultSignModes, "cosmos", "cosmosvaloper")
signingCtx := interfaceRegistry.SigningContext()
return tx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), tx.DefaultSignModes)
}

type TestBuilderTxConfig struct {
Expand Down
24 changes: 8 additions & 16 deletions x/auth/tx/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tx

import (
"errors"
"fmt"

"cosmossdk.io/core/address"
Expand Down Expand Up @@ -57,10 +58,6 @@ type ConfigOptions struct {
JSONDecoder sdk.TxDecoder
// JSONEncoder is the encoder that will be used to encode json transactions.
JSONEncoder sdk.TxEncoder
// AddressPredix is the bech32 address prefix.
AddressPrefix string
// ValidatorPrefix is the bech32 validator prefix.
ValidatorPrefix string
}

// DefaultSignModes are the default sign modes enabled for protobuf transactions.
Expand All @@ -80,14 +77,15 @@ 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.Codec, enabledSignModes []signingtypes.SignMode, addressPrefix,
validatorPrefix string, customSignModes ...txsigning.SignModeHandler,
func NewTxConfig(protoCodec codec.Codec, addressCodec, validatorAddressCodec address.Codec, enabledSignModes []signingtypes.SignMode, customSignModes ...txsigning.SignModeHandler,
) client.TxConfig {
txConfig, err := NewTxConfigWithOptions(protoCodec, ConfigOptions{
EnabledSignModes: enabledSignModes,
CustomSignModes: customSignModes,
AddressPrefix: addressPrefix,
ValidatorPrefix: validatorPrefix,
SigningOptions: &txsigning.Options{
AddressCodec: addressCodec,
ValidatorAddressCodec: validatorAddressCodec,
},
})
if err != nil {
panic(err)
Expand All @@ -110,10 +108,7 @@ func NewSigningOptions(addressPrefix, validatorPrefix string) (*txsigning.Option
func NewSigningHandlerMap(configOpts ConfigOptions) (*txsigning.HandlerMap, error) {
var err error
if configOpts.SigningOptions == nil {
configOpts.SigningOptions, err = NewSigningOptions(configOpts.AddressPrefix, configOpts.ValidatorPrefix)
if err != nil {
return nil, err
}
return nil, errors.New("not signing options not provided")
}
if configOpts.SigningContext == nil {
configOpts.SigningContext, err = txsigning.NewContext(*configOpts.SigningOptions)
Expand Down Expand Up @@ -184,10 +179,7 @@ func NewTxConfigWithOptions(protoCodec codec.Codec, configOptions ConfigOptions)
var err error
if configOptions.SigningContext == nil {
if configOptions.SigningOptions == nil {
configOptions.SigningOptions, err = NewSigningOptions(configOptions.AddressPrefix, configOptions.ValidatorPrefix)
if err != nil {
return nil, err
}
return nil, errors.New("signing options not provided")
}
if configOptions.SigningOptions.FileResolver == nil {
configOptions.SigningOptions.FileResolver = protoCodec.InterfaceRegistry()
Expand Down
6 changes: 0 additions & 6 deletions x/auth/tx/config/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
grpcstatus "google.golang.org/grpc/status"
"google.golang.org/protobuf/reflect/protoreflect"

authv1 "cosmossdk.io/api/cosmos/auth/module/v1"
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"
stakingv1 "cosmossdk.io/api/cosmos/staking/module/v1"
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/depinject"
Expand Down Expand Up @@ -47,8 +45,6 @@ type ModuleInputs struct {
ValidatorAddressCodec runtime.ValidatorAddressCodec
Codec codec.Codec
ProtoFileResolver txsigning.ProtoFileResolver
AuthConfig *authv1.Module
StakingConfig *stakingv1.Module
// BankKeeper is the expected bank keeper to be passed to AnteHandlers
BankKeeper authtypes.BankKeeper `optional:"true"`
MetadataBankKeeper BankKeeper `optional:"true"`
Expand Down Expand Up @@ -85,8 +81,6 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
CustomGetSigners: make(map[protoreflect.FullName]txsigning.GetSignersFunc),
},
CustomSignModes: customSignModeHandlers,
AddressPrefix: in.AuthConfig.Bech32Prefix,
ValidatorPrefix: in.StakingConfig.Bech32PrefixValidator,
}

for _, mode := range in.CustomGetSigners {
Expand Down

0 comments on commit 99b6d57

Please sign in to comment.