Skip to content

Commit

Permalink
refactor!: remove global config from x/auth and client (#19447)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Mar 6, 2024
1 parent 06a3989 commit 25aea8a
Show file tree
Hide file tree
Showing 140 changed files with 570 additions and 298 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

### API Breaking Changes

* (types) [#19447](https://github.com/cosmos/cosmos-sdk/pull/19447) `module.testutil.MakeTestEncodingConfig` now takes `CodecOptions` as argument.
* (types) [#19512](https://github.com/cosmos/cosmos-sdk/pull/19512) Remove basic manager and all related functions (`module.BasicManager`, `module.NewBasicManager`, `module.NewBasicManagerFromManager`, `NewGenesisOnlyAppModule`).
* The module manager now can do everything that the basic manager was doing.
* When using runtime, just inject the module manager when needed using your app config.
Expand Down
9 changes: 6 additions & 3 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ In this section we describe the changes made in Cosmos SDK' SimApp.

#### Client (`root.go`)

The `client` package has been refactored to make use of the address codecs (address, validator address, consensus address, etc.).
The `client` package has been refactored to make use of the address codecs (address, validator address, consensus address, etc.)
and address bech32 prefixes (address and validator address).
This is part of the work of abstracting the SDK from the global bech32 config.

This means the address codecs must be provided in the `client.Context` in the application client (usually `root.go`).
This means the address codecs and prefixes must be provided in the `client.Context` in the application client (usually `root.go`).

```diff
clientCtx = clientCtx.
+ WithAddressCodec(addressCodec).
+ WithValidatorAddressCodec(validatorAddressCodec).
+ WithConsensusAddressCodec(consensusAddressCodec)
+ WithConsensusAddressCodec(consensusAddressCodec).
+ WithAddressPrefix("cosmos").
+ WithValidatorPrefix("cosmosvaloper")
```

**When using `depinject` / `app v2`, the client codecs can be provided directly from application config.**
Expand Down
6 changes: 4 additions & 2 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,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)
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 @@ -566,7 +567,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)
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)
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)
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)
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
16 changes: 16 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type Context struct {
AddressCodec address.Codec
ValidatorAddressCodec address.Codec
ConsensusAddressCodec address.Codec

// Bech32 address prefixes.
AddressPrefix string
ValidatorPrefix string
}

// WithCmdContext returns a copy of the context with an updated context.Context,
Expand Down Expand Up @@ -337,6 +341,18 @@ func (ctx Context) WithConsensusAddressCodec(consensusAddressCodec address.Codec
return ctx
}

// WithAddressPrefix returns the context with the provided address bech32 prefix.
func (ctx Context) WithAddressPrefix(addressPrefix string) Context {
ctx.AddressPrefix = addressPrefix
return ctx
}

// WithValidatorPrefix returns the context with the provided validator bech32 prefix.
func (ctx Context) WithValidatorPrefix(validatorPrefix string) Context {
ctx.ValidatorPrefix = validatorPrefix
return ctx
}

// PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout
func (ctx Context) PrintString(str string) error {
return ctx.PrintBytes([]byte(str))
Expand Down
3 changes: 2 additions & 1 deletion client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -98,7 +99,7 @@ x: "10"
}

