Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/lock: Fix ExtendLockup API #1937

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions x/lockup/keeper/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,16 @@ func (k Keeper) unlockMaturedLockInternalLogic(ctx sdk.Context, lock types.Perio
// 2. Locks that are unlokcing are not allowed to change duration.
// 3. Locks that have synthetic lockup are not allowed to change.
// 4. Provided duration should be greater than the original duration.
func (k Keeper) ExtendLockup(ctx sdk.Context, lock types.PeriodLock, newDuration time.Duration) error {
func (k Keeper) ExtendLockup(ctx sdk.Context, lockID uint64, owner sdk.AccAddress, newDuration time.Duration) error {
lock, err := k.GetLockByID(ctx, lockID)
if err != nil {
return err
}

if lock.GetOwner() != owner.String() {
return types.ErrNotLockOwner
}

if lock.IsUnlocking() {
return fmt.Errorf("cannot edit unlocking lockup for lock %d", lock.ID)
}
Expand All @@ -384,8 +393,13 @@ func (k Keeper) ExtendLockup(ctx sdk.Context, lock types.PeriodLock, newDuration
return fmt.Errorf("cannot edit lockup with synthetic lock %d", lock.ID)
}

oldLock := lock
// completely delete existing lock refs
err = k.deleteLockRefs(ctx, unlockingPrefix(lock.IsUnlocking()), *lock)
if err != nil {
return err
}

oldDuration := lock.GetDuration()
if newDuration != 0 {
if newDuration <= lock.Duration {
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("new duration should be greater than the original")
Expand All @@ -400,25 +414,19 @@ func (k Keeper) ExtendLockup(ctx sdk.Context, lock types.PeriodLock, newDuration
lock.Duration = newDuration
}

// update lockup
err := k.deleteLockRefs(ctx, unlockingPrefix(oldLock.IsUnlocking()), oldLock)
if err != nil {
return err
}

err = k.addLockRefs(ctx, lock)
err = k.addLockRefs(ctx, *lock)
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

err = k.setLock(ctx, lock)
err = k.setLock(ctx, *lock)
if err != nil {
return err
}

k.hooks.OnLockupExtend(ctx,
lock.GetID(),
oldLock.GetDuration(),
oldDuration,
lock.GetDuration(),
)

Expand Down
6 changes: 3 additions & 3 deletions x/lockup/keeper/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,14 @@ func (suite *KeeperTestSuite) TestEditLockup() {
lock, _ := suite.App.LockupKeeper.GetLockByID(suite.Ctx, 1)

// duration decrease should fail
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, *lock, time.Second/2)
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, lock.ID, addr, time.Second/2)
suite.Require().Error(err)
// extending lock with same duration should fail
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, *lock, time.Second)
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, lock.ID, addr, time.Second)
suite.Require().Error(err)

// duration increase should success
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, *lock, time.Second*2)
err = suite.App.LockupKeeper.ExtendLockup(suite.Ctx, lock.ID, addr, time.Second*2)
suite.Require().NoError(err)

// check queries
Expand Down
13 changes: 7 additions & 6 deletions x/lockup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,19 @@ func createBeginUnlockEvent(lock *types.PeriodLock) sdk.Event {
func (server msgServer) ExtendLockup(goCtx context.Context, msg *types.MsgExtendLockup) (*types.MsgExtendLockupResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

lock, err := server.keeper.GetLockByID(ctx, msg.ID)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
return nil, err
}

if msg.Owner != lock.Owner {
return nil, sdkerrors.Wrapf(types.ErrNotLockOwner, fmt.Sprintf("msg sender (%s) and lock owner (%s) does not match", msg.Owner, lock.Owner))
err = server.keeper.ExtendLockup(ctx, msg.ID, owner, msg.Duration)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}

err = server.keeper.ExtendLockup(ctx, *lock, msg.Duration)
lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}

ctx.EventManager().EmitEvents(sdk.Events{
Expand Down