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

Review #190

Closed
wants to merge 25 commits into from
Closed

Review #190

wants to merge 25 commits into from

Conversation

migueldingli1997
Copy link
Contributor

@migueldingli1997 migueldingli1997 commented Oct 24, 2021

Description

Code review of the entire module. Refer to the Files changed for most of the review comments.


Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

  • Appropriate labels applied
  • Targeted PR against correct branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the module structure standards.
  • Wrote unit and integration
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

@migueldingli1997 migueldingli1997 marked this pull request as draft October 24, 2021 18:24
Copy link
Contributor Author

@migueldingli1997 migueldingli1997 left a comment

Choose a reason for hiding this comment

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

Left some extra comments

k.SetCurrentEpochDays(ctx, genState.CurrentEpochDays)
moduleAcc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)
k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The GetModuleAccount function creates the module account if it does not exist. This is similar to what the mint and auth modules do. No need to get and set; get is enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

It will be applied on #202, Thanks 🙇

removedFromStaking := queuedStaking.Amount.Neg()
queuedStaking.Amount = sdk.ZeroInt()
removedFromStaking := queuedStaking.Amount.Neg() // make negative a positive
staking.Amount = staking.Amount.Sub(removedFromStaking)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to .Sub for readability

Copy link
Contributor

Choose a reason for hiding this comment

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

It will be applied on #202, Thank you.

Comment on lines +297 to 302
k.DeleteQueuedStaking(ctx, coin.Denom, farmerAcc)
k.DecreaseTotalStakings(ctx, coin.Denom, removedFromStaking)
}

