Skip to content

Commit

Permalink
feat: add endblocker with valsetupdate type (cosmos#15829)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Apr 14, 2023
1 parent 722bea5 commit 8d6c23f
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/gov) [#15151](https://github.com/cosmos/cosmos-sdk/pull/15151) Add `burn_vote_quorum`, `burn_proposal_deposit_prevote` and `burn_vote_veto` params to allow applications to decide if they would like to burn deposits
* (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Allow runtime to pass event core api service to modules
* (telemetry) [#15657](https://github.com/cosmos/cosmos-sdk/pull/15657) Emit more data (go version, sdk version, upgrade height) in prom metrics
* (modulemanager) [#15829](https://github.com/cosmos/cosmos-sdk/pull/15829) add new endblocker interface to handle valset updates

### Improvements

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/building-modules/05-beginblock-endblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ and an example implementation of `EndBlocker` from the `staking` module:
```go reference
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/x/staking/abci.go#L22-L27
```

<!-- TODO: leaving this here to update docs with core api changes -->
6 changes: 2 additions & 4 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"encoding/json"
"fmt"

abci "github.com/cometbft/cometbft/abci/types"
"golang.org/x/exp/slices"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"

storetypes "cosmossdk.io/store/types"
abci "github.com/cometbft/cometbft/abci/types"
"golang.org/x/exp/slices"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
Expand Down
9 changes: 1 addition & 8 deletions runtime/services.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package runtime

import (
"context"

appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
Expand All @@ -26,14 +24,9 @@ func (a *App) registerRuntimeServices(cfg module.Configurator) error {
}

// ======================================================
// ValidatorUpdateService & BlockInfoService
// BlockInfoService
// ======================================================

// ValidatorUpdateService is the service that runtime will provide to the module that sets validator updates.
type ValidatorUpdateService interface {
SetValidatorUpdates(context.Context, []abci.ValidatorUpdate)
}

// BlockInfoService is the service that runtime will provide to modules which need Comet block information.
type BlockInfoService interface {
GetHeight() int64 // GetHeight returns the height of the block
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/evidence/keeper/infraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
)
Expand Down Expand Up @@ -100,7 +99,7 @@ func TestHandleDoubleSign(t *testing.T) {
selfDelegation := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
assert.DeepEqual(t,
f.bankKeeper.GetAllBalances(ctx, sdk.AccAddress(operatorAddr)).String(),
sdk.NewCoins(sdk.NewCoin(stakingParams.BondDenom, initAmt.Sub(selfDelegation))).String(),
Expand Down Expand Up @@ -172,7 +171,7 @@ func TestHandleDoubleSign_TooOld(t *testing.T) {
amt := tstaking.CreateValidatorWithValPower(operatorAddr, val, power, true)

// execute end-blocker and verify validator attributes
staking.EndBlocker(ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
assert.DeepEqual(t,
f.bankKeeper.GetAllBalances(ctx, sdk.AccAddress(operatorAddr)),
sdk.NewCoins(sdk.NewCoin(stakingParams.BondDenom, initAmt.Sub(amt))),
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
Expand Down Expand Up @@ -73,7 +72,7 @@ func createValidators(t *testing.T, ctx sdk.Context, app *simapp.SimApp, powers
_, _ = app.StakingKeeper.Delegate(ctx, addrs[1], app.StakingKeeper.TokensFromConsensusPower(ctx, powers[1]), stakingtypes.Unbonded, val2, true)
_, _ = app.StakingKeeper.Delegate(ctx, addrs[2], app.StakingKeeper.TokensFromConsensusPower(ctx, powers[2]), stakingtypes.Unbonded, val3, true)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

return addrs, valAddrs
}
11 changes: 5 additions & 6 deletions tests/integration/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

Expand Down Expand Up @@ -271,7 +270,7 @@ func TestTallyDelgatorOverride(t *testing.T) {
_, err := app.StakingKeeper.Delegate(ctx, addrs[4], delTokens, stakingtypes.Unbonded, val1, true)
assert.NilError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp, "", "test", "description", addrs[0], false)
Expand Down Expand Up @@ -309,7 +308,7 @@ func TestTallyDelgatorInherit(t *testing.T) {
_, err := app.StakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true)
assert.NilError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp, "", "test", "description", addrs[0], false)
Expand Down Expand Up @@ -350,7 +349,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) {
_, err = app.StakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val2, true)
assert.NilError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp, "", "test", "description", addrs[0], false)
Expand Down Expand Up @@ -394,7 +393,7 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {
_, err = app.StakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true)
assert.NilError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp, "", "test", "description", addrs[0], false)
Expand Down Expand Up @@ -435,7 +434,7 @@ func TestTallyJailedValidator(t *testing.T) {
_, err = app.StakingKeeper.Delegate(ctx, addrs[3], delTokens, stakingtypes.Unbonded, val3, true)
assert.NilError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

consAddr, err := val2.GetConsAddr()
assert.NilError(t, err)
Expand Down
33 changes: 18 additions & 15 deletions tests/integration/slashing/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"time"

cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
"github.com/cosmos/cosmos-sdk/x/staking"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestUnJailNotBonded(t *testing.T) {
tstaking.CreateValidatorWithValPower(addr, val, 100, true)
}

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)

// create a 6th validator with less power than the cliff validator (won't be bonded)
Expand All @@ -91,7 +91,7 @@ func TestUnJailNotBonded(t *testing.T) {
assert.NilError(t, err)
assert.Assert(t, res != nil)

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)

tstaking.CheckValidator(addr, stakingtypes.Unbonded, false)
Expand All @@ -100,7 +100,7 @@ func TestUnJailNotBonded(t *testing.T) {
assert.Equal(t, p.BondDenom, tstaking.Denom)
tstaking.Undelegate(sdk.AccAddress(addr), addr, f.stakingKeeper.TokensFromConsensusPower(f.ctx, 1), true)

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)

// verify that validator is jailed
Expand All @@ -109,12 +109,12 @@ func TestUnJailNotBonded(t *testing.T) {
// verify we cannot unjail (yet)
assert.ErrorContains(t, f.slashingKeeper.Unjail(f.ctx, addr), "cannot be unjailed")

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)
// bond to meet minimum self-delegation
tstaking.DelegateWithPower(sdk.AccAddress(addr), addr, 1)

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
f.ctx = f.ctx.WithBlockHeight(f.ctx.BlockHeight() + 1)

// verify we can immediately unjail
Expand All @@ -140,7 +140,7 @@ func TestHandleNewValidator(t *testing.T) {
// Validator created
amt := tstaking.CreateValidatorWithValPower(addr, val, 100, true)

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
assert.DeepEqual(
t, f.bankKeeper.GetAllBalances(f.ctx, sdk.AccAddress(addr)),
sdk.NewCoins(sdk.NewCoin(f.stakingKeeper.GetParams(f.ctx).BondDenom, InitTokens.Sub(amt))),
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestHandleAlreadyJailed(t *testing.T) {

amt := tstaking.CreateValidatorWithValPower(addr, val, power, true)

staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)

// 1000 first blocks OK
height := int64(0)
Expand All @@ -200,7 +200,7 @@ func TestHandleAlreadyJailed(t *testing.T) {
}

// end block
staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)

// validator should have been jailed and slashed
validator, _ := f.stakingKeeper.GetValidatorByConsAddr(f.ctx, sdk.GetConsAddress(val))
Expand Down Expand Up @@ -240,7 +240,8 @@ func TestValidatorDippingInAndOut(t *testing.T) {
valAddr := sdk.ValAddress(addr)

tstaking.CreateValidatorWithValPower(valAddr, val, power, true)
validatorUpdates := staking.EndBlocker(f.ctx, f.stakingKeeper)
validatorUpdates, err := f.stakingKeeper.EndBlocker(f.ctx)
require.NoError(t, err)
assert.Equal(t, 2, len(validatorUpdates))
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)

Expand All @@ -253,7 +254,8 @@ func TestValidatorDippingInAndOut(t *testing.T) {

// kick first validator out of validator set
tstaking.CreateValidatorWithValPower(sdk.ValAddress(pks[1].Address()), pks[1], power+1, true)
validatorUpdates = staking.EndBlocker(f.ctx, f.stakingKeeper)
validatorUpdates, err = f.stakingKeeper.EndBlocker(f.ctx)
require.NoError(t, err)
assert.Equal(t, 2, len(validatorUpdates))
tstaking.CheckValidator(sdk.ValAddress(pks[1].Address()), stakingtypes.Bonded, false)
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, false)
Expand All @@ -265,7 +267,8 @@ func TestValidatorDippingInAndOut(t *testing.T) {
// validator added back in
tstaking.DelegateWithPower(sdk.AccAddress(pks[2].Address()), valAddr, 50)

validatorUpdates = staking.EndBlocker(f.ctx, f.stakingKeeper)
validatorUpdates, err = f.stakingKeeper.EndBlocker(f.ctx)
require.NoError(t, err)
assert.Equal(t, 2, len(validatorUpdates))
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)
newPower := power + 50
Expand All @@ -287,7 +290,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
}

// should now be jailed & kicked
staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true)

// check all the signing information
Expand All @@ -307,7 +310,7 @@ func TestValidatorDippingInAndOut(t *testing.T) {
f.slashingKeeper.HandleValidatorSignature(f.ctx, val.Address(), newPower, true)

// validator should not be kicked since we reset counter/array when it was jailed
staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
tstaking.CheckValidator(valAddr, stakingtypes.Bonded, false)

// check start height is correctly set
Expand All @@ -323,6 +326,6 @@ func TestValidatorDippingInAndOut(t *testing.T) {
}

// validator should now be jailed & kicked
staking.EndBlocker(f.ctx, f.stakingKeeper)
f.stakingKeeper.EndBlocker(f.ctx)
tstaking.CheckValidator(valAddr, stakingtypes.Unbonding, true)
}
3 changes: 1 addition & 2 deletions tests/integration/staking/keeper/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -48,7 +47,7 @@ func bootstrapValidatorTest(t testing.TB, power int64, numAddrs int) (*simapp.Si
assert.NilError(t, err)

// end block to unbond genesis validator
staking.EndBlocker(ctx, app.StakingKeeper)
app.StakingKeeper.EndBlocker(ctx)

return app, ctx, addrDels, addrVals
}
Expand Down
Loading

0 comments on commit 8d6c23f

Please sign in to comment.