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

refactor(bech32): remove embedding of addresscodec #16197

Merged
merged 10 commits into from
May 22, 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
8 changes: 4 additions & 4 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func NewSimApp(

invCheckPeriod := cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod))
app.CrisisKeeper = crisiskeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[crisistypes.StoreKey]), invCheckPeriod,
app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.GetAddressCodec())
app.BankKeeper, authtypes.FeeCollectorName, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.AddressCodec())

app.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[feegrant.StoreKey]), app.AccountKeeper)

Expand All @@ -301,7 +301,7 @@ func NewSimApp(
stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()),
)

app.CircuitKeeper = circuitkeeper.NewKeeper(keys[circuittypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.GetAddressCodec())
app.CircuitKeeper = circuitkeeper.NewKeeper(keys[circuittypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AccountKeeper.AddressCodec())

app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), appCodec, app.MsgServiceRouter(), app.AccountKeeper)

Expand Down Expand Up @@ -352,7 +352,7 @@ func NewSimApp(

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), app.StakingKeeper, app.SlashingKeeper, app.AccountKeeper.GetAddressCodec(), runtime.ProvideCometInfoService(),
appCodec, runtime.NewKVStoreService(keys[evidencetypes.StoreKey]), app.StakingKeeper, app.SlashingKeeper, app.AccountKeeper.AddressCodec(), runtime.ProvideCometInfoService(),
)
// If evidence needs to be handled for the app, set routes in router here and seal
app.EvidenceKeeper = *evidenceKeeper
Expand Down Expand Up @@ -380,7 +380,7 @@ func NewSimApp(
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.GetAddressCodec()),
upgrade.NewAppModule(app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
evidence.NewAppModule(app.EvidenceKeeper),
params.NewAppModule(app.ParamsKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
Expand Down
2 changes: 1 addition & 1 deletion x/auth/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryAccounts() {
})

// Regression test
addr1, err := suite.accountKeeper.GetAddressCodec().StringToBytes("cosmos1892yr6fzlj7ud0kfkah2ctrav3a4p4n060ze8f")
addr1, err := suite.accountKeeper.AddressCodec().StringToBytes("cosmos1892yr6fzlj7ud0kfkah2ctrav3a4p4n060ze8f")
suite.Require().NoError(err)
pub1, err := hex.DecodeString("D1002E1B019000010BB7034500E71F011F1CA90D5B000E134BFB0F3603030D0303")
suite.Require().NoError(err)
Expand Down
8 changes: 4 additions & 4 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (s queryServer) Account(c context.Context, req *types.QueryAccountRequest)
}

ctx := sdk.UnwrapSDKContext(c)
addr, err := s.k.StringToBytes(req.Address)
addr, err := s.k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s queryServer) AddressBytesToString(ctx context.Context, req *types.Addres
return nil, errors.New("empty address bytes is not allowed")
}

text, err := s.k.BytesToString(req.AddressBytes)
text, err := s.k.addressCodec.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}
Expand All @@ -209,7 +209,7 @@ func (s queryServer) AddressStringToBytes(ctx context.Context, req *types.Addres
return nil, errors.New("empty address string is not allowed")
}

bz, err := s.k.StringToBytes(req.AddressString)
bz, err := s.k.addressCodec.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}
Expand All @@ -228,7 +228,7 @@ func (s queryServer) AccountInfo(goCtx context.Context, req *types.QueryAccountI
}

ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := s.k.StringToBytes(req.Address)
addr, err := s.k.addressCodec.StringToBytes(req.Address)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