func TestGetFromFields(t *testing.T) {
cfg := testutil.MakeTestEncodingConfig()
cfg := testutil.MakeTestEncodingConfig(codectestutil.CodecOptions{})
path := hd.CreateHDPath(118, 0, 0).String()

testCases := []struct {
Expand Down
3 changes: 2 additions & 1 deletion client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/integration"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
Expand All @@ -39,7 +40,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
keys := storetypes.NewKVStoreKeys(countertypes.StoreKey)
cms := integration.CreateMultiStore(keys, logger)
s.ctx = sdk.NewContext(cms, true, logger)
cfg := moduletestutil.MakeTestEncodingConfig(counter.AppModule{})
cfg := moduletestutil.MakeTestEncodingConfig(testutil.CodecOptions{}, counter.AppModule{})
s.cdc = cfg.Codec

queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, cfg.InterfaceRegistry)
Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *buf

// If we're using ledger, only thing we need is the path and the bech32 prefix.
if useLedger {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
bech32PrefixAccAddr := ctx.AddressPrefix
k, err := kb.SaveLedgerKey(name, hd.Secp256k1, bech32PrefixAccAddr, coinType, account, index)
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -39,7 +40,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
// Prepare a keybase
kbHome := t.TempDir()

cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
clientCtx := client.Context{}.
WithKeyringDir(kbHome).
WithCodec(cdc).
Expand Down Expand Up @@ -97,7 +98,7 @@ func Test_runAddCmdLedger(t *testing.T) {

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

clientCtx := client.Context{}.
WithKeyringDir(kbHome).
Expand Down Expand Up @@ -144,7 +145,7 @@ func Test_runAddCmdLedger(t *testing.T) {
}

func Test_runAddCmdLedgerDryRun(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
testData := []struct {
name string
args []string
Expand Down
9 changes: 5 additions & 4 deletions client/keys/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -28,7 +29,7 @@ func Test_runAddCmdBasic(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()

cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand Down Expand Up @@ -149,7 +150,7 @@ func Test_runAddCmdMultisigDupKeys(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()

cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand Down Expand Up @@ -217,7 +218,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
pubkey1 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtObiFVE4s+9+RX5SP8TN9r2mxpoaT4eGj9CJfK7VRzN"}`
pubkey2 := `{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A/se1vkqgdQ7VJQCM4mxN+L+ciGhnnJ4XYsQCRBMrdRi"}`
b64Pubkey := "QWhnOHhpdXBJcGZ2UlR2ak5la1ExclROUThTOW96YjdHK2RYQmFLVjl4aUo="
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

testData := []struct {
name string
Expand Down Expand Up @@ -349,7 +350,7 @@ func Test_runAddCmdDryRun(t *testing.T) {
func TestAddRecoverFileBackend(t *testing.T) {
cmd := AddKeyCommand()
cmd.Flags().AddFlagSet(Commands().PersistentFlags())
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
kbHome := t.TempDir()
Expand Down
3 changes: 2 additions & 1 deletion client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -34,7 +35,7 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName2 := "runDeleteCmd_Key2"

path := sdk.GetFullBIP44Path()
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec

cmd.SetArgs([]string{"blah", fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome)})
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
Expand Down
3 changes: 2 additions & 1 deletion client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand All @@ -19,7 +20,7 @@ import (
)

func Test_runExportCmd(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
testCases := []struct {
name string
keyringBackend string
Expand Down
7 changes: 4 additions & 3 deletions client/keys/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

func Test_runImportCmd(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
testCases := []struct {
name string
keyringBackend string
Expand Down Expand Up @@ -122,7 +123,7 @@ HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
}

func Test_runImportHexCmd(t *testing.T) {
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
testCases := []struct {
name string
keyringBackend string
Expand Down Expand Up @@ -184,7 +185,7 @@ func Test_runImportCmdWithEmptyName(t *testing.T) {
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
// Now add a temporary keybase
kbHome := t.TempDir()
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn, cdc)
require.NoError(t, err)

Expand Down
5 changes: 3 additions & 2 deletions client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down Expand Up @@ -39,7 +40,7 @@ func Test_runListCmd(t *testing.T) {
kbHome2 := t.TempDir()

mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome2, mockIn, cdc)
assert.NilError(t, err)

Expand Down Expand Up @@ -92,7 +93,7 @@ func Test_runListCmd(t *testing.T) {
func Test_runListKeyTypeCmd(t *testing.T) {
cmd := ListKeyTypesCmd()

cdc := moduletestutil.MakeTestEncodingConfig().Codec
cdc := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
kbHome := t.TempDir()
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)

Expand Down
3 changes: 2 additions & 1 deletion client/keys/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestMigrateTestSuite(t *testing.T) {

func (s *MigrateTestSuite) SetupSuite() {
s.dir = s.T().TempDir()
s.cdc = moduletestutil.MakeTestEncodingConfig().Codec
s.cdc = moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}).Codec
s.appName = "cosmos"
s.priv = cryptotypes.PrivKey(secp256k1.GenPrivKey())
s.pub = s.priv.PubKey()
Expand Down
Loading

0 comments on commit 25aea8a

Please sign in to comment.