Skip to content

Commit

Permalink
refactor(x): remove Address.String() (cosmos#20048)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano authored Apr 15, 2024
1 parent 0f39ba3 commit f499bbf
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 27 deletions.
14 changes: 10 additions & 4 deletions x/accounts/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,7 +70,7 @@ func GetTxInitCmd() *cobra.Command {
return err
}
msg := v1.MsgInit{
Sender: sender.String(),
Sender: sender,
AccountType: args[0],
Message: msgBytes,
}
Expand All @@ -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 {
Expand All @@ -101,7 +107,7 @@ func GetExecuteCmd() *cobra.Command {
return err
}
msg := v1.MsgExecute{
Sender: sender.String(),
Sender: sender,
Target: args[0],
Message: msgBytes,
}
Expand Down
12 changes: 11 additions & 1 deletion x/authz/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion x/circuit/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions x/circuit/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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() {
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions x/circuit/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 11 additions & 4 deletions x/consensus/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion x/consensus/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions x/mint/simulation/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
9 changes: 6 additions & 3 deletions x/mint/simulation/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions x/slashing/keeper/signing_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions x/slashing/keeper/signing_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f499bbf

Please sign in to comment.