if queuedStaking.Amount.IsPositive() {
} else if queuedStaking.Amount.IsPositive() {
k.SetQueuedStaking(ctx, coin.Denom, farmerAcc, queuedStaking)
} else {
k.DeleteQueuedStaking(ctx, coin.Denom, farmerAcc)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Joined conditions for readability. This required adding a call to .DeleteQueuedStaking in the if clause

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 2b08a97 (not merged)

@migueldingli1997
Copy link
Contributor Author

Apart from the comments and suggestions in the Files changed section; in general, more documentation (both inline and in the spec) are needed. This is not a simple module, and so the documentation requirements are greater.

@migueldingli1997 migueldingli1997 marked this pull request as ready for review October 25, 2021 16:31
@dongsam
Copy link
Contributor

dongsam commented Oct 26, 2021

@migueldingli1997 Thank you for your valuable review and suggestion. Would it be better to commit the modifications you suggested on this PR? Or the suggestions and fix commit may be mixed, so should we just talk here and proceed with the fix in a separate PR?

@migueldingli1997
Copy link
Contributor Author

@dongsam Let's just talk here and proceed with any fixes that we agree upon in a separate PR :)

@migueldingli1997 migueldingli1997 changed the title Draft: Review Review Oct 26, 2021
Copy link
Contributor

@jaybxyz jaybxyz left a comment

Choose a reason for hiding this comment

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

Appreciate for reviewing the farming module @migueldingli1997 ! They are so valuable feedbacks and suggestions.

README.md Show resolved Hide resolved
app/app.go Show resolved Hide resolved
proto/tendermint/farming/v1beta1/query.proto Show resolved Hide resolved
x/farming/keeper/proposal_handler.go Show resolved Hide resolved
@@ -17,6 +17,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
if !plan.GetTerminated() && ctx.BlockTime().After(plan.GetEndTime()) {
if err := k.TerminatePlan(ctx, plan); err != nil {
panic(err)
// Consider deleting terminated plans
Copy link
Contributor

Choose a reason for hiding this comment

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

We've discussed about this internally before, and we decided not to delete terminated plans, to provide historical information.

@@ -19,9 +19,9 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {

k.SetParams(ctx, genState.Params)
// TODO: what if CurrentEpochDays field was empty?
// ^ If it is empty, it will default to zero and the following error comes up: 'current epoch days must be positive'
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be fixed through #192.

@@ -210,6 +210,7 @@ func (k Keeper) WithdrawRewards(ctx sdk.Context, farmerAcc sdk.AccAddress, staki

currentEpoch := k.GetCurrentEpoch(ctx, stakingCoinDenom)
// TODO: handle if currentEpoch is 0
// ^ Ensure this is handled
Copy link
Contributor

Choose a reason for hiding this comment

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

It'll be fixed through #192.

Comment on lines +287 to +288
// Consider adding totalRewards to list of events, so that this shows up when querying the transaction.
// However I understand that WithdrawRewards itself does this for each denom one by one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to add total rewards withdrawn in both messages.

Let me fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Added event attribute in de9ca0c (not merged)

@@ -303,6 +306,8 @@ func (k Keeper) AllocationInfos(ctx sdk.Context) []AllocationInfo {
farmingPoolBalances := make(map[string]sdk.Coins) // farmingPoolAddress => sdk.Coins
allocCoins := make(map[string]map[uint64]sdk.Coins) // farmingPoolAddress => (planId => sdk.Coins)

// This complex function is lacking inline documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me add inline documentation around the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Added comments in b83ea9a (not merged)

@@ -363,6 +369,8 @@ func (k Keeper) AllocationInfos(ctx sdk.Context) []AllocationInfo {
func (k Keeper) AllocateRewards(ctx sdk.Context) error {
unitRewardsByDenom := map[string]sdk.DecCoins{} // (staking coin denom) => (unit rewards)

// This complex function is lacking inline documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me add internal documentation around the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Added comments in b83ea9a (not merged)

@@ -379,8 +387,7 @@ func (k Keeper) AllocateRewards(ctx sdk.Context) error {
continue
}

weightProportion := weight.Amount.QuoTruncate(totalWeight)
allocCoins, _ := sdk.NewDecCoinsFromCoins(allocInfo.Amount...).MulDecTruncate(weightProportion).TruncateDecimal()
allocCoins, _ := sdk.NewDecCoinsFromCoins(allocInfo.Amount...).MulDecTruncate(weight.Amount).TruncateDecimal()
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't need to calculate totalWeight anymore(since it'll always be 1 because of the validation logic in msg/proposal handlers) , we can accept this change.

Let me fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep that's the context, since totalWeight is expected to always be 1 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 4f0ea4a (not merged)

x/farming/keeper/staking.go Outdated Show resolved Hide resolved
Comment on lines +148 to +149
// ^ No need to check if balance has enough to pay for fee. Can just try to .SendCoins; it will error if insufficient tokens
// If we want a specific error, the error returned by SendCoins can be replaced by the above error.
Copy link
Contributor

Choose a reason for hiding this comment

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

That's true. I'll fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in cdd32e3 (not merged now)

app/app.go Outdated Show resolved Hide resolved
Co-authored-by: Dylan Frendo <ferendo@users.noreply.github.com>
app/app.go Outdated Show resolved Hide resolved
Comment on lines +148 to +149
// ^ No need to check if balance has enough to pay for fee. Can just try to .SendCoins; it will error if insufficient tokens
// If we want a specific error, the error returned by SendCoins can be replaced by the above error.
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in cdd32e3 (not merged now)

Comment on lines +199 to +202
return nil, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "lack of %s coins to pay private plan creation fee", diffs.String())
}
// ^ No need to check if balance has enough to pay for fee. Can just try to .SendCoins; it will error if insufficient tokens
// If we want a specific error, the error returned by SendCoins can be replaced by the above error.
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in cdd32e3 (not merged now)

Comment on lines +287 to +288
// Consider adding totalRewards to list of events, so that this shows up when querying the transaction.
// However I understand that WithdrawRewards itself does this for each denom one by one.
Copy link
Contributor

Choose a reason for hiding this comment

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

Added event attribute in de9ca0c (not merged)

@@ -303,6 +306,8 @@ func (k Keeper) AllocationInfos(ctx sdk.Context) []AllocationInfo {
farmingPoolBalances := make(map[string]sdk.Coins) // farmingPoolAddress => sdk.Coins
allocCoins := make(map[string]map[uint64]sdk.Coins) // farmingPoolAddress => (planId => sdk.Coins)

// This complex function is lacking inline documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

Added comments in b83ea9a (not merged)

@@ -336,9 +341,10 @@ func (k Keeper) AllocationInfos(ctx sdk.Context) []AllocationInfo {
}

var allocInfos []AllocationInfo
for farmingPool, coins := range allocCoins {
// Renamed coins -> planCoins to make it more clear that it's a map (planId => sdk.Coins) not just sdk.Coins
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in b83ea9a (not merged)

@@ -363,6 +369,8 @@ func (k Keeper) AllocationInfos(ctx sdk.Context) []AllocationInfo {
func (k Keeper) AllocateRewards(ctx sdk.Context) error {
unitRewardsByDenom := map[string]sdk.DecCoins{} // (staking coin denom) => (unit rewards)

// This complex function is lacking inline documentation
Copy link
Contributor

Choose a reason for hiding this comment

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

Added comments in b83ea9a (not merged)

@@ -379,8 +387,7 @@ func (k Keeper) AllocateRewards(ctx sdk.Context) error {
continue
}

weightProportion := weight.Amount.QuoTruncate(totalWeight)
allocCoins, _ := sdk.NewDecCoinsFromCoins(allocInfo.Amount...).MulDecTruncate(weightProportion).TruncateDecimal()
allocCoins, _ := sdk.NewDecCoinsFromCoins(allocInfo.Amount...).MulDecTruncate(weight.Amount).TruncateDecimal()
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 4f0ea4a (not merged)

Comment on lines +297 to 302
k.DeleteQueuedStaking(ctx, coin.Denom, farmerAcc)
k.DecreaseTotalStakings(ctx, coin.Denom, removedFromStaking)
}

if queuedStaking.Amount.IsPositive() {
} else if queuedStaking.Amount.IsPositive() {
k.SetQueuedStaking(ctx, coin.Denom, farmerAcc, queuedStaking)
} else {
k.DeleteQueuedStaking(ctx, coin.Denom, farmerAcc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 2b08a97 (not merged)

@@ -14,7 +14,7 @@ There are two types of farming plans in the `farming` module as below.
A public farming plan can only be created through governance proposal meaning that the proposal must be first agreed and passed in order to create a public plan.
### 2. Private Farming Plan

A private farming plan can be created with any account. The plan creator's account is used as `TerminationAddress`. There is a fee `PlanCreationFee` paid upon plan creation to prevent from spamming attack.
A private farming plan can be created with any account. The plan creator's account is used as `TerminationAddress` (consider explaining what a TerminationAddress is used for here). There is a fee `PlanCreationFee` paid upon plan creation to prevent from spamming attack.
Copy link
Contributor

Choose a reason for hiding this comment

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

It will be added on #232, Thanks!

if clientCtx.Offline {
return fmt.Errorf("cannot generate tx in offline mode with --all flag")
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I cherry-picked this commit on #232, Thanks for the suggestion.

@dongsam
Copy link
Contributor

dongsam commented Nov 24, 2021

Close the issue because all suggestions have been discussed or resolved. Thank you again :)

@dongsam dongsam closed this Nov 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants