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

Assess keeping invariants grouped #47

Closed
loredanacirstea opened this issue Apr 25, 2018 · 7 comments · Fixed by #64
Closed

Assess keeping invariants grouped #47

loredanacirstea opened this issue Apr 25, 2018 · 7 comments · Fixed by #64
Milestone

Comments

@loredanacirstea
Copy link
Contributor

(must be updated with a proper description)

As mentioned in this thread: #39 (comment)

keeping the invariants grouped? I think it is easier to verify them if we don't need to jump around in the contract code.
IMO this should not take priority over clarity if the input is invalid, only in cases where the input is valid but a race happened (e.g. two monitoring services called update at the same time, only one of the transactions will work)

@LefterisJP
Copy link
Contributor

Just my 2 cents. I also would really like to have all the require invariants somewhere. This would make each function so much easier to read and reason about.

@loredanacirstea
Copy link
Contributor Author

loredanacirstea commented Apr 28, 2018

Yes, I thought about this and I think I went too far with making sure the gas cost is minimal.
So, I would stick to using require strictly for input values, at the beginning of the function (as the official Solidity docs recommend anyway) and move all other invariants in asserts at the bottom of the function.
Does this sound good?

@LefterisJP
Copy link
Contributor

Grouping the invariants is fine but you should only use asserts for internal errors and things that should never ever happen. I am commenting on #64 about it.

Docs which help us understand the different between require() and `assert(): http://solidity.readthedocs.io/en/v0.4.23/control-structures.html?highlight=assert#error-handling-assert-require-revert-and-exceptions

@hackaugusto
Copy link
Contributor

hackaugusto commented Aug 17, 2018

Just to clarify my point, code should be written to be readable first. I don't have a hard rule for how to format code for readability, specially because of solidity's constraints on the number of variables in a stack, which require intermediary functions to be introduced, which may hide validation inside these functions.

But I would strive for a contract-based approach, where the beginning of a function starts with the requirements, first validating/requiring the value inputs, then the code is followed by the pure computation, which is followed by the validation of the computation and side-effects, and the code finishes with the asserts/invariants that must be true after the execution of the code.

@loredanacirstea
Copy link
Contributor Author

But I would strive for a contract-based approach, where the beginning of a function starts with the requirements, first validating/requiring the value inputs, then the code is followed by the computation, which is followed by the validation of the computation and external side-effects, and the code finishes with the asserts/invariants that must be true after the execution of the code.

I fully agree with this.
In my opinion this is how you get more secure code & more readable.
Especially more secure code. When you group all the post-state-change (computation) checks to the end of the function it is way harder to make sure that that variables did not change their state in between the computation and the check. Sometimes you need to change them and you end up needing to store the initial state in another local variable (very ugly).

I think @berndbohmeier also agrees with this.

@loredanacirstea
Copy link
Contributor Author

I reopened the issue. The checks should follow the above proposed format:

  • check function inputs
  • check other state changes preconditions
  • perform actions and immediately check if these action succeeded
  • check all other invariants (asserts) at the end

This issue will be closed when the above format is implemented.

err508 added a commit to err508/raiden-contracts that referenced this issue Aug 22, 2018
 as described in  raiden-network#238. Additionally, the function was refactored
to be more clear and readable, as discussed in raiden-network#47.
err508 added a commit to err508/raiden-contracts that referenced this issue Aug 22, 2018
 as described in  raiden-network#238. Additionally, the function was refactored
to be more clear and readable, as discussed in raiden-network#47.
@loredanacirstea
Copy link
Contributor Author

#248 implements the agreed changes.

err508 added a commit to err508/raiden-contracts that referenced this issue Aug 23, 2018
err508 added a commit to err508/raiden-contracts that referenced this issue Aug 23, 2018
err508 added a commit to err508/raiden-contracts that referenced this issue Aug 23, 2018
err508 added a commit to err508/raiden-contracts that referenced this issue Aug 23, 2018
err508 added a commit to err508/raiden-contracts that referenced this issue Aug 23, 2018
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 a pull request may close this issue.

3 participants