Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate away from using valBech32 globals (2/2) #17157

Merged
merged 22 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/genutil) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `GenAppStateFromConfig`, AddGenesisAccountCmd and `GenTxCmd` takes an addresscodec to decode addresses
* (x/distribution) [#17098](https://github.com/cosmos/cosmos-sdk/pull/17098) `NewMsgDepositValidatorRewardsPool`, `NewMsgFundCommunityPool`, `NewMsgWithdrawValidatorCommission` and `NewMsgWithdrawDelegatorReward` takes a string instead of `sdk.ValAddress` or `sdk.AccAddress`
* (client) [#17215](https://github.com/cosmos/cosmos-sdk/pull/17215) `server.StartCmd`,`server.ExportCmd`,`server.NewRollbackCmd`,`pruning.Cmd`,`genutilcli.InitCmd`,`genutilcli.GenTxCmd`,`genutilcli.CollectGenTxsCmd`,`genutilcli.AddGenesisAccountCmd`, do not take a home directory anymore. It is inferred from the root command.
* (x/staking) [#17157](https://github.com/cosmos/cosmos-sdk/pull/17157) `GetValidatorsByPowerIndexKey` and `ValidateBasic` for historical info takes a validator address codec in order to be able to decode/encode addresses.
* `GetOperator()` now returns the address as it is represented in state, by default this is an encoded address
* `GetConsAddr() ([]byte, error)` returns `[]byte` instead of sdk.ConsAddres.
* (x/distribution) `Delegate` & `SlashValidator` helper function added the mock staking keeper as a parameter passed to the function


### CLI Breaking Changes

Expand Down
14 changes: 11 additions & 3 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

// withdraw all validator commission
err := app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, valBz)
return false
})
if err != nil {
Expand Down Expand Up @@ -120,8 +124,12 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

// reinitialize all validators
err = app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
panic(err)
}
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
scraps, err := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, valBz)
if err != nil {
panic(err)
}
Expand All @@ -134,7 +142,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
panic(err)
}

if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()); err != nil {
if err := app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, valBz); err != nil {
panic(err)
}
return false
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/distribution/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,14 @@ func TestGRPCDelegationRewards(t *testing.T) {
validator, issuedShares := val.AddTokensFromDel(delTokens)
delegation := stakingtypes.NewDelegation(delAddr.String(), f.valAddr.String(), issuedShares)
assert.NilError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, delegation))
assert.NilError(t, f.distrKeeper.DelegatorStartingInfo.Set(f.sdkCtx, collections.Join(validator.GetOperator(), delAddr), types.NewDelegatorStartingInfo(2, math.LegacyNewDec(initialStake), 20)))
valBz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)
assert.NilError(t, f.distrKeeper.DelegatorStartingInfo.Set(f.sdkCtx, collections.Join(sdk.ValAddress(valBz), delAddr), types.NewDelegatorStartingInfo(2, math.LegacyNewDec(initialStake), 20)))

// setup validator rewards
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())}
historicalRewards := types.NewValidatorHistoricalRewards(decCoins, 2)
assert.NilError(t, f.distrKeeper.ValidatorHistoricalRewards.Set(f.sdkCtx, collections.Join(validator.GetOperator(), uint64(2)), historicalRewards))
assert.NilError(t, f.distrKeeper.ValidatorHistoricalRewards.Set(f.sdkCtx, collections.Join(sdk.ValAddress(valBz), uint64(2)), historicalRewards))
// setup current rewards and outstanding rewards
currentRewards := types.NewValidatorCurrentRewards(decCoins, 3)
assert.NilError(t, f.distrKeeper.ValidatorCurrentRewards.Set(f.sdkCtx, f.valAddr, currentRewards))
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/distribution/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ func TestMsgWithdrawDelegatorReward(t *testing.T) {
// setup delegation
delTokens := sdk.TokensFromConsensusPower(2, sdk.DefaultPowerReduction)
validator, issuedShares := validator.AddTokensFromDel(delTokens)
delegation := stakingtypes.NewDelegation(delAddr.String(), validator.GetOperator().String(), issuedShares)
valBz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
require.NoError(t, err)
delegation := stakingtypes.NewDelegation(delAddr.String(), validator.GetOperator(), issuedShares)
require.NoError(t, f.stakingKeeper.SetDelegation(f.sdkCtx, delegation))
require.NoError(t, f.distrKeeper.DelegatorStartingInfo.Set(f.sdkCtx, collections.Join(validator.GetOperator(), delAddr), distrtypes.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 20)))
require.NoError(t, f.distrKeeper.DelegatorStartingInfo.Set(f.sdkCtx, collections.Join(sdk.ValAddress(valBz), delAddr), distrtypes.NewDelegatorStartingInfo(2, math.LegacyOneDec(), 20)))
// setup validator rewards
decCoins := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyOneDec())}
historicalRewards := distrtypes.NewValidatorHistoricalRewards(decCoins, 2)
err = f.distrKeeper.ValidatorHistoricalRewards.Set(f.sdkCtx, collections.Join(validator.GetOperator(), uint64(2)), historicalRewards)
err = f.distrKeeper.ValidatorHistoricalRewards.Set(f.sdkCtx, collections.Join(sdk.ValAddress(valBz), uint64(2)), historicalRewards)
require.NoError(t, err)
// setup current rewards and outstanding rewards
currentRewards := distrtypes.NewValidatorCurrentRewards(decCoins, 3)
Expand Down
29 changes: 18 additions & 11 deletions tests/integration/staking/keeper/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,16 @@ func setValidator(t *testing.T, f *deterministicFixture, validator stakingtypes.
assert.NilError(t, f.stakingKeeper.SetValidator(f.ctx, validator))
assert.NilError(t, f.stakingKeeper.SetValidatorByPowerIndex(f.ctx, validator))
assert.NilError(t, f.stakingKeeper.SetValidatorByConsAddr(f.ctx, validator))
assert.NilError(t, f.stakingKeeper.Hooks().AfterValidatorCreated(f.ctx, validator.GetOperator()))
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)

