Skip to content

Commit

Permalink
fix: move dyncomm state changes to post handler
Browse files Browse the repository at this point in the history
  • Loading branch information
fragwuerdig committed Oct 6, 2023
1 parent 43c6797 commit 17541c7
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 20 deletions.
11 changes: 11 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
v5 "github.com/classic-terra/core/v2/app/upgrades/v5"

customante "github.com/classic-terra/core/v2/custom/auth/ante"
custompost "github.com/classic-terra/core/v2/custom/auth/post"
customauthtx "github.com/classic-terra/core/v2/custom/auth/tx"

"github.com/CosmWasm/wasmd/x/wasm"
Expand Down Expand Up @@ -219,7 +220,17 @@ func NewTerraApp(
panic(err)
}

postHandler, err := custompost.NewPostHandler(
custompost.HandlerOptions{
DyncommKeeper: app.DyncommKeeper,
},
)
if err != nil {
panic(err)
}

app.SetAnteHandler(anteHandler)
app.SetPostHandler(postHandler)
app.SetEndBlocker(app.EndBlocker)

if loadLatest {
Expand Down
23 changes: 23 additions & 0 deletions custom/auth/post/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package post

import (
dyncommkeeper "github.com/classic-terra/core/v2/x/dyncomm/keeper"
dyncommpost "github.com/classic-terra/core/v2/x/dyncomm/post"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
DyncommKeeper dyncommkeeper.Keeper
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, and deducts fees from the first
// signer.
func NewPostHandler(options HandlerOptions) (sdk.AnteHandler, error) {

return sdk.ChainAnteDecorators(
dyncommpost.NewDyncommPostDecorator(options.DyncommKeeper),
), nil

}
23 changes: 4 additions & 19 deletions x/dyncomm/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (dd DyncommDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool,
return next(ctx, tx, simulate)
}

if ctx.IsCheckTx() {
return next(ctx, tx, simulate)
}

msgs := tx.GetMsgs()
err := dd.FilterMsgsAndProcessMsgs(ctx, msgs...)

Expand All @@ -47,8 +51,6 @@ func (dd DyncommDecorator) FilterMsgsAndProcessMsgs(ctx sdk.Context, msgs ...sdk
switch msg.(type) {
case *stakingtypes.MsgEditValidator:
err = dd.ProcessEditValidator(ctx, msg)
case *stakingtypes.MsgCreateValidator:
err = dd.ProcessCreateValidator(ctx, msg)
default:
continue
}
Expand Down Expand Up @@ -79,23 +81,6 @@ func (dd DyncommDecorator) ProcessEditValidator(ctx sdk.Context, msg sdk.Msg) (e
return fmt.Errorf("commission for %s must be at least %f", operator, dynMinRate.MustFloat64())
}

// set new target rate from intendet
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, *newIntendedRate)

return nil

}

func (dd DyncommDecorator) ProcessCreateValidator(ctx sdk.Context, msg sdk.Msg) (err error) {

msgEditValidator := msg.(*stakingtypes.MsgCreateValidator)
newIntendedRate := msgEditValidator.Commission.Rate

// we don't know yet what the validators VP
// is gonna be. So let's just set the intended
// target rate here
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, newIntendedRate)

return nil

}
3 changes: 2 additions & 1 deletion x/dyncomm/keeper/dyncomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ func (k Keeper) UpdateValidatorMinRates(ctx sdk.Context, validator stakingtypes.
}

newValidator := validator
newValidator.Commission = stakingtypes.NewCommission(
newValidator.Commission = stakingtypes.NewCommissionWithTime(
newRate,
newMaxRate,
validator.Commission.MaxChangeRate,
validator.Commission.UpdateTime,
)

k.StakingKeeper.SetValidator(ctx, newValidator)
Expand Down
79 changes: 79 additions & 0 deletions x/dyncomm/post/post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package post

import (
dyncommkeeper "github.com/classic-terra/core/v2/x/dyncomm/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// DyncommDecorator does post runMsg store
// modifications for dyncomm module
type DyncommDecorator struct {
dyncommKeeper dyncommkeeper.Keeper
}

func NewDyncommPostDecorator(dk dyncommkeeper.Keeper) DyncommDecorator {
return DyncommDecorator{
dyncommKeeper: dk,
}
}

func (dd DyncommDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {

if simulate {
return next(ctx, tx, simulate)
}

if ctx.IsCheckTx() {
return next(ctx, tx, simulate)
}

msgs := tx.GetMsgs()
dd.FilterMsgsAndProcessMsgs(ctx, msgs...)

return next(ctx, tx, simulate)

}

func (dd DyncommDecorator) FilterMsgsAndProcessMsgs(ctx sdk.Context, msgs ...sdk.Msg) {

for _, msg := range msgs {

switch msg.(type) {
case *stakingtypes.MsgEditValidator:
dd.ProcessEditValidator(ctx, msg)
case *stakingtypes.MsgCreateValidator:
dd.ProcessCreateValidator(ctx, msg)
default:
continue
}

}

}

func (dd DyncommDecorator) ProcessEditValidator(ctx sdk.Context, msg sdk.Msg) {

msgEditValidator := msg.(*stakingtypes.MsgEditValidator)

// no update of CommissionRate provided
if msgEditValidator.CommissionRate == nil {
return
}

// post handler runs after successfully
// calling runMsgs -> we can set state changes here!
newIntendedRate := msgEditValidator.CommissionRate
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, *newIntendedRate)

}

func (dd DyncommDecorator) ProcessCreateValidator(ctx sdk.Context, msg sdk.Msg) {

// post handler runs after successfully
// calling runMsgs -> we can set state changes here!
msgEditValidator := msg.(*stakingtypes.MsgCreateValidator)
newIntendedRate := msgEditValidator.Commission.Rate
dd.dyncommKeeper.SetTargetCommissionRate(ctx, msgEditValidator.ValidatorAddress, newIntendedRate)

}

0 comments on commit 17541c7

Please sign in to comment.