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

feat(auth): make address codec pluggable #16621

Merged
merged 12 commits into from
Jun 28, 2023
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -78,7 +79,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())
Expand Down
4 changes: 4 additions & 0 deletions x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -72,6 +73,7 @@ func (suite *DeterministicTestSuite) SetupTest() {
storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
)
Expand Down Expand Up @@ -291,6 +293,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccounts() {
suite.storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
)
Expand Down Expand Up @@ -337,6 +340,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryModuleAccountByName() {
suite.storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
)
Expand Down
8 changes: 5 additions & 3 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
) AccountKeeper {
permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
Expand All @@ -127,7 +126,7 @@ func NewAccountKeeper(
sb := collections.NewSchemaBuilder(storeService)

ak := AccountKeeper{
addressCodec: authcodec.NewBech32Codec(bech32Prefix),
addressCodec: ac,
bech32Prefix: bech32Prefix,
storeService: storeService,
proto: proto,
Expand All @@ -138,6 +137,9 @@ func NewAccountKeeper(
AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"),
Accounts: collections.NewIndexedMap(sb, types.AddressStoreKeyPrefix, "accounts", sdk.AccAddressKey, codec.CollInterfaceValue[sdk.AccountI](cdc), NewAccountIndexes(sb)),
}
if bech32Prefix == "" {
ak.bech32Prefix = "bech32 is not used on this chain"
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}
schema, err := sb.Build()
if err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions x/auth/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -65,6 +66,7 @@ func (suite *KeeperTestSuite) SetupTest() {
storeService,
types.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
types.NewModuleAddress("gov").String(),
)
Expand Down
18 changes: 10 additions & 8 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,24 +203,18 @@ func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.Weighte

func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideAddressCodec),
appmodule.Provide(ProvideModule),
)
}

// 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)
}

type ModuleInputs struct {
depinject.In

Config *modulev1.Module
StoreService store.KVStoreService
Cdc codec.Codec

AddressCodec address.Codec `optional:"true"`
RandomGenesisAccountsFn types.RandomGenesisAccountsFn `optional:"true"`
AccountI func() sdk.AccountI `optional:"true"`

Expand All @@ -247,6 +241,14 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
authority = types.NewModuleAddressOrBech32Address(in.Config.Authority)
}

if in.AddressCodec == nil {
in.AddressCodec = authcodec.NewBech32Codec(in.Config.Bech32Prefix)
}

if in.Config.Bech32Prefix == "" {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
in.Config.Bech32Prefix = ""
}

if in.RandomGenesisAccountsFn == nil {
in.RandomGenesisAccountsFn = simulation.RandomGenesisAccounts
}
Expand All @@ -255,7 +257,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}
Expand Down
2 changes: 2 additions & 0 deletions x/auth/vesting/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,6 +57,7 @@ func (s *VestingTestSuite) SetupTest() {
storeService,
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
authtypes.NewModuleAddress("gov").String(),
)
Expand Down
2 changes: 2 additions & 0 deletions x/auth/vesting/types/vesting_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -58,6 +59,7 @@ func (s *VestingAccountTestSuite) SetupTest() {
storeService,
authtypes.ProtoBaseAccount,
maccPerms,
authcodec.NewBech32Codec("cosmos"),
"cosmos",
authtypes.NewModuleAddress("gov").String(),
)
Expand Down
3 changes: 2 additions & 1 deletion x/group/migrations/v2/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down