Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Change assert(is_err()) to assert_noop to check state consistency on errors#8587

Merged
8 commits merged intoparitytech:masterfrom
hirschenberger:issue_8545
Apr 13, 2021
Merged

Change assert(is_err()) to assert_noop to check state consistency on errors#8587
8 commits merged intoparitytech:masterfrom
hirschenberger:issue_8545

Conversation

@hirschenberger
Copy link
Copy Markdown
Contributor

fixes #8545

@ordian ordian added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Apr 9, 2021
Comment thread frame/transaction-payment/src/lib.rs Outdated
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
@hirschenberger
Copy link
Copy Markdown
Contributor Author

hirschenberger commented Apr 10, 2021

I think I need some help here with the failing test.

When I change the expected error type to a wrong value e.g. TransactionValidityError::Invalid(InvalidTransaction::BadProof) I get this, which is what I expect:

---- tests::bad_extrinsic_not_inserted stdout ----
on_initialize(1)
thread 'tests::bad_extrinsic_not_inserted' panicked at 'assertion failed: `(left == right)`
  left: `Err(TransactionValidityError::Invalid(InvalidTransaction::Future))`,
 right: `Err(TransactionValidityError::Invalid(InvalidTransaction::BadProof))`', frame/executive/src/lib.rs:860:13

When I change the type to the correct value: TransactionValidityError::Invalid(InvalidTransaction::Future) I get this strange error:

---- tests::bad_extrinsic_not_inserted stdout ----
on_initialize(1)
thread 'tests::bad_extrinsic_not_inserted' panicked at 'assertion failed: `(left == right)`
 left: `[43, 22, 252, 42, 247, 106, 220, 153, 188, 63, 218, 120, 117, 228, 9, 45, 42, 26, 239, 243, 16, 226, 217, 97, 143, 195, 251, 246, 181, 70, 68, 178]`,
right: `[65, 171, 6, 54, 28, 188, 146, 219, 140, 67, 51, 124, 41, 234, 137, 189, 241, 58, 79, 94, 30, 245, 108, 142, 60, 124, 58, 24, 198, 160, 189, 158]`', frame/executive/src/lib.rs:860:13

@shawntabrizi
Copy link
Copy Markdown
Member

@hirschenberger this means you caught a bug :)

@hirschenberger
Copy link
Copy Markdown
Contributor Author

Yippie ;-)

Any hints where it goes wrong? The lists are no UTF-8 strings and I assume also no SCALE encoded data. Do you think it's a substrate or rust issue?

@shawntabrizi
Copy link
Copy Markdown
Member

@hirschenberger it means that in the test, there is some extrinic which is being called, and returning an error. But it is also changing the state of the chain. This is ultimately what the noop assert tests for.

You need to look into the extrinsic to see if you can catch where it can fail after already changing state. I will look into this now, it can be a little complex and sensitive to solve these issues.

BTW can you please include your DOT / KSM address? Have you done that already in another PR?

@hirschenberger
Copy link
Copy Markdown
Contributor Author

hirschenberger commented Apr 10, 2021

I see, excited to see what the reason for the state change is. I'll also look in the code and try to help with the analysis.

Yes I already got a tip from you in another PR: #8541 (comment)

I was confused about the two binary lists and thought it was due to a wrong error type that was returned. How about changing assert_noop to display a descriptive error that the state changed by just adding a third parameter to the assert_eq in the macro?

hirschenberger added a commit to hirschenberger/substrate that referenced this pull request Apr 10, 2021
@gavofyork
Copy link
Copy Markdown
Member

gavofyork commented Apr 10, 2021

There is no bug here.

The function in question (apple_extrinsic) is not a dispatchable, therefore it does not conform to the usual no-change-on-error. apply_extrinsic is allowed to, and indeed will, mutate the state if given an erroring transaction since it is assumed that the transaction is added to the block regardless of whether it happened to be successful or not.

In this case, just revert the test to how it was before (or, if you like, use assert_err!() instead to ensure the error is correct).

Comment thread frame/contracts/src/exec.rs Outdated
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Comment thread frame/democracy/src/benchmarking.rs Outdated
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Comment thread frame/transaction-payment/src/lib.rs Outdated
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
hirschenberger added a commit to hirschenberger/substrate that referenced this pull request Apr 10, 2021
hirschenberger added a commit to hirschenberger/substrate that referenced this pull request Apr 10, 2021
@shawntabrizi
Copy link
Copy Markdown
Member

@hirschenberger no force push please :)

If you need help with git, happy to do so.

@hirschenberger
Copy link
Copy Markdown
Contributor Author

I think I'm done. CI got stuck.

@gui1117
Copy link
Copy Markdown
Contributor

gui1117 commented Apr 13, 2021

bot merge

@ghost
Copy link
Copy Markdown

ghost commented Apr 13, 2021

Trying merge.

@ghost ghost merged commit 62eca6c into paritytech:master Apr 13, 2021
hirschenberger added a commit to hirschenberger/substrate that referenced this pull request Apr 14, 2021
…errors (paritytech#8587)

* Change is_err() asserts in tests to assert_noop to check state consistency

fixes paritytech#8545

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Update frame/contracts/src/exec.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/democracy/src/benchmarking.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/transaction-payment/src/lib.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Don't assert no-changing state.

see: paritytech#8587 (comment)

* fix expected error

* Fix non-extrinsic-call asserts

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit use of .is_err() in Runtime Tests

6 participants