Skip to content

Commit

Permalink
Update keeper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iverc committed Jan 20, 2025
1 parent afee7dd commit 810254f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions x/tier/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (k Keeper) CompleteUnlocking(ctx context.Context) error {
func (k Keeper) Lock(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress, amt math.Int) error {
// Specified amt must be a positive integer
if !amt.IsPositive() {
return types.ErrInvalidAmount.Wrap("lock negative amount")
return types.ErrInvalidAmount.Wrap("lock non-positive amount")
}

validator, err := k.stakingKeeper.GetValidator(ctx, valAddr)
Expand Down Expand Up @@ -195,7 +195,7 @@ func (k Keeper) Unlock(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.
amt math.Int) (creationHeight int64, completionTime, unlockTime time.Time, err error) {
// Specified amt must be a positive integer
if !amt.IsPositive() {
return 0, time.Time{}, time.Time{}, types.ErrInvalidAmount.Wrap("unlock negative amount")
return 0, time.Time{}, time.Time{}, types.ErrInvalidAmount.Wrap("unlock non-positive amount")
}

err = k.SubtractLockup(ctx, delAddr, valAddr, amt)
Expand Down Expand Up @@ -251,7 +251,7 @@ func (k Keeper) Redelegate(ctx context.Context, delAddr sdk.AccAddress, srcValAd
amt math.Int) (time.Time, error) {
// Specified amt must be a positive integer
if !amt.IsPositive() {
return time.Time{}, types.ErrInvalidAmount.Wrap("redelegate negative amount")
return time.Time{}, types.ErrInvalidAmount.Wrap("redelegate non-positive amount")
}

// Subtract the lockup from the source validator
Expand Down Expand Up @@ -294,7 +294,7 @@ func (k Keeper) CancelUnlocking(ctx context.Context, delAddr sdk.AccAddress, val
creationHeight int64, amt math.Int) error {
// Specified amt must be a positive integer
if !amt.IsPositive() {
return types.ErrInvalidAmount.Wrap("cancel unlocking negative amount")
return types.ErrInvalidAmount.Wrap("cancel unlocking non-positive amount")
}

validator, err := k.stakingKeeper.GetValidator(ctx, valAddr)
Expand Down
25 changes: 25 additions & 0 deletions x/tier/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func TestLock(t *testing.T) {
// locking invalid amounts should fail
err = k.Lock(ctx, delAddr, valAddr, invalidAmount)
require.Error(t, err)
require.Contains(t, err.Error(), "lock non-positive amount")
err = k.Lock(ctx, delAddr, valAddr, math.ZeroInt())
require.Error(t, err)
require.Contains(t, err.Error(), "lock non-positive amount")

// lock valid amount
err = k.Lock(ctx, delAddr, valAddr, amount)
Expand Down Expand Up @@ -101,8 +103,10 @@ func TestUnlock(t *testing.T) {
// unlocking invalid amounts should fail
_, _, _, err = k.Unlock(ctx, delAddr, valAddr, invalidUnlockAmount)
require.Error(t, err)
require.Contains(t, err.Error(), "unlock non-positive amount")
_, _, _, err = k.Unlock(ctx, delAddr, valAddr, math.ZeroInt())
require.Error(t, err)
require.Contains(t, err.Error(), "unlock non-positive amount")

creationHeight, completionTime, unlockTime, err := k.Unlock(ctx, delAddr, valAddr, unlockAmount)
require.NoError(t, err)
Expand Down Expand Up @@ -148,8 +152,10 @@ func TestRedelegate(t *testing.T) {
// redelegating invalid amounts should fail
_, err = k.Redelegate(ctx, delAddr, srcValAddr, dstValAddr, invalidAmount)
require.Error(t, err)
require.Contains(t, err.Error(), "redelegate non-positive amount")
_, err = k.Redelegate(ctx, delAddr, srcValAddr, dstValAddr, math.ZeroInt())
require.Error(t, err)
require.Contains(t, err.Error(), "redelegate non-positive amount")

// redelegate from the source validator to the destination validator
completionTime, err := k.Redelegate(ctx, delAddr, srcValAddr, dstValAddr, math.NewInt(500))
Expand Down Expand Up @@ -212,9 +218,21 @@ func TestCompleteUnlocking(t *testing.T) {
balance = k.GetBankKeeper().GetBalance(ctx, delAddr, appparams.DefaultBondDenom)
require.Equal(t, initialDelegatorBalance.Sub(lockAmount), balance.Amount)

// completing unlocking lockup should be skipped if unlock time was not reached
err = k.CompleteUnlocking(ctx)
require.NoError(t, err)
balance = k.GetBankKeeper().GetBalance(ctx, delAddr, appparams.DefaultBondDenom)
require.Equal(t, initialDelegatorBalance.Sub(unlockAmount), balance.Amount)

// advance block time by 60 days
ctx = ctx.WithBlockHeight(3600 * 24 * 60).WithBlockTime(ctx.BlockTime().Add(60 * 24 * time.Hour))

// completing unlocking lockup should be skipped if module balance is less than required amount
err = k.CompleteUnlocking(ctx)
require.NoError(t, err)
balance = k.GetBankKeeper().GetBalance(ctx, delAddr, appparams.DefaultBondDenom)
require.Equal(t, initialDelegatorBalance.Sub(unlockAmount), balance.Amount)

// complete unbonding via the staking keeper
modAddr := authtypes.NewModuleAddress(types.ModuleName)
_, err = k.GetStakingKeeper().CompleteUnbonding(ctx, modAddr, valAddr)
Expand Down Expand Up @@ -281,6 +299,13 @@ func TestCancelUnlocking(t *testing.T) {
require.Equal(t, completionTime, unlockingLockup.CompletionTime)
require.Equal(t, unlockTime, unlockingLockup.UnlockTime)

err = k.CancelUnlocking(ctx, delAddr, valAddr, creationHeight, math.NewInt(-100))
require.Error(t, err)
require.Contains(t, err.Error(), "cancel unlocking non-positive amount")
err = k.CancelUnlocking(ctx, delAddr, valAddr, creationHeight, math.ZeroInt())
require.Error(t, err)
require.Contains(t, err.Error(), "cancel unlocking non-positive amount")

// partially cancel the unlocking lockup
err = k.CancelUnlocking(ctx, delAddr, valAddr, creationHeight, partialUnlockAmount)
require.NoError(t, err)
Expand Down

0 comments on commit 810254f

Please sign in to comment.