Hey I0rinc, thanks for reviewing,
PR description doesn’t explain what chain.Next or CChain::Contains do in case of chain.Tip() or how we can test this manually or automatically. It could also mention
https://github.com/bitcoin/bitcoin/blob/b58eebf1520f503bd4c7c5693546a6859064ce51/src/index/base.cpp#L220-L221
as an existing explanation.
There’s also no problem description, where is this code used, what are we looking at (to help reviewers and to prove you understood the change), what we’re trying to solve exactly (CChain::FindFork meant for reorgs, it’s walking the ancestor chain which is more expensive for tip than it needs to be).
I agree most what you say, CChain::FindFork meant for reorgs so the special case in this PR shouldn’t be handed by that (which in my word is: handles a specific case in a wrong way, which is not accurate), only one slight point: it’s hard to say it's walking the ancestor chain which is more expensive for tip than it needs to be is a reason, becasue FindFork() handle nullptr at it’s beginning, so yes, for pindex_prev == nullptr, FindFork() is slightly expensive due to a jmp call, but the main reason I create this PR is: FindFork() is not meant for this special case but for reorg, and the comment
https://github.com/bitcoin/bitcoin/blob/b58eebf1520f503bd4c7c5693546a6859064ce51/src/index/base.cpp#L163-L164
also explicitly indicates that FindFork() is not for that, that’s why my first version of this PR is “fix comment”
@optout21’s framing is best: “a slight optimization – early exit handling of a special case”
At the first version of this PR, what I did is to fix the comment on line 163 and line 164 which I quote above, I saw this PR as a fix because from the comment on line 163 and line 164, I could see the original author of NextSyncBlock() likely forgot the pindex_prev==nullptr case, it was just a coincidence that FindFork() returns the right result for pindex_prev==nullptr. Fix here means to fix the inconsistency between the comments(line 163, 164) and the code(line 165).
It’s also stating: “handles a specific case in a wrong way” which is too strong: the behavior was correct, just hacky - it still returned nullptr, it’s just a bit unorthodox to do it via out of bounds in