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 x/staking Validation and Delegation tests based on MsgCreateValidator.Pubkey type change. #7526

Merged
merged 19 commits into from
Oct 19, 2020

Conversation

robert-zaremba
Copy link
Collaborator

@robert-zaremba robert-zaremba commented Oct 13, 2020

This Changeset introduces set of improvements for writing tests based on
MsgCreateValidator.Pubkey type change from string to Any

The changes here are driven by the tests updates related to message creation
in x/staking.

Motivation

Lot of tests in the SDK don't have any abstraction. This makes tests hard to maintain. Common symptoms are:

  • lot of code repetition.
  • long tests (by the amount of lines)

As presented in this case, a small change in the code (type change and introducing one return error) can lead to many changes in the tests.

Solution

The idea is to create a testing subpackage which will provide functions
to make tests more dev-friendly and wrap higher level use-cases.
Here is a show-up of of creating a testing service for staking module. That service encapsulates common logic, handles the error checks.

Related to


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.

  • Targeted PR against correct branch (see CONTRIBUTING.md)
  • 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 tests
  • Updated relevant documentation (docs/) or specification (x/<module>/spec/)
  • Added relevant godoc comments.
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the Github PR explorer
  • Review Codecov Report in the comment section below once CI passes

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

I love PRs that have a negative line diff, so 👍 for now. But a lot of tests still need to be fixed, will review again once that's the case.

