-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Net: Divide ProcessMessage in smaller functions #9608
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
0a84625
to
f896b20
Compare
1934e8f
to
a7e24f2
Compare
This should use a switch statement, not a long if/else cascade. (the extra rescan and whatever checks can be moved inside their respective functions). |
You can't switch/case on strings.
|
Well damn, I thought it was an enum now. |
I feel like this just obscures the control flow. :-/ |
What problem is this PR fixing? |
@rebroad algorithmic readability, it is simpler to reason about these algorithms, IMHO, by these smaller functions than it is otherwise. concept ACK
How so? |
Right, readability and maintainability (ie ease of review for later changes on ProcessMessage). As noted, this can be indented later (although that obscures the git blame). IMO, before this PR ProcessMessage is both too long and has some lines with too many nestings. It is very hard for me to read this function as it, and I suspect I'm not the only one.
Yeah, how so? |
Use |
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
src/net_processing.cpp
Outdated
return MsgPong(pfrom, vRecv, nTimeReceived); | ||
} | ||
else if (strCommand == NetMsgType::FILTERLOAD) | ||
{ |
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.
According to our style guideline all those curly brackets are on the "wrong" line. If we want to stick to the current style guideline, new code should not violate it. If we don't want to stick with the current style guide, we should weaken in with regard to new lines.
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.
You are completely right, I'm happy correcting this.
a7e24f2
to
fd7b7b2
Compare
Added a couple of commits to hopefully squash. |
How does it obscure the control flow? Because there is only one caller to these functions, and they're all called in order. But now looking at the function to considering its invariants you can no longer see where they are called from, what threads they run, what orders things happen in... As far as nesting goes... it hardly changes the nesting-- just reduces the indentation by one. It doesn't change the cyclomatic complexity of the code (which is usually the motivation against large functions, avoiding gnarly intertwined functions that are not testable). I don't think it grievously hurts things, but I do not see the improvement and it feels a bit like movement for movement sake to me. No biggie. |
9677569
to
03097e2
Compare
Needed rebase. Rebased on top of #9659 some changes to use more const can be made even if the separation is not done. |
0a966e5
to
951fde4
Compare
Concept ACK, as long as you change the function names to |
951fde4
to
1d7a16f
Compare
Fixed nits ( #9608 (comment) #9579 (comment) ) and rebased without needing it. |
once over utACK 1d7a16f |
Concept ACK, but I don't see this as an improvement without separating control flow and dispatch. I believe that's part of @gmaxwell's concern as well? Added that here: theuni@f1e4e28 (needs tests). This has the advantage of moving the rules for message acceptance into one place. If the message is deemed suitable, it can be dispatched with no ordering/dependency concerns. |
@theuni At a first glance that looks like an improvement in readability. I'm happy with that being done before or after this, probably before is less disruptive. |
1d7a16f
to
d985df2
Compare
Needed rebase. |
d985df2
to
b4a918d
Compare
Needed rebase |
b4a918d
to
b9e617b
Compare
Needed rebase. |
b9e617b
to
64333d6
Compare
64333d6
to
2723925
Compare
I can reopen and rebase if there's interest, but it seems there has been no interest for a while, so closing. |
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see #9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix #13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as #9608 and #10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see bitcoin#9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix bitcoin#13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as bitcoin#9608 and bitcoin#10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see bitcoin#9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix bitcoin#13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as bitcoin#9608 and bitcoin#10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201 More of 13946
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see bitcoin#9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix bitcoin#13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as bitcoin#9608 and bitcoin#10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201
fa6c3de p2p: Clarify control flow in ProcessMessage() (MarcoFalke) Pull request description: `ProcessMessage` is effectively a massive switch case construct. In the past there were attempts to clarify the control flow in `ProcessMessage()` by moving each case into a separate static function (see bitcoin#9608). It was closed because it wasn't clear if moving each case into a function was the right approach. Though, we can quasi treat each case as a function by adding a return statement to each case. (Can be seen as a continuation of bugfix bitcoin#13162) This patch does exactly that. Also note that this patch is a subset of previous approaches such as bitcoin#9608 and bitcoin#10145. Review suggestion: `git diff HEAD~ --function-context` Tree-SHA512: 91f6106840de2f29bb4f10d27bae0616b03a91126e6c6013479e1dd79bee53f22a78902b631fe85517dd5dc0fa7239939b4fefc231851a13c819458559f6c201 Signed-off-by: cevap <dev@i2pmail.org>
As in #9579, indentation and further documentation can be done later to avoid further disruption on the same PR. Maybe at different times for different groups of functions.
The main goal of this PR ProcessMessage to make it more readable and maintainable (ie, changes to ProcessMessage should in principle be easier to review after this PR). Brings ProcessMessage to 142 lines.
This should be relatively easy to review.