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: improve leverage keeper tests and errors #1300

Merged
merged 45 commits into from
Sep 1, 2022
Merged

refactor: improve leverage keeper tests and errors #1300

merged 45 commits into from
Sep 1, 2022

Conversation

toteki
Copy link
Member

@toteki toteki commented Aug 30, 2022

Description

Except small behavior changes, this PR is limited to x/leverage/keeper/*_test.go

Behavior changes:

  • Fixed MsgCollateralize ValidateBasic allowing nil coin
  • Added uToken exchange rates >= 1 invariant
  • Change validation order to return more accurate error messages for a few transactions

Main improvements:

  • Switch keeper tests for each message type to table based, and greatly expanded testing of secondary effects on balances.
  • Reorganize errors and test for specific error types
  • Remove the cascading failures of x/leverage/client/tests which would make it hard to identify the root cause of a failure
  • Remove s.initBorrowScenario in favor of more specific test setup functions (e.g. s.borrow)
  • Check invariants after keeper_test.go functions
  • For all expected errors, expect a typed error, e.g. require.ErrorIs(err, types.ErrNotRegisteredToken)

Cosmetic improvements:

  • Use underscores to separate 6 digits for coins in test files. e.g, 400_000000 when creating 400 UMEE
  • Ensure expected value is always on the left in require.Equal(expected,actual) for accurate test fail messages

closes: #1310
closes: #1242


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@codecov-commenter
Copy link

codecov-commenter commented Aug 30, 2022

Codecov Report

Merging #1300 (ccec6f0) into main (06424cd) will increase coverage by 0.02%.
The diff coverage is 58.16%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1300      +/-   ##
==========================================
+ Coverage   51.65%   51.67%   +0.02%     
==========================================
  Files          68       68              
  Lines        6870     6697     -173     
==========================================
- Hits         3549     3461      -88     
+ Misses       3063     2969      -94     
- Partials      258      267       +9     
Impacted Files Coverage Δ
x/ibctransfer/keeper/keeper.go 0.00% <ø> (ø)
x/leverage/keeper/msg_server.go 1.17% <0.00%> (+0.35%) ⬆️
x/leverage/types/tx.go 0.00% <0.00%> (ø)
x/oracle/types/keys.go 100.00% <ø> (ø)
x/oracle/types/msgs.go 90.21% <0.00%> (-3.47%) ⬇️
x/leverage/keeper/exchange_rate.go 72.72% <12.50%> (ø)
x/leverage/keeper/oracle.go 60.29% <25.00%> (-7.28%) ⬇️
x/leverage/keeper/token.go 74.19% <50.00%> (-2.48%) ⬇️
x/leverage/types/token.go 72.52% <50.00%> (ø)
x/leverage/keeper/reserves.go 68.35% <62.50%> (-6.65%) ⬇️
... and 22 more

@toteki toteki marked this pull request as ready for review August 30, 2022 18:05
@toteki toteki requested review from a team as code owners August 30, 2022 18:05
@toteki
Copy link
Member Author

toteki commented Sep 1, 2022

Note: markdown link test fail was http 429: Too many requests - we'll have to look at that in the future.

@toteki toteki merged commit 9ec13b9 into main Sep 1, 2022
@toteki toteki deleted the adam/tests branch September 1, 2022 15:00
Copy link
Member

@robert-zaremba robert-zaremba left a comment

Choose a reason for hiding this comment

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

late review. Good job a nice improvements. Left few small things / suggestions.

s.Require().Equal(sdk.NewInt64Coin(uDenom, 0), collateral)
// get u/umee collateral amount of empty account address
collateral := s.tk.GetCollateralAmount(ctx, sdk.AccAddress{}, uDenom)
require.Equal(coin(uDenom, 0), collateral)
Copy link
Member

Choose a reason for hiding this comment

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

coin could be a keyword :D

fmt.Sprintf("amount of uToken exchange rates lower than one %d\n%s", count, msg),
), broken
}
}
Copy link
Member

Choose a reason for hiding this comment

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

do we really want to check it after each block by every validator? I think we should have that function only invalidate genesis.

Copy link
Member Author

@toteki toteki Sep 1, 2022

Choose a reason for hiding this comment

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

Some of these would be expensive computationally to do each block, but having checked other modules (e.g. x/bank invariants) I see that iteration over balances is done there as well.

Is there some setting that validators are using to run invariants less often as they operate other chains?

Copy link
Member

Choose a reason for hiding this comment

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

No, there is no such setting. But we can code it. The x/bank invariants are often disabled, because they take lot of time.

Copy link
Member

Choose a reason for hiding this comment

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

I've made a task for this issue: #1324

Copy link
Member

Choose a reason for hiding this comment

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

BTW, the bank invariants are not even registered in the simapp.


// check invariants
_, broken := keeper.ReserveAmountInvariant(app.LeverageKeeper)(ctx)
require.False(broken)
Copy link
Member

Choose a reason for hiding this comment

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

we should also test negative scenarios

// Note: Setting umee liquidation threshold to 0.05 to make the user eligible for liquidation
umeeToken := newToken("uumee", "UMEE")
umeeToken.CollateralWeight = sdk.MustNewDecFromStr("0.05")
umeeToken.LiquidationThreshold = sdk.MustNewDecFromStr("0.05")
Copy link
Member

Choose a reason for hiding this comment

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

this 3 lines are repeated often. Could be better to create a function: newUmeeToken(collateraW, liquidationT)

}

// newAccount creates a new account for testing, and funds it with any input tokens.
func (s *IntegrationTestSuite) newAccount(funds ...sdk.Coin) sdk.AccAddress {
Copy link
Member

Choose a reason for hiding this comment

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

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants