Skip to content

Commit

Permalink
CNS-844: call dualstaking ante handler + ante unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
oren-lava committed Jan 28, 2024
1 parent e653574 commit bcad76e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
4 changes: 4 additions & 0 deletions testutil/common/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/utils/sigs"
"github.com/lavanet/lava/utils/slices"
dualstakingante "github.com/lavanet/lava/x/dualstaking/ante"
dualstakingtypes "github.com/lavanet/lava/x/dualstaking/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
fixationstoretypes "github.com/lavanet/lava/x/fixationstore/types"
Expand Down Expand Up @@ -654,6 +655,9 @@ func (ts *Tester) TxReDelegateValidator(delegator, fromValidator, toValidator si
sdk.ValAddress(toValidator.Addr),
sdk.NewCoin(ts.Keepers.StakingKeeper.BondDenom(ts.Ctx), amount),
)
rf := dualstakingante.NewRedelegationFlager(ts.Keepers.Dualstaking)
err := rf.DisableRedelegationHooks(ts.Ctx, []sdk.Msg{msg})
require.NoError(ts.T, err)
return ts.Servers.StakingServer.BeginRedelegate(ts.GoCtx, msg)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (rf RedelegationFlager) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate boo
return ctx, err
}

err = rf.disableRedelegationHooks(ctx, msgs)
err = rf.DisableRedelegationHooks(ctx, msgs)
if err != nil {
return ctx, err
}
Expand All @@ -52,7 +52,7 @@ func (rf RedelegationFlager) unwrapAuthz(tx sdk.Tx) ([]sdk.Msg, error) {
return unwrappedMsgs, nil
}

func (rf RedelegationFlager) disableRedelegationHooks(ctx sdk.Context, msgs []sdk.Msg) error {
func (rf RedelegationFlager) DisableRedelegationHooks(ctx sdk.Context, msgs []sdk.Msg) error {
redelegations := false
others := false
for _, msg := range msgs {
Expand Down
60 changes: 60 additions & 0 deletions x/dualstaking/ante/disable_redelegation_hooks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ante_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
commontypes "github.com/lavanet/lava/common/types"
"github.com/lavanet/lava/testutil/common"
"github.com/lavanet/lava/x/dualstaking/ante"
"github.com/stretchr/testify/require"
)

// TestDisableRedelegationHooks verifies that the DisableRedelegationHooks works as expected, i.e. disables
// the dualstaking hooks only for redelegations message
func TestDisableRedelegationHooks(t *testing.T) {
ts := *common.NewTester(t)
testCases := []struct {
msg sdk.Msg
expectedFlagValue bool
}{
{newStakingRedelegateMsg(), true},
{newStakingDelegateMsg(), false},
{newStakingUndelegateMsg(), false},
}

rf := ante.NewRedelegationFlager(ts.Keepers.Dualstaking)

for _, testCase := range testCases {
err := rf.DisableRedelegationHooks(ts.Ctx, []sdk.Msg{testCase.msg})
require.NoError(t, err)
disableHooksFlag := ts.Keepers.Dualstaking.GetDisableDualstakingHook(ts.Ctx)
require.Equal(t, testCase.expectedFlagValue, disableHooksFlag)
}
}

func newStakingRedelegateMsg() *stakingtypes.MsgBeginRedelegate {
return stakingtypes.NewMsgBeginRedelegate(
sdk.AccAddress("del1"),
sdk.ValAddress("val1"),
sdk.ValAddress("val2"),
sdk.NewCoin(commontypes.TokenDenom, sdk.OneInt()),
)
}

func newStakingDelegateMsg() *stakingtypes.MsgDelegate {
return stakingtypes.NewMsgDelegate(
sdk.AccAddress("del1"),
sdk.ValAddress("val1"),
sdk.NewCoin(commontypes.TokenDenom, sdk.OneInt()),
)
}

func newStakingUndelegateMsg() *stakingtypes.MsgUndelegate {
return stakingtypes.NewMsgUndelegate(
sdk.AccAddress("del1"),
sdk.ValAddress("val1"),
sdk.NewCoin(commontypes.TokenDenom, sdk.OneInt()),
)
}

0 comments on commit bcad76e

Please sign in to comment.