This PR is a refactor only (i.e., any functional changes should be reported in review) which does the following:
- Replace
state.DoS
with more descriptive calls where straightforward. Rather than just call DoS or Invalid, different causes of invalidity have different functions. This makes it easier to quickly find all related causes of that class of error. - Convert all
nDoS
usage to an enum class with named levels (none 0, low 1, medium 10, elevated 20, high 50, and critical 100) - Use a custom enum class for reporting corruption to make it more clear where corruption occurs
- return false directly from
CValidtationState
update call sites. state.DoS never returns true, so it makes it easier to see that the return value is not dependent on the call. - Don’t pass
error()
as an argument to a function in DoS. error always returns false, and this is confusing for readers/reviewers.
If anyone is interested, there’s an unsquashed version too, but I figured this is simple enough to review squashed.
The only code quality ‘decrease’ is that some reject codes move from validation.h
to consensus/validation.h
. This abstraction barrier violation is already present (the CValidationState
class is expected to handle those reject codes appropriately) so I think that this change is a lesser evil.
Motivation
I’m currently working on reworking the separation between reporting errors and DoS. As a first step in this process, I’ve cleaned up the interface without making any functional changes or major architecture changes. The resulting code should be easier to read and review changes to. The future plan to split CValidationState
up into ~3 different subclasses (one to handle DoS, one to handle consensus correctness, and one to handle system errors) and then migrate the DoS completely to net_processing.cpp
. This is a superior architecture because it better respects the boundaries between events on the network and faults in validation. I decided to start with this PR because I think it is an low hanging fruit immediate improvement independent of further modularization efforts.
Thanks to @ryanofsky and @TheBlueMatt for feedback on an earlier version of this PR.