From 3781c462a9442a4142d8a38b77e587984d9a4c08 Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 22 Aug 2023 14:45:54 +0200 Subject: [PATCH] chore: minor bech32 global removal (#17469) Co-authored-by: marbar3778 --- CHANGELOG.md | 1 + x/auth/keeper/grpc_query.go | 7 ++++++- x/auth/module.go | 7 ++++++- x/authz/keeper/grpc_query.go | 15 +++++++++++++-- x/gov/keeper/msg_server.go | 5 ++++- x/gov/migrations/v3/convert.go | 2 +- x/gov/migrations/v3/store_test.go | 2 +- x/gov/types/v1beta1/vote.go | 7 ------- x/mint/module.go | 7 ++++++- x/mint/testutil/expected_keepers_mocks.go | 15 +++++++++++++++ x/mint/types/expected_keepers.go | 2 ++ x/staking/module.go | 7 ++++++- 12 files changed, 61 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c36fc88036ae..891188ad640a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (types) [#17348](https://github.com/cosmos/cosmos-sdk/pull/17348) Remove the `WrapServiceResult` function. * The `*sdk.Result` returned by the msg server router will not contain the `.Data` field. * (x/staking) [#17335](https://github.com/cosmos/cosmos-sdk/pull/17335) Remove usage of `"github.com/cosmos/cosmos-sdk/x/staking/types".Infraction_*` in favour of `"cosmossdk.io/api/cosmos/staking/v1beta1".Infraction_` in order to remove dependency between modules on staking +* (x/gov) [#17496](https://github.com/cosmos/cosmos-sdk/pull/17469) in `x/gov/types/v1beta1/vote.go` `NewVote` was removed, constructing the struct is required for this type * (types) [#17426](https://github.com/cosmos/cosmos-sdk/pull/17426) `NewContext` does not take a `cmtproto.Header{}` any longer. * `WithChainID` / `WithBlockHeight` / `WithBlockHeader` must be used to set values on the context diff --git a/x/auth/keeper/grpc_query.go b/x/auth/keeper/grpc_query.go index 6e547f89128b..dba40207e459 100644 --- a/x/auth/keeper/grpc_query.go +++ b/x/auth/keeper/grpc_query.go @@ -39,7 +39,12 @@ func (s queryServer) AccountAddressByID(ctx context.Context, req *types.QueryAcc return nil, status.Errorf(codes.NotFound, "account address not found with account number %d", req.Id) } - return &types.QueryAccountAddressByIDResponse{AccountAddress: address.String()}, nil + addr, err := s.k.addressCodec.BytesToString(address) + if err != nil { + return nil, err + } + + return &types.QueryAccountAddressByIDResponse{AccountAddress: addr}, nil } func (s queryServer) Accounts(ctx context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) { diff --git a/x/auth/module.go b/x/auth/module.go index 1a9bfdc52208..ae942a774e5c 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -232,7 +232,12 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.AccountI = types.ProtoBaseAccount } - k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, authority.String()) + auth, err := in.AddressCodec.BytesToString(authority) + if err != nil { + panic(err) + } + + k := keeper.NewAccountKeeper(in.Cdc, in.StoreService, in.AccountI, maccPerms, in.AddressCodec, in.Config.Bech32Prefix, auth) m := NewAppModule(in.Cdc, k, in.RandomGenesisAccountsFn, in.LegacySubspace) return ModuleOutputs{AccountKeeper: k, Module: m} diff --git a/x/authz/keeper/grpc_query.go b/x/authz/keeper/grpc_query.go index de4cd060d026..333b441b36cf 100644 --- a/x/authz/keeper/grpc_query.go +++ b/x/authz/keeper/grpc_query.go @@ -115,9 +115,15 @@ func (k Keeper) GranterGrants(ctx context.Context, req *authz.QueryGranterGrants } grantee := firstAddressFromGrantStoreKey(key) + + granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + if err != nil { + return nil, err + } + return &authz.GrantAuthorization{ Granter: req.Granter, - Grantee: grantee.String(), + Grantee: granteeAddr, Authorization: any, Expiration: auth.Expiration, }, nil @@ -163,10 +169,15 @@ func (k Keeper) GranteeGrants(ctx context.Context, req *authz.QueryGranteeGrants return nil, status.Errorf(codes.Internal, err.Error()) } + granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter) + if err != nil { + return nil, err + } + return &authz.GrantAuthorization{ Authorization: authorizationAny, Expiration: auth.Expiration, - Granter: granter.String(), + Granter: granterAddr, Grantee: req.Grantee, }, nil }, func() *authz.Grant { diff --git a/x/gov/keeper/msg_server.go b/x/gov/keeper/msg_server.go index 1e153008db47..c27adfc75784 100644 --- a/x/gov/keeper/msg_server.go +++ b/x/gov/keeper/msg_server.go @@ -150,7 +150,10 @@ func (k msgServer) CancelProposal(goCtx context.Context, msg *v1.MsgCancelPropos func (k msgServer) ExecLegacyContent(goCtx context.Context, msg *v1.MsgExecLegacyContent) (*v1.MsgExecLegacyContentResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - govAcct := k.GetGovernanceAccount(ctx).GetAddress().String() + govAcct, err := k.authKeeper.AddressCodec().BytesToString(k.GetGovernanceAccount(ctx).GetAddress()) + if err != nil { + return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid governance account address: %s", err) + } if govAcct != msg.Authority { return nil, errors.Wrapf(govtypes.ErrInvalidSigner, "expected %s got %s", govAcct, msg.Authority) } diff --git a/x/gov/migrations/v3/convert.go b/x/gov/migrations/v3/convert.go index d584ac5eced5..9511fca6f4e2 100644 --- a/x/gov/migrations/v3/convert.go +++ b/x/gov/migrations/v3/convert.go @@ -1,3 +1,4 @@ +//nolint:staticcheck // legacy types used for migration package v3 import ( @@ -163,7 +164,6 @@ func convertToNewVotes(oldVotes v1beta1.Votes) (v1.Votes, error) { case oldVote.Option != v1beta1.OptionEmpty: newWVOs = v1.NewNonSplitVoteOption(v1.VoteOption(oldVote.Option)) - default: return nil, fmt.Errorf("vote does not have neither InterfaceRegistryOptions nor Option") } diff --git a/x/gov/migrations/v3/store_test.go b/x/gov/migrations/v3/store_test.go index 1bd37b66f65a..1937d5c37eb3 100644 --- a/x/gov/migrations/v3/store_test.go +++ b/x/gov/migrations/v3/store_test.go @@ -47,7 +47,7 @@ func TestMigrateStore(t *testing.T) { {Option: v1beta1.OptionNo, Weight: math.LegacyMustNewDecFromStr("0.3")}, {Option: v1beta1.OptionYes, Weight: math.LegacyMustNewDecFromStr("0.7")}, } - vote1 := v1beta1.NewVote(1, voter, options) + vote1 := v1beta1.Vote{ProposalId: 1, Voter: voter.String(), Options: options} vote1Bz := cdc.MustMarshal(&vote1) store.Set(v1gov.VoteKey(1, voter), vote1Bz) diff --git a/x/gov/types/v1beta1/vote.go b/x/gov/types/v1beta1/vote.go index b3d47be3100a..86407be8def7 100644 --- a/x/gov/types/v1beta1/vote.go +++ b/x/gov/types/v1beta1/vote.go @@ -5,15 +5,8 @@ import ( "strings" sdkmath "cosmossdk.io/math" - - sdk "github.com/cosmos/cosmos-sdk/types" ) -// NewVote creates a new Vote instance. -func NewVote(proposalID uint64, voter sdk.AccAddress, options WeightedVoteOptions) Vote { - return Vote{ProposalId: proposalID, Voter: voter.String(), Options: options} -} - // Empty returns whether a vote is empty. func (v Vote) Empty() bool { return v.String() == (&Vote{}).String() diff --git a/x/mint/module.go b/x/mint/module.go index 211ce814d2d7..b1815f81f33b 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -238,6 +238,11 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } + as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + k := keeper.NewKeeper( in.Cdc, in.StoreService, @@ -245,7 +250,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.AccountKeeper, in.BankKeeper, feeCollectorName, - authority.String(), + as, ) // when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn diff --git a/x/mint/testutil/expected_keepers_mocks.go b/x/mint/testutil/expected_keepers_mocks.go index 26be50dd5327..baf38f7dc2bf 100644 --- a/x/mint/testutil/expected_keepers_mocks.go +++ b/x/mint/testutil/expected_keepers_mocks.go @@ -8,6 +8,7 @@ import ( context "context" reflect "reflect" + address "cosmossdk.io/core/address" math "cosmossdk.io/math" types "github.com/cosmos/cosmos-sdk/types" gomock "github.com/golang/mock/gomock" @@ -89,6 +90,20 @@ func (m *MockAccountKeeper) EXPECT() *MockAccountKeeperMockRecorder { return m.recorder } +// AddressCodec mocks base method. +func (m *MockAccountKeeper) AddressCodec() address.Codec { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AddressCodec") + ret0, _ := ret[0].(address.Codec) + return ret0 +} + +// AddressCodec indicates an expected call of AddressCodec. +func (mr *MockAccountKeeperMockRecorder) AddressCodec() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddressCodec", reflect.TypeOf((*MockAccountKeeper)(nil).AddressCodec)) +} + // GetModuleAccount mocks base method. func (m *MockAccountKeeper) GetModuleAccount(ctx context.Context, moduleName string) types.ModuleAccountI { m.ctrl.T.Helper() diff --git a/x/mint/types/expected_keepers.go b/x/mint/types/expected_keepers.go index 82edcc6a2092..a184ff2cd118 100644 --- a/x/mint/types/expected_keepers.go +++ b/x/mint/types/expected_keepers.go @@ -3,6 +3,7 @@ package types // noalias import ( context "context" + "cosmossdk.io/core/address" "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,6 +17,7 @@ type StakingKeeper interface { // AccountKeeper defines the contract required for account APIs. type AccountKeeper interface { + AddressCodec() address.Codec GetModuleAddress(name string) sdk.AccAddress // TODO remove with genesis 2-phases refactor https://github.com/cosmos/cosmos-sdk/issues/2862 diff --git a/x/staking/module.go b/x/staking/module.go index a90a2dc52ccc..1e0809cf1d3e 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -232,12 +232,17 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } + as, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + k := keeper.NewKeeper( in.Cdc, in.StoreService, in.AccountKeeper, in.BankKeeper, - authority.String(), + as, in.ValidatorAddressCodec, in.ConsensusAddressCodec, )