// AccountKeeperI is the interface contract that x/auth's keeper implements.
type AccountKeeperI interface {
address.Codec

// Return a new account with the next account number and the specified address. Does not save the new account to the store.
NewAccountWithAddress(context.Context, sdk.AccAddress) sdk.AccountI

Expand Down Expand Up @@ -56,12 +54,14 @@ type AccountKeeperI interface {

// GetModulePermissions fetches per-module account permissions
GetModulePermissions() map[string]types.PermissionsForAddress

AddressCodec() address.Codec
}

// AccountKeeper encodes/decodes accounts using the go-amino (binary)
// encoding/decoding library.
type AccountKeeper struct {
address.Codec
addressCodec address.Codec

storeService store.KVStoreService
cdc codec.BinaryCodec
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewAccountKeeper(
sb := collections.NewSchemaBuilder(storeService)

return AccountKeeper{
Codec: authcodec.NewBech32Codec(bech32Prefix),
addressCodec: authcodec.NewBech32Codec(bech32Prefix),
bech32Prefix: bech32Prefix,
storeService: storeService,
proto: proto,
Expand All @@ -117,10 +117,10 @@ func (ak AccountKeeper) GetAuthority() string {
return ak.authority
}

// GetAddressCodec returns the x/auth module's address.
// AddressCodec returns the x/auth module's address.
// x/auth is tied to bech32 encoded user accounts
func (ak AccountKeeper) GetAddressCodec() address.Codec {
return ak.Codec
func (ak AccountKeeper) AddressCodec() address.Codec {
return ak.addressCodec
}

// Logger returns a module-specific logger.
Expand Down
2 changes: 1 addition & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (am AppModule) IsAppModule() {}
// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAccountsFn types.RandomGenesisAccountsFn, ss exported.Subspace) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{ac: accountKeeper.GetAddressCodec()},
AppModuleBasic: AppModuleBasic{ac: accountKeeper.AddressCodec()},
accountKeeper: accountKeeper,
randGenAccountsFn: randGenAccountsFn,
legacySubspace: ss,
Expand Down
2 changes: 1 addition & 1 deletion x/auth/vesting/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type AppModule struct {

func NewAppModule(ak keeper.AccountKeeper, bk types.BankKeeper) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{ac: ak},
AppModuleBasic: AppModuleBasic{ac: ak.AddressCodec()},
accountKeeper: ak,
bankKeeper: bk,
}
Expand Down
12 changes: 6 additions & 6 deletions x/auth/vesting/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func NewMsgServerImpl(k keeper.AccountKeeper, bk types.BankKeeper) types.MsgServ
var _ types.MsgServer = msgServer{}

func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCreateVestingAccount) (*types.MsgCreateVestingAccountResponse, error) {
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
from, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}

to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
to, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}
Expand Down Expand Up @@ -95,12 +95,12 @@ func (s msgServer) CreateVestingAccount(goCtx context.Context, msg *types.MsgCre
}

func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *types.MsgCreatePermanentLockedAccount) (*types.MsgCreatePermanentLockedAccountResponse, error) {
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
from, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}

to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
to, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}
Expand Down Expand Up @@ -150,12 +150,12 @@ func (s msgServer) CreatePermanentLockedAccount(goCtx context.Context, msg *type
}

func (s msgServer) CreatePeriodicVestingAccount(goCtx context.Context, msg *types.MsgCreatePeriodicVestingAccount) (*types.MsgCreatePeriodicVestingAccountResponse, error) {
from, err := s.AccountKeeper.StringToBytes(msg.FromAddress)
from, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'from' address: %s", err)
}

to, err := s.AccountKeeper.StringToBytes(msg.ToAddress)
to, err := s.AccountKeeper.AddressCodec().StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid 'to' address: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions x/authz/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

// AccountKeeper defines the expected account keeper (noalias)
type AccountKeeper interface {
address.Codec

AddressCodec() address.Codec
GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
SetAccount(ctx context.Context, acc sdk.AccountI)
Expand Down
4 changes: 2 additions & 2 deletions x/authz/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) {
continue
}

grantee, err := k.authKeeper.StringToBytes(entry.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(entry.Grantee)
if err != nil {
panic(err)
}
granter, err := k.authKeeper.StringToBytes(entry.Granter)
granter, err := k.authKeeper.AddressCodec().StringToBytes(entry.Granter)
if err != nil {
panic(err)
}
Expand Down
7 changes: 2 additions & 5 deletions x/authz/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/golang/mock/gomock"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
Expand Down Expand Up @@ -51,11 +52,7 @@ func (suite *GenesisTestSuite) SetupTest() {
// gomock initializations
ctrl := gomock.NewController(suite.T())
suite.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)

suite.accountKeeper.EXPECT().StringToBytes(granteeAddr.String()).Return(granteeAddr, nil).AnyTimes()
suite.accountKeeper.EXPECT().BytesToString(granterAddr).Return(granterAddr.String(), nil).AnyTimes()
suite.accountKeeper.EXPECT().StringToBytes(granterAddr.String()).Return(granterAddr, nil).AnyTimes()
suite.accountKeeper.EXPECT().BytesToString(granterAddr).Return(granterAddr.String(), nil).AnyTimes()
suite.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

suite.baseApp = baseapp.NewBaseApp(
"authz",
Expand Down
8 changes: 4 additions & 4 deletions x/authz/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (k Keeper) Grants(ctx context.Context, req *authz.QueryGrantsRequest) (*aut
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

granter, err := k.authKeeper.StringToBytes(req.Granter)
granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}

grantee, err := k.authKeeper.StringToBytes(req.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrants
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

granter, err := k.authKeeper.StringToBytes(req.Granter)
granter, err := k.authKeeper.AddressCodec().StringToBytes(req.Granter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrants
return nil, status.Errorf(codes.InvalidArgument, "empty request")
}

grantee, err := k.authKeeper.StringToBytes(req.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(req.Grantee)
if err != nil {
return nil, err
}
Expand Down
10 changes: 3 additions & 7 deletions x/authz/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper_test

import (
"errors"
"testing"
"time"

Expand All @@ -14,6 +13,7 @@ import (
storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -68,12 +68,8 @@ func (s *TestSuite) SetupTest() {
// gomock initializations
ctrl := gomock.NewController(s.T())
s.accountKeeper = authztestutil.NewMockAccountKeeper(ctrl)
for _, addr := range s.addrs {
s.accountKeeper.EXPECT().StringToBytes(addr.String()).Return(addr, nil).AnyTimes()
s.accountKeeper.EXPECT().BytesToString(addr).Return(addr.String(), nil).AnyTimes()
}
s.accountKeeper.EXPECT().StringToBytes("").Return(nil, errors.New("empty address string is not allowed")).AnyTimes()
s.accountKeeper.EXPECT().StringToBytes("invalid").Return(nil, errors.New("invalid bech32 string")).AnyTimes()

s.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

s.bankKeeper = authztestutil.NewMockBankKeeper(ctrl)
banktypes.RegisterInterfaces(s.encCfg.InterfaceRegistry)
Expand Down
10 changes: 5 additions & 5 deletions x/authz/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGra
return nil, authz.ErrGranteeIsGranter
}

grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}

granter, err := k.authKeeper.StringToBytes(msg.Granter)
granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
Expand Down Expand Up @@ -65,12 +65,12 @@ func (k Keeper) Revoke(goCtx context.Context, msg *authz.MsgRevoke) (*authz.MsgR
return nil, authz.ErrGranteeIsGranter
}

grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}

granter, err := k.authKeeper.StringToBytes(msg.Granter)
granter, err := k.authKeeper.AddressCodec().StringToBytes(msg.Granter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid granter address: %s", err)
}
Expand All @@ -94,7 +94,7 @@ func (k Keeper) Exec(goCtx context.Context, msg *authz.MsgExec) (*authz.MsgExecR
return nil, errors.New("empty address string is not allowed")
}

grantee, err := k.authKeeper.StringToBytes(msg.Grantee)
grantee, err := k.authKeeper.AddressCodec().StringToBytes(msg.Grantee)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid grantee address: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion x/authz/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec/address"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -26,7 +27,7 @@ func (suite *TestSuite) TestGrant() {
addrs := suite.createAccounts(2)
curBlockTime := ctx.BlockTime()

suite.accountKeeper.EXPECT().StringToBytes(sdk.AccAddress("valid").String()).Return(sdk.AccAddress("valid"), nil).AnyTimes()
suite.accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

oneHour := curBlockTime.Add(time.Hour)
oneYear := curBlockTime.AddDate(1, 0, 0)
Expand Down
4 changes: 2 additions & 2 deletions x/authz/module/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -61,8 +62,7 @@ func TestExpiredGrantsQueue(t *testing.T) {
accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee3).Return(authtypes.NewBaseAccountWithAddress(grantee3)).AnyTimes()
accountKeeper.EXPECT().GetAccount(gomock.Any(), grantee4).Return(authtypes.NewBaseAccountWithAddress(grantee4)).AnyTimes()

accountKeeper.EXPECT().StringToBytes(granter.String()).Return(granter, nil).AnyTimes()
accountKeeper.EXPECT().BytesToString(granter).Return(granter.String(), nil).AnyTimes()
accountKeeper.EXPECT().AddressCodec().Return(address.NewBech32Codec("cosmos")).AnyTimes()

authzKeeper := keeper.NewKeeper(storeService, encCfg.Codec, baseApp.MsgServiceRouter(), accountKeeper)

Expand Down
2 changes: 1 addition & 1 deletion x/authz/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type AppModule struct {
// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper, bk authz.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak},
AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak.AddressCodec()},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
Expand Down
Loading