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

Refactor lock method #1936

Merged
merged 6 commits into from
Jul 5, 2022
Merged

Refactor lock method #1936

merged 6 commits into from
Jul 5, 2022

Conversation

mattverse
Copy link
Member

@mattverse mattverse commented Jul 1, 2022

Sub component of: #1838

What is the purpose of the change

The lock method in the lockup module was a public method before current change, which was an imo dangerous design, as it is and should be only called by either of the two methods

  • CreateLock
  • AddTokensToLockByID

That being said, there shouldn't be any cases where the Lock method is called directly, but called only through the two entrypoints mentioned above, not to mention the code duplication that we had for the two methods above.

This PR changes the Lock method into a private method and refactors code duplication logic as well.

Brief Changelog

  • API change for the Lock method in x/lockup module.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? no
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? no
  • How is the feature or change documented? not documented

@mattverse mattverse requested a review from a team July 1, 2022 05:32
@mattverse mattverse mentioned this pull request Jul 1, 2022
7 tasks
x/lockup/keeper/lock.go Show resolved Hide resolved
Comment on lines -83 to -85
err = k.addTokenToLock(ctx, lock, coin)
if err != nil {
return nil, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong? addTokenToLock did more, e.g. handling of the main accumulation store? Why was this deleted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh the claim is that all functionality is there between the synth lock line, and lock.

Why not keep addTokenToLock and just have that function as a subcomponent call lock? (But retain the synth lock functionality)

That would be maintaining the standard in this package of having the exported method be doing validation logic & hook / return type logic, and the private method doing the state change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't fully follow the logic here. There are two calls that addTokenToLock makes that are not related to the synth lock related accumulation store update, which are:

  1. setLock to update state with the new lock instance:
    err := k.setLock(ctx, *lock)
  2. Update to accumulationStore:
    k.accumulationStore(ctx, coin.Denom).Increase(accumulationKey(lock.Duration), coin.Amount)

How/where are these being accounted for in the new implementation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I see the lock call above now. The change that adds an addition parameter to lock also makes sense now. Clever refactor :)

Copy link
Member Author

@mattverse mattverse Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ValarDragon wdym by ? Are you referring to synth lock accumulation store part?

(But retain the synth lock functionality)

@@ -144,47 +144,17 @@ func (suite *KeeperTestSuite) TestUnlockPeriodLockByID() {
suite.Require().Len(locks, 0)
}

func (suite *KeeperTestSuite) TestLock() {
Copy link
Member

@p0mvn p0mvn Jul 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to do either or both:

  • have additional tests for CreateLock that would cover the removed logic
  • use the export_test.go trick that will allow us to
    • export lock for tests only and
    • keep testing the now unexported lock method

Copy link
Member Author

@mattverse mattverse Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah missed the tests! thanks for catching this! Just added this in the most recent commit!

Copy link
Contributor

@AlpinYukseloglu AlpinYukseloglu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few clarifying questions but overall LGTM! Love this refactor, cleans things up quite a bit :)

x/lockup/keeper/lock.go Show resolved Hide resolved
Comment on lines -83 to -85
err = k.addTokenToLock(ctx, lock, coin)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't fully follow the logic here. There are two calls that addTokenToLock makes that are not related to the synth lock related accumulation store update, which are:

  1. setLock to update state with the new lock instance:
    err := k.setLock(ctx, *lock)
  2. Update to accumulationStore:
    k.accumulationStore(ctx, coin.Denom).Increase(accumulationKey(lock.Duration), coin.Amount)

How/where are these being accounted for in the new implementation?

x/lockup/keeper/lock.go Show resolved Hide resolved
Comment on lines -83 to -85
err = k.addTokenToLock(ctx, lock, coin)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I see the lock call above now. The change that adds an addition parameter to lock also makes sense now. Clever refactor :)

x/lockup/keeper/lock.go Show resolved Hide resolved
@mattverse
Copy link
Member Author

merging this upon being blocker for future work for adding testing for lockup!

@mattverse mattverse merged commit c115593 into main Jul 5, 2022
@mattverse mattverse deleted the mattverse/refactor-lock branch July 5, 2022 07:29
@ValarDragon
Copy link
Member

I'm a bit concerned about this change / haven't convinced myself they are at functionality parity

@mattverse
Copy link
Member Author

Apologies if I went ahead and rushed into merging this, I supposed there weren't extra reviews.

Is there something you're concerned about or would like to give a review on? We can either revert this commit or create a new PR depending on the review

@mattverse
Copy link
Member Author

A Note to reviewers to clarify no logic has been removed / changed per this PR

Previous logic for AddTokensToLockByID

  • SendCoinsFromAccountToModule
  • addTokenToLock
    • lock.Coins = lock.Coins.Add(coin)
    • setLock
    • accumulation store increase
    • synth denom accumulation store increase
    • hooks.AfterAddTokensToLock
  • hooks.OnTokenLocked

Refactored Logic for AddTokensToLockByID

  • lock.Coins = lock.Coins.Add(coin)
  • lock
    • SendCoinsFromAccountToModule
    • setLock
    • accumulation store increase
    • hooks.OnTokenLocked
  • synth denom accumulation store increase
  • hooks.AfterAddTokensToLock

Previous logic for CreateLock

  • Lock
    • SendCoinsFromAccountToModule
    • store.Set(lock)
    • addlockRefs
    • accumulation store increase
    • hooks.OnTokenLocked
  • setLastLockID

Refactored Logic for CreateLock

  • lock
    • SendCoinsFromAccountToModule
    • setLock
    • accumulation store increase
    • hooks.OnTokenLocked
  • addLockRefs
  • SetLastLockID

@mattverse mattverse added the A:backport/v10.x backport patches to v10.x branch label Jul 12, 2022
mergify bot pushed a commit that referenced this pull request Jul 12, 2022
* Add lock method refactor

* Delete duplciated testing

* Update x/lockup/keeper/lock.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* Add tests implement feedback from code review

* Add test cases

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
(cherry picked from commit c115593)

# Conflicts:
#	x/lockup/keeper/admin_keeper_test.go
#	x/lockup/keeper/lock.go
@mattverse mattverse restored the mattverse/refactor-lock branch July 12, 2022 06:44
@mattverse mattverse added A:backport/v10.x backport patches to v10.x branch api-breaking Breaking changes that impact API only (not state machine). and removed A:backport/v10.x backport patches to v10.x branch labels Jul 12, 2022
mattverse added a commit that referenced this pull request Jul 12, 2022
* Refactor `lock` method (#1936)

* Add lock method refactor

* Delete duplciated testing

* Update x/lockup/keeper/lock.go

Co-authored-by: Aleksandr Bezobchuk <[email protected]>

* Add tests implement feedback from code review

* Add test cases

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
(cherry picked from commit c115593)

# Conflicts:
#	x/lockup/keeper/admin_keeper_test.go
#	x/lockup/keeper/lock.go

* Fix merge conflict

* Merge

Co-authored-by: Matt, Park <[email protected]>
Co-authored-by: mattverse <[email protected]>
ValarDragon added a commit that referenced this pull request Aug 1, 2022
ValarDragon added a commit that referenced this pull request Aug 3, 2022
* Revert "x/lock: Fix `ExtendLockup` API  (backport #1937) (#2030)"

This reverts commit bd32316.

* Revert "Refactor `lock` method (backport #1936) (#2029)"

This reverts commit d733f64.

* Update changelog

* Update changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v10.x backport patches to v10.x branch api-breaking Breaking changes that impact API only (not state machine). C:x/lockup
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants