From b832358dd6ec25255ba2bf3926dcb53d78c2c4b2 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 12:16:46 +0100 Subject: [PATCH 1/7] del: remove AccAccount String --- x/distribution/CHANGELOG.md | 2 + x/distribution/depinject.go | 8 +- x/distribution/keeper/allocation_test.go | 24 +++-- x/distribution/keeper/delegation_test.go | 94 +++++++++++++++---- x/distribution/keeper/genesis.go | 18 +++- x/distribution/keeper/grpc_query.go | 14 ++- x/distribution/keeper/grpc_query_test.go | 8 +- x/distribution/keeper/keeper.go | 7 +- x/distribution/keeper/keeper_test.go | 8 +- x/distribution/keeper/msg_server_test.go | 54 +++++++---- .../migrations/v4/migrate_funds_test.go | 15 +-- x/distribution/simulation/operations.go | 18 +++- x/distribution/simulation/proposals.go | 9 +- x/distribution/simulation/proposals_test.go | 8 +- x/distribution/testutil/staking_helper.go | 11 ++- x/distribution/types/msg.go | 6 +- 16 files changed, 233 insertions(+), 71 deletions(-) diff --git a/x/distribution/CHANGELOG.md b/x/distribution/CHANGELOG.md index 610ac8103bce..44e6d007fe23 100644 --- a/x/distribution/CHANGELOG.md +++ b/x/distribution/CHANGELOG.md @@ -31,6 +31,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes +* []() Removes Accounts String method + * `NewMsgSetWithdrawAddress` now takes strings as argument instead of `sdk.AccAddress` * [#19445](https://github.com/cosmos/cosmos-sdk/pull/19445) `appmodule.Environment` is received on the Keeper to get access to different application services * [#17115](https://github.com/cosmos/cosmos-sdk/pull/17115) Use collections for `PreviousProposer` and `ValidatorSlashEvents`: * remove from `Keeper`: `GetPreviousProposerConsAddr`, `SetPreviousProposerConsAddr`, `GetValidatorHistoricalReferenceCount`, `GetValidatorSlashEvent`, `SetValidatorSlashEvent`. diff --git a/x/distribution/depinject.go b/x/distribution/depinject.go index 09303459a44a..b09900a94cb1 100644 --- a/x/distribution/depinject.go +++ b/x/distribution/depinject.go @@ -9,7 +9,6 @@ import ( "cosmossdk.io/x/distribution/keeper" "cosmossdk.io/x/distribution/types" staking "cosmossdk.io/x/staking/types" - "github.com/cosmos/cosmos-sdk/codec" ) @@ -57,6 +56,11 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority) } + authorityAddr, err := in.AccountKeeper.AddressCodec().BytesToString(authority) + if err != nil { + panic(err) + } + k := keeper.NewKeeper( in.Cdc, in.Environment, @@ -65,7 +69,7 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { in.StakingKeeper, in.PoolKeeper, feeCollectorName, - authority.String(), + authorityAddr, ) m := NewAppModule(in.Cdc, k, in.AccountKeeper, in.BankKeeper, in.StakingKeeper, in.PoolKeeper) diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index d0286dd64131..324524cae2c9 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -32,7 +32,8 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { ctrl := gomock.NewController(t) key := storetypes.NewKVStoreKey(disttypes.StoreKey) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) - encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) + cdcOpts := codectestutil.CodecOptions{} + encCfg := moduletestutil.MakeTestEncodingConfig(cdcOpts, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) @@ -47,6 +48,9 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress()) stakingKeeper.EXPECT().ValidatorAddressCodec().Return(valCodec).AnyTimes() + authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -55,7 +59,7 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // create validator with 50% commission @@ -92,7 +96,8 @@ func TestAllocateTokensToManyValidators(t *testing.T) { ctrl := gomock.NewController(t) key := storetypes.NewKVStoreKey(disttypes.StoreKey) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) - encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) + cdcOpts := codectestutil.CodecOptions{} + encCfg := moduletestutil.MakeTestEncodingConfig(cdcOpts, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) @@ -107,6 +112,9 @@ func TestAllocateTokensToManyValidators(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -115,7 +123,7 @@ func TestAllocateTokensToManyValidators(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool & set params @@ -224,7 +232,8 @@ func TestAllocateTokensTruncation(t *testing.T) { ctrl := gomock.NewController(t) key := storetypes.NewKVStoreKey(disttypes.StoreKey) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) - encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) + cdcOpts := codectestutil.CodecOptions{} + encCfg := moduletestutil.MakeTestEncodingConfig(cdcOpts, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}) bankKeeper := distrtestutil.NewMockBankKeeper(ctrl) @@ -239,6 +248,9 @@ func TestAllocateTokensTruncation(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -247,7 +259,7 @@ func TestAllocateTokensTruncation(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index fc11b8b27d7f..d480b943a289 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -44,6 +44,9 @@ func TestCalculateRewardsBasic(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -52,7 +55,7 @@ func TestCalculateRewardsBasic(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -66,8 +69,11 @@ func TestCalculateRewardsBasic(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // delegation mock - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -147,6 +153,9 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -155,7 +164,7 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -171,7 +180,10 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) // set mock calls stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) @@ -253,6 +265,9 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -261,7 +276,7 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -277,8 +292,11 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // delegation mocks - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -380,6 +398,9 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -388,7 +409,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -401,9 +422,12 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) require.NoError(t, err) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr0) + require.NoError(t, err) + val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - del0 := stakingtypes.NewDelegation(addr0.String(), valAddr.String(), val.DelegatorShares) + del0 := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) // set mock calls stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) @@ -423,7 +447,7 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { // second delegation addr1 := sdk.AccAddress(valConsAddr1) - _, del1, err := distrtestutil.Delegate(ctx, distrKeeper, addr1, &val, math.NewInt(100), nil, stakingKeeper) + _, del1, err := distrtestutil.Delegate(ctx, distrKeeper, addr1, &val, math.NewInt(100), nil, stakingKeeper, accountKeeper.AddressCodec()) require.NoError(t, err) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr1, valAddr).Return(del1, nil) @@ -480,6 +504,9 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -488,7 +515,7 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -503,8 +530,11 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // delegation mock - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(5) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(3) @@ -558,6 +588,9 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -566,7 +599,7 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -581,8 +614,11 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // delegation mock - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(5) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -677,6 +713,9 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -685,7 +724,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -701,8 +740,11 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // validator and delegation mocks - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -746,6 +788,7 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), nil, stakingKeeper, + accountKeeper.AddressCodec(), ) require.NoError(t, err) @@ -819,6 +862,9 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -827,7 +873,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -841,8 +887,11 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // validator and delegation mocks - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(5) @@ -871,6 +920,7 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { math.NewInt(100), nil, stakingKeeper, + accountKeeper.AddressCodec(), ) require.NoError(t, err) @@ -1022,6 +1072,9 @@ func Test100PercentCommissionReward(t *testing.T) { env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := accountKeeper.AddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -1030,7 +1083,7 @@ func Test100PercentCommissionReward(t *testing.T) { stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) // reset fee pool @@ -1044,8 +1097,11 @@ func Test100PercentCommissionReward(t *testing.T) { require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDec(0)) + addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) + require.NoError(t, err) + // validator and delegation mocks - del := stakingtypes.NewDelegation(addr.String(), valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(3) diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index 527ef35ba952..e5fcfc91ef4a 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -159,9 +159,17 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) var dwi []types.DelegatorWithdrawInfo err = k.DelegatorsWithdrawAddress.Walk(ctx, nil, func(key, value sdk.AccAddress) (stop bool, err error) { + keyAddr, err := k.authKeeper.AddressCodec().BytesToString(key) + if err != nil { + return true, err + } + valueAddr, err := k.authKeeper.AddressCodec().BytesToString(value) + if err != nil { + return true, err + } dwi = append(dwi, types.DelegatorWithdrawInfo{ - DelegatorAddress: key.String(), - WithdrawAddress: value.String(), + DelegatorAddress: keyAddr, + WithdrawAddress: valueAddr, }) return false, nil }) @@ -231,8 +239,12 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) dels := make([]types.DelegatorStartingInfoRecord, 0) err = k.DelegatorStartingInfo.Walk(ctx, nil, func(key collections.Pair[sdk.ValAddress, sdk.AccAddress], value types.DelegatorStartingInfo) (stop bool, err error) { + delAddr, err := k.authKeeper.AddressCodec().BytesToString(key.K2()) + if err != nil { + return true, err + } dels = append(dels, types.DelegatorStartingInfoRecord{ - DelegatorAddress: key.K2().String(), + DelegatorAddress: delAddr, ValidatorAddress: key.K1().String(), StartingInfo: value, }) diff --git a/x/distribution/keeper/grpc_query.go b/x/distribution/keeper/grpc_query.go index 38889665a0e4..62b602c8a805 100644 --- a/x/distribution/keeper/grpc_query.go +++ b/x/distribution/keeper/grpc_query.go @@ -86,9 +86,14 @@ func (k Querier) ValidatorDistributionInfo(ctx context.Context, req *types.Query return nil, err } + operatorAddr, err := k.authKeeper.AddressCodec().BytesToString(delAdr) + if err != nil { + return nil, err + } + return &types.QueryValidatorDistributionInfoResponse{ Commission: validatorCommission.Commission, - OperatorAddress: delAdr.String(), + OperatorAddress: operatorAddr, SelfBondRewards: rewards, }, nil } @@ -356,7 +361,12 @@ func (k Querier) DelegatorWithdrawAddress(ctx context.Context, req *types.QueryD return nil, err } - return &types.QueryDelegatorWithdrawAddressResponse{WithdrawAddress: withdrawAddr.String()}, nil + addr, err := k.authKeeper.AddressCodec().BytesToString(withdrawAddr) + if err != nil { + return nil, err + } + + return &types.QueryDelegatorWithdrawAddressResponse{WithdrawAddress: addr}, nil } // Deprecated: DO NOT USE diff --git a/x/distribution/keeper/grpc_query_test.go b/x/distribution/keeper/grpc_query_test.go index bd7c4fb03d0c..7a794656fc99 100644 --- a/x/distribution/keeper/grpc_query_test.go +++ b/x/distribution/keeper/grpc_query_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "testing" "github.com/golang/mock/gomock" @@ -58,7 +59,10 @@ func TestQueryValidatorDistributionInfo(t *testing.T) { val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) require.NoError(t, err) - del := stakingtypes.NewDelegation(addrs[0].String(), val.OperatorAddress, val.DelegatorShares) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + + del := stakingtypes.NewDelegation(addr0Str, val.OperatorAddress, val.DelegatorShares) dep.stakingKeeper.EXPECT().Validator(gomock.Any(), gomock.Any()).Return(val, nil).AnyTimes() dep.stakingKeeper.EXPECT().Delegation(gomock.Any(), gomock.Any(), gomock.Any()).Return(del, nil).AnyTimes() @@ -80,7 +84,7 @@ func TestQueryValidatorDistributionInfo(t *testing.T) { { name: "not a validator", req: &types.QueryValidatorDistributionInfoRequest{ - ValidatorAddress: addrs[0].String(), + ValidatorAddress: addr0Str, }, resp: &types.QueryValidatorDistributionInfoResponse{}, errMsg: `expected 'cosmosvaloper' got 'cosmos'`, diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index fab55212f68b..845666de3d21 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -165,9 +165,14 @@ func (k Keeper) SetWithdrawAddr(ctx context.Context, delegatorAddr, withdrawAddr return types.ErrSetWithdrawAddrDisabled } + addr, err := k.authKeeper.AddressCodec().BytesToString(withdrawAddr) + if err != nil { + return err + } + if err = k.environment.EventService.EventManager(ctx).EmitKV( types.EventTypeSetWithdrawAddress, - event.NewAttribute(types.AttributeKeyWithdrawAddress, withdrawAddr.String()), + event.NewAttribute(types.AttributeKeyWithdrawAddress, addr), ); err != nil { return err } diff --git a/x/distribution/keeper/keeper_test.go b/x/distribution/keeper/keeper_test.go index 3456466401f9..c148529fcf41 100644 --- a/x/distribution/keeper/keeper_test.go +++ b/x/distribution/keeper/keeper_test.go @@ -39,7 +39,8 @@ func initFixture(t *testing.T) (sdk.Context, []sdk.AccAddress, keeper.Keeper, de ctrl := gomock.NewController(t) key := storetypes.NewKVStoreKey(types.StoreKey) testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) - encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, distribution.AppModule{}) + cdcOpts := codectestutil.CodecOptions{} + encCfg := moduletestutil.MakeTestEncodingConfig(cdcOpts, distribution.AppModule{}) ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}) addrs := simtestutil.CreateIncrementalAccounts(2) @@ -59,6 +60,9 @@ func initFixture(t *testing.T) (sdk.Context, []sdk.AccAddress, keeper.Keeper, de env := runtime.NewEnvironment(runtime.NewKVStoreService(key), log.NewNopLogger()) + authorityAddr, err := cdcOpts.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + distrKeeper := keeper.NewKeeper( encCfg.Codec, env, @@ -67,7 +71,7 @@ func initFixture(t *testing.T) (sdk.Context, []sdk.AccAddress, keeper.Keeper, de stakingKeeper, poolKeeper, "fee_collector", - authtypes.NewModuleAddress("gov").String(), + authorityAddr, ) params := types.DefaultParams() diff --git a/x/distribution/keeper/msg_server_test.go b/x/distribution/keeper/msg_server_test.go index a4aa0f97d0c8..b761487333d9 100644 --- a/x/distribution/keeper/msg_server_test.go +++ b/x/distribution/keeper/msg_server_test.go @@ -11,6 +11,7 @@ import ( "cosmossdk.io/x/distribution/keeper" "cosmossdk.io/x/distribution/types" + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -18,6 +19,11 @@ func TestMsgSetWithdrawAddress(t *testing.T) { ctx, addrs, distrKeeper, _ := initFixture(t) msgServer := keeper.NewMsgServerImpl(distrKeeper) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + addr1Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[1]) + require.NoError(t, err) + cases := []struct { name string msg *types.MsgSetWithdrawAddress @@ -26,8 +32,8 @@ func TestMsgSetWithdrawAddress(t *testing.T) { { name: "success", msg: &types.MsgSetWithdrawAddress{ - DelegatorAddress: addrs[0].String(), - WithdrawAddress: addrs[1].String(), + DelegatorAddress: addr0Str, + WithdrawAddress: addr1Str, }, errMsg: "", }, @@ -35,14 +41,14 @@ func TestMsgSetWithdrawAddress(t *testing.T) { name: "invalid delegator address", msg: &types.MsgSetWithdrawAddress{ DelegatorAddress: "invalid", - WithdrawAddress: addrs[1].String(), + WithdrawAddress: addr1Str, }, errMsg: "invalid address", }, { name: "invalid withdraw address", msg: &types.MsgSetWithdrawAddress{ - DelegatorAddress: addrs[0].String(), + DelegatorAddress: addr0Str, WithdrawAddress: "invalid", }, errMsg: "invalid address", @@ -68,6 +74,9 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { dep.stakingKeeper.EXPECT().Validator(gomock.Any(), gomock.Any()).AnyTimes() msgServer := keeper.NewMsgServerImpl(distrKeeper) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + cases := []struct { name string preRun func() @@ -85,7 +94,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { { name: "invalid validator address", msg: &types.MsgWithdrawDelegatorReward{ - DelegatorAddress: addrs[0].String(), + DelegatorAddress: addr0Str, ValidatorAddress: "invalid", }, errMsg: "invalid validator address", @@ -93,7 +102,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { { name: "no validator", msg: &types.MsgWithdrawDelegatorReward{ - DelegatorAddress: addrs[0].String(), + DelegatorAddress: addr0Str, ValidatorAddress: sdk.ValAddress(addrs[1]).String(), }, errMsg: "no validator distribution info", @@ -165,6 +174,9 @@ func TestMsgFundCommunityPool(t *testing.T) { dep.poolKeeper.EXPECT().FundCommunityPool(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() msgServer := keeper.NewMsgServerImpl(distrKeeper) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + cases := []struct { name string msg *types.MsgFundCommunityPool //nolint:staticcheck // Testing deprecated method @@ -181,7 +193,7 @@ func TestMsgFundCommunityPool(t *testing.T) { { name: "success", msg: &types.MsgFundCommunityPool{ //nolint:staticcheck // Testing deprecated method - Depositor: addrs[0].String(), + Depositor: addr0Str, Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000))), }, }, @@ -205,6 +217,11 @@ func TestMsgUpdateParams(t *testing.T) { ctx, addrs, distrKeeper, _ := initFixture(t) msgServer := keeper.NewMsgServerImpl(distrKeeper) + authorityAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + cases := []struct { name string msg *types.MsgUpdateParams @@ -221,7 +238,7 @@ func TestMsgUpdateParams(t *testing.T) { { name: "incorrect authority", msg: &types.MsgUpdateParams{ - Authority: addrs[0].String(), + Authority: addr0Str, Params: types.DefaultParams(), }, errMsg: "expected authority account as only signer for proposal message", @@ -229,7 +246,7 @@ func TestMsgUpdateParams(t *testing.T) { { name: "invalid params", msg: &types.MsgUpdateParams{ - Authority: authtypes.NewModuleAddress("gov").String(), + Authority: authorityAddr, Params: types.Params{CommunityTax: math.LegacyNewDec(-1)}, }, errMsg: "community tax must be positive", @@ -237,7 +254,7 @@ func TestMsgUpdateParams(t *testing.T) { { name: "success", msg: &types.MsgUpdateParams{ - Authority: authtypes.NewModuleAddress("gov").String(), + Authority: authorityAddr, Params: types.DefaultParams(), }, }, @@ -262,6 +279,11 @@ func TestMsgCommunityPoolSpend(t *testing.T) { dep.poolKeeper.EXPECT().DistributeFromCommunityPool(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() msgServer := keeper.NewMsgServerImpl(distrKeeper) + authorityAddr, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) + addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) + require.NoError(t, err) + cases := []struct { name string msg *types.MsgCommunityPoolSpend //nolint:staticcheck // Testing deprecated method @@ -278,7 +300,7 @@ func TestMsgCommunityPoolSpend(t *testing.T) { { name: "incorrect authority", msg: &types.MsgCommunityPoolSpend{ //nolint:staticcheck // Testing deprecated method - Authority: addrs[0].String(), + Authority: addr0Str, Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))), }, errMsg: "expected authority account as only signer for proposal message", @@ -286,7 +308,7 @@ func TestMsgCommunityPoolSpend(t *testing.T) { { name: "invalid recipient address", msg: &types.MsgCommunityPoolSpend{ //nolint:staticcheck // Testing deprecated method - Authority: authtypes.NewModuleAddress("gov").String(), + Authority: authorityAddr, Recipient: "invalid", Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))), }, @@ -295,16 +317,16 @@ func TestMsgCommunityPoolSpend(t *testing.T) { { name: "invalid amount", msg: &types.MsgCommunityPoolSpend{ //nolint:staticcheck // Testing deprecated method - Authority: authtypes.NewModuleAddress("gov").String(), - Recipient: addrs[0].String(), + Authority: authorityAddr, + Recipient: addr0Str, }, errMsg: "invalid coins", }, { name: "success", msg: &types.MsgCommunityPoolSpend{ //nolint:staticcheck // Testing deprecated method - Authority: authtypes.NewModuleAddress("gov").String(), - Recipient: addrs[0].String(), + Authority: authorityAddr, + Recipient: addr0Str, Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000))), }, }, diff --git a/x/distribution/migrations/v4/migrate_funds_test.go b/x/distribution/migrations/v4/migrate_funds_test.go index b8994061cbd8..7158037befe5 100644 --- a/x/distribution/migrations/v4/migrate_funds_test.go +++ b/x/distribution/migrations/v4/migrate_funds_test.go @@ -37,13 +37,14 @@ func TestFundsMigration(t *testing.T) { cms := integration.CreateMultiStore(keys, logger) encCfg := moduletestutil.MakeTestEncodingConfig(codectestutil.CodecOptions{}, auth.AppModule{}, bank.AppModule{}, distribution.AppModule{}) ctx := sdk.NewContext(cms, true, logger) - + addressCodec := addresscodec.NewBech32Codec(sdk.Bech32MainPrefix) maccPerms := map[string][]string{ pooltypes.ModuleName: nil, disttypes.ModuleName: {authtypes.Minter}, } - authority := authtypes.NewModuleAddress("gov") + authority, err := addressCodec.BytesToString(authtypes.NewModuleAddress("gov")) + require.NoError(t, err) // create account keeper accountKeeper := authkeeper.NewAccountKeeper( @@ -51,9 +52,9 @@ func TestFundsMigration(t *testing.T) { encCfg.Codec, authtypes.ProtoBaseAccount, maccPerms, - addresscodec.NewBech32Codec(sdk.Bech32MainPrefix), + addressCodec, sdk.Bech32MainPrefix, - authority.String(), + authority, ) // create bank keeper @@ -62,7 +63,7 @@ func TestFundsMigration(t *testing.T) { encCfg.Codec, accountKeeper, map[string]bool{}, - authority.String(), + authority, ) // gomock initializations @@ -79,7 +80,7 @@ func TestFundsMigration(t *testing.T) { stakingKeeper, poolKeeper, disttypes.ModuleName, - authority.String(), + authority, ) // Set feepool @@ -87,7 +88,7 @@ func TestFundsMigration(t *testing.T) { feepool := disttypes.FeePool{ CommunityPool: sdk.NewDecCoinsFromCoins(poolAmount), } - err := distrKeeper.FeePool.Set(ctx, feepool) + err = distrKeeper.FeePool.Set(ctx, feepool) require.NoError(t, err) distrAcc := authtypes.NewEmptyModuleAccount(disttypes.ModuleName) diff --git a/x/distribution/simulation/operations.go b/x/distribution/simulation/operations.go index 1940617aacd8..6fdecf6e3c63 100644 --- a/x/distribution/simulation/operations.go +++ b/x/distribution/simulation/operations.go @@ -91,7 +91,16 @@ func SimulateMsgSetWithdrawAddress(txConfig client.TxConfig, ak types.AccountKee account := ak.GetAccount(ctx, simAccount.Address) spendable := bk.SpendableCoins(ctx, account.GetAddress()) - msg := types.NewMsgSetWithdrawAddress(simAccount.Address, simToAccount.Address) + addr, err := ak.AddressCodec().BytesToString(simAccount.Address) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgSetWithdrawAddress{}), "error converting delegator address"), nil, err + } + toAddr, err := ak.AddressCodec().BytesToString(simToAccount.Address) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgSetWithdrawAddress{}), "error converting withdraw address"), nil, err + } + + msg := types.NewMsgSetWithdrawAddress(addr, toAddr) txCtx := simulation.OperationInput{ R: r, @@ -142,7 +151,12 @@ func SimulateMsgWithdrawDelegatorReward(txConfig client.TxConfig, ak types.Accou account := ak.GetAccount(ctx, simAccount.Address) spendable := bk.SpendableCoins(ctx, account.GetAddress()) - msg := types.NewMsgWithdrawDelegatorReward(simAccount.Address.String(), validator.GetOperator()) + addr, err := ak.AddressCodec().BytesToString(simAccount.Address) + if err != nil { + return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgWithdrawDelegatorReward{}), "error converting delegator address"), nil, err + } + + msg := types.NewMsgWithdrawDelegatorReward(addr, validator.GetOperator()) txCtx := simulation.OperationInput{ R: r, diff --git a/x/distribution/simulation/proposals.go b/x/distribution/simulation/proposals.go index d8559fb747b5..98592d1f8e5f 100644 --- a/x/distribution/simulation/proposals.go +++ b/x/distribution/simulation/proposals.go @@ -32,7 +32,7 @@ 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, cdc coreaddress.Codec) (sdk.Msg, error) { // use the default gov module account address as authority var authority sdk.AccAddress = address.Module("gov") @@ -40,8 +40,13 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ []simtypes.Account, _ coreaddress.C params.CommunityTax = simtypes.RandomDecAmount(r, sdkmath.LegacyNewDec(1)) params.WithdrawAddrEnabled = r.Intn(2) == 0 + authorityAddr, err := cdc.BytesToString(authority) + if err != nil { + return nil, err + } + return &types.MsgUpdateParams{ - Authority: authority.String(), + Authority: authorityAddr, Params: params, }, nil } diff --git a/x/distribution/simulation/proposals_test.go b/x/distribution/simulation/proposals_test.go index 2af78c81e36f..7903be80eb62 100644 --- a/x/distribution/simulation/proposals_test.go +++ b/x/distribution/simulation/proposals_test.go @@ -20,7 +20,7 @@ func TestProposalMsgs(t *testing.T) { // initialize parameters s := rand.NewSource(1) r := rand.New(s) - + addressCodec := codectestutil.CodecOptions{}.GetAddressCodec() accounts := simtypes.RandomAccounts(r, 3) // execute ProposalMsgs function @@ -33,12 +33,14 @@ 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, addressCodec) assert.NilError(t, err) msgUpdateParams, ok := msg.(*types.MsgUpdateParams) assert.Assert(t, ok) - assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) + addr, err := addressCodec.BytesToString(sdk.AccAddress(address.Module("gov"))) + assert.NilError(t, err) + assert.Equal(t, addr, msgUpdateParams.Authority) assert.DeepEqual(t, sdkmath.LegacyNewDec(0), msgUpdateParams.Params.CommunityTax) assert.Equal(t, true, msgUpdateParams.Params.WithdrawAddrEnabled) } diff --git a/x/distribution/testutil/staking_helper.go b/x/distribution/testutil/staking_helper.go index b3bbd3028e56..a3b35deed576 100644 --- a/x/distribution/testutil/staking_helper.go +++ b/x/distribution/testutil/staking_helper.go @@ -3,6 +3,7 @@ package testutil import ( "fmt" + "cosmossdk.io/core/address" "cosmossdk.io/math" "cosmossdk.io/x/distribution/keeper" stakingtypes "cosmossdk.io/x/staking/types" @@ -112,6 +113,7 @@ func Delegate( amount math.Int, delegation *stakingtypes.Delegation, sk *MockStakingKeeper, + addressCodec address.Codec, ) ( newShares math.LegacyDec, updatedDel stakingtypes.Delegation, @@ -125,7 +127,14 @@ func Delegate( err = distrKeeper.Hooks().BeforeDelegationSharesModified(ctx, delegator, valBz) } else { err = distrKeeper.Hooks().BeforeDelegationCreated(ctx, delegator, valBz) - del := stakingtypes.NewDelegation(delegator.String(), validator.GetOperator(), math.LegacyZeroDec()) + if err != nil { + return math.LegacyZeroDec(), stakingtypes.Delegation{}, err + } + delAddr, err := addressCodec.BytesToString(delegator) + if err != nil { + return math.LegacyZeroDec(), stakingtypes.Delegation{}, err + } + del := stakingtypes.NewDelegation(delAddr, validator.GetOperator(), math.LegacyZeroDec()) delegation = &del } diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index 7388d797c331..faece58cd33d 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -15,10 +15,10 @@ var ( _ sdk.Msg = (*MsgDepositValidatorRewardsPool)(nil) ) -func NewMsgSetWithdrawAddress(delAddr, withdrawAddr sdk.AccAddress) *MsgSetWithdrawAddress { +func NewMsgSetWithdrawAddress(delAddr, withdrawAddr string) *MsgSetWithdrawAddress { return &MsgSetWithdrawAddress{ - DelegatorAddress: delAddr.String(), - WithdrawAddress: withdrawAddr.String(), + DelegatorAddress: delAddr, + WithdrawAddress: withdrawAddr, } } From 5c183941f91a969c1686a5aa5eb17acf5f9604a2 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 12:26:45 +0100 Subject: [PATCH 2/7] update: remove ConsAddress String --- x/distribution/CHANGELOG.md | 3 ++- x/distribution/keeper/genesis.go | 6 +++++- x/distribution/types/genesis.go | 8 ++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/x/distribution/CHANGELOG.md b/x/distribution/CHANGELOG.md index 44e6d007fe23..89a8040548b1 100644 --- a/x/distribution/CHANGELOG.md +++ b/x/distribution/CHANGELOG.md @@ -32,7 +32,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes * []() Removes Accounts String method - * `NewMsgSetWithdrawAddress` now takes strings as argument instead of `sdk.AccAddress` + * `NewMsgSetWithdrawAddress` now takes strings as argument instead of `sdk.AccAddress`. + * `NewGenesisState` now takes a string as argument instead of `sdk.ConsAddress`. * [#19445](https://github.com/cosmos/cosmos-sdk/pull/19445) `appmodule.Environment` is received on the Keeper to get access to different application services * [#17115](https://github.com/cosmos/cosmos-sdk/pull/17115) Use collections for `PreviousProposer` and `ValidatorSlashEvents`: * remove from `Keeper`: `GetPreviousProposerConsAddr`, `SetPreviousProposerConsAddr`, `GetValidatorHistoricalReferenceCount`, `GetValidatorSlashEvent`, `SetValidatorSlashEvent`. diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index e5fcfc91ef4a..100f7898982d 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -272,5 +272,9 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) return nil, err } - return types.NewGenesisState(params, feePool, dwi, pp, outstanding, acc, his, cur, dels, slashes), nil + ppAddr, err := k.stakingKeeper.ConsensusAddressCodec().BytesToString(pp) + if err != nil { + return nil, err + } + return types.NewGenesisState(params, feePool, dwi, ppAddr, outstanding, acc, his, cur, dels, slashes), nil } diff --git a/x/distribution/types/genesis.go b/x/distribution/types/genesis.go index 0affa8c34736..d973fb94f473 100644 --- a/x/distribution/types/genesis.go +++ b/x/distribution/types/genesis.go @@ -1,11 +1,7 @@ package types -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - func NewGenesisState( - params Params, fp FeePool, dwis []DelegatorWithdrawInfo, pp sdk.ConsAddress, r []ValidatorOutstandingRewardsRecord, + params Params, fp FeePool, dwis []DelegatorWithdrawInfo, pp string, r []ValidatorOutstandingRewardsRecord, acc []ValidatorAccumulatedCommissionRecord, historical []ValidatorHistoricalRewardsRecord, cur []ValidatorCurrentRewardsRecord, dels []DelegatorStartingInfoRecord, slashes []ValidatorSlashEventRecord, ) *GenesisState { @@ -13,7 +9,7 @@ func NewGenesisState( Params: params, FeePool: fp, DelegatorWithdrawInfos: dwis, - PreviousProposer: pp.String(), + PreviousProposer: pp, OutstandingRewards: r, ValidatorAccumulatedCommissions: acc, ValidatorHistoricalRewards: historical, From e7fb825af4a191ff13ceb7fccf8fa1e6b5a6c445 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 13:09:43 +0100 Subject: [PATCH 3/7] update: remove ValAddress String --- x/distribution/keeper/allocation_test.go | 24 ++++++-- x/distribution/keeper/delegation_test.go | 72 +++++++++++++++++------ x/distribution/keeper/genesis.go | 47 ++++++++++++--- x/distribution/keeper/grpc_query_test.go | 5 +- x/distribution/keeper/msg_server_test.go | 11 +++- x/distribution/testutil/staking_helper.go | 5 +- 6 files changed, 124 insertions(+), 40 deletions(-) diff --git a/x/distribution/keeper/allocation_test.go b/x/distribution/keeper/allocation_test.go index 324524cae2c9..22d5bea88860 100644 --- a/x/distribution/keeper/allocation_test.go +++ b/x/distribution/keeper/allocation_test.go @@ -63,7 +63,9 @@ func TestAllocateTokensToValidatorWithCommission(t *testing.T) { ) // create validator with 50% commission - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk0)).Return(val, nil).AnyTimes() @@ -132,14 +134,18 @@ func TestAllocateTokensToManyValidators(t *testing.T) { // create validator with 50% commission valAddr0 := sdk.ValAddress(valConsAddr0) - val0, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val0, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val0.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk0)).Return(val0, nil).AnyTimes() // create second validator with 0% commission valAddr1 := sdk.ValAddress(valConsAddr1) - val1, err := distrtestutil.CreateValidator(valConsPk1, math.NewInt(100)) + operatorAddr, err = stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk1.Address()) + require.NoError(t, err) + val1, err := distrtestutil.CreateValidator(valConsPk1, operatorAddr, math.NewInt(100)) require.NoError(t, err) val1.Commission = stakingtypes.NewCommission(math.LegacyNewDec(0), math.LegacyNewDec(0), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk1)).Return(val1, nil).AnyTimes() @@ -268,21 +274,27 @@ func TestAllocateTokensTruncation(t *testing.T) { // create validator with 10% commission valAddr0 := sdk.ValAddress(valConsAddr0) - val0, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val0, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val0.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk0)).Return(val0, nil).AnyTimes() // create second validator with 10% commission valAddr1 := sdk.ValAddress(valConsAddr1) - val1, err := distrtestutil.CreateValidator(valConsPk1, math.NewInt(100)) + operatorAddr, err = stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk1.Address()) + require.NoError(t, err) + val1, err := distrtestutil.CreateValidator(valConsPk1, operatorAddr, math.NewInt(100)) require.NoError(t, err) val1.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk1)).Return(val1, nil).AnyTimes() // create third validator with 10% commission valAddr2 := sdk.ValAddress(valConsAddr2) - val2, err := stakingtypes.NewValidator(sdk.ValAddress(valConsAddr2).String(), valConsPk1, stakingtypes.Description{}) + valAddr2Str, err := cdcOpts.GetValidatorCodec().BytesToString(valAddr2) + require.NoError(t, err) + val2, err := stakingtypes.NewValidator(valAddr2Str, valConsPk1, stakingtypes.Description{}) require.NoError(t, err) val2.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDec(0)) stakingKeeper.EXPECT().ValidatorByConsAddr(gomock.Any(), sdk.GetConsAddress(valConsPk2)).Return(val2, nil).AnyTimes() diff --git a/x/distribution/keeper/delegation_test.go b/x/distribution/keeper/delegation_test.go index d480b943a289..39fd4dbf754d 100644 --- a/x/distribution/keeper/delegation_test.go +++ b/x/distribution/keeper/delegation_test.go @@ -65,15 +65,19 @@ func TestCalculateRewardsBasic(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(1000)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(1000)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // delegation mock - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -176,14 +180,18 @@ func TestCalculateRewardsAfterSlash(t *testing.T) { addr := sdk.AccAddress(valAddr) valPower := int64(100) stake := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction) - val, err := distrtestutil.CreateValidator(valConsPk0, stake) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, stake) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) // set mock calls stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) @@ -288,15 +296,19 @@ func TestCalculateRewardsAfterManySlashes(t *testing.T) { addr := sdk.AccAddress(valAddr) valPower := int64(100) stake := sdk.TokensFromConsensusPower(valPower, sdk.DefaultPowerReduction) - val, err := distrtestutil.CreateValidator(valConsPk0, stake) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, stake) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // delegation mocks - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -419,15 +431,19 @@ func TestCalculateRewardsMultiDelegator(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr0 := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr0) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) - del0 := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del0 := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) // set mock calls stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(4) @@ -525,16 +541,20 @@ func TestWithdrawDelegationRewardsBasic(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // delegation mock - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(5) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(3) @@ -609,16 +629,20 @@ func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // delegation mock - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(5) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -736,15 +760,19 @@ func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, sdk.TokensFromConsensusPower(valPower, sdk.DefaultPowerReduction)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, sdk.TokensFromConsensusPower(valPower, sdk.DefaultPowerReduction)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // validator and delegation mocks - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil) @@ -883,15 +911,19 @@ func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDecWithPrec(5, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // validator and delegation mocks - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(5) @@ -1093,15 +1125,19 @@ func Test100PercentCommissionReward(t *testing.T) { // create validator with 50% commission valAddr := sdk.ValAddress(valConsAddr0) addr := sdk.AccAddress(valAddr) - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) val.Commission = stakingtypes.NewCommission(math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDecWithPrec(10, 1), math.LegacyNewDec(0)) addrStr, err := accountKeeper.AddressCodec().BytesToString(addr) require.NoError(t, err) + valAddrStr, err := stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr) + require.NoError(t, err) // validator and delegation mocks - del := stakingtypes.NewDelegation(addrStr, valAddr.String(), val.DelegatorShares) + del := stakingtypes.NewDelegation(addrStr, valAddrStr, val.DelegatorShares) stakingKeeper.EXPECT().Validator(gomock.Any(), valAddr).Return(val, nil).Times(3) stakingKeeper.EXPECT().Delegation(gomock.Any(), addr, valAddr).Return(del, nil).Times(3) diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index 100f7898982d..b6c0d0fb5c33 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -185,8 +185,13 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0) err = k.ValidatorOutstandingRewards.Walk(ctx, nil, func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool, err error) { + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(addr) + if err != nil { + return true, err + } + outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{ - ValidatorAddress: addr.String(), + ValidatorAddress: valAddr, OutstandingRewards: rewards.Rewards, }) return false, nil @@ -198,8 +203,13 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) acc := make([]types.ValidatorAccumulatedCommissionRecord, 0) err = k.ValidatorsAccumulatedCommission.Walk(ctx, nil, func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool, err error) { + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(addr) + if err != nil { + return true, err + } + acc = append(acc, types.ValidatorAccumulatedCommissionRecord{ - ValidatorAddress: addr.String(), + ValidatorAddress: valAddr, Accumulated: commission, }) return false, nil @@ -211,8 +221,13 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) his := make([]types.ValidatorHistoricalRewardsRecord, 0) err = k.ValidatorHistoricalRewards.Walk(ctx, nil, func(key collections.Pair[sdk.ValAddress, uint64], rewards types.ValidatorHistoricalRewards) (stop bool, err error) { + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(key.K1()) + if err != nil { + return true, err + } + his = append(his, types.ValidatorHistoricalRewardsRecord{ - ValidatorAddress: key.K1().String(), + ValidatorAddress: valAddr, Period: key.K2(), Rewards: rewards, }) @@ -226,8 +241,13 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) cur := make([]types.ValidatorCurrentRewardsRecord, 0) err = k.ValidatorCurrentRewards.Walk(ctx, nil, func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool, err error) { + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(val) + if err != nil { + return true, err + } + cur = append(cur, types.ValidatorCurrentRewardsRecord{ - ValidatorAddress: val.String(), + ValidatorAddress: valAddr, Rewards: rewards, }) return false, nil @@ -243,9 +263,15 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) if err != nil { return true, err } + + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(key.K1()) + if err != nil { + return true, err + } + dels = append(dels, types.DelegatorStartingInfoRecord{ DelegatorAddress: delAddr, - ValidatorAddress: key.K1().String(), + ValidatorAddress: valAddr, StartingInfo: value, }) return false, nil @@ -258,10 +284,15 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) err = k.ValidatorSlashEvents.Walk( ctx, nil, - func(k collections.Triple[sdk.ValAddress, uint64, uint64], event types.ValidatorSlashEvent) (stop bool, err error) { + func(key collections.Triple[sdk.ValAddress, uint64, uint64], event types.ValidatorSlashEvent) (stop bool, err error) { + valAddr, err := k.stakingKeeper.ValidatorAddressCodec().BytesToString(key.K1()) + if err != nil { + return true, err + } + slashes = append(slashes, types.ValidatorSlashEventRecord{ - ValidatorAddress: k.K1().String(), - Height: k.K2(), + ValidatorAddress: valAddr, + Height: key.K2(), Period: event.ValidatorPeriod, ValidatorSlashEvent: event, }) diff --git a/x/distribution/keeper/grpc_query_test.go b/x/distribution/keeper/grpc_query_test.go index 7a794656fc99..9818b97a0852 100644 --- a/x/distribution/keeper/grpc_query_test.go +++ b/x/distribution/keeper/grpc_query_test.go @@ -55,8 +55,9 @@ func TestQueryParams(t *testing.T) { func TestQueryValidatorDistributionInfo(t *testing.T) { ctx, addrs, distrKeeper, dep := initFixture(t) queryServer := keeper.NewQuerier(distrKeeper) - - val, err := distrtestutil.CreateValidator(valConsPk0, math.NewInt(100)) + operatorAddr, err := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(valConsPk0.Address()) + require.NoError(t, err) + val, err := distrtestutil.CreateValidator(valConsPk0, operatorAddr, math.NewInt(100)) require.NoError(t, err) addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) diff --git a/x/distribution/keeper/msg_server_test.go b/x/distribution/keeper/msg_server_test.go index b761487333d9..e76f6339e901 100644 --- a/x/distribution/keeper/msg_server_test.go +++ b/x/distribution/keeper/msg_server_test.go @@ -76,6 +76,8 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { addr0Str, err := codectestutil.CodecOptions{}.GetAddressCodec().BytesToString(addrs[0]) require.NoError(t, err) + valAddr1Str, err := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(addrs[1]) + require.NoError(t, err) cases := []struct { name string @@ -87,7 +89,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { name: "invalid delegator address", msg: &types.MsgWithdrawDelegatorReward{ DelegatorAddress: "invalid", - ValidatorAddress: sdk.ValAddress(addrs[1]).String(), + ValidatorAddress: valAddr1Str, }, errMsg: "invalid delegator address", }, @@ -103,7 +105,7 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) { name: "no validator", msg: &types.MsgWithdrawDelegatorReward{ DelegatorAddress: addr0Str, - ValidatorAddress: sdk.ValAddress(addrs[1]).String(), + ValidatorAddress: valAddr1Str, }, errMsg: "no validator distribution info", }, @@ -130,6 +132,9 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { ctx, addrs, distrKeeper, _ := initFixture(t) msgServer := keeper.NewMsgServerImpl(distrKeeper) + valAddr1Str, err := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(addrs[1]) + require.NoError(t, err) + cases := []struct { name string preRun func() @@ -146,7 +151,7 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { { name: "no validator commission to withdraw", msg: &types.MsgWithdrawValidatorCommission{ - ValidatorAddress: sdk.ValAddress(addrs[1]).String(), + ValidatorAddress: valAddr1Str, }, errMsg: "no validator commission to withdraw", }, diff --git a/x/distribution/testutil/staking_helper.go b/x/distribution/testutil/staking_helper.go index a3b35deed576..d84e92cc476c 100644 --- a/x/distribution/testutil/staking_helper.go +++ b/x/distribution/testutil/staking_helper.go @@ -12,9 +12,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -func CreateValidator(pk cryptotypes.PubKey, stake math.Int) (stakingtypes.Validator, error) { - valConsAddr := sdk.GetConsAddress(pk) - val, err := stakingtypes.NewValidator(sdk.ValAddress(valConsAddr).String(), pk, stakingtypes.Description{Moniker: "TestValidator"}) +func CreateValidator(pk cryptotypes.PubKey, operator string, stake math.Int) (stakingtypes.Validator, error) { + val, err := stakingtypes.NewValidator(operator, pk, stakingtypes.Description{Moniker: "TestValidator"}) val.Tokens = stake val.DelegatorShares = math.LegacyNewDecFromInt(val.Tokens) return val, err From ca5569ec58b3f100f8dfb113787582383bf06356 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 13:35:57 +0100 Subject: [PATCH 4/7] update: remove AddressFromBech32 --- x/distribution/client/common/common.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/x/distribution/client/common/common.go b/x/distribution/client/common/common.go index 1e6aa4e28ce8..e17180a9c092 100644 --- a/x/distribution/client/common/common.go +++ b/x/distribution/client/common/common.go @@ -6,18 +6,17 @@ import ( "cosmossdk.io/x/distribution/types" "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" ) // QueryDelegationRewards queries a delegation rewards between a delegator and a // validator. func QueryDelegationRewards(clientCtx client.Context, delAddr, valAddr string) ([]byte, int64, error) { - delegatorAddr, err := sdk.AccAddressFromBech32(delAddr) + delegatorAddr, err := clientCtx.AddressCodec.StringToBytes(delAddr) if err != nil { return nil, 0, err } - validatorAddr, err := sdk.ValAddressFromBech32(valAddr) + validatorAddr, err := clientCtx.ValidatorAddressCodec.StringToBytes(valAddr) if err != nil { return nil, 0, err } From f0ee9862cc9016d93e1ccc625a827ed0e77b350a Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 13:40:00 +0100 Subject: [PATCH 5/7] lint --- x/distribution/depinject.go | 1 + x/distribution/keeper/grpc_query_test.go | 2 +- x/distribution/keeper/msg_server_test.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/x/distribution/depinject.go b/x/distribution/depinject.go index b09900a94cb1..299f750c60c7 100644 --- a/x/distribution/depinject.go +++ b/x/distribution/depinject.go @@ -9,6 +9,7 @@ import ( "cosmossdk.io/x/distribution/keeper" "cosmossdk.io/x/distribution/types" staking "cosmossdk.io/x/staking/types" + "github.com/cosmos/cosmos-sdk/codec" ) diff --git a/x/distribution/keeper/grpc_query_test.go b/x/distribution/keeper/grpc_query_test.go index 9818b97a0852..14bbbd66902f 100644 --- a/x/distribution/keeper/grpc_query_test.go +++ b/x/distribution/keeper/grpc_query_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "testing" "github.com/golang/mock/gomock" @@ -13,6 +12,7 @@ import ( "cosmossdk.io/x/distribution/types" stakingtypes "cosmossdk.io/x/staking/types" + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) diff --git a/x/distribution/keeper/msg_server_test.go b/x/distribution/keeper/msg_server_test.go index e76f6339e901..8c5cad6d39ac 100644 --- a/x/distribution/keeper/msg_server_test.go +++ b/x/distribution/keeper/msg_server_test.go @@ -134,7 +134,7 @@ func TestMsgWithdrawValidatorCommission(t *testing.T) { valAddr1Str, err := codectestutil.CodecOptions{}.GetValidatorCodec().BytesToString(addrs[1]) require.NoError(t, err) - + cases := []struct { name string preRun func() From d28cd9af6c6d71998676833c0f5b91ad5405a851 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 13:58:11 +0100 Subject: [PATCH 6/7] fix: tests --- x/distribution/CHANGELOG.md | 2 +- x/distribution/client/common/common_test.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/x/distribution/CHANGELOG.md b/x/distribution/CHANGELOG.md index 89a8040548b1..40d4d948779f 100644 --- a/x/distribution/CHANGELOG.md +++ b/x/distribution/CHANGELOG.md @@ -31,7 +31,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking Changes -* []() Removes Accounts String method +* [#19868](https://github.com/cosmos/cosmos-sdk/pull/19868) Removes Accounts String method * `NewMsgSetWithdrawAddress` now takes strings as argument instead of `sdk.AccAddress`. * `NewGenesisState` now takes a string as argument instead of `sdk.ConsAddress`. * [#19445](https://github.com/cosmos/cosmos-sdk/pull/19445) `appmodule.Environment` is received on the Keeper to get access to different application services diff --git a/x/distribution/client/common/common_test.go b/x/distribution/client/common/common_test.go index 05fa593079e0..07c172c4dd54 100644 --- a/x/distribution/client/common/common_test.go +++ b/x/distribution/client/common/common_test.go @@ -1,6 +1,7 @@ package common import ( + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "testing" "github.com/stretchr/testify/require" @@ -10,7 +11,11 @@ import ( ) func TestQueryDelegationRewardsAddrValidation(t *testing.T) { - clientCtx := client.Context{}.WithLegacyAmino(legacy.Cdc) + cdcOpts := codectestutil.CodecOptions{} + clientCtx := client.Context{}. + WithLegacyAmino(legacy.Cdc). + WithAddressCodec(cdcOpts.GetAddressCodec()). + WithValidatorAddressCodec(cdcOpts.GetValidatorCodec()) type args struct { delAddr string From 83f38357b719e3347340b14e5598ac2c3728c445 Mon Sep 17 00:00:00 2001 From: Julian Toledano Date: Tue, 26 Mar 2024 13:58:27 +0100 Subject: [PATCH 7/7] fix --- x/distribution/client/common/common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/client/common/common_test.go b/x/distribution/client/common/common_test.go index 07c172c4dd54..2020e3b8f109 100644 --- a/x/distribution/client/common/common_test.go +++ b/x/distribution/client/common/common_test.go @@ -1,13 +1,13 @@ package common import ( - codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" "testing" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec/legacy" + codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil" ) func TestQueryDelegationRewardsAddrValidation(t *testing.T) {