Skip to content

Commit

Permalink
refactor: rollback EndBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed May 24, 2024
1 parent 59e955f commit 5fa26a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
9 changes: 4 additions & 5 deletions custom/staking/abci.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package bank

import (
"context"
"time"

abci "github.com/cometbft/cometbft/abci/types"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

// "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/cosmos/cosmos-sdk/x/staking/types"

customstakingkeeper "github.com/notional-labs/composable/v6/custom/staking/keeper"
)

// Called every block, update validator set
func EndBlocker(ctx sdk.Context, k *customstakingkeeper.Keeper) []abci.ValidatorUpdate {
// EndBlocker returns the end blocker for the staking module.
func EndBlocker(ctx context.Context, k *customstakingkeeper.Keeper) ([]abci.ValidatorUpdate, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

return k.BlockValidatorUpdates(ctx, ctx.BlockHeight())
return k.BlockValidatorUpdates(ctx)
}
31 changes: 18 additions & 13 deletions custom/staking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Keeper struct {
authKeeper minttypes.AccountKeeper
}

func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicometbft.ValidatorUpdate {
func (k Keeper) BlockValidatorUpdates(ctx context.Context) ([]abcicometbft.ValidatorUpdate, error) {
// Calculate validator set changes.
//
// NOTE: ApplyAndReturnValidatorSetUpdates has to come before
Expand All @@ -39,7 +39,9 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
// unbonded after the Endblocker (go from Bonded -> Unbonding during
// ApplyAndReturnValidatorSetUpdates and then Unbonding -> Unbonded during
// UnbondAllMatureValidatorQueue).
params := k.Stakingmiddleware.GetParams(ctx)
sdkCtx := sdk.UnwrapSDKContext(ctx)
params := k.Stakingmiddleware.GetParams(sdkCtx)
height := sdkCtx.BlockHeight()
shouldExecuteBatch := (height % int64(params.BlocksPerEpoch)) == 0
var validatorUpdates []abcicometbft.ValidatorUpdate
if shouldExecuteBatch {
Expand All @@ -52,17 +54,20 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
}

// unbond all mature validators from the unbonding queue
k.UnbondAllMatureValidators(ctx)
err := k.UnbondAllMatureValidators(ctx)
if err != nil {
return nil, err
}

// Remove all mature unbonding delegations from the ubd queue.
matureUnbonds, err := k.DequeueAllMatureUBDQueue(ctx, ctx.BlockHeader().Time)
matureUnbonds, err := k.DequeueAllMatureUBDQueue(ctx, sdkCtx.BlockHeader().Time)
if err != nil {
panic(err)
return nil, err
}
for _, dvPair := range matureUnbonds {
addr, err := sdk.ValAddressFromBech32(dvPair.ValidatorAddress)
if err != nil {
panic(err)
return nil, err
}
delegatorAddress := sdk.MustAccAddressFromBech32(dvPair.DelegatorAddress)

Expand All @@ -71,7 +76,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
continue
}

ctx.EventManager().EmitEvent(
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteUnbonding,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
Expand All @@ -82,18 +87,18 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
}

// Remove all mature redelegations from the red queue.
matureRedelegations, err := k.DequeueAllMatureRedelegationQueue(ctx, ctx.BlockHeader().Time)
matureRedelegations, err := k.DequeueAllMatureRedelegationQueue(ctx, sdkCtx.BlockHeader().Time)
if err != nil {
panic(err)
return nil, err
}
for _, dvvTriplet := range matureRedelegations {
valSrcAddr, err := sdk.ValAddressFromBech32(dvvTriplet.ValidatorSrcAddress)
if err != nil {
panic(err)
return nil, err
}
valDstAddr, err := sdk.ValAddressFromBech32(dvvTriplet.ValidatorDstAddress)
if err != nil {
panic(err)
return nil, err
}
delegatorAddress := sdk.MustAccAddressFromBech32(dvvTriplet.DelegatorAddress)

Expand All @@ -107,7 +112,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
continue
}

ctx.EventManager().EmitEvent(
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCompleteRedelegation,
sdk.NewAttribute(sdk.AttributeKeyAmount, balances.String()),
Expand All @@ -118,7 +123,7 @@ func (k Keeper) BlockValidatorUpdates(ctx sdk.Context, height int64) []abcicomet
)
}

return validatorUpdates
return validatorUpdates, nil
}

func NewKeeper(
Expand Down
8 changes: 7 additions & 1 deletion custom/staking/module.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package bank

import (
"fmt"
"context"
abci "github.com/cometbft/cometbft/abci/types"

"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
stakingmodule "github.com/cosmos/cosmos-sdk/x/staking"
Expand Down Expand Up @@ -60,3 +62,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
panic(fmt.Sprintf("failed to migrate x/staking from version 4 to 5: %v", err))
}
}

func (am AppModule) EndBlock(ctx context.Context) ([]abci.ValidatorUpdate, error) {
return EndBlocker(ctx, &am.keeper)
}

0 comments on commit 5fa26a1

Please sign in to comment.