-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add an optional extra level of checking: ASSUME(...) - an opt-in side-effect safe assert(...) #16136
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
Conversation
If you're running the CHECK no matter what, could consider making the failure configurable at runtime (could default to not abort on mainnet, but to abort on testnet/regtest maybe?). Something like:
|
@ajtowns I like the idea of making the default depend on the network. I'll let others chime in regarding how to choose the defaults and how to override it and then update based on the feedback :-) |
Concept ACK -- I often want something that's like an assert but wouldn't cause cascading failure on the network if I'm wrong. |
Concept ACK, but I also really like the idea of LogPrinting these (either no matter what or at least if some level of debug logging is on). Part of the benefit would be in catching rare conditions that might not occur in regular testing but would show up occasionally among thousands of real world users. The LogPrint could direct the user to report the error on GitHub. |
The link could even prefill all the additional debug information. E.g. https://github.com/bitcoin/bitcoin/issues/new?title=file_name.cpp:LINE_NR%20CHECK%20failed%20bla&body=more%20bla |
Concept ACK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK if done right (with proper examples and documentation), but currently it seems to be confusing assert
with CHECK
.
And maybe it should be called ASSUME
?
This change seems good, but I'd suggest calling the macro
For reference:
Re: #16136 (comment) and #16136 (comment) I think what Marco and Alex are asking for is useful but out of scope for an assert or check macro. I think asserts and checks are best for succinctly conveying assumptions made by the code author that in no cases should ever be violated, and conveying at a very coarse level how crucial those assumptions are (abort-worthy or not). The main point of asserts and checks should be to make code more readable and communicate how to think about it. Once you're in the realm of conditions that you think should never happen, but are more complicated and might end up happening anyway, you are really better off writing an actual log print with text that would put the error into context. You may also want to abort in these cases, which is why logging frameworks support |
02fd0cc
to
a32e448
Compare
Why was this closed? |
@MarcoFalke I try to limit the number of PR:s I have opened and it seemed like this had a low probability of being merged. Seems I was wrong: now re-opened :) |
Not interested in bikeshedding and this is fine if other reviewers want it, but I'll just point out that as of b1eba55 this PR makes A good way to provide more clarity, I think, would be to follow convention used in other libraries and add a pair of |
@ryanofsky What about |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this, but am not sure it makes sense :)
src/assume.h
Outdated
// Bitcoin Core is always compiled with assertions enabled. assert(...) | ||
// is thus only appropriate for documenting critical assumptions | ||
// where a failed assumption should lead to immediate abortion also | ||
// in production environments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not really clear to me when you should choose assert
vs ASSUME
-- neither is any quicker because both always run the test, and if the assumptions you had when writing the code were wrong, how confident can you really be that continuing won't cause horrible corruption to decide that continuing is okay?
Should be moved to the existing |
e006452
to
2812b0d
Compare
@MarcoFalke Good idea: addressed! Please re-review. |
Would it make sense to add a wrapper around our current We have vague error reports like #17298, that obviously lack details in the debug log, because logging wasn't enabled. Maybe an wrapper around our current assert that still aborts the program, but also dumps a full |
@MarcoFalke That could be nifty to have. I would like to keep this PR and My suggestion is that we start with something super simple in and then we can iterate from that as we gain experience with the different use cases :) |
…able-debug or --enable-fuzz.
Fine, but at the very least we should decide on naming. I guess, given that this patch does not use the |
@MarcoFalke I think I've encountered a few places where |
ACK 674f9d5, didn't look at the configure changes |
Closing due to lack of interest :) |
We have |
I think |
Revived in #20138 with an actual use-case (I hope) |
As suggested by sipa in the open issue #4576 (2014) -- "[discussion] Dealing with assertions and optional consistency checking" -- as an alternative to
assert(…)
in situations whereassert(…)
is not appropriate:ASSUME(expression)
works like this:-DABORT_ON_FAILED_ASSUME
(set by--enable-debug
and/or--enable-fuzz
): Evaluate expression and abort ifexpression
isfalse
.-DABORT_ON_FAILED_ASSUME
: Evaluateexpression
and continue execution.Example:
If
!IsFoo()
and-DABORT_ON_FAILED_ASSUME
, then:Otherwise the execution continues.
Resolves #4576.