assert.NilError(t, f.stakingKeeper.Hooks().AfterValidatorCreated(f.ctx, valbz))

delegatorAddress := sdk.AccAddress(validator.GetOperator())
delegatorAddress := sdk.AccAddress(valbz)
coins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, validator.BondedTokens()))
assert.NilError(t, banktestutil.FundAccount(f.ctx, f.bankKeeper, delegatorAddress, coins))

_, err := f.stakingKeeper.Delegate(f.ctx, delegatorAddress, validator.BondedTokens(), stakingtypes.Unbonded, validator, true)
_, err = f.stakingKeeper.Delegate(f.ctx, delegatorAddress, validator.BondedTokens(), stakingtypes.Unbonded, validator, true)
assert.NilError(t, err)
}

Expand Down Expand Up @@ -435,8 +438,9 @@ func TestGRPCValidatorUnbondingDelegations(t *testing.T) {
delegator := testdata.AddressGenerator(rt).Draw(rt, "delegator")
shares, err := createDelegationAndDelegate(t, rt, f, delegator, validator)
assert.NilError(t, err)

_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, validator.GetOperator(), shares)
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, valbz, shares)
assert.NilError(t, err)
}

Expand Down Expand Up @@ -512,7 +516,9 @@ func TestGRPCUnbondingDelegation(t *testing.T) {
shares, err := createDelegationAndDelegate(t, rt, f, delegator, validator)
assert.NilError(t, err)

_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, validator.GetOperator(), shares)
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, valbz, shares)
assert.NilError(t, err)

req := &stakingtypes.QueryUnbondingDelegationRequest{
Expand Down Expand Up @@ -621,8 +627,9 @@ func TestGRPCDelegatorUnbondingDelegations(t *testing.T) {
validator := createAndSetValidatorWithStatus(t, rt, f, stakingtypes.Bonded)
shares, err := createDelegationAndDelegate(t, rt, f, delegator, validator)
assert.NilError(t, err)

_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, validator.GetOperator(), shares)
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Undelegate(f.ctx, delegator, valbz, shares)
assert.NilError(t, err)
}

