From f499bbf2138b171d6e5396a37df7699952e76bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Toledano?= Date: Mon, 15 Apr 2024 15:57:32 +0200 Subject: [PATCH] refactor(x): remove Address.String() (#20048) --- x/accounts/cli/cli.go | 14 ++++++++++---- x/authz/keeper/keeper.go | 12 +++++++++++- x/circuit/depinject.go | 7 ++++++- x/circuit/keeper/genesis_test.go | 13 ++++++++----- x/circuit/keeper/keeper_test.go | 6 ++++-- x/consensus/depinject.go | 15 +++++++++++---- x/consensus/keeper/keeper_test.go | 5 ++++- x/mint/simulation/proposals.go | 9 +++++++-- x/mint/simulation/proposals_test.go | 9 ++++++--- x/slashing/keeper/signing_info.go | 12 ++++++++++-- x/slashing/keeper/signing_info_test.go | 7 +++++-- 11 files changed, 82 insertions(+), 27 deletions(-) diff --git a/x/accounts/cli/cli.go b/x/accounts/cli/cli.go index ed7de6d51618..a8ebbd914d21 100644 --- a/x/accounts/cli/cli.go +++ b/x/accounts/cli/cli.go @@ -49,7 +49,10 @@ func GetTxInitCmd() *cobra.Command { if err != nil { return err } - sender := clientCtx.GetFromAddress() + sender, err := clientCtx.AddressCodec.BytesToString(clientCtx.GetFromAddress()) + if err != nil { + return err + } // we need to convert the message from json to a protobuf message // to know which message to use, we need to know the account type @@ -67,7 +70,7 @@ func GetTxInitCmd() *cobra.Command { return err } msg := v1.MsgInit{ - Sender: sender.String(), + Sender: sender, AccountType: args[0], Message: msgBytes, } @@ -89,7 +92,10 @@ func GetExecuteCmd() *cobra.Command { if err != nil { return err } - sender := clientCtx.GetFromAddress() + sender, err := clientCtx.AddressCodec.BytesToString(clientCtx.GetFromAddress()) + if err != nil { + return err + } schema, err := getSchemaForAccount(clientCtx, args[0]) if err != nil { @@ -101,7 +107,7 @@ func GetExecuteCmd() *cobra.Command { return err } msg := v1.MsgExecute{ - Sender: sender.String(), + Sender: sender, Target: args[0], Message: msgBytes, } diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 3feafa900325..8d580f5e1f8b 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -220,8 +220,18 @@ func (k Keeper) DeleteGrant(ctx context.Context, grantee, granter sdk.AccAddress skey := grantStoreKey(grantee, granter, msgType) grant, found := k.getGrant(ctx, skey) if !found { + granterAddr, err := k.authKeeper.AddressCodec().BytesToString(granter) + if err != nil { + return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, + "could not convert granter address to string") + } + granteeAddr, err := k.authKeeper.AddressCodec().BytesToString(grantee) + if err != nil { + return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, + "could not convert grantee address to string") + } return errorsmod.Wrapf(authz.ErrNoAuthorizationFound, - "failed to delete grant with given granter: %s, grantee: %s & msgType: %s ", granter.String(), grantee.String(), msgType) + "failed to delete grant with given granter: %s, grantee: %s & msgType: %s ", granterAddr, granteeAddr, msgType) } if grant.Expiration != nil { diff --git a/x/circuit/depinject.go b/x/circuit/depinject.go index 1ec1d4d9662d..c8e4a085ede2 100644 --- a/x/circuit/depinject.go +++ b/x/circuit/depinject.go @@ -51,10 +51,15 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } + authorityAddr, err := in.AddressCodec.BytesToString(authority) + if err != nil { + panic(err) + } + circuitkeeper := keeper.NewKeeper( in.Environment, in.Cdc, - authority.String(), + authorityAddr, in.AddressCodec, ) m := NewAppModule(in.Cdc, circuitkeeper) diff --git a/x/circuit/keeper/genesis_test.go b/x/circuit/keeper/genesis_test.go index e77c474923ca..b628b0514876 100644 --- a/x/circuit/keeper/genesis_test.go +++ b/x/circuit/keeper/genesis_test.go @@ -18,7 +18,6 @@ import ( codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "github.com/cosmos/cosmos-sdk/runtime" "github.com/cosmos/cosmos-sdk/testutil" - sdk "github.com/cosmos/cosmos-sdk/types" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" ) @@ -43,14 +42,16 @@ func (s *GenesisTestSuite) SetupTest() { sdkCtx := testCtx.Ctx s.ctx = sdkCtx s.cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry) - authority := authtypes.NewModuleAddress("gov") ac := addresscodec.NewBech32Codec("cosmos") - bz, err := ac.StringToBytes(authority.String()) + authority, err := ac.BytesToString(authtypes.NewModuleAddress("gov")) + s.Require().NoError(err) + + bz, err := ac.StringToBytes(authority) s.Require().NoError(err) s.addrBytes = bz - s.keeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), s.cdc, authority.String(), ac) + s.keeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()), s.cdc, authority, ac) } func (s *GenesisTestSuite) TestInitExportGenesis() { @@ -62,8 +63,10 @@ func (s *GenesisTestSuite) TestInitExportGenesis() { s.Require().NoError(err) var accounts []*types.GenesisAccountPermissions + addr, err := addresscodec.NewBech32Codec("cosmos").BytesToString(s.addrBytes) + s.Require().NoError(err) genAccsPerms := types.GenesisAccountPermissions{ - Address: sdk.AccAddress(s.addrBytes).String(), + Address: addr, Permissions: &perms, } accounts = append(accounts, &genAccsPerms) diff --git a/x/circuit/keeper/keeper_test.go b/x/circuit/keeper/keeper_test.go index 38ce34b1e14a..37a93547b63e 100644 --- a/x/circuit/keeper/keeper_test.go +++ b/x/circuit/keeper/keeper_test.go @@ -46,9 +46,11 @@ func initFixture(t *testing.T) *fixture { mockStoreKey := storetypes.NewKVStoreKey("test") env := runtime.NewEnvironment(runtime.NewKVStoreService(mockStoreKey), log.NewNopLogger()) - k := keeper.NewKeeper(env, encCfg.Codec, authtypes.NewModuleAddress("gov").String(), ac) + authority, err := ac.BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + k := keeper.NewKeeper(env, encCfg.Codec, authority, ac) - bz, err := ac.StringToBytes(authtypes.NewModuleAddress("gov").String()) + bz, err := ac.StringToBytes(authority) require.NoError(t, err) return &fixture{ diff --git a/x/consensus/depinject.go b/x/consensus/depinject.go index 695c609e172d..03729f90ab0a 100644 --- a/x/consensus/depinject.go +++ b/x/consensus/depinject.go @@ -2,6 +2,7 @@ package consensus import ( modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1" + "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" @@ -28,9 +29,10 @@ func init() { type ModuleInputs struct { depinject.In - Config *modulev1.Module - Cdc codec.Codec - Environment appmodule.Environment + Config *modulev1.Module + Cdc codec.Codec + Environment appmodule.Environment + AddressCodec address.Codec } type ModuleOutputs struct { @@ -48,7 +50,12 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } - k := keeper.NewKeeper(in.Cdc, in.Environment, authority.String()) + authorityAddr, err := in.AddressCodec.BytesToString(authority) + if err != nil { + panic(err) + } + + k := keeper.NewKeeper(in.Cdc, in.Environment, authorityAddr) m := NewAppModule(in.Cdc, k) baseappOpt := func(app *baseapp.BaseApp) { app.SetParamStore(k.ParamsStore) diff --git a/x/consensus/keeper/keeper_test.go b/x/consensus/keeper/keeper_test.go index 64edfe1018f4..782da78c9c8a 100644 --- a/x/consensus/keeper/keeper_test.go +++ b/x/consensus/keeper/keeper_test.go @@ -36,7 +36,10 @@ func (s *KeeperTestSuite) SetupTest() { encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}) env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) - keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, env, authtypes.NewModuleAddress("gov").String()) + authority, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + s.Require().NoError(err) + + keeper := consensusparamkeeper.NewKeeper(encCfg.Codec, env, authority) s.ctx = ctx s.consensusParamsKeeper = &keeper diff --git a/x/mint/simulation/proposals.go b/x/mint/simulation/proposals.go index e1d5471bf970..be6320ac8326 100644 --- a/x/mint/simulation/proposals.go +++ b/x/mint/simulation/proposals.go @@ -32,10 +32,15 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg { } // SimulateMsgUpdateParams returns a random MsgUpdateParams -func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.Codec) (sdk.Msg, error) { +func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, ac coreaddress.Codec) (sdk.Msg, error) { // use the default gov module account address as authority var authority sdk.AccAddress = address.Module("gov") + authorityAddr, err := ac.BytesToString(authority) + if err != nil { + return nil, err + } + params := types.DefaultParams() params.BlocksPerYear = uint64(simtypes.RandIntBetween(r, 1, 1000000)) params.GoalBonded = sdkmath.LegacyNewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) @@ -45,7 +50,7 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.C params.MintDenom = simtypes.RandStringOfLength(r, 10) return &types.MsgUpdateParams{ - Authority: authority.String(), + Authority: authorityAddr, Params: params, }, nil } diff --git a/x/mint/simulation/proposals_test.go b/x/mint/simulation/proposals_test.go index 2f7ee4f85439..767bbfad1508 100644 --- a/x/mint/simulation/proposals_test.go +++ b/x/mint/simulation/proposals_test.go @@ -11,12 +11,12 @@ import ( "cosmossdk.io/x/mint/types" codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/address" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" ) func TestProposalMsgs(t *testing.T) { + ac := codectestutil.CodecOptions{}.GetAddressCodec() // initialize parameters s := rand.NewSource(1) r := rand.New(s) @@ -33,12 +33,15 @@ func TestProposalMsgs(t *testing.T) { assert.Equal(t, simulation.OpWeightMsgUpdateParams, w0.AppParamsKey()) assert.Equal(t, simulation.DefaultWeightMsgUpdateParams, w0.DefaultWeight()) - msg, err := w0.MsgSimulatorFn()(r, accounts, codectestutil.CodecOptions{}.GetAddressCodec()) + msg, err := w0.MsgSimulatorFn()(r, accounts, ac) assert.NilError(t, err) msgUpdateParams, ok := msg.(*types.MsgUpdateParams) assert.Assert(t, ok) - assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) + authority, err := ac.BytesToString(address.Module("gov")) + assert.NilError(t, err) + + assert.Equal(t, authority, msgUpdateParams.Authority) assert.Equal(t, uint64(122877), msgUpdateParams.Params.BlocksPerYear) assert.DeepEqual(t, sdkmath.LegacyNewDecWithPrec(95, 2), msgUpdateParams.Params.GoalBonded) assert.DeepEqual(t, sdkmath.LegacyNewDecWithPrec(94, 2), msgUpdateParams.Params.InflationMax) diff --git a/x/slashing/keeper/signing_info.go b/x/slashing/keeper/signing_info.go index 35fca38e2661..d4ab17a35582 100644 --- a/x/slashing/keeper/signing_info.go +++ b/x/slashing/keeper/signing_info.go @@ -28,7 +28,11 @@ func (k Keeper) HasValidatorSigningInfo(ctx context.Context, consAddr sdk.ConsAd func (k Keeper) JailUntil(ctx context.Context, consAddr sdk.ConsAddress, jailTime time.Time) error { signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr) if err != nil { - return errorsmod.Wrap(err, fmt.Sprintf("cannot jail validator with consensus address %s that does not have any signing information", consAddr.String())) + addr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr) + if err != nil { + return types.ErrNoSigningInfoFound.Wrapf("could not convert consensus address to string. Error: %s", err.Error()) + } + return errorsmod.Wrap(err, fmt.Sprintf("cannot jail validator with consensus address %s that does not have any signing information", addr)) } signInfo.JailedUntil = jailTime @@ -39,7 +43,11 @@ func (k Keeper) JailUntil(ctx context.Context, consAddr sdk.ConsAddress, jailTim func (k Keeper) Tombstone(ctx context.Context, consAddr sdk.ConsAddress) error { signInfo, err := k.ValidatorSigningInfo.Get(ctx, consAddr) if err != nil { - return types.ErrNoSigningInfoFound.Wrap(fmt.Sprintf("cannot tombstone validator with consensus address %s that does not have any signing information", consAddr.String())) + addr, err := k.sk.ConsensusAddressCodec().BytesToString(consAddr) + if err != nil { + return types.ErrNoSigningInfoFound.Wrapf("could not convert consensus address to string. Error: %s", err.Error()) + } + return types.ErrNoSigningInfoFound.Wrap(fmt.Sprintf("cannot tombstone validator with consensus address %s that does not have any signing information", addr)) } if signInfo.Tombstoned { diff --git a/x/slashing/keeper/signing_info_test.go b/x/slashing/keeper/signing_info_test.go index ff0c65bb17ef..b616bc9e6c4e 100644 --- a/x/slashing/keeper/signing_info_test.go +++ b/x/slashing/keeper/signing_info_test.go @@ -121,15 +121,18 @@ func (s *KeeperTestSuite) TestPerformConsensusPubKeyUpdate() { oldConsAddr := sdk.ConsAddress(pks[0].Address()) newConsAddr := sdk.ConsAddress(pks[1].Address()) + consAddr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(newConsAddr) + s.Require().NoError(err) + newInfo := slashingtypes.NewValidatorSigningInfo( - newConsAddr.String(), + consAddr, int64(4), time.Unix(2, 0).UTC(), false, int64(10), ) - err := slashingKeeper.ValidatorSigningInfo.Set(ctx, oldConsAddr, newInfo) + err = slashingKeeper.ValidatorSigningInfo.Set(ctx, oldConsAddr, newInfo) require.NoError(err) s.stakingKeeper.EXPECT().ValidatorIdentifier(gomock.Any(), oldConsAddr).Return(oldConsAddr, nil)