Skip to content

Commit

Permalink
Merge pull request #1029 from lavanet/CNS-734-use-antehandler-to-mark…
Browse files Browse the repository at this point in the history
…-redelegation-tx

CNS-734: use antehandler to mark redelegation
  • Loading branch information
Yaroms authored Dec 10, 2023
2 parents 7256c30 + 99fa374 commit c0aadad
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
dualstakingante "github.com/lavanet/lava/x/dualstaking/ante"
dualstakingkeeper "github.com/lavanet/lava/x/dualstaking/keeper"
)

func NewAnteHandler(accountKeeper ante.AccountKeeper, bankKeeper authtypes.BankKeeper, signModeHandler signing.SignModeHandler, feegrantKeeper ante.FeegrantKeeper, sigGasConsumer ante.SignatureVerificationGasConsumer) sdk.AnteHandler {
func NewAnteHandler(accountKeeper ante.AccountKeeper, bankKeeper authtypes.BankKeeper, dualstakingKeeper dualstakingkeeper.Keeper, signModeHandler signing.SignModeHandler, feegrantKeeper ante.FeegrantKeeper, sigGasConsumer ante.SignatureVerificationGasConsumer) sdk.AnteHandler {
anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewExtensionOptionsDecorator(nil),
Expand All @@ -21,6 +23,7 @@ func NewAnteHandler(accountKeeper ante.AccountKeeper, bankKeeper authtypes.BankK
ante.NewSigGasConsumeDecorator(accountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(accountKeeper, signModeHandler),
ante.NewIncrementSequenceDecorator(accountKeeper),
dualstakingante.NewRedelegationFlager(dualstakingKeeper),
}
return sdk.ChainAnteDecorators(anteDecorators...)
}
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var Upgrades = []upgrades.Upgrade{
upgrades.Upgrade_0_30_1,
upgrades.Upgrade_0_30_2,
upgrades.Upgrade_0_31_0,
upgrades.Upgrade_0_31_1,
}

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals
Expand Down Expand Up @@ -801,6 +802,7 @@ func New(
NewAnteHandler(
app.AccountKeeper,
app.BankKeeper,
app.DualstakingKeeper,
encodingConfig.TxConfig.SignModeHandler(),
app.FeeGrantKeeper,
ante.DefaultSigVerificationGasConsumer),
Expand Down
6 changes: 6 additions & 0 deletions app/upgrades/empty_upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ var Upgrade_0_31_0 = Upgrade{
CreateUpgradeHandler: defaultUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{},
}

var Upgrade_0_31_1 = Upgrade{
UpgradeName: "v0.31.1",
CreateUpgradeHandler: defaultUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{},
}
21 changes: 21 additions & 0 deletions scripts/add_validator.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# this script is adding another validator to the chain (without a running node) (this validator will be soon jailed due to inactivity)
clear
rm -rf ~/.lava_test
lavad init validator2 --chain-id lava --home ~/.lava_test
lavad config broadcast-mode sync --home ~/.lava_test
lavad config keyring-backend test --home ~/.lava_test
lavad keys add validator2 --home ~/.lava_test

GASPRICE="0.000000001ulava"
lavad tx bank send $(lavad keys show alice -a) $(lavad keys show validator2 -a --home ~/.lava_test) 500000ulava -y --from alice --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE

lavad tx staking create-validator -y --from validator2 --amount="50000ulava" --pubkey=$(lavad tendermint show-validator --home ~/.lava_test) --commission-rate="0.10" \
--commission-max-rate="0.20" \
--commission-max-change-rate="0.01" \
--min-self-delegation="1000" \
--gas-adjustment "1.5" \
--gas "auto" \
--gas-prices $GASPRICE \
--home ~/.lava_test

lavad tx staking redelegate lava@valoper1yhzkfrcdwf2hwpc4cre8er5tamp6wdm4stx2ec lava@valoper1z025w20ms6cpdht585nhsw682jph4yc7hx0gqc 500000000000ulava -y --from user1 --gas-adjustment "1.5" --gas "auto" --gas-prices $GASPRICE
40 changes: 40 additions & 0 deletions x/dualstaking/ante/ante_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ante

import (
"fmt"

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

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/lavanet/lava/x/dualstaking/keeper"
)

// RedelegationFlager sets the dualstaking redelegation flag when needed.
// when the user sends redelegation tx we dont want the hooks to do anything
type RedelegationFlager struct {
keeper.Keeper
}

func NewRedelegationFlager(dualstaking keeper.Keeper) RedelegationFlager {
return RedelegationFlager{Keeper: dualstaking}
}

func (rf RedelegationFlager) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
redelegations := false
others := false
for _, msg := range tx.GetMsgs() {
if _, ok := msg.(*stakingtypes.MsgBeginRedelegate); ok {
redelegations = true
} else {
others = true
}
}

if redelegations && others {
return ctx, fmt.Errorf("cannot send batch requests with redelegation messages")
}

keeper.DisableDualstakingHook = redelegations

return next(ctx, tx, simulate)
}
8 changes: 8 additions & 0 deletions x/dualstaking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (h Hooks) BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAd
// create new delegation period record
// add description
func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
if DisableDualstakingHook {
return nil
}

diff, err := h.k.VerifyDelegatorBalance(ctx, delAddr)
if err != nil {
return err
Expand Down Expand Up @@ -138,6 +142,10 @@ func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _
}

func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
if DisableDualstakingHook {
return nil
}

delegation, found := h.k.stakingKeeper.GetDelegation(ctx, delAddr, valAddr)
if !found {
return fmt.Errorf("could not find delegation for dualstaking hook")
Expand Down
5 changes: 5 additions & 0 deletions x/dualstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/lavanet/lava/x/dualstaking/types"
)

// DisableDualstakingHook : dualstaking uses hookd to catch delegations/unbonding tx's to do the same action on the providers delegations.
// in the case of redelegation, since the user doesnt put/takes tokens back we dont want to take action in the providers delegations.
// this flag is a local flag used to mark the next hooks to do nothing since this was cause by redelegation tx (redelegation = delegation + unbond)
var DisableDualstakingHook bool

type (
Keeper struct {
cdc codec.BinaryCodec
Expand Down
7 changes: 6 additions & 1 deletion x/protocol/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,15 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
// panic:ok: at start up, migration cannot proceed anyhow
panic(fmt.Errorf("%s: failed to register migration to v14: %w", types.ModuleName, err))
}

if err := cfg.RegisterMigration(types.ModuleName, 14, migrator.MigrateVersion); err != nil {
// panic:ok: at start up, migration cannot proceed anyhow
panic(fmt.Errorf("%s: failed to register migration to v15: %w", types.ModuleName, err))
}
}

// ConsensusVersion implements ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 14 }
func (AppModule) ConsensusVersion() uint64 { return 15 }

// RegisterInvariants registers the capability module's invariants.
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
Expand Down
2 changes: 1 addition & 1 deletion x/protocol/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var _ paramtypes.ParamSet = (*Params)(nil)

const (
TARGET_VERSION = "0.31.0"
TARGET_VERSION = "0.31.1"
MIN_VERSION = "0.30.1"
)

Expand Down

0 comments on commit c0aadad

Please sign in to comment.