Expand Down Expand Up @@ -656,15 +663,15 @@ func TestGRPCHistoricalInfo(t *testing.T) {

rapid.Check(t, func(rt *rapid.T) {
numVals := rapid.IntRange(1, 5).Draw(rt, "num-vals")
vals := make(stakingtypes.Validators, 0, numVals)
vals := stakingtypes.Validators{}
for i := 0; i < numVals; i++ {
validator := createAndSetValidatorWithStatus(t, rt, f, stakingtypes.Bonded)
vals = append(vals, validator)
vals.Validators = append(vals.Validators, validator)
}

historicalInfo := stakingtypes.HistoricalInfo{
Header: cmtproto.Header{},
Valset: vals,
Valset: vals.Validators,
}

height := rapid.Int64Min(0).Draw(rt, "height")
Expand Down
21 changes: 14 additions & 7 deletions tests/integration/staking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createValidatorAccs(t *testing.T, f *fixture) ([]sdk.AccAddress, []types.Va
// have its order changed
sortedVals := make([]types.Validator, len(validators))
copy(sortedVals, validators)
hi := types.NewHistoricalInfo(header, sortedVals, f.stakingKeeper.PowerReduction(f.sdkCtx))
hi := types.NewHistoricalInfo(header, types.Validators{Validators: sortedVals}, f.stakingKeeper.PowerReduction(f.sdkCtx))
assert.NilError(t, f.stakingKeeper.HistoricalInfo.Set(f.sdkCtx, uint64(5), hi))

return addrs, validators
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestGRPCQueryDelegatorValidators(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, 1, len(res.Validators))
assert.Assert(t, res.Pagination.NextKey != nil)
assert.Equal(t, uint64(len(delValidators)), res.Pagination.Total)
assert.Equal(t, uint64(len(delValidators.Validators)), res.Pagination.Total)
} else {
assert.ErrorContains(t, err, tc.expErrMsg)
assert.Assert(t, res == nil)
Expand Down Expand Up @@ -811,11 +811,16 @@ func TestGRPCQueryRedelegations(t *testing.T) {
applyValidatorSetUpdates(t, ctx, f.stakingKeeper, -1)

rdAmount := f.stakingKeeper.TokensFromConsensusPower(ctx, 1)
_, err = f.stakingKeeper.BeginRedelegation(ctx, addrAcc1, val1.GetOperator(), val2.GetOperator(), math.LegacyNewDecFromInt(rdAmount))
val1bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(val1.GetOperator())
assert.NilError(t, err)
val2bz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(val2.GetOperator())
assert.NilError(t, err)

_, err = f.stakingKeeper.BeginRedelegation(ctx, addrAcc1, val1bz, val2bz, math.LegacyNewDecFromInt(rdAmount))
assert.NilError(t, err)
applyValidatorSetUpdates(t, ctx, f.stakingKeeper, -1)

redel, found := f.stakingKeeper.GetRedelegation(ctx, addrAcc1, val1.GetOperator(), val2.GetOperator())
redel, found := f.stakingKeeper.GetRedelegation(ctx, addrAcc1, val1bz, val2bz)
assert.Assert(t, found)

var req *types.QueryRedelegationsRequest
Expand Down Expand Up @@ -876,7 +881,7 @@ func TestGRPCQueryRedelegations(t *testing.T) {
"query redelegations with sourceValAddr only",
func() {
req = &types.QueryRedelegationsRequest{
SrcValidatorAddr: val1.GetOperator().String(),
SrcValidatorAddr: val1.GetOperator(),
Pagination: &query.PageRequest{Limit: 1, CountTotal: true},
}
},
Expand Down Expand Up @@ -924,7 +929,9 @@ func TestGRPCQueryValidatorUnbondingDelegations(t *testing.T) {

// undelegate
undelAmount := f.stakingKeeper.TokensFromConsensusPower(ctx, 2)
_, _, err := f.stakingKeeper.Undelegate(ctx, addrAcc1, val1.GetOperator(), math.LegacyNewDecFromInt(undelAmount))
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(val1.GetOperator())
assert.NilError(t, err)
_, _, err = f.stakingKeeper.Undelegate(ctx, addrAcc1, valbz, math.LegacyNewDecFromInt(undelAmount))
assert.NilError(t, err)
applyValidatorSetUpdates(t, ctx, f.stakingKeeper, -1)

Expand Down Expand Up @@ -958,7 +965,7 @@ func TestGRPCQueryValidatorUnbondingDelegations(t *testing.T) {
"valid request",
func() {
req = &types.QueryValidatorUnbondingDelegationsRequest{
ValidatorAddr: val1.GetOperator().String(),
ValidatorAddr: val1.GetOperator(),
Pagination: &query.PageRequest{Limit: 1, CountTotal: true},
}
},
Expand Down
10 changes: 8 additions & 2 deletions tests/integration/staking/keeper/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ func TestSlashAtNegativeHeight(t *testing.T) {
// end block
applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1)

validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator())
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)

validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, valbz)
assert.Assert(t, found)
// power decreased
assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx)))
Expand Down Expand Up @@ -237,7 +240,10 @@ func TestSlashValidatorAtCurrentHeight(t *testing.T) {
// end block
applyValidatorSetUpdates(t, f.sdkCtx, f.stakingKeeper, 1)

validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, validator.GetOperator())
valbz, err := f.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
assert.NilError(t, err)

validator, found = f.stakingKeeper.GetValidator(f.sdkCtx, valbz)
assert.Assert(t, found)
// power decreased
assert.Equal(t, int64(5), validator.GetConsensusPower(f.stakingKeeper.PowerReduction(f.sdkCtx)))
Expand Down
Loading
Loading