From d415349e6d777158baf51854779cfd0f73d071e3 Mon Sep 17 00:00:00 2001 From: Marko Date: Wed, 28 Jun 2023 13:57:26 +0200 Subject: [PATCH] feat(auth): make address codec pluggable (#16621) Co-authored-by: Matt Kocubinski Co-authored-by: Julien Robert (cherry picked from commit 256e37c523bdbf3225628018f33059764377e059) # Conflicts: # CHANGELOG.md # simapp/app_test.go # testutil/integration/example_test.go --- CHANGELOG.md | 25 +++++++++ depinject/container_test.go | 47 ++++++++++++++++ simapp/app.go | 2 +- simapp/app_test.go | 53 +++++++++++++++++++ simapp/app_v2.go | 6 +++ .../bank/keeper/deterministic_test.go | 2 + .../distribution/keeper/msg_server_test.go | 2 + .../evidence/keeper/infraction_test.go | 5 +- tests/integration/gov/keeper/keeper_test.go | 2 + .../slashing/keeper/keeper_test.go | 2 + .../integration/staking/keeper/common_test.go | 2 + .../staking/keeper/determinstic_test.go | 2 + testutil/integration/example_test.go | 6 +++ x/auth/ante/testutil_test.go | 3 +- x/auth/keeper/deterministic_test.go | 4 ++ x/auth/keeper/grpc_query.go | 4 ++ x/auth/keeper/keeper.go | 5 +- x/auth/keeper/keeper_test.go | 2 + x/auth/module.go | 22 ++++++-- x/auth/vesting/msg_server_test.go | 2 + x/auth/vesting/types/vesting_account_test.go | 2 + x/group/migrations/v2/migrate_test.go | 3 +- 22 files changed, 191 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f776db70db5..082f5a7273c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,31 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/staking) [#16324](https://github.com/cosmos/cosmos-sdk/pull/16324) `NewKeeper` now takes a `KVStoreService` instead of a `StoreKey`, and methods in the `Keeper` now take a `context.Context` instead of a `sdk.Context` and return an `error`. Notable changes: * `Validator` method now returns `types.ErrNoValidatorFound` instead of `nil` when not found. +<<<<<<< HEAD +======= +* (x/distribution) [#16440](https://github.com/cosmos/cosmos-sdk/pull/16440) use collections for `DelegatorWithdrawAddresState`: + * remove `Keeper`: `SetDelegatorWithdrawAddr`, `DeleteDelegatorWithdrawAddr`, `IterateDelegatorWithdrawAddrs`. +* (x/distribution) [#16459](https://github.com/cosmos/cosmos-sdk/pull/16459) use collections for `ValidatorCurrentRewards` state management: + * remove `Keeper`: `IterateValidatorCurrentRewards`, `GetValidatorCurrentRewards`, `SetValidatorCurrentRewards`, `DeleteValidatorCurrentRewards` +* (x/authz) [#16509](https://github.com/cosmos/cosmos-sdk/pull/16509) `AcceptResponse` has been moved to sdk/types/authz and the `Updated` field is now of the type `sdk.Msg` instead of `authz.Authorization`. +* (x/distribution) [#16483](https://github.com/cosmos/cosmos-sdk/pull/16483) use collections for `DelegatorStartingInfo` state management: + * remove `Keeper`: `IterateDelegatorStartingInfo`, `GetDelegatorStartingInfo`, `SetDelegatorStartingInfo`, `DeleteDelegatorStartingInfo`, `HasDelegatorStartingInfo` +* (x/distribution) [#16571](https://github.com/cosmos/cosmos-sdk/pull/16571) use collections for `ValidatorAccumulatedCommission` state management: + * remove `Keeper`: `IterateValidatorAccumulatedCommission`, `GetValidatorAccumulatedCommission`, `SetValidatorAccumulatedCommission`, `DeleteValidatorAccumulatedCommission` +* (x/distribution) [#16590](https://github.com/cosmos/cosmos-sdk/pull/16590) use collections for `ValidatorOutstandingRewards` state management: + * remove `Keeper`: `IterateValidatorOutstandingRewards`, `GetValidatorOutstandingRewards`, `SetValidatorOutstandingRewards`, `DeleteValidatorOutstandingRewards` +* (x/distribution) [#16607](https://github.com/cosmos/cosmos-sdk/pull/16607) use collections for `ValidatorHistoricalRewards` state management: + * remove `Keeper`: `IterateValidatorHistoricalRewards`, `GetValidatorHistoricalRewards`, `SetValidatorHistoricalRewards`, `DeleteValidatorHistoricalRewards`, `DeleteValidatorHistoricalReward`, `DeleteAllValidatorHistoricalRewards` +* (x/auth) [#16621](https://github.com/cosmos/cosmos-sdk/pull/16621) Pass address codec to auth new keeper constructor + +### Bug Fixes + +* (x/auth/vesting) [#16733](https://github.com/cosmos/cosmos-sdk/pull/16733) panic on overflowing and negative EndTimes when creating a PeriodicVestingAccount +* [#16547](https://github.com/cosmos/cosmos-sdk/pull/16547) Ensure a transaction's gas limit cannot exceed the block gas limit. +* (x/auth/types) [#16554](https://github.com/cosmos/cosmos-sdk/pull/16554) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking. +* (baseapp) [#16613](https://github.com/cosmos/cosmos-sdk/pull/16613) Ensure each message in a transaction has a registered handler, otherwise `CheckTx` will fail. +* [#16639](https://github.com/cosmos/cosmos-sdk/pull/16639) Make sure we don't execute blocks beyond the halt height. +>>>>>>> 256e37c52 (feat(auth): make address codec pluggable (#16621)) ## [v0.50.0-alpha.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.0-alpha.0) - 2023-06-07 diff --git a/depinject/container_test.go b/depinject/container_test.go index cf9394a56b11..43010004cc35 100644 --- a/depinject/container_test.go +++ b/depinject/container_test.go @@ -744,3 +744,50 @@ func TestConditionalDebugging(t *testing.T) { require.Empty(t, logs) require.True(t, success) } + +type TestFuncTypesInputs struct { + depinject.In + + DuckReturner func() Duck `optional:"true"` +} + +type smallMallard struct{} + +func (smallMallard) quack() {} + +func DuckProvider(in TestFuncTypesInputs) Duck { + if in.DuckReturner != nil { + return in.DuckReturner() + } + return Mallard{} +} + +func TestFuncTypes(t *testing.T) { + var duckReturnerFactory func() Duck + err := depinject.Inject( + depinject.Supply(func() Duck { return smallMallard{} }), + &duckReturnerFactory) + require.NoError(t, err) + _, ok := duckReturnerFactory().(smallMallard) + require.True(t, ok) + + var duck Duck + err = depinject.Inject( + depinject.Configs( + depinject.Supply(func() Duck { return smallMallard{} }), + depinject.Provide(DuckProvider), + ), + &duck) + _, ok = duck.(smallMallard) + require.True(t, ok) + require.NoError(t, err) + + err = depinject.Inject( + depinject.Configs( + depinject.Provide(DuckProvider), + ), + &duck) + _, ok = duck.(Mallard) + require.True(t, ok) + require.NoError(t, err) +} diff --git a/simapp/app.go b/simapp/app.go index feed299e107e..0e5f6f0a7bde 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -310,7 +310,7 @@ func NewSimApp( bApp.SetParamStore(app.ConsensusParamsKeeper.ParamsStore) // add keepers - app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String()) + app.AccountKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec(sdk.Bech32MainPrefix), sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String()) app.BankKeeper = bankkeeper.NewBaseKeeper( appCodec, diff --git a/simapp/app_test.go b/simapp/app_test.go index d5929d28714f..d72fe2fdada1 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -17,8 +17,20 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" +<<<<<<< HEAD +======= + "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/depinject" + "cosmossdk.io/log" + "cosmossdk.io/x/evidence" + feegrantmodule "cosmossdk.io/x/feegrant/module" + "cosmossdk.io/x/upgrade" + +>>>>>>> 256e37c52 (feat(auth): make address codec pluggable (#16621)) "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/testutil/mock" + "github.com/cosmos/cosmos-sdk/testutil/network" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" @@ -295,3 +307,44 @@ func TestProtoAnnotations(t *testing.T) { err = msgservice.ValidateProtoAnnotations(r) require.NoError(t, err) } + +var _ address.Codec = (*customAddressCodec)(nil) + +type customAddressCodec struct{} + +func (c customAddressCodec) StringToBytes(text string) ([]byte, error) { + return []byte(text), nil +} + +func (c customAddressCodec) BytesToString(bz []byte) (string, error) { + return string(bz), nil +} + +func TestAddressCodecFactory(t *testing.T) { + var addrCodec address.Codec + err := depinject.Inject( + depinject.Configs( + network.MinimumAppConfig(), + depinject.Supply(log.NewNopLogger()), + ), + &addrCodec) + require.NoError(t, err) + require.NotNil(t, addrCodec) + _, ok := addrCodec.(customAddressCodec) + require.False(t, ok) + + // Set the address codec to the custom one + err = depinject.Inject( + depinject.Configs( + network.MinimumAppConfig(), + depinject.Supply( + log.NewNopLogger(), + func() address.Codec { return customAddressCodec{} }, + ), + ), + &addrCodec) + require.NoError(t, err) + require.NotNil(t, addrCodec) + _, ok = addrCodec.(customAddressCodec) + require.True(t, ok) +} diff --git a/simapp/app_v2.go b/simapp/app_v2.go index 936458c86512..7b2306552338 100644 --- a/simapp/app_v2.go +++ b/simapp/app_v2.go @@ -134,6 +134,12 @@ func NewSimApp( // // func() sdk.AccountI { return authtypes.ProtoBaseAccount() }, + // For providing a different address codec, add it below. + // By default the auth module uses a Bech32 address codec, + // with the prefix defined in the auth module configuration. + // + // func() address.Codec { return <- custom address codec type -> } + // // MINT // diff --git a/tests/integration/bank/keeper/deterministic_test.go b/tests/integration/bank/keeper/deterministic_test.go index 92e7c03cef41..99ad5822f6c7 100644 --- a/tests/integration/bank/keeper/deterministic_test.go +++ b/tests/integration/bank/keeper/deterministic_test.go @@ -10,6 +10,7 @@ import ( "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -80,6 +81,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/distribution/keeper/msg_server_test.go b/tests/integration/distribution/keeper/msg_server_test.go index ca61531a0ff3..1949d305ab1e 100644 --- a/tests/integration/distribution/keeper/msg_server_test.go +++ b/tests/integration/distribution/keeper/msg_server_test.go @@ -14,6 +14,7 @@ import ( "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" sdk "github.com/cosmos/cosmos-sdk/types" @@ -79,6 +80,7 @@ func initFixture(t testing.TB) *fixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/evidence/keeper/infraction_test.go b/tests/integration/evidence/keeper/infraction_test.go index 03268cdaa366..0f1e183ada00 100644 --- a/tests/integration/evidence/keeper/infraction_test.go +++ b/tests/integration/evidence/keeper/infraction_test.go @@ -19,7 +19,7 @@ import ( "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/codec/address" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/runtime" @@ -101,6 +101,7 @@ func initFixture(t testing.TB) *fixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) @@ -121,7 +122,7 @@ func initFixture(t testing.TB) *fixture { slashingKeeper := slashingkeeper.NewKeeper(cdc, codec.NewLegacyAmino(), runtime.NewKVStoreService(keys[slashingtypes.StoreKey]), stakingKeeper, authority.String()) - evidenceKeeper := keeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), stakingKeeper, slashingKeeper, address.NewBech32Codec("cosmos"), runtime.ProvideCometInfoService()) + evidenceKeeper := keeper.NewKeeper(cdc, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), stakingKeeper, slashingKeeper, addresscodec.NewBech32Codec("cosmos"), runtime.ProvideCometInfoService()) router := evidencetypes.NewRouter() router = router.AddRoute(evidencetypes.RouteEquivocation, testEquivocationHandler(evidenceKeeper)) evidenceKeeper.SetRouter(router) diff --git a/tests/integration/gov/keeper/keeper_test.go b/tests/integration/gov/keeper/keeper_test.go index 4d2a6b75404b..c19a3ac29b29 100644 --- a/tests/integration/gov/keeper/keeper_test.go +++ b/tests/integration/gov/keeper/keeper_test.go @@ -9,6 +9,7 @@ import ( "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/baseapp" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" sdk "github.com/cosmos/cosmos-sdk/types" @@ -71,6 +72,7 @@ func initFixture(t testing.TB) *fixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/slashing/keeper/keeper_test.go b/tests/integration/slashing/keeper/keeper_test.go index a31a8f080bfb..8f76cc34768b 100644 --- a/tests/integration/slashing/keeper/keeper_test.go +++ b/tests/integration/slashing/keeper/keeper_test.go @@ -13,6 +13,7 @@ import ( "gotest.tools/v3/assert" "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -72,6 +73,7 @@ func initFixture(t testing.TB) *fixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/staking/keeper/common_test.go b/tests/integration/staking/keeper/common_test.go index 5a6334715a1a..d2755d626134 100644 --- a/tests/integration/staking/keeper/common_test.go +++ b/tests/integration/staking/keeper/common_test.go @@ -12,6 +12,7 @@ import ( cmtprototypes "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" @@ -114,6 +115,7 @@ func initFixture(t testing.TB) *fixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/tests/integration/staking/keeper/determinstic_test.go b/tests/integration/staking/keeper/determinstic_test.go index 4117e4616f7e..32f51d39b144 100644 --- a/tests/integration/staking/keeper/determinstic_test.go +++ b/tests/integration/staking/keeper/determinstic_test.go @@ -12,6 +12,7 @@ import ( "pgregory.net/rapid" "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/runtime" @@ -86,6 +87,7 @@ func initDeterministicFixture(t *testing.T) *deterministicFixture { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, + addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authority.String(), ) diff --git a/testutil/integration/example_test.go b/testutil/integration/example_test.go index 549b2cc12005..d7b7c2a5521c 100644 --- a/testutil/integration/example_test.go +++ b/testutil/integration/example_test.go @@ -7,10 +7,14 @@ import ( "cosmossdk.io/log" storetypes "cosmossdk.io/store/types" +<<<<<<< HEAD cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" "github.com/google/go-cmp/cmp" +======= + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" +>>>>>>> 256e37c52 (feat(auth): make address codec pluggable (#16621)) "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil/integration" sdk "github.com/cosmos/cosmos-sdk/types" @@ -44,6 +48,7 @@ func Example() { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, map[string][]string{minttypes.ModuleName: {authtypes.Minter}}, + addresscodec.NewBech32Codec("cosmos"), "cosmos", authority, ) @@ -129,6 +134,7 @@ func Example_oneModule() { runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, map[string][]string{minttypes.ModuleName: {authtypes.Minter}}, + addresscodec.NewBech32Codec("cosmos"), "cosmos", authority, ) diff --git a/x/auth/ante/testutil_test.go b/x/auth/ante/testutil_test.go index c22c4b7bd71e..6a98e057f567 100644 --- a/x/auth/ante/testutil_test.go +++ b/x/auth/ante/testutil_test.go @@ -29,6 +29,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth/ante" antetestutil "github.com/cosmos/cosmos-sdk/x/auth/ante/testutil" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" "github.com/cosmos/cosmos-sdk/x/auth/keeper" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" authtestutil "github.com/cosmos/cosmos-sdk/x/auth/testutil" @@ -79,7 +80,7 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite { } suite.accountKeeper = keeper.NewAccountKeeper( - suite.encCfg.Codec, runtime.NewKVStoreService(key), types.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix, types.NewModuleAddress("gov").String(), + suite.encCfg.Codec, runtime.NewKVStoreService(key), types.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, types.NewModuleAddress("gov").String(), ) suite.accountKeeper.GetModuleAccount(suite.ctx, types.FeeCollectorName) err := suite.accountKeeper.Params.Set(suite.ctx, types.DefaultParams()) diff --git a/x/auth/keeper/deterministic_test.go b/x/auth/keeper/deterministic_test.go index 63fb8cc4d193..630fa173fd4c 100644 --- a/x/auth/keeper/deterministic_test.go +++ b/x/auth/keeper/deterministic_test.go @@ -21,6 +21,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -72,6 +73,7 @@ func (suite *DeterministicTestSuite) SetupTest() { storeService, types.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", types.NewModuleAddress("gov").String(), ) @@ -291,6 +293,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccounts() { suite.storeService, types.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", types.NewModuleAddress("gov").String(), ) @@ -337,6 +340,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccountByName() { suite.storeService, types.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", types.NewModuleAddress("gov").String(), ) diff --git a/x/auth/keeper/grpc_query.go b/x/auth/keeper/grpc_query.go index 57e8b113a2c9..c875c4232bc1 100644 --- a/x/auth/keeper/grpc_query.go +++ b/x/auth/keeper/grpc_query.go @@ -163,6 +163,10 @@ func (s queryServer) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRe return nil, err } + if bech32Prefix == "" { + return &types.Bech32PrefixResponse{Bech32Prefix: "bech32 is not used on this chain"}, nil + } + return &types.Bech32PrefixResponse{Bech32Prefix: bech32Prefix}, nil } diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 5ef50152dda8..f06219339a01 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -19,7 +19,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -117,7 +116,7 @@ var _ AccountKeeperI = &AccountKeeper{} // may use auth.Keeper to access the accounts permissions map. func NewAccountKeeper( cdc codec.BinaryCodec, storeService store.KVStoreService, proto func() sdk.AccountI, - maccPerms map[string][]string, bech32Prefix, authority string, + maccPerms map[string][]string, ac address.Codec, bech32Prefix, authority string, ) AccountKeeper { permAddrs := make(map[string]types.PermissionsForAddress) for name, perms := range maccPerms { @@ -127,7 +126,7 @@ func NewAccountKeeper( sb := collections.NewSchemaBuilder(storeService) ak := AccountKeeper{ - addressCodec: authcodec.NewBech32Codec(bech32Prefix), + addressCodec: ac, bech32Prefix: bech32Prefix, storeService: storeService, proto: proto, diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 628bcdbc1640..3ae7f9831ed6 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -17,6 +17,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" "github.com/cosmos/cosmos-sdk/x/auth/keeper" "github.com/cosmos/cosmos-sdk/x/auth/types" ) @@ -65,6 +66,7 @@ func (suite *KeeperTestSuite) SetupTest() { storeService, types.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", types.NewModuleAddress("gov").String(), ) diff --git a/x/auth/module.go b/x/auth/module.go index 40bd46896d63..bdb796050a11 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -203,15 +203,28 @@ func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weighte func init() { appmodule.Register(&modulev1.Module{}, - appmodule.Provide(ProvideAddressCodec), appmodule.Provide(ProvideModule), + appmodule.Provide(ProvideAddressCodec), ) } +// AddressCodecFactory is a type alias for a function that returns an address.Codec +type AddressCodecFactory func() address.Codec + +type AddressCodecInputs struct { + depinject.In + + Config *modulev1.Module + AddressCodecFactory func() address.Codec `optional:"true"` +} + // ProvideAddressCodec provides an address.Codec to the container for any // modules that want to do address string <> bytes conversion. -func ProvideAddressCodec(config *modulev1.Module) address.Codec { - return authcodec.NewBech32Codec(config.Bech32Prefix) +func ProvideAddressCodec(in AddressCodecInputs) address.Codec { + if in.AddressCodecFactory != nil { + return in.AddressCodecFactory() + } + return authcodec.NewBech32Codec(in.Config.Bech32Prefix) } type ModuleInputs struct { @@ -221,6 +234,7 @@ type ModuleInputs struct { StoreService store.KVStoreService Cdc codec.Codec + AddressCodec address.Codec RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"` AccountI func() sdk.AccountI `optional:"true"` @@ -255,7 +269,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.AccountI = types.ProtoBaseAccount } - k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.Config.Bech32Prefix, authority.String()) + k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, authority.String()) m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace) return ModuleOutputs{AccountKeeper: k, Module: m} diff --git a/x/auth/vesting/msg_server_test.go b/x/auth/vesting/msg_server_test.go index 2a121271e10a..cb1476c25897 100644 --- a/x/auth/vesting/msg_server_test.go +++ b/x/auth/vesting/msg_server_test.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/vesting" @@ -56,6 +57,7 @@ func (s *VestingTestSuite) SetupTest() { storeService, authtypes.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", authtypes.NewModuleAddress("gov").String(), ) diff --git a/x/auth/vesting/types/vesting_account_test.go b/x/auth/vesting/types/vesting_account_test.go index dd60e2ae21a2..fac801fdd80f 100644 --- a/x/auth/vesting/types/vesting_account_test.go +++ b/x/auth/vesting/types/vesting_account_test.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/testdata" sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec" "github.com/cosmos/cosmos-sdk/x/auth/keeper" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/cosmos/cosmos-sdk/x/auth/vesting" @@ -58,6 +59,7 @@ func (s *VestingAccountTestSuite) SetupTest() { storeService, authtypes.ProtoBaseAccount, maccPerms, + authcodec.NewBech32Codec("cosmos"), "cosmos", authtypes.NewModuleAddress("gov").String(), ) diff --git a/x/group/migrations/v2/migrate_test.go b/x/group/migrations/v2/migrate_test.go index 169665ce9f12..5c29567dae89 100644 --- a/x/group/migrations/v2/migrate_test.go +++ b/x/group/migrations/v2/migrate_test.go @@ -8,6 +8,7 @@ import ( storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" @@ -78,7 +79,7 @@ func createGroupPolicies(ctx sdk.Context, storeKey storetypes.StoreKey, cdc code // createOldPolicyAccount re-creates the group policy account using a module account func createOldPolicyAccount(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Codec, policies []sdk.AccAddress) ([]*authtypes.ModuleAccount, group.AccountKeeper) { - accountKeeper := authkeeper.NewAccountKeeper(cdc, runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), authtypes.ProtoBaseAccount, nil, sdk.Bech32MainPrefix, authorityAddr.String()) + accountKeeper := authkeeper.NewAccountKeeper(cdc, runtime.NewKVStoreService(storeKey.(*storetypes.KVStoreKey)), authtypes.ProtoBaseAccount, nil, addresscodec.NewBech32Codec("cosmos"), sdk.Bech32MainPrefix, authorityAddr.String()) oldPolicyAccounts := make([]*authtypes.ModuleAccount, len(policies)) for i, policyAddr := range policies {