proto/cosmos/staking/v1beta1/tx.proto Show resolved Hide resolved
testutil/network/network.go Outdated Show resolved Hide resolved
x/staking/teststaking/service.go Outdated Show resolved Hide resolved
x/slashing/keeper/test_common.go Outdated Show resolved Hide resolved
aaronc
aaronc previously requested changes Oct 14, 2020
// PackAny is a checked and safe version of UnsafePackAny. It assures that
// `x` implements the proto.Message interface and uses it to serialize `x`.
// TODO: should be moved away: https://github.com/cosmos/cosmos-sdk/issues/7479
func PackAny(x interface{}) (*Any, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
func PackAny(x interface{}) (*Any, error) {
func LegacyPackAny(x interface{}) (*Any, error) {

The name should clearly indicate that this is only for legacy code. I would even add a deprecated comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've market this function as deprecated.

@robert-zaremba
Copy link
Collaborator Author

I must say that I'm using that the test-build-check make job is useful in this task. Would be good to revisit it. #7364

@aaronc aaronc changed the title Refactore x/staking Validation and Delegation tests based on MsgCreateValidator.Pubkey type change. Refactor x/staking Validation and Delegation tests based on MsgCreateValidator.Pubkey type change. Oct 14, 2020
@aaronc aaronc added this to the v0.40 [Stargate] milestone Oct 14, 2020
This Changeset introduces set of improvements for writing tests.

The idea is to create a testing subpackage which will provide functions
to make tests more dev-friendly and wrap higher level use-cases.
Here is a show-up of of creating a service for staking module
for tests.

This PR also changes the `x/staking/types.MsgCreateValidator.Pubkey` from string
to types.Any. This change motivated the other change to show the pattern I'm describing here.
@robert-zaremba robert-zaremba force-pushed the robert/staking-test-service branch from 312d579 to 1a29257 Compare October 16, 2020 00:32
Copy link
Contributor

@blushi blushi left a comment

Choose a reason for hiding this comment

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

You need to implement UnpackInterfaces method on MsgCreateValidator since it now contains an Any.

EDIT: done in 11924dd

x/staking/types/msg_test.go Show resolved Hide resolved
x/staking/types/msg_test.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Oct 16, 2020

Codecov Report

Merging #7526 into master will decrease coverage by 0.00%.
The diff coverage is 41.66%.

@@            Coverage Diff             @@
##           master    #7526      +/-   ##
==========================================
- Coverage   54.15%   54.15%   -0.01%     
==========================================
  Files         606      606              
  Lines       38164    38187      +23     
==========================================
+ Hits        20669    20680      +11     
- Misses      15399    15410      +11     
- Partials     2096     2097       +1     

@clevinson
Copy link
Contributor

There was one remaining failing test which I just fixed, so I think all tests should be passing now. I left out migrating Validator.ConsensusPubKey, as I think this PR is mostly focused on the testing improvements.

I added this to the description of #7477 so we don't forget.

@robert-zaremba
Copy link
Collaborator Author

Thank you for helping with fixing tests.

@robert-zaremba
Copy link
Collaborator Author

There are few other things failing, but the crux is solved (Protobuf decoding) 💪
I will be tomorrow with a computer and can handle the remaining tests.

@robert-zaremba
Copy link
Collaborator Author

I fixed the remaining 2 tests and renamed Service to Handler.
IMHO the PR is ready to be merged.

Copy link
Contributor

@amaury1093 amaury1093 left a comment

Choose a reason for hiding this comment

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

utACK

Comment on lines 23 to 26
// description := Description{}
// commission1 := NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
// msg, err := NewMsgCreateValidator(valAddr1, pk1, coinPos, description, commission1, sdk.OneInt())
// require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

let's not leave this uncommented

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, thanks for spotting.

x/staking/teststaking/service.go Outdated Show resolved Hide resolved
}

default:
// We only allow ed25519 pubkeys to be bech32-ed right now.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// We only allow ed25519 pubkeys to be bech32-ed right now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm removing this in other PR.

@robert-zaremba robert-zaremba added the A:automerge Automatically merge PR once all prerequisites pass. label Oct 19, 2020
@robert-zaremba robert-zaremba removed the A:automerge Automatically merge PR once all prerequisites pass. label Oct 19, 2020
Copy link
Contributor

@blushi blushi left a comment

Choose a reason for hiding this comment

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

Looks good to me overall, although I believe new testslashing and teststaking folders names are a bit redundant given that they're already in x/slashing and x/staking resp. Why not using a more concise name?

@robert-zaremba
Copy link
Collaborator Author

Looks good to me overall, although I believe new testslashing and teststaking folders names are a bit redundant given that they're already in x/slashing and x/staking resp. Why not using a more concise name?

I was already explaning that in few places. The problem is that we want to use very generic names (helper, test, common.... 😦 ) but we put there context specific code (eg, things related to given module, or codec). This causes name clashes and abuses package aliases.

  • If I rename it to testing (alessio made that suggestion) then it will conflict with the std lib, which we MUST NOT do.
  • if I rename to helper / common etc... it will conflict with other types and we end up doing aforementioned alisases as we do with codectypes, sdk, authtypes etc....
  • moreover, this confuses the editor, and makes the automatic import not working, eg: helper.NewHelper can refer to any module helper package.

TL;DR: cointinuing this practice is rather diminishing the quality of code, DevX and tooling support.
and with aliases, the name won't be more concise 😉

@blushi blushi self-requested a review October 19, 2020 12:41
@robert-zaremba robert-zaremba added the A:automerge Automatically merge PR once all prerequisites pass. label Oct 19, 2020
@mergify mergify bot merged commit 8eaf2ec into master Oct 19, 2020
@mergify mergify bot deleted the robert/staking-test-service branch October 19, 2020 13:04
@robert-zaremba robert-zaremba mentioned this pull request Oct 20, 2020
9 tasks
clevinson added a commit that referenced this pull request Oct 29, 2020
…Validator.Pubkey type change. (#7526)

* testing: refactore Validation and Delegation handling of x/staking

This Changeset introduces set of improvements for writing tests.

The idea is to create a testing subpackage which will provide functions
to make tests more dev-friendly and wrap higher level use-cases.
Here is a show-up of of creating a service for staking module
for tests.

This PR also changes the `x/staking/types.MsgCreateValidator.Pubkey` from string
to types.Any. This change motivated the other change to show the pattern I'm describing here.

* add validator checks

* type change fixes

* use deprecated

* adding test slashing

* new network comment update

* working on tests

* Fix TestMsgPkDecode test

* Add UnpackInterfaces to MsgCreateValidator

* Fix tests

* Convert bech32 pubkey to proto

* Fix test

* fix v039/migrate_test/TestMigrate

* fix tests

* testslashing: rename Service to Helper

* file rename

* update TestMsgDecode

Co-authored-by: blushi <marie.gauthier63@gmail.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Cory Levinson <cjlevinson@gmail.com>
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
…Validator.Pubkey type change. (cosmos#7526)

* testing: refactore Validation and Delegation handling of x/staking

This Changeset introduces set of improvements for writing tests.

The idea is to create a testing subpackage which will provide functions
to make tests more dev-friendly and wrap higher level use-cases.
Here is a show-up of of creating a service for staking module
for tests.

This PR also changes the `x/staking/types.MsgCreateValidator.Pubkey` from string
to types.Any. This change motivated the other change to show the pattern I'm describing here.

* add validator checks

* type change fixes

* use deprecated

* adding test slashing

* new network comment update

* working on tests

* Fix TestMsgPkDecode test

* Add UnpackInterfaces to MsgCreateValidator

* Fix tests

* Convert bech32 pubkey to proto

* Fix test

* fix v039/migrate_test/TestMigrate

* fix tests

* testslashing: rename Service to Helper

* file rename

* update TestMsgDecode

Co-authored-by: blushi <marie.gauthier63@gmail.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Cory Levinson <cjlevinson@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:automerge Automatically merge PR once all prerequisites pass.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants